| 1 | package git |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/sha256" |
| 6 | "encoding/hex" |
| 7 | "fmt" |
| 8 | "os" |
| 9 | "os/exec" |
| 10 | "path/filepath" |
| 11 | "sort" |
| 12 | "strings" |
| 13 | ) |
| 14 | |
| 15 | // A clone's common git dir holds files that every worktree of the clone |
| 16 | // shares and that decide what git does there: its config, info/attributes |
| 17 | // and the hooks directory. A worker that writes one of them reaches every |
| 18 | // other worker's git commands, and the operator's, so launch snapshots them |
| 19 | // and finalize compares (SharedSnapshot, SharedChanges). Git can't keep a |
| 20 | // worker from writing them: the worker runs as the operator, git replaces |
| 21 | // its config by rename whatever the file's mode, and a worker's own |
| 22 | // `git push -u` legitimately writes branch tracking there. |
| 23 | |
| 24 | // SharedGitDir returns the common git dir of the clone worktreeDir belongs to. |
| 25 | func SharedGitDir(ctx context.Context, worktreeDir string) (string, error) { |
| 26 | return absGitPath(ctx, worktreeDir, "--git-common-dir") |
| 27 | } |
| 28 | |
| 29 | // SharedSnapshot digests the shared files of the common git dir commonDir, |
| 30 | // by path relative to it: "config", "info/attributes" and "hooks/<name>". An |
| 31 | // absent file has no entry. The config's digest is of its entries, without |
| 32 | // the branch tracking that git push -u and launch write there and the |
| 33 | // remote resolution gh records. Nothing klaus or a worker runs writes |
| 34 | // info/attributes or the shared hooks, so theirs include the file's |
| 35 | // modification time, and "hooks" is the directory's: a file written and then |
| 36 | // put back, or a hook added and then removed, still differs. |
| 37 | func SharedSnapshot(ctx context.Context, commonDir string) map[string]string { |
| 38 | snap := map[string]string{} |
| 39 | if d, ok := configDigest(ctx, filepath.Join(commonDir, "config")); ok { |
| 40 | snap["config"] = d |
| 41 | } |
| 42 | if d, ok := fileDigest(filepath.Join(commonDir, "info", "attributes")); ok { |
| 43 | snap["info/attributes"] = d |
| 44 | } |
| 45 | hooks := filepath.Join(commonDir, "hooks") |
| 46 | if info, err := os.Stat(hooks); err == nil { |
| 47 | snap["hooks"] = fmt.Sprintf("dir %d", info.ModTime().UnixNano()) |
| 48 | entries, _ := os.ReadDir(hooks) |
| 49 | for _, e := range entries { |
| 50 | if d, ok := fileDigest(filepath.Join(hooks, e.Name())); ok { |
| 51 | snap["hooks/"+e.Name()] = d |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | return snap |
| 56 | } |
| 57 | |
| 58 | // SharedChanges names, sorted, the files whose digests differ between two |
| 59 | // SharedSnapshots: changed, added or removed. |
| 60 | func SharedChanges(before, after map[string]string) []string { |
| 61 | var changed []string |
| 62 | for k, v := range before { |
| 63 | if w, ok := after[k]; !ok || w != v { |
| 64 | changed = append(changed, k) |
| 65 | } |
| 66 | } |
| 67 | for k := range after { |
| 68 | if _, ok := before[k]; !ok { |
| 69 | changed = append(changed, k) |
| 70 | } |
| 71 | } |
| 72 | sort.Strings(changed) |
| 73 | return changed |
| 74 | } |
| 75 | |
| 76 | // fileDigest is a file's content, mode and modification time; ok is false when it |
| 77 | // can't be read. |
| 78 | func fileDigest(path string) (string, bool) { |
| 79 | info, err := os.Stat(path) |
| 80 | if err != nil { |
| 81 | return "", false |
| 82 | } |
| 83 | var data []byte |
| 84 | if info.Mode().IsRegular() { |
| 85 | if data, err = os.ReadFile(path); err != nil { |
| 86 | return "", false |
| 87 | } |
| 88 | } |
| 89 | sum := sha256.Sum256(data) |
| 90 | return fmt.Sprintf("%s %s %d", hex.EncodeToString(sum[:]), info.Mode(), info.ModTime().UnixNano()), true |
| 91 | } |
| 92 | |
| 93 | // configDigest is the digest of a git config file's entries, as git reads |
| 94 | // them and in order, leaving out benignConfigKey's; a file git can't read is digested |
| 95 | // whole. ok is false when the file doesn't exist. |
| 96 | func configDigest(ctx context.Context, path string) (string, bool) { |
| 97 | data, err := os.ReadFile(path) |
| 98 | if err != nil { |
| 99 | return "", false |
| 100 | } |
| 101 | // --file reads the file alone, includes unfollowed; listing runs nothing. |
| 102 | out, err := exec.CommandContext(ctx, "git", "config", "--file", path, "--list", "-z").Output() |
| 103 | if err != nil { |
| 104 | sum := sha256.Sum256(data) |
| 105 | return "raw " + hex.EncodeToString(sum[:]), true |
| 106 | } |
| 107 | var entries []string |
| 108 | for _, e := range strings.Split(string(out), "\x00") { |
| 109 | key, _, _ := strings.Cut(e, "\n") |
| 110 | if e != "" && !benignConfigKey(key) { |
| 111 | entries = append(entries, e) |
| 112 | } |
| 113 | } |
| 114 | // In file order: a later entry overrides an earlier one. |
| 115 | sum := sha256.Sum256([]byte(strings.Join(entries, "\x00"))) |
| 116 | return hex.EncodeToString(sum[:]), true |
| 117 | } |
| 118 | |
| 119 | // benignConfigKey reports whether a shared config key is one that runs write |
| 120 | // in the ordinary course: a branch's upstream (git push -u, launch's tracking |
| 121 | // worktrees) and the base remote gh resolves. |
| 122 | func benignConfigKey(key string) bool { |
| 123 | switch { |
| 124 | case strings.HasPrefix(key, "branch."): |
| 125 | return strings.HasSuffix(key, ".remote") || strings.HasSuffix(key, ".merge") |
| 126 | case strings.HasPrefix(key, "remote."): |
| 127 | return strings.HasSuffix(key, ".gh-resolved") |
| 128 | } |
| 129 | return false |
| 130 | } |