git.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. import type { Locale } from "./protocol.js";
  2. export type GitHeadRefType = "branch" | "detached";
  3. export type GitChangeKind = "modified" | "added" | "deleted" | "renamed";
  4. export type GitChangeSourceId = "unstaged" | "staged" | "branch" | "last-turn";
  5. export type GitRepositoryChangeSourceId = Extract<
  6. GitChangeSourceId,
  7. "unstaged" | "staged" | "branch"
  8. >;
  9. export type GitChangeSectionId =
  10. | "staged"
  11. | "unstaged"
  12. | "untracked"
  13. | "conflicted"
  14. | "branch"
  15. | "last-turn";
  16. export type GitDiffAvailability = "patch" | "binary" | "truncated" | "unavailable";
  17. export type GitBranchMutationAction = "switch" | "create-and-switch";
  18. export type GitBranchMutationIssueCode =
  19. | "invalid-branch-name"
  20. | "branch-already-exists"
  21. | "target-branch-not-found"
  22. | "tracked-changes-would-be-overwritten"
  23. | "untracked-changes-would-be-overwritten"
  24. | "conflicts-present"
  25. | "operation-in-progress"
  26. | "branch-in-other-worktree"
  27. | "unknown";
  28. export interface GitRepositorySummary {
  29. workspacePath: string;
  30. repoRoot: string;
  31. workspaceInRepoPath: string;
  32. /** Git 元数据 watcher 边界;workspace 内容 watcher 由 UI 按 workspace Host 平台决定。 */
  33. autoRefreshWatchPaths: GitRepositoryAutoRefreshWatchPath[];
  34. branchName: string | null;
  35. trackingBranchName: string | null;
  36. headRefType: GitHeadRefType;
  37. ahead: number;
  38. behind: number;
  39. isDirty: boolean;
  40. isGitAvailable: boolean;
  41. isRepository: boolean;
  42. }
  43. export interface GitRepositoryAutoRefreshWatchPath {
  44. path: string;
  45. recursive: boolean;
  46. }
  47. export interface GitFileChange {
  48. path: string;
  49. repoRelativePath: string;
  50. workspaceRelativePath: string;
  51. x?: string;
  52. y?: string;
  53. kind: GitChangeKind;
  54. section: GitChangeSectionId;
  55. added: number;
  56. removed: number;
  57. isStaged: boolean;
  58. isUntracked: boolean;
  59. isConflicted: boolean;
  60. }
  61. export interface GitDiffRequest {
  62. path: string;
  63. staged?: boolean;
  64. sourceId?: GitChangeSourceId;
  65. }
  66. export interface GitDiffResult {
  67. path: string;
  68. availability: GitDiffAvailability;
  69. patch: string | null;
  70. beforeContent: string | null;
  71. afterContent: string | null;
  72. summary?: string | null;
  73. }
  74. export interface GitIdentity {
  75. userName: string | null;
  76. userEmail: string | null;
  77. nameSource: string | null;
  78. emailSource: string | null;
  79. scopeLabel?: string | null;
  80. }
  81. export interface GitRepositoryRequest {
  82. workspacePath: string;
  83. }
  84. export type GitCommitGraphRefKind = "branch" | "remote" | "tag" | "head";
  85. export interface GitCommitGraphRef {
  86. name: string;
  87. kind: GitCommitGraphRefKind;
  88. }
  89. export interface GitCommitGraphCommit {
  90. hash: string;
  91. parents: string[];
  92. refs: GitCommitGraphRef[];
  93. subject: string;
  94. authorName: string | null;
  95. authoredAtMs: number | null;
  96. }
  97. export interface GitCommitGraphRequest extends GitRepositoryRequest {
  98. maxCount?: number;
  99. skip?: number;
  100. }
  101. export interface GitCommitGraphResult {
  102. commits: GitCommitGraphCommit[];
  103. hasMore: boolean;
  104. }
  105. export interface GitRefreshRequest extends GitRepositoryRequest {
  106. includeIdentity?: boolean;
  107. includeBranchComparison?: boolean;
  108. }
  109. export type GitWorkspaceRepositoryKind = "not-repository" | "main-tree" | "linked-worktree";
  110. export interface GitWorkspaceRepositoryInfo {
  111. workspacePath: string;
  112. kind: GitWorkspaceRepositoryKind;
  113. isGitAvailable: boolean;
  114. }
  115. export interface GitSwitchBranchRequest extends GitRepositoryRequest {
  116. targetBranchName: string;
  117. }
  118. export interface GitCreateBranchRequest extends GitRepositoryRequest {
  119. branchName: string;
  120. startPoint?: string;
  121. }
  122. export interface GitChangesRequest extends GitRepositoryRequest {
  123. sourceId: Extract<GitRepositoryChangeSourceId, "unstaged" | "staged">;
  124. }
  125. export interface GitIgnoredPathsRequest extends GitRepositoryRequest {
  126. paths: string[];
  127. }
  128. export interface GitDiffQuery extends GitRepositoryRequest, GitDiffRequest {}
  129. export interface GitBranchComparison {
  130. baseRef: string | null;
  131. headRef: string | null;
  132. comparisonLabel: string | null;
  133. changes: GitFileChange[];
  134. }
  135. export interface GitLocalBranch {
  136. name: string;
  137. isCurrent: boolean;
  138. upstreamName: string | null;
  139. commitHash: string | null;
  140. commitTimestampMs: number | null;
  141. }
  142. export interface GitLocalBranchListResult {
  143. headRefType: GitHeadRefType;
  144. currentBranchName: string | null;
  145. branches: GitLocalBranch[];
  146. }
  147. export interface GitBranchMutationIssue {
  148. code: GitBranchMutationIssueCode;
  149. message: string;
  150. paths?: string[];
  151. detail?: string | null;
  152. }
  153. export interface GitBranchMutationResult {
  154. ok: boolean;
  155. action: GitBranchMutationAction;
  156. branchName: string | null;
  157. didChange: boolean;
  158. created: boolean;
  159. summary: GitRepositorySummary;
  160. issues: GitBranchMutationIssue[];
  161. }
  162. export interface GitPathMutationRequest extends GitRepositoryRequest {
  163. paths: string[];
  164. }
  165. export interface GitDiscardPathsRequest extends GitPathMutationRequest {
  166. staged?: boolean;
  167. }
  168. export interface GitCommitRequest extends GitRepositoryRequest {
  169. message: string;
  170. paths?: string[];
  171. stagedOnly?: boolean;
  172. }
  173. export interface GitCommitResult {
  174. commitHash: string;
  175. summary: GitRepositorySummary;
  176. }
  177. export interface GitGenerateCommitMessageRequest extends GitRepositoryRequest {
  178. workspaceIdentity?: string;
  179. locale?: Locale;
  180. includeUnstaged?: boolean;
  181. currentSessionFilePaths?: string[];
  182. conversationContext?: GitCommitMessageConversationContext;
  183. }
  184. export interface GitCommitMessageConversationContext {
  185. sessionId?: string;
  186. omittedMessageCount?: number;
  187. messages: GitCommitMessageConversationMessage[];
  188. }
  189. export interface GitCommitMessageConversationMessage {
  190. role: "user" | "assistant";
  191. content: string;
  192. }
  193. export interface GitGenerateCommitMessageResult {
  194. message: string;
  195. providerId: string;
  196. model: string;
  197. }
  198. export interface GitPushRequest extends GitRepositoryRequest {}
  199. export interface GitPushResult {
  200. branchName: string | null;
  201. trackingBranchName: string | null;
  202. remoteName: string | null;
  203. setUpstream: boolean;
  204. summary: GitRepositorySummary;
  205. }
  206. export interface GitRefreshResult {
  207. summary: GitRepositorySummary;
  208. identity: GitIdentity | null;
  209. unstagedChanges: GitFileChange[];
  210. stagedChanges: GitFileChange[];
  211. branchComparison: GitBranchComparison | null;
  212. }
  213. export type GitCheckpointScope = "workspace";
  214. export type GitCheckpointConflictReason =
  215. | "content-mismatch"
  216. | "missing-in-worktree"
  217. | "unexpected-file-in-worktree"
  218. | "type-mismatch";
  219. export interface GitCheckpointMeta {
  220. checkpointId: string;
  221. workspacePath: string;
  222. repoRoot: string;
  223. workspaceInRepoPath: string;
  224. createdAt: number;
  225. refName: string;
  226. commitOid: string;
  227. scope: GitCheckpointScope;
  228. }
  229. export interface GitCheckpointRequest extends GitRepositoryRequest {
  230. checkpointId: string;
  231. }
  232. export interface GitCheckpointDiffQuery extends GitRepositoryRequest {
  233. fromCheckpointId: string;
  234. toCheckpointId: string;
  235. }
  236. export interface GitCheckpointFileDiff {
  237. path: string;
  238. repoRelativePath: string;
  239. workspaceRelativePath: string;
  240. originalPath?: string | null;
  241. kind: GitChangeKind;
  242. added: number;
  243. removed: number;
  244. }
  245. export interface GitCheckpointDiff {
  246. fromCheckpointId: string;
  247. toCheckpointId: string;
  248. files: GitCheckpointFileDiff[];
  249. }
  250. export interface GitCheckpointRestoreQuery extends GitCheckpointDiffQuery {
  251. force?: boolean;
  252. }
  253. export interface GitCheckpointConflict {
  254. path: string;
  255. repoRelativePath: string;
  256. workspaceRelativePath: string;
  257. reason: GitCheckpointConflictReason;
  258. }
  259. export interface GitCheckpointRestoreResult {
  260. success: boolean;
  261. conflicts?: GitCheckpointConflict[];
  262. restoredPaths?: string[];
  263. }