gitCheckpointHelpers.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import { rm } from "node:fs/promises";
  2. import { isAbsolute, resolve, sep } from "node:path";
  3. import type { GitCheckpointDiff, GitCheckpointFileDiff } from "@zcode/shared";
  4. import { normalizeGitPath, toWorkspaceRelativeGitPath } from "../config.js";
  5. import { getWorkspaceHash } from "../../paths.js";
  6. interface GitCheckpointNameStatusEntry {
  7. kind: GitCheckpointFileDiff["kind"];
  8. path: string;
  9. originalPath: string | null;
  10. }
  11. interface GitTreeEntry {
  12. mode: string;
  13. type: string;
  14. objectId: string;
  15. path: string;
  16. }
  17. export function toAbsolutePath(repoRoot: string, repoRelativePath: string): string {
  18. return resolve(repoRoot, ...normalizeGitPath(repoRelativePath).split("/"));
  19. }
  20. export function getWorkspacePathspec(workspaceInRepoPath: string): string {
  21. return workspaceInRepoPath === "." ? "." : workspaceInRepoPath;
  22. }
  23. export function getCheckpointRefName(workspacePath: string, checkpointId: string): string {
  24. const workspaceHash = getWorkspaceHash(workspacePath);
  25. return `refs/zcode/checkpoints/${workspaceHash}/${checkpointId}`;
  26. }
  27. function mapNameStatusKind(status: string): GitCheckpointFileDiff["kind"] {
  28. const normalized = status[0] ?? "M";
  29. if (normalized === "A") {
  30. return "added";
  31. }
  32. if (normalized === "D") {
  33. return "deleted";
  34. }
  35. if (normalized === "R" || normalized === "C") {
  36. return "renamed";
  37. }
  38. return "modified";
  39. }
  40. export function parseNameStatus(stdout: string): GitCheckpointNameStatusEntry[] {
  41. const records = stdout.split("\0").filter((record) => record.length > 0);
  42. const entries: GitCheckpointNameStatusEntry[] = [];
  43. for (let index = 0; index < records.length; index += 1) {
  44. const status = records[index]!;
  45. const kind = mapNameStatusKind(status);
  46. if (kind === "renamed") {
  47. const originalPath = records[index + 1] ?? null;
  48. const path = records[index + 2] ?? null;
  49. index += 2;
  50. if (!originalPath || !path) {
  51. continue;
  52. }
  53. entries.push({
  54. kind,
  55. originalPath: normalizeGitPath(originalPath),
  56. path: normalizeGitPath(path),
  57. });
  58. continue;
  59. }
  60. const path = records[index + 1] ?? null;
  61. index += 1;
  62. if (!path) {
  63. continue;
  64. }
  65. entries.push({
  66. kind,
  67. originalPath: null,
  68. path: normalizeGitPath(path),
  69. });
  70. }
  71. return entries;
  72. }
  73. export function parseNumstat(stdout: string): Map<string, { added: number; removed: number }> {
  74. const records = stdout.split("\0").filter((record) => record.length > 0);
  75. const stats = new Map<string, { added: number; removed: number }>();
  76. for (let index = 0; index < records.length; index += 1) {
  77. const record = records[index]!;
  78. const fields = record.split("\t");
  79. if (fields.length < 3) {
  80. continue;
  81. }
  82. const added = fields[0] === "-" ? 0 : Number.parseInt(fields[0] ?? "0", 10) || 0;
  83. const removed = fields[1] === "-" ? 0 : Number.parseInt(fields[1] ?? "0", 10) || 0;
  84. const pathField = fields.slice(2).join("\t");
  85. if (pathField.length > 0) {
  86. stats.set(normalizeGitPath(pathField), { added, removed });
  87. continue;
  88. }
  89. const originalPath = records[index + 1] ?? "";
  90. const renamedPath = records[index + 2] ?? "";
  91. index += 2;
  92. if (!renamedPath) {
  93. continue;
  94. }
  95. stats.set(normalizeGitPath(renamedPath), { added, removed });
  96. if (originalPath) {
  97. stats.set(normalizeGitPath(originalPath), { added, removed });
  98. }
  99. }
  100. return stats;
  101. }
  102. export function parseLsTree(stdout: string): Map<string, GitTreeEntry> {
  103. const records = stdout.split("\0").filter((record) => record.length > 0);
  104. const entries = new Map<string, GitTreeEntry>();
  105. for (const record of records) {
  106. const tabIndex = record.indexOf("\t");
  107. if (tabIndex < 0) {
  108. continue;
  109. }
  110. const header = record.slice(0, tabIndex).split(" ");
  111. if (header.length < 3) {
  112. continue;
  113. }
  114. const path = normalizeGitPath(record.slice(tabIndex + 1));
  115. entries.set(path, {
  116. mode: header[0] ?? "100644",
  117. type: header[1] ?? "blob",
  118. objectId: header[2] ?? "",
  119. path,
  120. });
  121. }
  122. return entries;
  123. }
  124. export function mergeCheckpointDiff(params: {
  125. repoRoot: string;
  126. workspaceInRepoPath: string;
  127. fromCheckpointId: string;
  128. toCheckpointId: string;
  129. nameStatusEntries: GitCheckpointNameStatusEntry[];
  130. numstat: Map<string, { added: number; removed: number }>;
  131. }): GitCheckpointDiff {
  132. const files: GitCheckpointFileDiff[] = params.nameStatusEntries.map((entry) => {
  133. const stat = params.numstat.get(entry.path) ??
  134. params.numstat.get(entry.originalPath ?? "") ?? {
  135. added: 0,
  136. removed: 0,
  137. };
  138. return {
  139. path: toAbsolutePath(params.repoRoot, entry.path),
  140. repoRelativePath: entry.path,
  141. workspaceRelativePath: toWorkspaceRelativeGitPath(entry.path, params.workspaceInRepoPath),
  142. originalPath: entry.originalPath ? toAbsolutePath(params.repoRoot, entry.originalPath) : null,
  143. kind: entry.kind,
  144. added: stat.added,
  145. removed: stat.removed,
  146. };
  147. });
  148. return {
  149. fromCheckpointId: params.fromCheckpointId,
  150. toCheckpointId: params.toCheckpointId,
  151. files,
  152. };
  153. }
  154. export function buildAffectedRepoPaths(files: GitCheckpointFileDiff[]): string[] {
  155. const values = new Set<string>();
  156. for (const file of files) {
  157. values.add(file.repoRelativePath);
  158. if (file.originalPath) {
  159. values.add(file.originalPath);
  160. }
  161. }
  162. return [...values];
  163. }
  164. export function buildCheckpointEnv(tempIndexPath: string): NodeJS.ProcessEnv {
  165. return {
  166. GIT_INDEX_FILE: tempIndexPath,
  167. GIT_AUTHOR_NAME: "ZCode Checkpoint",
  168. GIT_AUTHOR_EMAIL: "checkpoint@zcode.local",
  169. GIT_COMMITTER_NAME: "ZCode Checkpoint",
  170. GIT_COMMITTER_EMAIL: "checkpoint@zcode.local",
  171. };
  172. }
  173. export async function removeFileIfExists(path: string): Promise<void> {
  174. await rm(path, {
  175. force: true,
  176. recursive: true,
  177. });
  178. }
  179. function toRepoRelativePathFromAbsolute(repoRoot: string, absolutePath: string): string {
  180. return normalizeGitPath(absolutePath.replace(`${repoRoot}${sep}`, ""));
  181. }
  182. export function normalizeAffectedRepoPath(repoRoot: string, path: string): string {
  183. return normalizeGitPath(isAbsolute(path) ? toRepoRelativePathFromAbsolute(repoRoot, path) : path);
  184. }