|
| 1 | +package git |
| 2 | + |
| 3 | +import ( |
| 4 | + "os/exec" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestFullHash(t *testing.T) { |
| 10 | + requireGit(t) |
| 11 | + repoPath, cleanup := initTempRepo(t) |
| 12 | + defer cleanup() |
| 13 | + |
| 14 | + shortHash := makeCommit(t, repoPath, "test commit") |
| 15 | + |
| 16 | + fullHash, err := FullHash(repoPath, shortHash) |
| 17 | + if err != nil { |
| 18 | + t.Fatalf("FullHash: %v", err) |
| 19 | + } |
| 20 | + |
| 21 | + if len(fullHash) != 40 { |
| 22 | + t.Errorf("FullHash: expected 40 chars, got %d (%q)", len(fullHash), fullHash) |
| 23 | + } |
| 24 | + |
| 25 | + if !strings.HasPrefix(fullHash, shortHash) { |
| 26 | + t.Errorf("FullHash: %q is not a prefix of full hash %q", shortHash, fullHash) |
| 27 | + } |
| 28 | + |
| 29 | + headFull, err := FullHash(repoPath, "HEAD") |
| 30 | + if err != nil { |
| 31 | + t.Fatalf("FullHash(HEAD): %v", err) |
| 32 | + } |
| 33 | + if headFull != fullHash { |
| 34 | + t.Errorf("FullHash(HEAD)=%q, want %q", headFull, fullHash) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +func TestFullHash_InvalidRef(t *testing.T) { |
| 39 | + requireGit(t) |
| 40 | + repoPath, cleanup := initTempRepo(t) |
| 41 | + defer cleanup() |
| 42 | + |
| 43 | + makeCommit(t, repoPath, "initial") |
| 44 | + |
| 45 | + _, err := FullHash(repoPath, "nonexistent-ref") |
| 46 | + if err == nil { |
| 47 | + t.Error("FullHash(nonexistent): expected error, got nil") |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func TestPreservationRefName(t *testing.T) { |
| 52 | + root := "a1b2c3d4e5f6789012345678901234567890abcd" |
| 53 | + child := "b2c3d4e5f6789012345678901234567890abcde" |
| 54 | + |
| 55 | + refName := PreservationRefName(root, child) |
| 56 | + expected := "refs/squash-archive/" + root + "/" + child |
| 57 | + |
| 58 | + if refName != expected { |
| 59 | + t.Errorf("PreservationRefName=%q, want %q", refName, expected) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func TestCreatePreservationRefs(t *testing.T) { |
| 64 | + requireGit(t) |
| 65 | + repoPath, cleanup := initTempRepo(t) |
| 66 | + defer cleanup() |
| 67 | + |
| 68 | + hash1 := makeCommit(t, repoPath, "commit 1") |
| 69 | + hash2 := makeCommitUnique(t, repoPath, "commit 2", "2") |
| 70 | + hash3 := makeCommitUnique(t, repoPath, "commit 3", "3") |
| 71 | + |
| 72 | + fullRoot, _ := FullHash(repoPath, hash1) |
| 73 | + fullChild1, _ := FullHash(repoPath, hash2) |
| 74 | + fullChild2, _ := FullHash(repoPath, hash3) |
| 75 | + |
| 76 | + err := CreatePreservationRefs(repoPath, fullRoot, []string{fullChild1, fullChild2}) |
| 77 | + if err != nil { |
| 78 | + t.Fatalf("CreatePreservationRefs: %v", err) |
| 79 | + } |
| 80 | + |
| 81 | + ref1 := PreservationRefName(fullRoot, fullChild1) |
| 82 | + ref2 := PreservationRefName(fullRoot, fullChild2) |
| 83 | + |
| 84 | + for _, ref := range []string{ref1, ref2} { |
| 85 | + cmd := exec.Command("git", "show-ref", "--verify", ref) |
| 86 | + cmd.Dir = repoPath |
| 87 | + if err := cmd.Run(); err != nil { |
| 88 | + t.Errorf("ref %s does not exist", ref) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + for _, tc := range []struct { |
| 93 | + ref string |
| 94 | + expected string |
| 95 | + }{ |
| 96 | + {ref1, fullChild1}, |
| 97 | + {ref2, fullChild2}, |
| 98 | + } { |
| 99 | + cmd := exec.Command("git", "rev-parse", tc.ref) |
| 100 | + cmd.Dir = repoPath |
| 101 | + out, err := cmd.Output() |
| 102 | + if err != nil { |
| 103 | + t.Fatalf("rev-parse %s: %v", tc.ref, err) |
| 104 | + } |
| 105 | + got := strings.TrimSpace(string(out)) |
| 106 | + if got != tc.expected { |
| 107 | + t.Errorf("ref %s points to %q, want %q", tc.ref, got, tc.expected) |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func TestCreatePreservationRefs_Idempotent(t *testing.T) { |
| 113 | + requireGit(t) |
| 114 | + repoPath, cleanup := initTempRepo(t) |
| 115 | + defer cleanup() |
| 116 | + |
| 117 | + hash1 := makeCommit(t, repoPath, "commit 1") |
| 118 | + hash2 := makeCommitUnique(t, repoPath, "commit 2", "2") |
| 119 | + |
| 120 | + fullRoot, _ := FullHash(repoPath, hash1) |
| 121 | + fullChild, _ := FullHash(repoPath, hash2) |
| 122 | + |
| 123 | + if err := CreatePreservationRefs(repoPath, fullRoot, []string{fullChild}); err != nil { |
| 124 | + t.Fatalf("CreatePreservationRefs (1st): %v", err) |
| 125 | + } |
| 126 | + if err := CreatePreservationRefs(repoPath, fullRoot, []string{fullChild}); err != nil { |
| 127 | + t.Fatalf("CreatePreservationRefs (2nd): %v", err) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func TestPreservationRefsExist(t *testing.T) { |
| 132 | + requireGit(t) |
| 133 | + repoPath, cleanup := initTempRepo(t) |
| 134 | + defer cleanup() |
| 135 | + |
| 136 | + hash1 := makeCommit(t, repoPath, "commit 1") |
| 137 | + hash2 := makeCommitUnique(t, repoPath, "commit 2", "2") |
| 138 | + |
| 139 | + fullRoot, _ := FullHash(repoPath, hash1) |
| 140 | + fullChild, _ := FullHash(repoPath, hash2) |
| 141 | + |
| 142 | + exists, err := PreservationRefsExist(repoPath, fullRoot, []string{fullChild}) |
| 143 | + if err != nil { |
| 144 | + t.Fatalf("PreservationRefsExist: %v", err) |
| 145 | + } |
| 146 | + if exists { |
| 147 | + t.Error("PreservationRefsExist: expected false before creation") |
| 148 | + } |
| 149 | + |
| 150 | + if err := CreatePreservationRefs(repoPath, fullRoot, []string{fullChild}); err != nil { |
| 151 | + t.Fatalf("CreatePreservationRefs: %v", err) |
| 152 | + } |
| 153 | + |
| 154 | + exists, err = PreservationRefsExist(repoPath, fullRoot, []string{fullChild}) |
| 155 | + if err != nil { |
| 156 | + t.Fatalf("PreservationRefsExist: %v", err) |
| 157 | + } |
| 158 | + if !exists { |
| 159 | + t.Error("PreservationRefsExist: expected true after creation") |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +func TestWriteMetadata_CreatesPreservationRefs(t *testing.T) { |
| 164 | + requireGit(t) |
| 165 | + repoPath, cleanup := initTempRepo(t) |
| 166 | + defer cleanup() |
| 167 | + |
| 168 | + makeCommit(t, repoPath, "base") |
| 169 | + child1Short := makeCommitUnique(t, repoPath, "child1", "c1") |
| 170 | + child2Short := makeCommitUnique(t, repoPath, "child2", "c2") |
| 171 | + rootShort := makeCommitUnique(t, repoPath, "squash", "sq") |
| 172 | + |
| 173 | + baseShort := child1Short |
| 174 | + |
| 175 | + children := []string{child1Short, child2Short} |
| 176 | + err := WriteMetadata(repoPath, rootShort, baseShort, children, "test") |
| 177 | + if err != nil { |
| 178 | + t.Fatalf("WriteMetadata: %v", err) |
| 179 | + } |
| 180 | + |
| 181 | + rootFull, _ := FullHash(repoPath, rootShort) |
| 182 | + child1Full, _ := FullHash(repoPath, child1Short) |
| 183 | + child2Full, _ := FullHash(repoPath, child2Short) |
| 184 | + |
| 185 | + exists, err := PreservationRefsExist(repoPath, rootFull, []string{child1Full, child2Full}) |
| 186 | + if err != nil { |
| 187 | + t.Fatalf("PreservationRefsExist: %v", err) |
| 188 | + } |
| 189 | + if !exists { |
| 190 | + t.Error("WriteMetadata did not create preservation refs") |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +func makeCommitUnique(t *testing.T, repoPath, msg, uniqueContent string) string { |
| 195 | + t.Helper() |
| 196 | + |
| 197 | + cmd := exec.Command("git", "config", "commit.gpgsign", "false") |
| 198 | + cmd.Dir = repoPath |
| 199 | + cmd.Run() |
| 200 | + |
| 201 | + cmd = exec.Command("git", "rev-parse", "--short", "HEAD") |
| 202 | + cmd.Dir = repoPath |
| 203 | + out, _ := cmd.Output() |
| 204 | + currentHead := strings.TrimSpace(string(out)) |
| 205 | + |
| 206 | + f := repoPath + "/f.txt" |
| 207 | + cmd = exec.Command("bash", "-c", "echo '"+uniqueContent+"' >> "+f) |
| 208 | + cmd.Dir = repoPath |
| 209 | + if err := cmd.Run(); err != nil { |
| 210 | + t.Fatalf("append to file: %v", err) |
| 211 | + } |
| 212 | + |
| 213 | + cmd = exec.Command("git", "add", "f.txt") |
| 214 | + cmd.Dir = repoPath |
| 215 | + if out, err := cmd.CombinedOutput(); err != nil { |
| 216 | + t.Fatalf("git add: %v %s", err, out) |
| 217 | + } |
| 218 | + |
| 219 | + cmd = exec.Command("git", "commit", "-m", msg) |
| 220 | + cmd.Dir = repoPath |
| 221 | + if out, err := cmd.CombinedOutput(); err != nil { |
| 222 | + if strings.Contains(string(out), "nothing to commit") { |
| 223 | + return currentHead |
| 224 | + } |
| 225 | + t.Fatalf("git commit: %v %s", err, out) |
| 226 | + } |
| 227 | + |
| 228 | + cmd = exec.Command("git", "rev-parse", "--short", "HEAD") |
| 229 | + cmd.Dir = repoPath |
| 230 | + out, err := cmd.Output() |
| 231 | + if err != nil { |
| 232 | + t.Fatalf("git rev-parse: %v", err) |
| 233 | + } |
| 234 | + return strings.TrimSpace(string(out)) |
| 235 | +} |
0 commit comments