| 1 | package git |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | ) |
| 9 | |
| 10 | // SyncSnapshot makes path a detached worktree of repoDir checked out at |
| 11 | // commit. When path is already one of repoDir's worktrees it moves it to |
| 12 | // commit, discarding anything written there; otherwise it replaces path with |
| 13 | // a new worktree. A snapshot holds no branch, so it never keeps a launch from |
| 14 | // checking one out. |
| 15 | func SyncSnapshot(ctx context.Context, repoDir, path, commit string) error { |
| 16 | if snapshotOf(ctx, repoDir, path) { |
| 17 | if _, err := runGit(ctx, path, "checkout", "--quiet", "--detach", "--force", commit); err != nil { |
| 18 | return err |
| 19 | } |
| 20 | _, err := runGit(ctx, path, "clean", "-ffdxq") |
| 21 | return err |
| 22 | } |
| 23 | if err := RemoveSnapshot(ctx, path); err != nil { |
| 24 | return err |
| 25 | } |
| 26 | if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil { |
| 27 | return err |
| 28 | } |
| 29 | // Prune first: a snapshot whose directory was deleted is still |
| 30 | // registered at path, and git refuses to add a worktree there. |
| 31 | if _, err := runGit(ctx, repoDir, "worktree", "prune"); err != nil { |
| 32 | return err |
| 33 | } |
| 34 | _, err := runGit(ctx, repoDir, "worktree", "add", "--quiet", "--detach", path, commit) |
| 35 | return err |
| 36 | } |
| 37 | |
| 38 | // SnapshotCommit returns the commit the snapshot at path has checked out, |
| 39 | // or "" when there is no snapshot there. |
| 40 | func SnapshotCommit(ctx context.Context, path string) string { |
| 41 | if _, err := os.Stat(filepath.Join(path, ".git")); err != nil { |
| 42 | return "" |
| 43 | } |
| 44 | return RefCommit(ctx, path, "HEAD") |
| 45 | } |
| 46 | |
| 47 | // RemoveSnapshot removes the snapshot at path and its registration in the |
| 48 | // clone it came from. A path that isn't a worktree is removed outright. |
| 49 | func RemoveSnapshot(ctx context.Context, path string) error { |
| 50 | if _, err := os.Lstat(path); os.IsNotExist(err) { |
| 51 | return nil |
| 52 | } |
| 53 | if _, err := os.Stat(filepath.Join(path, ".git")); err == nil { |
| 54 | if common, err := runGit(ctx, path, "rev-parse", "--path-format=absolute", "--git-common-dir"); err == nil { |
| 55 | _, _ = runGit(ctx, path, "worktree", "remove", "--force", path) |
| 56 | defer func() { _, _ = runGit(ctx, "", "--git-dir="+common, "worktree", "prune") }() |
| 57 | } |
| 58 | } |
| 59 | if err := os.RemoveAll(path); err != nil { |
| 60 | return fmt.Errorf("removing snapshot %s: %w", path, err) |
| 61 | } |
| 62 | return nil |
| 63 | } |
| 64 | |
| 65 | // snapshotOf reports whether path is the top of a worktree of repoDir. |
| 66 | func snapshotOf(ctx context.Context, repoDir, path string) bool { |
| 67 | if _, err := os.Stat(filepath.Join(path, ".git")); err != nil { |
| 68 | return false |
| 69 | } |
| 70 | top, err := runGit(ctx, path, "rev-parse", "--show-toplevel") |
| 71 | if err != nil || !sameFile(top, path) { |
| 72 | return false |
| 73 | } |
| 74 | theirs, err := runGit(ctx, path, "rev-parse", "--path-format=absolute", "--git-common-dir") |
| 75 | if err != nil { |
| 76 | return false |
| 77 | } |
| 78 | ours, err := runGit(ctx, repoDir, "rev-parse", "--path-format=absolute", "--git-common-dir") |
| 79 | return err == nil && sameFile(theirs, ours) |
| 80 | } |
| 81 | |
| 82 | func sameFile(a, b string) bool { |
| 83 | ai, err := os.Stat(a) |
| 84 | if err != nil { |
| 85 | return false |
| 86 | } |
| 87 | bi, err := os.Stat(b) |
| 88 | return err == nil && os.SameFile(ai, bi) |
| 89 | } |