gitCheckpointService.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { randomUUID } from "node:crypto";
  2. import type {
  3. GitCheckpointDiff,
  4. GitCheckpointDiffQuery,
  5. GitCheckpointMeta,
  6. GitCheckpointRequest,
  7. GitCheckpointRestoreQuery,
  8. GitCheckpointRestoreResult,
  9. GitRepositoryRequest,
  10. } from "@zcode/shared";
  11. import type { IGitCheckpointService } from "./gitCheckpoint.js";
  12. import { createGitCheckpointRepo, type GitCheckpointRepo } from "./repo/gitCheckpointRepo.js";
  13. import { GitCheckpointStore } from "./repo/gitCheckpointStore.js";
  14. function ensureCheckpointExists(
  15. meta: GitCheckpointMeta | null,
  16. checkpointId: string,
  17. ): GitCheckpointMeta {
  18. if (!meta) {
  19. throw new Error(`Checkpoint does not exist: ${checkpointId}`);
  20. }
  21. return meta;
  22. }
  23. function ensureSameWorkspace(meta: GitCheckpointMeta, workspacePath: string): void {
  24. if (meta.workspacePath !== workspacePath) {
  25. throw new Error(`Checkpoint workspace mismatch: ${meta.checkpointId}`);
  26. }
  27. }
  28. function ensureComparableCheckpoints(left: GitCheckpointMeta, right: GitCheckpointMeta): void {
  29. if (left.repoRoot !== right.repoRoot) {
  30. throw new Error("Checkpoint repoRoot mismatch.");
  31. }
  32. if (left.scope !== right.scope) {
  33. throw new Error("Checkpoint scope mismatch.");
  34. }
  35. if (left.workspaceInRepoPath !== right.workspaceInRepoPath) {
  36. throw new Error("Checkpoint workspace scope mismatch.");
  37. }
  38. }
  39. export function createGitCheckpointService(options?: {
  40. store?: GitCheckpointStore;
  41. repo?: GitCheckpointRepo;
  42. }): IGitCheckpointService {
  43. const store = options?.store ?? new GitCheckpointStore();
  44. const repo = options?.repo ?? createGitCheckpointRepo();
  45. async function loadCheckpoint(
  46. workspacePath: string,
  47. checkpointId: string,
  48. ): Promise<GitCheckpointMeta> {
  49. const meta = ensureCheckpointExists(
  50. await store.load(workspacePath, checkpointId),
  51. checkpointId,
  52. );
  53. ensureSameWorkspace(meta, workspacePath);
  54. return meta;
  55. }
  56. return {
  57. async createCheckpoint(params: GitRepositoryRequest): Promise<GitCheckpointMeta> {
  58. const checkpointId = randomUUID();
  59. const meta = await repo.createCheckpoint({
  60. workspacePath: params.workspacePath,
  61. checkpointId,
  62. });
  63. await store.save(meta);
  64. return meta;
  65. },
  66. async diffCheckpoints(params: GitCheckpointDiffQuery): Promise<GitCheckpointDiff> {
  67. const [from, to] = await Promise.all([
  68. loadCheckpoint(params.workspacePath, params.fromCheckpointId),
  69. loadCheckpoint(params.workspacePath, params.toCheckpointId),
  70. ]);
  71. ensureComparableCheckpoints(from, to);
  72. return await repo.diffCheckpoints({
  73. workspacePath: params.workspacePath,
  74. from,
  75. to,
  76. });
  77. },
  78. async restoreBetweenCheckpoints(
  79. params: GitCheckpointRestoreQuery,
  80. ): Promise<GitCheckpointRestoreResult> {
  81. const [from, to] = await Promise.all([
  82. loadCheckpoint(params.workspacePath, params.fromCheckpointId),
  83. loadCheckpoint(params.workspacePath, params.toCheckpointId),
  84. ]);
  85. ensureComparableCheckpoints(from, to);
  86. return await repo.restoreBetweenCheckpoints({
  87. workspacePath: params.workspacePath,
  88. from,
  89. to,
  90. force: params.force,
  91. });
  92. },
  93. async deleteCheckpoint(params: GitCheckpointRequest): Promise<void> {
  94. const checkpoint = await loadCheckpoint(params.workspacePath, params.checkpointId);
  95. await repo.deleteCheckpoint({
  96. workspacePath: params.workspacePath,
  97. checkpoint,
  98. });
  99. await store.delete(params.workspacePath, params.checkpointId);
  100. },
  101. };
  102. }