| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "crypto/sha256" |
| 6 | "fmt" |
| 7 | "io" |
| 8 | "os" |
| 9 | "os/exec" |
| 10 | "path/filepath" |
| 11 | "regexp" |
| 12 | "strconv" |
| 13 | "strings" |
| 14 | "testing" |
| 15 | ) |
| 16 | |
| 17 | // compareRepo is an origin and a clone of it, for compare's tests. |
| 18 | type compareRepo struct { |
| 19 | t *testing.T |
| 20 | origin, work string |
| 21 | } |
| 22 | |
| 23 | func newCompareRepo(t *testing.T) *compareRepo { |
| 24 | t.Helper() |
| 25 | dir := t.TempDir() |
| 26 | t.Setenv("HOME", dir) |
| 27 | t.Setenv("XDG_CONFIG_HOME", filepath.Join(dir, "config")) |
| 28 | t.Setenv("GIT_CONFIG_NOSYSTEM", "1") |
| 29 | t.Setenv("VALLEY_KNOWN_SIGNERS", "") |
| 30 | r := &compareRepo{t: t, origin: filepath.Join(dir, "origin.git"), work: filepath.Join(dir, "work")} |
| 31 | r.in(dir, "init", "--quiet", "--bare", "--initial-branch=main", r.origin) |
| 32 | r.in(dir, "clone", "--quiet", r.origin, r.work) |
| 33 | r.git("config", "user.name", "compare-test") |
| 34 | r.git("config", "user.email", "compare-test@localhost") |
| 35 | r.git("config", "valley.principal", "alice") |
| 36 | r.git("checkout", "--quiet", "-b", "main") |
| 37 | r.file("README", "base\n") |
| 38 | r.git("commit", "--quiet", "-m", "base") |
| 39 | r.git("push", "--quiet", "origin", "main") |
| 40 | t.Chdir(r.work) |
| 41 | return r |
| 42 | } |
| 43 | |
| 44 | func (r *compareRepo) in(dir string, args ...string) string { |
| 45 | r.t.Helper() |
| 46 | cmd := exec.Command("git", args...) |
| 47 | cmd.Dir = dir |
| 48 | var stderr bytes.Buffer |
| 49 | cmd.Stderr = &stderr |
| 50 | out, err := cmd.Output() |
| 51 | if err != nil { |
| 52 | r.t.Fatalf("git %s: %v\n%s", strings.Join(args, " "), err, stderr.String()) |
| 53 | } |
| 54 | return strings.TrimSpace(string(out)) |
| 55 | } |
| 56 | |
| 57 | func (r *compareRepo) git(args ...string) string { |
| 58 | r.t.Helper() |
| 59 | return r.in(r.work, args...) |
| 60 | } |
| 61 | |
| 62 | // file writes a file in the work tree and stages it. |
| 63 | func (r *compareRepo) file(name, content string) { |
| 64 | r.t.Helper() |
| 65 | if err := os.WriteFile(filepath.Join(r.work, name), []byte(content), 0o644); err != nil { |
| 66 | r.t.Fatal(err) |
| 67 | } |
| 68 | r.git("add", name) |
| 69 | } |
| 70 | |
| 71 | // valley runs a verb of valley and returns what it printed. |
| 72 | func (r *compareRepo) valley(args ...string) string { |
| 73 | r.t.Helper() |
| 74 | var stdout, stderr bytes.Buffer |
| 75 | if code := run(args, &stdout, &stderr, os.Exit); code != 0 { |
| 76 | r.t.Fatalf("valley %s: %s", strings.Join(args, " "), stderr.String()) |
| 77 | } |
| 78 | return stdout.String() |
| 79 | } |
| 80 | |
| 81 | // runCompare sets a version against its approved one, as review does. |
| 82 | func (r *compareRepo) runCompare(args ...string) (string, error) { |
| 83 | var out bytes.Buffer |
| 84 | err := runCompare(git{env: gitEnvironment(os.Environ()), stderr: io.Discard}, args, &out) |
| 85 | return out.String(), err |
| 86 | } |
| 87 | |
| 88 | // moveMain adds a commit to main on origin, as a landing would, and |
| 89 | // fetches it. It returns main's new tip. |
| 90 | func (r *compareRepo) moveMain(name string) string { |
| 91 | r.git("checkout", "--quiet", "main") |
| 92 | r.file(name, name+"\n") |
| 93 | r.git("commit", "--quiet", "-m", "land "+name) |
| 94 | r.git("push", "--quiet", "origin", "main") |
| 95 | return r.git("rev-parse", "HEAD") |
| 96 | } |
| 97 | |
| 98 | // rebaseOnto rebases a branch onto main and pushes it, and returns its |
| 99 | // new head. |
| 100 | func (r *compareRepo) rebaseOnto(branch string) string { |
| 101 | r.git("checkout", "--quiet", branch) |
| 102 | r.git("rebase", "--quiet", "main") |
| 103 | r.git("push", "--quiet", "--force", "origin", branch) |
| 104 | r.git("checkout", "--quiet", "main") |
| 105 | return r.git("rev-parse", branch) |
| 106 | } |
| 107 | |
| 108 | // setRef points a ref on origin at an object, or deletes it for "". |
| 109 | func (r *compareRepo) setRef(ref, oid string) { |
| 110 | if oid == "" { |
| 111 | r.in(r.origin, "update-ref", "-d", ref) |
| 112 | return |
| 113 | } |
| 114 | r.in(r.origin, "update-ref", ref, oid) |
| 115 | } |
| 116 | |
| 117 | // outcomeRecord writes an outcome record on origin, as the integrator |
| 118 | // writes one, and returns its blob. |
| 119 | func (r *compareRepo) outcomeRecord(head, decision string) string { |
| 120 | cmd := exec.Command("git", "hash-object", "-w", "--stdin") |
| 121 | cmd.Dir = r.origin |
| 122 | cmd.Stdin = strings.NewReader(head + " " + r.git("rev-parse", "main") + " 0123456789abcdef " + decision + "\n") |
| 123 | out, err := cmd.Output() |
| 124 | if err != nil { |
| 125 | r.t.Fatal(err) |
| 126 | } |
| 127 | return strings.TrimSpace(string(out)) |
| 128 | } |
| 129 | |
| 130 | func (r *compareRepo) compare(id string, version int, head string) (kind, text string) { |
| 131 | r.t.Helper() |
| 132 | return r.compareWith(id, version, head) |
| 133 | } |
| 134 | |
| 135 | func (r *compareRepo) compareVerbose(id string, version int, head string) (kind, text string) { |
| 136 | r.t.Helper() |
| 137 | return r.compareWith(id, version, head, "-v") |
| 138 | } |
| 139 | |
| 140 | func (r *compareRepo) compareWith(id string, version int, head string, flags ...string) (kind, text string) { |
| 141 | r.t.Helper() |
| 142 | args := append(append([]string{}, flags...), "--tip", r.git("rev-parse", "origin/main"), id, strconv.Itoa(version), head) |
| 143 | out, err := r.runCompare(args...) |
| 144 | if err != nil { |
| 145 | r.t.Fatalf("compare %s: %s", strings.Join(args, " "), err) |
| 146 | } |
| 147 | kind, text, _ = strings.Cut(out, "\n") |
| 148 | return kind, text |
| 149 | } |
| 150 | |
| 151 | // A request or an outcome under a branch's request name is evidence that |
| 152 | // a head was approved only while nothing else maps to that name: no |
| 153 | // version of another change, and no branch on origin that is not the |
| 154 | // change's own. Under c-<change ID> it always is. An ambiguous name stops |
| 155 | // the search: an older version is not compared instead. |
| 156 | func TestTheApprovedVersionIsNamedUnambiguously(t *testing.T) { |
| 157 | r := newCompareRepo(t) |
| 158 | id := strings.Repeat("x", 32) |
| 159 | r.git("checkout", "--quiet", "-b", "a/b") |
| 160 | r.file("feature.txt", "feature\n") |
| 161 | r.git("commit", "--quiet", "-m", "add the feature") |
| 162 | r.git("push", "--quiet", "origin", "a/b") |
| 163 | r.valley("ready", "--change", id, "a/b") |
| 164 | v1 := r.git("rev-parse", "a/b") |
| 165 | byBranch := requestPrefix + "a-b" |
| 166 | r.setRef(byBranch, v1) |
| 167 | r.moveMain("other") |
| 168 | v2 := r.rebaseOnto("a/b") |
| 169 | r.valley("ready", "--change", id, "a/b") |
| 170 | r.git("fetch", "--quiet", "origin") |
| 171 | |
| 172 | clean := func(why string) { |
| 173 | t.Helper() |
| 174 | if kind, text := r.compare(id, 2, v2); kind != compareClean || !strings.Contains(text, "rebased since you approved v1 at "+short(v1)+", with the same file changes.") { |
| 175 | t.Fatalf("%s: %s\n%s", why, kind, text) |
| 176 | } |
| 177 | } |
| 178 | unknown := func(why, ref string) { |
| 179 | t.Helper() |
| 180 | kind, text := r.compare(id, 2, v2) |
| 181 | if kind != compareNone || !strings.Contains(text, "is named by "+ref+", and another change or branch maps to that name too") { |
| 182 | t.Fatalf("%s: %s\n%s", why, kind, text) |
| 183 | } |
| 184 | } |
| 185 | clean("a request under the branch's name") |
| 186 | |
| 187 | // A plain branch on origin whose name maps to a-b as well. |
| 188 | r.git("push", "--quiet", "origin", "main:refs/heads/a-b") |
| 189 | unknown("a request under a name a plain branch maps to", byBranch) |
| 190 | r.setRef("refs/heads/a-b", "") |
| 191 | clean("a request once the plain branch is gone") |
| 192 | |
| 193 | // Another change, whose version names a branch that maps to a-b. |
| 194 | r.git("checkout", "--quiet", "-b", "a-b", "main") |
| 195 | r.file("y.txt", "y\n") |
| 196 | r.git("commit", "--quiet", "-m", "another change") |
| 197 | r.git("push", "--quiet", "origin", "a-b") |
| 198 | r.valley("ready", "--change", strings.Repeat("y", 32), "a-b") |
| 199 | r.git("checkout", "--quiet", "main") |
| 200 | r.setRef("refs/heads/a-b", "") |
| 201 | unknown("a request under a name another change's version maps to", byBranch) |
| 202 | |
| 203 | // The outcome of the same request, after the integrator consumed it: |
| 204 | // ambiguous under a-b, and evidence under c-<change ID>. |
| 205 | r.setRef(byBranch, "") |
| 206 | r.setRef(outcomePrefix+"a-b", r.outcomeRecord(v1, "stale")) |
| 207 | unknown("an outcome under a name another change's version maps to", outcomePrefix+"a-b") |
| 208 | r.setRef(outcomePrefix+"a-b", "") |
| 209 | r.setRef(outcomePrefix+"c-"+id, r.outcomeRecord(v1, "refused-not-ready")) |
| 210 | if kind, text := r.compare(id, 2, v2); kind != compareNone || text != "" { |
| 211 | t.Fatalf("an outcome that judged no approval counted: %s\n%s", kind, text) |
| 212 | } |
| 213 | r.setRef(outcomePrefix+"c-"+id, r.outcomeRecord(v1, "land")) |
| 214 | clean("an outcome under c-<change ID>") |
| 215 | |
| 216 | // A version shown is never its own reference, and a greater version |
| 217 | // named only ambiguously stops the search before v1. |
| 218 | r.git("checkout", "--quiet", "a/b") |
| 219 | r.file("feature.txt", "feature, edited\n") |
| 220 | r.git("commit", "--quiet", "-m", "edit the feature") |
| 221 | r.git("push", "--quiet", "origin", "a/b") |
| 222 | r.valley("ready", "--change", id, "a/b") |
| 223 | r.git("checkout", "--quiet", "main") |
| 224 | v3 := r.git("rev-parse", "a/b") |
| 225 | r.setRef(requestPrefix+"c-"+id, v3) |
| 226 | if kind, text := r.compare(id, 3, v3); kind != compareChanged || !strings.Contains(text, "edited since you approved v1 at "+short(v1)+".") { |
| 227 | t.Fatalf("v3 is not set against v1, the greatest version below it with an approval: %s\n%s", kind, text) |
| 228 | } |
| 229 | r.setRef(requestPrefix+"c-"+id, "") |
| 230 | r.setRef(byBranch, v2) |
| 231 | if kind, text := r.compare(id, 3, v3); kind != compareNone || !strings.Contains(text, "v2 at "+short(v2)+" is named by "+byBranch) { |
| 232 | t.Fatalf("v2 named ambiguously did not stop the search before v1: %s\n%s", kind, text) |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | // A branch named c/<ID> or c-<ID> maps to c-<ID>, which is reserved for |
| 237 | // the change with that ID. A request or an outcome under it is that |
| 238 | // change's, so it is no evidence of approval for a change whose versions |
| 239 | // name the branch, even when it names one of their heads. |
| 240 | func TestAnotherChangesReservedNameIsNoEvidence(t *testing.T) { |
| 241 | id, other := strings.Repeat("x", 32), strings.Repeat("y", 32) |
| 242 | for _, prefix := range []string{"c/", "c-"} { |
| 243 | t.Run(prefix, func(t *testing.T) { |
| 244 | r := newCompareRepo(t) |
| 245 | branch := prefix + other |
| 246 | r.git("checkout", "--quiet", "-b", branch) |
| 247 | r.file("feature.txt", "feature\n") |
| 248 | r.git("commit", "--quiet", "-m", "add the feature") |
| 249 | r.git("push", "--quiet", "origin", branch) |
| 250 | r.valley("ready", "--change", id, branch) |
| 251 | v1 := r.git("rev-parse", branch) |
| 252 | r.moveMain("other") |
| 253 | v2 := r.rebaseOnto(branch) |
| 254 | r.valley("ready", "--change", id, branch) |
| 255 | r.git("fetch", "--quiet", "origin") |
| 256 | |
| 257 | reserved := "c-" + other |
| 258 | r.setRef(requestPrefix+reserved, v1) |
| 259 | r.setRef(outcomePrefix+reserved, r.outcomeRecord(v1, "land")) |
| 260 | if kind, text := r.compare(id, 2, v2); kind != compareNone || text != "" { |
| 261 | t.Fatalf("a request and an outcome under %s counted for %s: %s\n%s", reserved, id[:4], kind, text) |
| 262 | } |
| 263 | r.setRef(requestPrefix+"c-"+id, v1) |
| 264 | if kind, text := r.compare(id, 2, v2); kind != compareClean || !strings.Contains(text, "rebased since you approved v1 at "+short(v1)+", with the same file changes.") { |
| 265 | t.Fatalf("a request under the change's own c-<change ID>: %s\n%s", kind, text) |
| 266 | } |
| 267 | }) |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | // land adds a commit to main on origin that writes a file, as a landing |
| 272 | // would, and fetches it. It returns main's new tip. |
| 273 | func (r *compareRepo) land(name, content, subject string) string { |
| 274 | r.git("checkout", "--quiet", "main") |
| 275 | r.file(name, content) |
| 276 | r.git("commit", "--quiet", "-m", subject) |
| 277 | r.git("push", "--quiet", "origin", "main") |
| 278 | return r.git("rev-parse", "HEAD") |
| 279 | } |
| 280 | |
| 281 | // amend replaces the branch's last commit with the work tree's staged |
| 282 | // files, keeping its message, and returns the new head after readying it. |
| 283 | func (r *compareRepo) amend(id, branch string, args ...string) string { |
| 284 | r.git(append([]string{"commit", "--quiet", "--amend"}, args...)...) |
| 285 | r.git("push", "--quiet", "--force", "origin", branch) |
| 286 | r.valley("ready", "--change", id, branch) |
| 287 | r.git("checkout", "--quiet", "main") |
| 288 | return r.git("rev-parse", branch) |
| 289 | } |
| 290 | |
| 291 | // lines is one line for each of n, numbered, with the line at edit |
| 292 | // replaced. |
| 293 | func lines(n, edit int, replacement string) string { |
| 294 | var b strings.Builder |
| 295 | for i := 1; i <= n; i++ { |
| 296 | if i == edit { |
| 297 | b.WriteString(replacement + "\n") |
| 298 | } else { |
| 299 | b.WriteString("line " + strconv.Itoa(i) + "\n") |
| 300 | } |
| 301 | } |
| 302 | return b.String() |
| 303 | } |
| 304 | |
| 305 | func mustSay(t *testing.T, what, text string, want ...string) { |
| 306 | t.Helper() |
| 307 | for _, line := range want { |
| 308 | if !strings.Contains(text, line) { |
| 309 | t.Fatalf("%s does not say %q:\n%s", what, line, text) |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | // A clean restack names the landings between the two bases, oldest |
| 315 | // first. An edited version lists the files whose change differs and what |
| 316 | // it changes beyond the approved version carried onto main, in git's |
| 317 | // ordinary form, whatever the person's diff configuration; -v adds the |
| 318 | // range-diff. A message or |
| 319 | // an author that changed is an edit, shown whatever the files say. |
| 320 | func TestCompareSaysWhatChanged(t *testing.T) { |
| 321 | r := newCompareRepo(t) |
| 322 | id := strings.Repeat("z", 32) |
| 323 | r.git("checkout", "--quiet", "-b", "topic") |
| 324 | r.file("tool.sh", "#!/bin/sh\n") |
| 325 | r.file("image.bin", "\x00\x01") |
| 326 | r.git("commit", "--quiet", "-m", "add the tool") |
| 327 | r.git("push", "--quiet", "origin", "topic") |
| 328 | r.valley("ready", "--change", id, "topic") |
| 329 | v1 := r.git("rev-parse", "topic") |
| 330 | b0 := r.git("rev-parse", "main") |
| 331 | r.setRef(requestPrefix+"c-"+id, v1) |
| 332 | one, two := r.moveMain("one"), r.moveMain("two") |
| 333 | v2 := r.rebaseOnto("topic") |
| 334 | r.valley("ready", "--change", id, "topic") |
| 335 | r.git("fetch", "--quiet", "origin") |
| 336 | |
| 337 | kind, text := r.compare(id, 2, v2) |
| 338 | want := "zzzz v2 at " + short(v2) + ": rebased since you approved v1 at " + short(v1) + ", with the same file changes.\n" + |
| 339 | "Main moved " + short(b0) + " → " + short(two) + ", landing 2 commits:\n" + |
| 340 | " " + short(one) + " land one\n" + |
| 341 | " " + short(two) + " land two\n" |
| 342 | if kind != compareClean || text != want { |
| 343 | t.Fatalf("clean:\n%s\n%s\nwant:\n%s", kind, text, want) |
| 344 | } |
| 345 | |
| 346 | // v3: the binary file's content and the tool's mode change. git |
| 347 | // range-diff calls the two commits equal, since a binary file's patch |
| 348 | // says only that it differs, but the commit's patch is not the same. |
| 349 | r.setRef(requestPrefix+"c-"+id, v2) |
| 350 | r.git("checkout", "--quiet", "topic") |
| 351 | r.file("image.bin", "\x00\x02") |
| 352 | if err := os.Chmod(filepath.Join(r.work, "tool.sh"), 0o755); err != nil { |
| 353 | t.Fatal(err) |
| 354 | } |
| 355 | r.git("add", "tool.sh") |
| 356 | v3 := r.amend(id, "topic", "--no-edit") |
| 357 | r.git("config", "diff.noprefix", "true") |
| 358 | kind, text = r.compare(id, 3, v3) |
| 359 | r.git("config", "--unset", "diff.noprefix") |
| 360 | mustSay(t, "an edited commit", text, |
| 361 | "zzzz v3 at "+short(v3)+": edited since you approved v2 at "+short(v2)+".\n"+ |
| 362 | "Its base is still "+short(two)+".\n"+ |
| 363 | "1 of 1 commit changed, and 2 files differ from v2.\n2 files changed since v2:\n image.bin\n tool.sh\n"+ |
| 364 | "\n── what v3 changes beyond v2 carried onto main "+short(two)+" ──\ndiff --git a/image.bin b/image.bin\n", |
| 365 | "Binary files a/image.bin and b/image.bin differ\ndiff --git a/tool.sh b/tool.sh\nold mode 100644\nnew mode 100755\n", |
| 366 | ) |
| 367 | if kind != compareChanged || strings.Contains(text, "Commit messages") || strings.Contains(text, "v2: absent") || strings.Contains(text, "range-diff") { |
| 368 | t.Fatalf("an edited commit: %s\n%s", kind, text) |
| 369 | } |
| 370 | _, verbose := r.compareVerbose(id, 3, v3) |
| 371 | mustSay(t, "-v", verbose, " image.bin\n v2: absent -> 100644 ", " tool.sh\n v2: absent -> 100644 ", " v3: absent -> 100755 ", |
| 372 | "── git range-diff "+short(two)+".."+short(v2)+" "+short(two)+".."+short(v3)+" ──\n", |
| 373 | "1: "+short(v2)+" = 1: "+short(v3)+" add the tool") |
| 374 | |
| 375 | // v4: the same files, with another message and another author. |
| 376 | r.setRef(requestPrefix+"c-"+id, v3) |
| 377 | r.git("checkout", "--quiet", "topic") |
| 378 | v4 := r.amend(id, "topic", "-m", "add the tool, renamed", "--author", "Someone Else <else@localhost>") |
| 379 | kind, text = r.compare(id, 4, v4) |
| 380 | mustSay(t, "a changed message and author", text, |
| 381 | ": edited since you approved v3 at "+short(v3)+", with the same file changes.\n", |
| 382 | "Commit messages edited since you approved v3. Read them before approving:\n"+ |
| 383 | " commit 1 of 1, v3 "+short(v3)+" → v4 "+short(v4)+":\n -add the tool\n +add the tool, renamed\n", |
| 384 | "Commit authors edited since you approved v3:\n v3, commit 1 of 1, "+short(v3)+": compare-test <compare-test@localhost> ", |
| 385 | " v4, commit 1 of 1, "+short(v4)+": Someone Else <else@localhost> ", |
| 386 | "Its base is still "+short(two)+".\n", |
| 387 | ) |
| 388 | if kind != compareClean { |
| 389 | t.Fatalf("a changed message alone is still a clean file delta: %s", kind) |
| 390 | } |
| 391 | |
| 392 | // The version given must have the head given. |
| 393 | if _, err := r.runCompare("--tip", two, id, "3", v4); err == nil || !strings.Contains(err.Error(), "v3 of change zzzz records the head") { |
| 394 | t.Fatalf("compare took a head its version does not record: %v", err) |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | // A rebase whose commit is untouched, onto landings that edited the same |
| 399 | // file, says the commit is unchanged and names those landings, with no |
| 400 | // range-diff. An edit after it names, for each file whose change |
| 401 | // differs, what it changes beyond the approved version carried onto |
| 402 | // main, and with -v the landings that edited each file and the |
| 403 | // range-diff. |
| 404 | func TestCompareAttributesFilesMainAlsoEdited(t *testing.T) { |
| 405 | r := newCompareRepo(t) |
| 406 | id := strings.Repeat("w", 32) |
| 407 | r.land("shared.txt", lines(20, 0, ""), "add shared") |
| 408 | b0 := r.git("rev-parse", "main") |
| 409 | r.git("checkout", "--quiet", "-b", "topic") |
| 410 | r.file("shared.txt", lines(20, 2, "topic's line 2")) |
| 411 | r.git("commit", "--quiet", "-m", "edit line 2") |
| 412 | r.git("push", "--quiet", "origin", "topic") |
| 413 | r.valley("ready", "--change", id, "topic") |
| 414 | v1 := r.git("rev-parse", "topic") |
| 415 | r.setRef(requestPrefix+"c-"+id, v1) |
| 416 | shared := r.land("shared.txt", lines(20, 18, "main's line 18"), "edit line 18 of shared, with a subject long enough that the listing has to cut it short") |
| 417 | b1 := r.moveMain("unrelated") |
| 418 | v2 := r.rebaseOnto("topic") |
| 419 | r.valley("ready", "--change", id, "topic") |
| 420 | r.git("fetch", "--quiet", "origin") |
| 421 | |
| 422 | kind, text := r.compare(id, 2, v2) |
| 423 | mustSay(t, "a rebase onto a file main also edited", text, |
| 424 | "wwww v2 at "+short(v2)+": rebased since you approved v1 at "+short(v1)+".\n"+ |
| 425 | "Its commit is unchanged: same patch, same message.\n"+ |
| 426 | "Main moved "+short(b0)+" → "+short(b1)+" and also edited 1 of its files:\n"+ |
| 427 | " shared.txt "+short(shared)+" edit line 18 of shared, with a subject long enough that the listing has to cu…\n"+ |
| 428 | "\n── v2's diff of this file against main "+short(b1)+" ──\ndiff --git a/shared.txt b/shared.txt\n", |
| 429 | "-line 2\n+topic's line 2\n", |
| 430 | ) |
| 431 | if kind != compareChanged || strings.Contains(text, "range-diff") { |
| 432 | t.Fatalf("a rebase onto a file main also edited: %s\n%s", kind, text) |
| 433 | } |
| 434 | |
| 435 | // v3: the commit is edited and adds a file, after main edits the |
| 436 | // shared file again. |
| 437 | r.setRef(requestPrefix+"c-"+id, v2) |
| 438 | again := r.land("shared.txt", lines(20, 18, "main's line 18, again"), "edit line 18 again") |
| 439 | b2 := r.git("rev-parse", "main") |
| 440 | r.rebaseOnto("topic") |
| 441 | r.git("checkout", "--quiet", "topic") |
| 442 | r.file("shared.txt", strings.Replace(r.git("show", "HEAD:shared.txt")+"\n", "topic's line 2", "topic's line 2, edited", 1)) |
| 443 | r.file("new.txt", "new\n") |
| 444 | v3 := r.amend(id, "topic", "--no-edit") |
| 445 | r.git("fetch", "--quiet", "origin") |
| 446 | kind, text = r.compare(id, 3, v3) |
| 447 | mustSay(t, "an edit after a rebase", text, |
| 448 | "wwww v3 at "+short(v3)+": edited since you approved v2 at "+short(v2)+".\n"+ |
| 449 | "Main moved "+short(b1)+" → "+short(b2)+".\n"+ |
| 450 | "1 of 1 commit changed, and 2 files differ from v2.\n"+ |
| 451 | "2 files changed since v2:\n new.txt\n shared.txt\n"+ |
| 452 | "\n── what v3 changes beyond v2 carried onto main "+short(b2)+" ──\ndiff --git a/new.txt b/new.txt\n", |
| 453 | "@@ -1,5 +1,5 @@\n line 1\n-topic's line 2\n+topic's line 2, edited\n", |
| 454 | ) |
| 455 | _, verbose := r.compareVerbose(id, 3, v3) |
| 456 | mustSay(t, "-v", verbose, " new.txt not edited on main\n", " shared.txt "+short(again)+" edit line 18 again\n", |
| 457 | "\n── git range-diff "+short(b1)+".."+short(v2)+" "+short(b2)+".."+short(v3)+" ──\n") |
| 458 | if kind != compareChanged { |
| 459 | t.Fatalf("an edit after a rebase: %s", kind) |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | // style changes no line's text, styles a comparison's title, headings and |
| 464 | // summary, and leaves a diff's lines as they are; with NO_COLOR set, or |
| 465 | // for no terminal, it copies the page. |
| 466 | func TestStyleKeepsEveryLine(t *testing.T) { |
| 467 | page := "layer heading\n" + |
| 468 | "zzzz v3 at 1234abc: edited since you approved v2 at 5678def.\n" + |
| 469 | "does oc-1234567: a knot\n" + |
| 470 | "Main moved 1111aaa → 2222bbb.\n" + |
| 471 | " shared.txt 3333ccc edit line 18\n" + |
| 472 | "\n── v3's diff of this file against main 2222bbb ──\n" + |
| 473 | "diff --git a/f b/f\n" + |
| 474 | "+edited 4444ddd\n" |
| 475 | style := func(args ...string) string { |
| 476 | var out bytes.Buffer |
| 477 | if err := runStyle(args, strings.NewReader(page), &out); err != nil { |
| 478 | t.Fatal(err) |
| 479 | } |
| 480 | return out.String() |
| 481 | } |
| 482 | t.Setenv("NO_COLOR", "") |
| 483 | styled := style("--paged") |
| 484 | if got := styleEscape.ReplaceAllString(styled, ""); got != page { |
| 485 | t.Fatalf("style changed the text:\n%s", got) |
| 486 | } |
| 487 | for _, want := range []string{ |
| 488 | "layer heading\n", |
| 489 | "\x1b[1mzzzz v3 at 1234abc: \x1b[33medited\x1b[39m since you approved v2 at 5678def.\x1b[22m\n", |
| 490 | "Main moved \x1b[2m1111aaa\x1b[22m → \x1b[2m2222bbb\x1b[22m.\n", |
| 491 | "\x1b[36m── v3's diff of this file against main 2222bbb ──\x1b[39m\n", |
| 492 | "diff --git a/f b/f\n+edited 4444ddd\n", |
| 493 | } { |
| 494 | if !strings.Contains(styled, want) { |
| 495 | t.Fatalf("style does not give %q:\n%q", want, styled) |
| 496 | } |
| 497 | } |
| 498 | if got := style(); got != page { |
| 499 | t.Fatalf("style styled a page for no terminal:\n%q", got) |
| 500 | } |
| 501 | t.Setenv("NO_COLOR", "1") |
| 502 | if got := style("--paged"); got != page { |
| 503 | t.Fatalf("style styled a page with NO_COLOR set:\n%q", got) |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | // numbered is n numbered lines, with the lines edits names replaced. |
| 508 | func numbered(n int, edits map[int]string) string { |
| 509 | var b strings.Builder |
| 510 | for i := 1; i <= n; i++ { |
| 511 | if e, ok := edits[i]; ok { |
| 512 | b.WriteString(e + "\n") |
| 513 | } else { |
| 514 | b.WriteString("line " + strconv.Itoa(i) + "\n") |
| 515 | } |
| 516 | } |
| 517 | return b.String() |
| 518 | } |
| 519 | |
| 520 | // commitFiles writes the files on the branch checked out, commits and |
| 521 | // pushes them, and returns the commit. A file whose content is "" is |
| 522 | // deleted. |
| 523 | func (r *compareRepo) commitFiles(branch, subject string, files map[string]string) string { |
| 524 | r.t.Helper() |
| 525 | r.git("checkout", "--quiet", branch) |
| 526 | for name, content := range files { |
| 527 | if content == "" { |
| 528 | r.git("rm", "--quiet", name) |
| 529 | } else { |
| 530 | r.file(name, content) |
| 531 | } |
| 532 | } |
| 533 | r.git("commit", "--quiet", "-m", subject) |
| 534 | r.git("push", "--quiet", "--force", "origin", branch) |
| 535 | return r.git("rev-parse", "HEAD") |
| 536 | } |
| 537 | |
| 538 | // newCarryRepo is main holding a.txt, b.txt and c.txt, 20 lines each, and |
| 539 | // a change that edits line 2 of each, readied and approved as v1. It |
| 540 | // returns v1 and main's tip under it. Every commit has one author date, |
| 541 | // so that an author line differs only where a test changes it. |
| 542 | func newCarryRepo(t *testing.T, id string) (r *compareRepo, v1, b0 string) { |
| 543 | t.Setenv("GIT_AUTHOR_DATE", "1700000000 +0000") |
| 544 | r = newCompareRepo(t) |
| 545 | b0 = r.commitFiles("main", "add the files", map[string]string{"a.txt": numbered(20, nil), "b.txt": numbered(20, nil), "c.txt": numbered(20, nil)}) |
| 546 | r.git("checkout", "--quiet", "-b", "topic") |
| 547 | edit := numbered(20, map[int]string{2: "topic's line 2"}) |
| 548 | r.commitFiles("topic", "edit line 2", map[string]string{"a.txt": edit, "b.txt": edit, "c.txt": edit}) |
| 549 | r.valley("ready", "--change", id, "topic") |
| 550 | v1 = r.git("rev-parse", "topic") |
| 551 | r.setRef(requestPrefix+"c-"+id, v1) |
| 552 | return r, v1, b0 |
| 553 | } |
| 554 | |
| 555 | // mainEditsLine5 lands an edit of line 5 of each of the three files: near |
| 556 | // enough the change's line 2 that a rebase changes its commit's patch, far |
| 557 | // enough that it merges. |
| 558 | func (r *compareRepo) mainEditsLine5() string { |
| 559 | r.t.Helper() |
| 560 | edit := numbered(20, map[int]string{5: "main's line 5"}) |
| 561 | return r.commitFiles("main", "edit line 5", map[string]string{"a.txt": edit, "b.txt": edit, "c.txt": edit}) |
| 562 | } |
| 563 | |
| 564 | // readyRebased rebases the change by hand onto main, applies files on |
| 565 | // top, amending its commit, readies the result and returns it. |
| 566 | func (r *compareRepo) readyRebased(id string, files map[string]string) string { |
| 567 | r.t.Helper() |
| 568 | r.git("checkout", "--quiet", "topic") |
| 569 | r.git("rebase", "--quiet", "main") |
| 570 | for name, content := range files { |
| 571 | if content == "" { |
| 572 | r.git("rm", "--quiet", name) |
| 573 | } else { |
| 574 | r.file(name, content) |
| 575 | } |
| 576 | } |
| 577 | if len(files) > 0 { |
| 578 | r.git("commit", "--quiet", "--amend", "--no-edit") |
| 579 | } |
| 580 | r.git("push", "--quiet", "--force", "origin", "topic") |
| 581 | r.valley("ready", "--change", id, "topic") |
| 582 | r.git("checkout", "--quiet", "main") |
| 583 | r.git("fetch", "--quiet", "origin") |
| 584 | return r.git("rev-parse", "topic") |
| 585 | } |
| 586 | |
| 587 | // blobIDs are the object names a diff's index lines carry, which the |
| 588 | // expected pages leave out. |
| 589 | var blobIDs = regexp.MustCompile(`(?m)^( *)index [0-9a-f]+\.\.[0-9a-f]+`) |
| 590 | |
| 591 | // page is want with each {name} replaced by the short form of the object |
| 592 | // ID after it in vars. |
| 593 | func page(want string, vars ...string) string { |
| 594 | for i := 0; i+1 < len(vars); i += 2 { |
| 595 | want = strings.ReplaceAll(want, "{"+vars[i]+"}", short(vars[i+1])) |
| 596 | } |
| 597 | return want |
| 598 | } |
| 599 | |
| 600 | func wantPage(t *testing.T, what, kind, text, wantKind, want string) { |
| 601 | t.Helper() |
| 602 | if got := blobIDs.ReplaceAllString(text, "${1}index …"); kind != wantKind || got != want { |
| 603 | t.Fatalf("%s: %s\n%s\nwant %s:\n%s", what, kind, got, wantKind, want) |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | // A hand rebase onto main's edits of nearby lines changes the commit and |
| 608 | // none of the author's edits: every file is the approved version carried |
| 609 | // onto main, and the page says so in a few lines, with no range-diff. |
| 610 | func TestEditedPageCarriesUnchangedFiles(t *testing.T) { |
| 611 | id := strings.Repeat("k", 32) |
| 612 | r, v1, b0 := newCarryRepo(t, id) |
| 613 | b1 := r.mainEditsLine5() |
| 614 | v2 := r.readyRebased(id, nil) |
| 615 | kind, text := r.compare(id, 2, v2) |
| 616 | wantPage(t, "a hand rebase", kind, text, compareChanged, |
| 617 | "kkkk v2 at "+short(v2)+": edited since you approved v1 at "+short(v1)+".\n"+ |
| 618 | "Main moved "+short(b0)+" → "+short(b1)+".\n"+ |
| 619 | "1 of 1 commit changed, and 3 files differ from v1.\n"+ |
| 620 | "3 files carried unchanged onto main:\n a.txt\n b.txt\n c.txt\n"+ |
| 621 | "\n── what v2 changes beyond v1 carried onto main "+short(b1)+" ──\n"+ |
| 622 | " none: v2 is v1 carried onto main and nothing else.\n") |
| 623 | |
| 624 | // v3: one further edit, and a new file. |
| 625 | r.setRef(requestPrefix+"c-"+id, v2) |
| 626 | v3 := r.readyRebased(id, map[string]string{ |
| 627 | "b.txt": numbered(20, map[int]string{2: "topic's line 2", 5: "main's line 5", 14: "topic's line 14"}), |
| 628 | "new.txt": "new\n"}) |
| 629 | kind, text = r.compare(id, 3, v3) |
| 630 | further := page(`kkkk v3 at {v3}: edited since you approved v2 at {v2}. |
| 631 | Its base is still {b1}. |
| 632 | 1 of 1 commit changed, and 2 files differ from v2. |
| 633 | 2 files changed since v2: |
| 634 | b.txt |
| 635 | new.txt |
| 636 | `, "v2", v2, "v3", v3, "b1", b1) |
| 637 | beyond := page(` |
| 638 | ── what v3 changes beyond v2 carried onto main {b1} ── |
| 639 | diff --git a/b.txt b/b.txt |
| 640 | index … 100644 |
| 641 | --- a/b.txt |
| 642 | +++ b/b.txt |
| 643 | @@ -11,7 +11,7 @@ line 10 |
| 644 | line 11 |
| 645 | line 12 |
| 646 | line 13 |
| 647 | -line 14 |
| 648 | +topic's line 14 |
| 649 | line 15 |
| 650 | line 16 |
| 651 | line 17 |
| 652 | diff --git a/new.txt b/new.txt |
| 653 | new file mode 100644 |
| 654 | index … |
| 655 | --- /dev/null |
| 656 | +++ b/new.txt |
| 657 | @@ -0,0 +1 @@ |
| 658 | +new |
| 659 | `, "b1", b1) |
| 660 | wantPage(t, "a further edit", kind, text, compareChanged, further+beyond) |
| 661 | |
| 662 | // -v adds each file's images and the range-diff. |
| 663 | _, text = r.compareVerbose(id, 3, v3) |
| 664 | sum := func(rev string) string { |
| 665 | out := r.git("cat-file", "blob", rev) |
| 666 | return fmt.Sprintf("%x", sha256.Sum256([]byte(out+"\n")))[:12] |
| 667 | } |
| 668 | verbose := strings.Replace(further, " b.txt\n new.txt\n", " b.txt\n"+ |
| 669 | " v2: 100644 "+sum(b1+":b.txt")+" -> 100644 "+sum(v2+":b.txt")+"\n"+ |
| 670 | " v3: 100644 "+sum(b1+":b.txt")+" -> 100644 "+sum(v3+":b.txt")+"\n"+ |
| 671 | " new.txt\n v2: unchanged\n v3: absent -> 100644 "+sum(v3+":new.txt")+"\n", 1) + page(` |
| 672 | ── git range-diff {b1}..{v2} {b1}..{v3} ── |
| 673 | 1: {v2} ! 1: {v3} edit line 2 |
| 674 | @@ b.txt |
| 675 | line 3 |
| 676 | line 4 |
| 677 | main's line 5 |
| 678 | +@@ b.txt: line 10 |
| 679 | + line 11 |
| 680 | + line 12 |
| 681 | + line 13 |
| 682 | +-line 14 |
| 683 | ++topic's line 14 |
| 684 | + line 15 |
| 685 | + line 16 |
| 686 | + line 17 |
| 687 | |
| 688 | ## c.txt ## |
| 689 | @@ |
| 690 | @@ c.txt |
| 691 | line 3 |
| 692 | line 4 |
| 693 | main's line 5 |
| 694 | + |
| 695 | + ## new.txt (new) ## |
| 696 | +@@ |
| 697 | ++new |
| 698 | `, "b1", b1, "v2", v2, "v3", v3) + beyond |
| 699 | wantPage(t, "-v", kind, text, compareChanged, verbose) |
| 700 | } |
| 701 | |
| 702 | // A file dropped from the change shows as the approved edit reverted, and |
| 703 | // an added line moved within its file shows though its text is the same. |
| 704 | // With main where it was, the carry is the approved version itself. |
| 705 | func TestEditedPageShowsDroppedAndMovedLines(t *testing.T) { |
| 706 | id := strings.Repeat("l", 32) |
| 707 | r, v1, b0 := newCarryRepo(t, id) |
| 708 | v2 := r.readyRebased(id, map[string]string{ |
| 709 | "c.txt": numbered(20, nil), |
| 710 | "a.txt": strings.Replace(strings.Replace(numbered(20, nil), "line 2\n", "", 1), "line 10\n", "line 10\ntopic's line 2\n", 1), |
| 711 | }) |
| 712 | kind, text := r.compare(id, 2, v2) |
| 713 | wantPage(t, "a dropped file and a moved line", kind, text, compareChanged, page(`llll v2 at {v2}: edited since you approved v1 at {v1}. |
| 714 | Its base is still {b0}. |
| 715 | 1 of 1 commit changed, and 2 files differ from v1. |
| 716 | 2 files changed since v1: |
| 717 | a.txt |
| 718 | c.txt |
| 719 | |
| 720 | ── what v2 changes beyond v1 carried onto main {b0} ── |
| 721 | diff --git a/a.txt b/a.txt |
| 722 | index … 100644 |
| 723 | --- a/a.txt |
| 724 | +++ b/a.txt |
| 725 | @@ -1,5 +1,4 @@ |
| 726 | line 1 |
| 727 | -topic's line 2 |
| 728 | line 3 |
| 729 | line 4 |
| 730 | line 5 |
| 731 | @@ -8,6 +7,7 @@ line 7 |
| 732 | line 8 |
| 733 | line 9 |
| 734 | line 10 |
| 735 | +topic's line 2 |
| 736 | line 11 |
| 737 | line 12 |
| 738 | line 13 |
| 739 | diff --git a/c.txt b/c.txt |
| 740 | index … 100644 |
| 741 | --- a/c.txt |
| 742 | +++ b/c.txt |
| 743 | @@ -1,5 +1,5 @@ |
| 744 | line 1 |
| 745 | -topic's line 2 |
| 746 | +line 2 |
| 747 | line 3 |
| 748 | line 4 |
| 749 | line 5 |
| 750 | `, "v1", v1, "v2", v2, "b0", b0)) |
| 751 | } |
| 752 | |
| 753 | // A content conflict the author resolved shows as the carry's conflict |
| 754 | // markers removed and the resolution added, naming the landing that |
| 755 | // edited the file, whatever merge attribute the person's configuration |
| 756 | // sets. |
| 757 | func TestEditedPageShowsAResolvedConflict(t *testing.T) { |
| 758 | id := strings.Repeat("m", 32) |
| 759 | r, v1, b0 := newCarryRepo(t, id) |
| 760 | b1 := r.commitFiles("main", "reword line 2 of a", map[string]string{"a.txt": numbered(20, map[int]string{2: "main's line 2"})}) |
| 761 | r.git("checkout", "--quiet", "topic") |
| 762 | r.git("reset", "--quiet", "--hard", b1) |
| 763 | edit := numbered(20, map[int]string{2: "topic's line 2"}) |
| 764 | r.file("a.txt", numbered(20, map[int]string{2: "main's and topic's line 2"})) |
| 765 | r.file("b.txt", edit) |
| 766 | r.file("c.txt", edit) |
| 767 | r.git("commit", "--quiet", "-m", "edit line 2") |
| 768 | r.git("push", "--quiet", "--force", "origin", "topic") |
| 769 | r.valley("ready", "--change", id, "topic") |
| 770 | r.git("checkout", "--quiet", "main") |
| 771 | r.git("fetch", "--quiet", "origin") |
| 772 | v2 := r.git("rev-parse", "topic") |
| 773 | kind, text := r.compare(id, 2, v2) |
| 774 | wantPage(t, "a resolved conflict", kind, text, compareChanged, page(`mmmm v2 at {v2}: edited since you approved v1 at {v1}. |
| 775 | Main moved {b0} → {b1}. |
| 776 | 1 of 1 commit changed, and 1 file differs from v1. |
| 777 | 1 file in conflict with main: |
| 778 | a.txt {b1} reword line 2 of a |
| 779 | |
| 780 | ── what v2 changes beyond v1 carried onto main {b1} ── |
| 781 | none: outside its conflicts with main, v2 is v1 carried onto main and nothing else. |
| 782 | |
| 783 | ── a.txt: CONFLICT (contents) ── |
| 784 | Removed: the conflict, main's side and v1's. Added: v2's resolution. |
| 785 | diff --git a/a.txt b/a.txt |
| 786 | index … 100644 |
| 787 | --- a/a.txt |
| 788 | +++ b/a.txt |
| 789 | @@ -1,9 +1,5 @@ |
| 790 | line 1 |
| 791 | -<<<<<<< `+b1+` |
| 792 | -main's line 2 |
| 793 | -======= |
| 794 | -topic's line 2 |
| 795 | ->>>>>>> `+v1+` |
| 796 | +main's and topic's line 2 |
| 797 | line 3 |
| 798 | line 4 |
| 799 | line 5 |
| 800 | `, "v1", v1, "v2", v2, "b0", b0, "b1", b1)) |
| 801 | if err := os.MkdirAll(filepath.Join(os.Getenv("XDG_CONFIG_HOME"), "git"), 0o755); err != nil { |
| 802 | t.Fatal(err) |
| 803 | } |
| 804 | for _, f := range []string{filepath.Join(os.Getenv("XDG_CONFIG_HOME"), "git", "attributes"), filepath.Join(r.work, ".git", "info", "attributes")} { |
| 805 | if err := os.WriteFile(f, []byte("* merge=union\n"), 0o644); err != nil { |
| 806 | t.Fatal(err) |
| 807 | } |
| 808 | } |
| 809 | _, union := r.compare(id, 2, v2) |
| 810 | if union != text { |
| 811 | t.Errorf("merge=union changed the page:\n%s", union) |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | // A path in any other conflict shows each version's own diff of it, so an |
| 816 | // approved deletion the shown version does not make is in view, and a |
| 817 | // rename conflict names every path it involves. |
| 818 | func TestEditedPageShowsOtherConflicts(t *testing.T) { |
| 819 | id := strings.Repeat("n", 32) |
| 820 | r, _, _ := newCarryRepo(t, id) |
| 821 | base := r.commitFiles("main", "add gone and r", map[string]string{"gone.txt": numbered(5, nil), "r.txt": numbered(8, nil)}) |
| 822 | r.git("checkout", "--quiet", "topic") |
| 823 | r.git("rebase", "--quiet", "main") |
| 824 | r.git("rm", "--quiet", "gone.txt") |
| 825 | r.git("mv", "r.txt", "s.txt") |
| 826 | r.git("commit", "--quiet", "--amend", "--no-edit") |
| 827 | r.git("push", "--quiet", "--force", "origin", "topic") |
| 828 | r.valley("ready", "--change", id, "topic") |
| 829 | v1 := r.git("rev-parse", "topic") |
| 830 | r.setRef(requestPrefix+"c-"+id, v1) |
| 831 | r.git("checkout", "--quiet", "main") |
| 832 | r.git("mv", "r.txt", "t.txt") |
| 833 | b1 := r.commitFiles("main", "edit gone, rename r", map[string]string{"gone.txt": numbered(5, map[int]string{3: "main's line 3"})}) |
| 834 | r.git("checkout", "--quiet", "topic") |
| 835 | r.git("reset", "--quiet", "--hard", b1) |
| 836 | edit := numbered(20, map[int]string{2: "topic's line 2"}) |
| 837 | r.commitFiles("topic", "edit line 2", map[string]string{"a.txt": edit, "b.txt": edit, "c.txt": edit}) |
| 838 | r.valley("ready", "--change", id, "topic") |
| 839 | r.git("checkout", "--quiet", "main") |
| 840 | r.git("fetch", "--quiet", "origin") |
| 841 | v2 := r.git("rev-parse", "topic") |
| 842 | kind, text := r.compare(id, 3, v2) |
| 843 | wantPage(t, "other conflicts", kind, text, compareChanged, page(`nnnn v3 at {v3}: edited since you approved v2 at {v2}. |
| 844 | Main moved {b0} → {b1}. |
| 845 | 1 of 1 commit changed, and 3 files differ from v2. |
| 846 | 4 files in conflict with main: |
| 847 | gone.txt {b1} edit gone, rename r |
| 848 | r.txt {b1} edit gone, rename r |
| 849 | s.txt not edited on main |
| 850 | t.txt {b1} edit gone, rename r |
| 851 | |
| 852 | ── what v3 changes beyond v2 carried onto main {b1} ── |
| 853 | none: outside its conflicts with main, v3 is v2 carried onto main and nothing else. |
| 854 | |
| 855 | ── gone.txt: CONFLICT (modify/delete) ── |
| 856 | v2's diff against main {b0}, indented: |
| 857 | diff --git a/gone.txt b/gone.txt |
| 858 | deleted file mode 100644 |
| 859 | index … |
| 860 | --- a/gone.txt |
| 861 | +++ /dev/null |
| 862 | @@ -1,5 +0,0 @@ |
| 863 | -line 1 |
| 864 | -line 2 |
| 865 | -line 3 |
| 866 | -line 4 |
| 867 | -line 5 |
| 868 | ── v3's diff of gone.txt against main {b1} ── |
| 869 | none: v3 leaves it as main has it. |
| 870 | |
| 871 | ── r.txt: CONFLICT (rename/rename) ── |
| 872 | v2's diff against main {b0}, indented: |
| 873 | diff --git a/r.txt b/r.txt |
| 874 | deleted file mode 100644 |
| 875 | index … |
| 876 | --- a/r.txt |
| 877 | +++ /dev/null |
| 878 | @@ -1,8 +0,0 @@ |
| 879 | -line 1 |
| 880 | -line 2 |
| 881 | -line 3 |
| 882 | -line 4 |
| 883 | -line 5 |
| 884 | -line 6 |
| 885 | -line 7 |
| 886 | -line 8 |
| 887 | ── v3's diff of r.txt against main {b1} ── |
| 888 | none: v3 leaves it as main has it. |
| 889 | |
| 890 | ── s.txt: CONFLICT (rename/rename) ── |
| 891 | v2's diff against main {b0}, indented: |
| 892 | diff --git a/s.txt b/s.txt |
| 893 | new file mode 100644 |
| 894 | index … |
| 895 | --- /dev/null |
| 896 | +++ b/s.txt |
| 897 | @@ -0,0 +1,8 @@ |
| 898 | +line 1 |
| 899 | +line 2 |
| 900 | +line 3 |
| 901 | +line 4 |
| 902 | +line 5 |
| 903 | +line 6 |
| 904 | +line 7 |
| 905 | +line 8 |
| 906 | ── v3's diff of s.txt against main {b1} ── |
| 907 | none: v3 leaves it as main has it. |
| 908 | |
| 909 | ── t.txt: CONFLICT (rename/rename) ── |
| 910 | v2's diff against main {b0}, indented: |
| 911 | none: v2 leaves it as main has it. |
| 912 | ── v3's diff of t.txt against main {b1} ── |
| 913 | none: v3 leaves it as main has it. |
| 914 | `, "v2", v1, "v3", v2, "b0", base, "b1", b1)) |
| 915 | // A note beside the approved version's indented diff, whose new side |
| 916 | // is not the shown head, anchors to nothing. |
| 917 | all := anchors([]byte(text)) |
| 918 | for n, line := range strings.Split(text, "\n") { |
| 919 | if strings.HasPrefix(line, " ") && anchorOf(all, n+1) != "(unanchored)" { |
| 920 | t.Errorf("line %d, %q, anchors to %s", n+1, line, anchorOf(all, n+1)) |
| 921 | } |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | // A message edited in one line of many shows as a line diff of the two; |
| 926 | // an author or an encoding that differs shows both in full. |
| 927 | func TestEditedMessagesShowAsALineDiff(t *testing.T) { |
| 928 | id := strings.Repeat("o", 32) |
| 929 | r, _, b0 := newCarryRepo(t, id) |
| 930 | r.git("checkout", "--quiet", "topic") |
| 931 | body := "edit line 2\n\n" + numbered(12, nil) |
| 932 | v2 := r.amend(id, "topic", "-m", body) |
| 933 | r.setRef(requestPrefix+"c-"+id, v2) |
| 934 | r.git("checkout", "--quiet", "topic") |
| 935 | v3 := r.amend(id, "topic", "-m", strings.Replace(body, "line 6\n", "line six\n", 1)) |
| 936 | kind, text := r.compare(id, 3, v3) |
| 937 | wantPage(t, "a message", kind, text, compareClean, page(`oooo v3 at {v3}: edited since you approved v2 at {v2}, with the same file changes. |
| 938 | Commit messages edited since you approved v2. Read them before approving: |
| 939 | commit 1 of 1, v2 {v2} → v3 {v3}: |
| 940 | (4 unchanged lines) |
| 941 | line 3 |
| 942 | line 4 |
| 943 | line 5 |
| 944 | -line 6 |
| 945 | +line six |
| 946 | line 7 |
| 947 | line 8 |
| 948 | line 9 |
| 949 | (3 unchanged lines) |
| 950 | Its base is still {b0}. |
| 951 | `, "v2", v2, "v3", v3, "b0", b0)) |
| 952 | r.setRef(requestPrefix+"c-"+id, v3) |
| 953 | r.git("checkout", "--quiet", "topic") |
| 954 | v4 := r.amend(id, "topic", "--no-edit", "--author", "Someone Else <else@localhost>") |
| 955 | kind, text = r.compare(id, 4, v4) |
| 956 | date := strings.SplitN(r.git("log", "-1", "--format=%ad", "--date=raw", v4), "\n", 2)[0] |
| 957 | wantPage(t, "an author", kind, text, compareClean, page(`oooo v4 at {v4}: edited since you approved v3 at {v3}, with the same file changes. |
| 958 | Commit authors edited since you approved v3: |
| 959 | v3, commit 1 of 1, {v3}: compare-test <compare-test@localhost> `+r.git("log", "-1", "--format=%ad", "--date=raw", v3)+` |
| 960 | v4, commit 1 of 1, {v4}: Someone Else <else@localhost> `+date+` |
| 961 | Its base is still {b0}. |
| 962 | `, "v3", v3, "v4", v4, "b0", b0)) |
| 963 | r.setRef(requestPrefix+"c-"+id, v4) |
| 964 | r.git("checkout", "--quiet", "topic") |
| 965 | r.git("config", "i18n.commitEncoding", "ISO-8859-1") |
| 966 | v5 := r.amend(id, "topic", "--no-edit") |
| 967 | r.git("config", "--unset", "i18n.commitEncoding") |
| 968 | kind, text = r.compare(id, 5, v5) |
| 969 | wantPage(t, "an encoding", kind, text, compareClean, page(`oooo v5 at {v5}: edited since you approved v4 at {v4}, with the same file changes. |
| 970 | Commit messages edited since you approved v4. Read them before approving: |
| 971 | v4, commit 1 of 1, {v4}: |
| 972 | edit line 2 |
| 973 | |
| 974 | line 1 |
| 975 | line 2 |
| 976 | line 3 |
| 977 | line 4 |
| 978 | line 5 |
| 979 | line six |
| 980 | line 7 |
| 981 | line 8 |
| 982 | line 9 |
| 983 | line 10 |
| 984 | line 11 |
| 985 | line 12 |
| 986 | v5, commit 1 of 1, {v5}: |
| 987 | (encoding ISO-8859-1) |
| 988 | edit line 2 |
| 989 | |
| 990 | line 1 |
| 991 | line 2 |
| 992 | line 3 |
| 993 | line 4 |
| 994 | line 5 |
| 995 | line six |
| 996 | line 7 |
| 997 | line 8 |
| 998 | line 9 |
| 999 | line 10 |
| 1000 | line 11 |
| 1001 | line 12 |
| 1002 | Its base is still {b0}. |
| 1003 | `, "v4", v4, "v5", v5, "b0", b0)) |
| 1004 | } |
| 1005 | |
| 1006 | // When the carry cannot be computed, the page lists every file whose |
| 1007 | // change differs, then the range-diff and the shown version's diff. |
| 1008 | func TestEditedPageFallsBack(t *testing.T) { |
| 1009 | id := strings.Repeat("p", 32) |
| 1010 | r, v1, b0 := newCarryRepo(t, id) |
| 1011 | b1 := r.mainEditsLine5() |
| 1012 | v2 := r.readyRebased(id, nil) |
| 1013 | t.Setenv("TMPDIR", filepath.Join(r.work, "absent")) |
| 1014 | kind, text := r.compare(id, 2, v2) |
| 1015 | wantPage(t, "no carry", kind, text, compareChanged, page(`pppp v2 at {v2}: edited since you approved v1 at {v1}. |
| 1016 | Main moved {b0} → {b1}. |
| 1017 | 1 of 1 commit changed, and 3 files differ from v1: |
| 1018 | a.txt {b1} edit line 5 |
| 1019 | b.txt {b1} edit line 5 |
| 1020 | c.txt {b1} edit line 5 |
| 1021 | |
| 1022 | The carry of v1 onto main {b1} could not be computed: no scratch git directory could be made. |
| 1023 | |
| 1024 | ── git range-diff {b0}..{v1} {b1}..{v2} ── |
| 1025 | 1: {v1} ! 1: {v2} edit line 2 |
| 1026 | @@ a.txt |
| 1027 | +topic's line 2 |
| 1028 | line 3 |
| 1029 | line 4 |
| 1030 | - line 5 |
| 1031 | + main's line 5 |
| 1032 | |
| 1033 | ## b.txt ## |
| 1034 | @@ |
| 1035 | @@ b.txt |
| 1036 | +topic's line 2 |
| 1037 | line 3 |
| 1038 | line 4 |
| 1039 | - line 5 |
| 1040 | + main's line 5 |
| 1041 | |
| 1042 | ## c.txt ## |
| 1043 | @@ |
| 1044 | @@ c.txt |
| 1045 | +topic's line 2 |
| 1046 | line 3 |
| 1047 | line 4 |
| 1048 | - line 5 |
| 1049 | + main's line 5 |
| 1050 | |
| 1051 | ── v2's diff of these 3 files against main {b1} ── |
| 1052 | diff --git a/a.txt b/a.txt |
| 1053 | index … 100644 |
| 1054 | --- a/a.txt |
| 1055 | +++ b/a.txt |
| 1056 | @@ -1,5 +1,5 @@ |
| 1057 | line 1 |
| 1058 | -line 2 |
| 1059 | +topic's line 2 |
| 1060 | line 3 |
| 1061 | line 4 |
| 1062 | main's line 5 |
| 1063 | diff --git a/b.txt b/b.txt |
| 1064 | index … 100644 |
| 1065 | --- a/b.txt |
| 1066 | +++ b/b.txt |
| 1067 | @@ -1,5 +1,5 @@ |
| 1068 | line 1 |
| 1069 | -line 2 |
| 1070 | +topic's line 2 |
| 1071 | line 3 |
| 1072 | line 4 |
| 1073 | main's line 5 |
| 1074 | diff --git a/c.txt b/c.txt |
| 1075 | index … 100644 |
| 1076 | --- a/c.txt |
| 1077 | +++ b/c.txt |
| 1078 | @@ -1,5 +1,5 @@ |
| 1079 | line 1 |
| 1080 | -line 2 |
| 1081 | +topic's line 2 |
| 1082 | line 3 |
| 1083 | line 4 |
| 1084 | main's line 5 |
| 1085 | `, "v1", v1, "v2", v2, "b0", b0, "b1", b1)) |
| 1086 | } |
| 1087 | |
| 1088 | func TestConflictReportsParse(t *testing.T) { |
| 1089 | tree := strings.Repeat("a", 40) |
| 1090 | c, err := parseConflicts(tree + "\x00100644 " + strings.Repeat("b", 40) + " 2\ta.txt\x00\x00" + |
| 1091 | "1\x00a.txt\x00Auto-merging\x00Auto-merging a.txt\x00" + |
| 1092 | "1\x00a.txt\x00CONFLICT (contents)\x00Merge conflict in a.txt\x00" + |
| 1093 | "3\x00r.txt\x00s.txt\x00t.txt\x00CONFLICT (rename/rename)\x00r.txt renamed\x00" + |
| 1094 | "1\x00d\x00CONFLICT (file/directory)\x00d is a directory\x00") |
| 1095 | if err != nil || c.tree != tree || fmt.Sprint(c.conflicts) != "map[a.txt:[CONFLICT (contents)] d:[CONFLICT (file/directory)] r.txt:[CONFLICT (rename/rename)] s.txt:[CONFLICT (rename/rename)] t.txt:[CONFLICT (rename/rename)]]" { |
| 1096 | t.Fatalf("%v %v", c, err) |
| 1097 | } |
| 1098 | for _, bad := range []string{"", tree, tree + "\x00", tree + "\x00junk\x00", tree + "\x00\x002\x00a\x00T\x00m\x00", tree + "\x00\x00x\x00a\x00T\x00m\x00"} { |
| 1099 | if _, err := parseConflicts(bad); err == nil { |
| 1100 | t.Errorf("%q parsed", bad) |
| 1101 | } |
| 1102 | } |
| 1103 | } |