| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "path/filepath" |
| 8 | "strconv" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | ) |
| 12 | |
| 13 | // compareRepo is an origin and a clone of it, for compare's tests. |
| 14 | type compareRepo struct { |
| 15 | t *testing.T |
| 16 | origin, work string |
| 17 | } |
| 18 | |
| 19 | func newCompareRepo(t *testing.T) *compareRepo { |
| 20 | t.Helper() |
| 21 | dir := t.TempDir() |
| 22 | t.Setenv("HOME", dir) |
| 23 | t.Setenv("XDG_CONFIG_HOME", filepath.Join(dir, "config")) |
| 24 | t.Setenv("GIT_CONFIG_NOSYSTEM", "1") |
| 25 | t.Setenv("VALLEY_KNOWN_SIGNERS", "") |
| 26 | r := &compareRepo{t: t, origin: filepath.Join(dir, "origin.git"), work: filepath.Join(dir, "work")} |
| 27 | r.in(dir, "init", "--quiet", "--bare", "--initial-branch=main", r.origin) |
| 28 | r.in(dir, "clone", "--quiet", r.origin, r.work) |
| 29 | r.git("config", "user.name", "compare-test") |
| 30 | r.git("config", "user.email", "compare-test@localhost") |
| 31 | r.git("config", "valley.principal", "alice") |
| 32 | r.git("checkout", "--quiet", "-b", "main") |
| 33 | r.file("README", "base\n") |
| 34 | r.git("commit", "--quiet", "-m", "base") |
| 35 | r.git("push", "--quiet", "origin", "main") |
| 36 | t.Chdir(r.work) |
| 37 | return r |
| 38 | } |
| 39 | |
| 40 | func (r *compareRepo) in(dir string, args ...string) string { |
| 41 | r.t.Helper() |
| 42 | cmd := exec.Command("git", args...) |
| 43 | cmd.Dir = dir |
| 44 | var stderr bytes.Buffer |
| 45 | cmd.Stderr = &stderr |
| 46 | out, err := cmd.Output() |
| 47 | if err != nil { |
| 48 | r.t.Fatalf("git %s: %v\n%s", strings.Join(args, " "), err, stderr.String()) |
| 49 | } |
| 50 | return strings.TrimSpace(string(out)) |
| 51 | } |
| 52 | |
| 53 | func (r *compareRepo) git(args ...string) string { |
| 54 | r.t.Helper() |
| 55 | return r.in(r.work, args...) |
| 56 | } |
| 57 | |
| 58 | // file writes a file in the work tree and stages it. |
| 59 | func (r *compareRepo) file(name, content string) { |
| 60 | r.t.Helper() |
| 61 | if err := os.WriteFile(filepath.Join(r.work, name), []byte(content), 0o644); err != nil { |
| 62 | r.t.Fatal(err) |
| 63 | } |
| 64 | r.git("add", name) |
| 65 | } |
| 66 | |
| 67 | // valley runs a verb of valley-go and returns what it printed. |
| 68 | func (r *compareRepo) valley(args ...string) string { |
| 69 | r.t.Helper() |
| 70 | var stdout, stderr bytes.Buffer |
| 71 | if code := run(args, &stdout, &stderr); code != 0 { |
| 72 | r.t.Fatalf("valley-go %s: %s", strings.Join(args, " "), stderr.String()) |
| 73 | } |
| 74 | return stdout.String() |
| 75 | } |
| 76 | |
| 77 | // moveMain adds a commit to main on origin, as a landing would, and |
| 78 | // fetches it. It returns main's new tip. |
| 79 | func (r *compareRepo) moveMain(name string) string { |
| 80 | r.git("checkout", "--quiet", "main") |
| 81 | r.file(name, name+"\n") |
| 82 | r.git("commit", "--quiet", "-m", "land "+name) |
| 83 | r.git("push", "--quiet", "origin", "main") |
| 84 | return r.git("rev-parse", "HEAD") |
| 85 | } |
| 86 | |
| 87 | // rebaseOnto rebases a branch onto main and pushes it, and returns its |
| 88 | // new head. |
| 89 | func (r *compareRepo) rebaseOnto(branch string) string { |
| 90 | r.git("checkout", "--quiet", branch) |
| 91 | r.git("rebase", "--quiet", "main") |
| 92 | r.git("push", "--quiet", "--force", "origin", branch) |
| 93 | r.git("checkout", "--quiet", "main") |
| 94 | return r.git("rev-parse", branch) |
| 95 | } |
| 96 | |
| 97 | // setRef points a ref on origin at an object, or deletes it for "". |
| 98 | func (r *compareRepo) setRef(ref, oid string) { |
| 99 | if oid == "" { |
| 100 | r.in(r.origin, "update-ref", "-d", ref) |
| 101 | return |
| 102 | } |
| 103 | r.in(r.origin, "update-ref", ref, oid) |
| 104 | } |
| 105 | |
| 106 | // outcomeRecord writes an outcome record on origin, as the integrator |
| 107 | // writes one, and returns its blob. |
| 108 | func (r *compareRepo) outcomeRecord(head, decision string) string { |
| 109 | cmd := exec.Command("git", "hash-object", "-w", "--stdin") |
| 110 | cmd.Dir = r.origin |
| 111 | cmd.Stdin = strings.NewReader(head + " " + r.git("rev-parse", "main") + " 0123456789abcdef " + decision + "\n") |
| 112 | out, err := cmd.Output() |
| 113 | if err != nil { |
| 114 | r.t.Fatal(err) |
| 115 | } |
| 116 | return strings.TrimSpace(string(out)) |
| 117 | } |
| 118 | |
| 119 | func (r *compareRepo) compare(id string, version int, head string) (kind, text string) { |
| 120 | r.t.Helper() |
| 121 | return r.compareWith(id, version, head) |
| 122 | } |
| 123 | |
| 124 | func (r *compareRepo) compareVerbose(id string, version int, head string) (kind, text string) { |
| 125 | r.t.Helper() |
| 126 | return r.compareWith(id, version, head, "-v") |
| 127 | } |
| 128 | |
| 129 | func (r *compareRepo) compareWith(id string, version int, head string, flags ...string) (kind, text string) { |
| 130 | r.t.Helper() |
| 131 | args := append(append([]string{"compare"}, flags...), "--tip", r.git("rev-parse", "origin/main"), id, strconv.Itoa(version), head) |
| 132 | out := r.valley(args...) |
| 133 | kind, text, _ = strings.Cut(out, "\n") |
| 134 | return kind, text |
| 135 | } |
| 136 | |
| 137 | // A request or an outcome under a branch's request name is evidence that |
| 138 | // a head was approved only while nothing else maps to that name: no |
| 139 | // version of another change, and no branch on origin that is not the |
| 140 | // change's own. Under c-<change ID> it always is. An ambiguous name stops |
| 141 | // the search: an older version is not compared instead. |
| 142 | func TestTheApprovedVersionIsNamedUnambiguously(t *testing.T) { |
| 143 | r := newCompareRepo(t) |
| 144 | id := strings.Repeat("x", 32) |
| 145 | r.git("checkout", "--quiet", "-b", "a/b") |
| 146 | r.file("feature.txt", "feature\n") |
| 147 | r.git("commit", "--quiet", "-m", "add the feature") |
| 148 | r.git("push", "--quiet", "origin", "a/b") |
| 149 | r.valley("ready", "--change", id, "a/b") |
| 150 | v1 := r.git("rev-parse", "a/b") |
| 151 | byBranch := requestPrefix + "a-b" |
| 152 | r.setRef(byBranch, v1) |
| 153 | r.moveMain("other") |
| 154 | v2 := r.rebaseOnto("a/b") |
| 155 | r.valley("ready", "--change", id, "a/b") |
| 156 | r.git("fetch", "--quiet", "origin") |
| 157 | |
| 158 | clean := func(why string) { |
| 159 | t.Helper() |
| 160 | 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.") { |
| 161 | t.Fatalf("%s: %s\n%s", why, kind, text) |
| 162 | } |
| 163 | } |
| 164 | unknown := func(why, ref string) { |
| 165 | t.Helper() |
| 166 | kind, text := r.compare(id, 2, v2) |
| 167 | if kind != compareNone || !strings.Contains(text, "is named by "+ref+", and another change or branch maps to that name too") { |
| 168 | t.Fatalf("%s: %s\n%s", why, kind, text) |
| 169 | } |
| 170 | } |
| 171 | clean("a request under the branch's name") |
| 172 | |
| 173 | // A plain branch on origin whose name maps to a-b as well. |
| 174 | r.git("push", "--quiet", "origin", "main:refs/heads/a-b") |
| 175 | unknown("a request under a name a plain branch maps to", byBranch) |
| 176 | r.setRef("refs/heads/a-b", "") |
| 177 | clean("a request once the plain branch is gone") |
| 178 | |
| 179 | // Another change, whose version names a branch that maps to a-b. |
| 180 | r.git("checkout", "--quiet", "-b", "a-b", "main") |
| 181 | r.file("y.txt", "y\n") |
| 182 | r.git("commit", "--quiet", "-m", "another change") |
| 183 | r.git("push", "--quiet", "origin", "a-b") |
| 184 | r.valley("ready", "--change", strings.Repeat("y", 32), "a-b") |
| 185 | r.git("checkout", "--quiet", "main") |
| 186 | r.setRef("refs/heads/a-b", "") |
| 187 | unknown("a request under a name another change's version maps to", byBranch) |
| 188 | |
| 189 | // The outcome of the same request, after the integrator consumed it: |
| 190 | // ambiguous under a-b, and evidence under c-<change ID>. |
| 191 | r.setRef(byBranch, "") |
| 192 | r.setRef(outcomePrefix+"a-b", r.outcomeRecord(v1, "stale")) |
| 193 | unknown("an outcome under a name another change's version maps to", outcomePrefix+"a-b") |
| 194 | r.setRef(outcomePrefix+"a-b", "") |
| 195 | r.setRef(outcomePrefix+"c-"+id, r.outcomeRecord(v1, "refused-not-ready")) |
| 196 | if kind, text := r.compare(id, 2, v2); kind != compareNone || text != "" { |
| 197 | t.Fatalf("an outcome that judged no approval counted: %s\n%s", kind, text) |
| 198 | } |
| 199 | r.setRef(outcomePrefix+"c-"+id, r.outcomeRecord(v1, "land")) |
| 200 | clean("an outcome under c-<change ID>") |
| 201 | |
| 202 | // A version shown is never its own reference, and a greater version |
| 203 | // named only ambiguously stops the search before v1. |
| 204 | r.git("checkout", "--quiet", "a/b") |
| 205 | r.file("feature.txt", "feature, edited\n") |
| 206 | r.git("commit", "--quiet", "-m", "edit the feature") |
| 207 | r.git("push", "--quiet", "origin", "a/b") |
| 208 | r.valley("ready", "--change", id, "a/b") |
| 209 | r.git("checkout", "--quiet", "main") |
| 210 | v3 := r.git("rev-parse", "a/b") |
| 211 | r.setRef(requestPrefix+"c-"+id, v3) |
| 212 | if kind, text := r.compare(id, 3, v3); kind != compareChanged || !strings.Contains(text, "edited since you approved v1 at "+short(v1)+".") { |
| 213 | t.Fatalf("v3 is not set against v1, the greatest version below it with an approval: %s\n%s", kind, text) |
| 214 | } |
| 215 | r.setRef(requestPrefix+"c-"+id, "") |
| 216 | r.setRef(byBranch, v2) |
| 217 | if kind, text := r.compare(id, 3, v3); kind != compareNone || !strings.Contains(text, "v2 at "+short(v2)+" is named by "+byBranch) { |
| 218 | t.Fatalf("v2 named ambiguously did not stop the search before v1: %s\n%s", kind, text) |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | // A branch named c/<ID> or c-<ID> maps to c-<ID>, which is reserved for |
| 223 | // the change with that ID. A request or an outcome under it is that |
| 224 | // change's, so it is no evidence of approval for a change whose versions |
| 225 | // name the branch, even when it names one of their heads. |
| 226 | func TestAnotherChangesReservedNameIsNoEvidence(t *testing.T) { |
| 227 | id, other := strings.Repeat("x", 32), strings.Repeat("y", 32) |
| 228 | for _, prefix := range []string{"c/", "c-"} { |
| 229 | t.Run(prefix, func(t *testing.T) { |
| 230 | r := newCompareRepo(t) |
| 231 | branch := prefix + other |
| 232 | r.git("checkout", "--quiet", "-b", branch) |
| 233 | r.file("feature.txt", "feature\n") |
| 234 | r.git("commit", "--quiet", "-m", "add the feature") |
| 235 | r.git("push", "--quiet", "origin", branch) |
| 236 | r.valley("ready", "--change", id, branch) |
| 237 | v1 := r.git("rev-parse", branch) |
| 238 | r.moveMain("other") |
| 239 | v2 := r.rebaseOnto(branch) |
| 240 | r.valley("ready", "--change", id, branch) |
| 241 | r.git("fetch", "--quiet", "origin") |
| 242 | |
| 243 | reserved := "c-" + other |
| 244 | r.setRef(requestPrefix+reserved, v1) |
| 245 | r.setRef(outcomePrefix+reserved, r.outcomeRecord(v1, "land")) |
| 246 | if kind, text := r.compare(id, 2, v2); kind != compareNone || text != "" { |
| 247 | t.Fatalf("a request and an outcome under %s counted for %s: %s\n%s", reserved, id[:4], kind, text) |
| 248 | } |
| 249 | r.setRef(requestPrefix+"c-"+id, v1) |
| 250 | 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.") { |
| 251 | t.Fatalf("a request under the change's own c-<change ID>: %s\n%s", kind, text) |
| 252 | } |
| 253 | }) |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | // land adds a commit to main on origin that writes a file, as a landing |
| 258 | // would, and fetches it. It returns main's new tip. |
| 259 | func (r *compareRepo) land(name, content, subject string) string { |
| 260 | r.git("checkout", "--quiet", "main") |
| 261 | r.file(name, content) |
| 262 | r.git("commit", "--quiet", "-m", subject) |
| 263 | r.git("push", "--quiet", "origin", "main") |
| 264 | return r.git("rev-parse", "HEAD") |
| 265 | } |
| 266 | |
| 267 | // amend replaces the branch's last commit with the work tree's staged |
| 268 | // files, keeping its message, and returns the new head after readying it. |
| 269 | func (r *compareRepo) amend(id, branch string, args ...string) string { |
| 270 | r.git(append([]string{"commit", "--quiet", "--amend"}, args...)...) |
| 271 | r.git("push", "--quiet", "--force", "origin", branch) |
| 272 | r.valley("ready", "--change", id, branch) |
| 273 | r.git("checkout", "--quiet", "main") |
| 274 | return r.git("rev-parse", branch) |
| 275 | } |
| 276 | |
| 277 | // lines is one line for each of n, numbered, with the line at edit |
| 278 | // replaced. |
| 279 | func lines(n, edit int, replacement string) string { |
| 280 | var b strings.Builder |
| 281 | for i := 1; i <= n; i++ { |
| 282 | if i == edit { |
| 283 | b.WriteString(replacement + "\n") |
| 284 | } else { |
| 285 | b.WriteString("line " + strconv.Itoa(i) + "\n") |
| 286 | } |
| 287 | } |
| 288 | return b.String() |
| 289 | } |
| 290 | |
| 291 | func mustSay(t *testing.T, what, text string, want ...string) { |
| 292 | t.Helper() |
| 293 | for _, line := range want { |
| 294 | if !strings.Contains(text, line) { |
| 295 | t.Fatalf("%s does not say %q:\n%s", what, line, text) |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | // A clean restack names the landings between the two bases, oldest |
| 301 | // first. An edited version lists the files whose change differs, the |
| 302 | // range-diff, and the shown version's diff of those files in git's |
| 303 | // ordinary form, whatever the person's diff configuration. A message or |
| 304 | // an author that changed is an edit, shown whatever the files say. |
| 305 | func TestCompareSaysWhatChanged(t *testing.T) { |
| 306 | r := newCompareRepo(t) |
| 307 | id := strings.Repeat("z", 32) |
| 308 | r.git("checkout", "--quiet", "-b", "topic") |
| 309 | r.file("tool.sh", "#!/bin/sh\n") |
| 310 | r.file("image.bin", "\x00\x01") |
| 311 | r.git("commit", "--quiet", "-m", "add the tool") |
| 312 | r.git("push", "--quiet", "origin", "topic") |
| 313 | r.valley("ready", "--change", id, "topic") |
| 314 | v1 := r.git("rev-parse", "topic") |
| 315 | b0 := r.git("rev-parse", "main") |
| 316 | r.setRef(requestPrefix+"c-"+id, v1) |
| 317 | one, two := r.moveMain("one"), r.moveMain("two") |
| 318 | v2 := r.rebaseOnto("topic") |
| 319 | r.valley("ready", "--change", id, "topic") |
| 320 | r.git("fetch", "--quiet", "origin") |
| 321 | |
| 322 | kind, text := r.compare(id, 2, v2) |
| 323 | want := "zzzz v2 at " + short(v2) + ": rebased since you approved v1 at " + short(v1) + ", with the same file changes.\n" + |
| 324 | "Main moved " + short(b0) + " → " + short(two) + ", landing 2 commits:\n" + |
| 325 | " " + short(one) + " land one\n" + |
| 326 | " " + short(two) + " land two\n" |
| 327 | if kind != compareClean || text != want { |
| 328 | t.Fatalf("clean:\n%s\n%s\nwant:\n%s", kind, text, want) |
| 329 | } |
| 330 | |
| 331 | // v3: the binary file's content and the tool's mode change. git |
| 332 | // range-diff calls the two commits equal, since a binary file's patch |
| 333 | // says only that it differs, but the commit's patch is not the same. |
| 334 | r.setRef(requestPrefix+"c-"+id, v2) |
| 335 | r.git("checkout", "--quiet", "topic") |
| 336 | r.file("image.bin", "\x00\x02") |
| 337 | if err := os.Chmod(filepath.Join(r.work, "tool.sh"), 0o755); err != nil { |
| 338 | t.Fatal(err) |
| 339 | } |
| 340 | r.git("add", "tool.sh") |
| 341 | v3 := r.amend(id, "topic", "--no-edit") |
| 342 | r.git("config", "diff.noprefix", "true") |
| 343 | kind, text = r.compare(id, 3, v3) |
| 344 | r.git("config", "--unset", "diff.noprefix") |
| 345 | mustSay(t, "an edited commit", text, |
| 346 | "zzzz v3 at "+short(v3)+": edited since you approved v2 at "+short(v2)+".\n"+ |
| 347 | "Its base is still "+short(two)+".\n"+ |
| 348 | "1 of 1 commit changed, and 2 files differ from v2:\n image.bin\n tool.sh\n\n", |
| 349 | "── git range-diff "+short(two)+".."+short(v2)+" "+short(two)+".."+short(v3)+" ──\n", |
| 350 | "1: "+short(v2)+" = 1: "+short(v3)+" add the tool", |
| 351 | "\n── v3's diff of these 2 files against main "+short(two)+" ──\ndiff --git a/image.bin b/image.bin\n", |
| 352 | "Binary files /dev/null and b/image.bin differ\ndiff --git a/tool.sh b/tool.sh\nnew file mode 100755\n", |
| 353 | "@@ -0,0 +1 @@\n+#!/bin/sh\n", |
| 354 | ) |
| 355 | if kind != compareChanged || strings.Contains(text, "Commit messages") || strings.Contains(text, "v2: absent") { |
| 356 | t.Fatalf("an edited commit: %s\n%s", kind, text) |
| 357 | } |
| 358 | _, verbose := r.compareVerbose(id, 3, v3) |
| 359 | mustSay(t, "-v", verbose, " image.bin\n v2: absent -> 100644 ", " tool.sh\n v2: absent -> 100644 ", " v3: absent -> 100755 ") |
| 360 | |
| 361 | // v4: the same files, with another message and another author. |
| 362 | r.setRef(requestPrefix+"c-"+id, v3) |
| 363 | r.git("checkout", "--quiet", "topic") |
| 364 | v4 := r.amend(id, "topic", "-m", "add the tool, renamed", "--author", "Someone Else <else@localhost>") |
| 365 | kind, text = r.compare(id, 4, v4) |
| 366 | mustSay(t, "a changed message and author", text, |
| 367 | ": edited since you approved v3 at "+short(v3)+", with the same file changes.\n", |
| 368 | "Commit messages edited since you approved v3. Read them before approving:\n"+ |
| 369 | " v3, commit 1 of 1, "+short(v3)+":\n add the tool\n"+ |
| 370 | " v4, commit 1 of 1, "+short(v4)+":\n add the tool, renamed\n", |
| 371 | "Commit authors edited since you approved v3:\n v3, commit 1 of 1, "+short(v3)+": compare-test <compare-test@localhost> ", |
| 372 | " v4, commit 1 of 1, "+short(v4)+": Someone Else <else@localhost> ", |
| 373 | "Its base is still "+short(two)+".\n", |
| 374 | ) |
| 375 | if kind != compareClean { |
| 376 | t.Fatalf("a changed message alone is still a clean file delta: %s", kind) |
| 377 | } |
| 378 | |
| 379 | // The version given must have the head given. |
| 380 | var stdout, stderr bytes.Buffer |
| 381 | if code := run([]string{"compare", "--tip", two, id, "3", v4}, &stdout, &stderr); code == 0 || !strings.Contains(stderr.String(), "v3 of change zzzz records the head") { |
| 382 | t.Fatalf("compare took a head its version does not record: %d %s", code, stderr.String()) |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | // A rebase whose commit is untouched, onto landings that edited the same |
| 387 | // file, says the commit is unchanged and names those landings, with no |
| 388 | // range-diff. An edit after it names, for each file whose change |
| 389 | // differs, the landings that edited it or none, then the range-diff. |
| 390 | func TestCompareAttributesFilesMainAlsoEdited(t *testing.T) { |
| 391 | r := newCompareRepo(t) |
| 392 | id := strings.Repeat("w", 32) |
| 393 | r.land("shared.txt", lines(20, 0, ""), "add shared") |
| 394 | b0 := r.git("rev-parse", "main") |
| 395 | r.git("checkout", "--quiet", "-b", "topic") |
| 396 | r.file("shared.txt", lines(20, 2, "topic's line 2")) |
| 397 | r.git("commit", "--quiet", "-m", "edit line 2") |
| 398 | r.git("push", "--quiet", "origin", "topic") |
| 399 | r.valley("ready", "--change", id, "topic") |
| 400 | v1 := r.git("rev-parse", "topic") |
| 401 | r.setRef(requestPrefix+"c-"+id, v1) |
| 402 | 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") |
| 403 | b1 := r.moveMain("unrelated") |
| 404 | v2 := r.rebaseOnto("topic") |
| 405 | r.valley("ready", "--change", id, "topic") |
| 406 | r.git("fetch", "--quiet", "origin") |
| 407 | |
| 408 | kind, text := r.compare(id, 2, v2) |
| 409 | mustSay(t, "a rebase onto a file main also edited", text, |
| 410 | "wwww v2 at "+short(v2)+": rebased since you approved v1 at "+short(v1)+".\n"+ |
| 411 | "Its commit is unchanged: same patch, same message.\n"+ |
| 412 | "Main moved "+short(b0)+" → "+short(b1)+" and also edited 1 of its files:\n"+ |
| 413 | " shared.txt "+short(shared)+" edit line 18 of shared, with a subject long enough that the listing has to cu…\n"+ |
| 414 | "\n── v2's diff of this file against main "+short(b1)+" ──\ndiff --git a/shared.txt b/shared.txt\n", |
| 415 | "-line 2\n+topic's line 2\n", |
| 416 | ) |
| 417 | if kind != compareChanged || strings.Contains(text, "range-diff") { |
| 418 | t.Fatalf("a rebase onto a file main also edited: %s\n%s", kind, text) |
| 419 | } |
| 420 | |
| 421 | // v3: the commit is edited and adds a file, after main edits the |
| 422 | // shared file again. |
| 423 | r.setRef(requestPrefix+"c-"+id, v2) |
| 424 | again := r.land("shared.txt", lines(20, 18, "main's line 18, again"), "edit line 18 again") |
| 425 | b2 := r.git("rev-parse", "main") |
| 426 | r.rebaseOnto("topic") |
| 427 | r.git("checkout", "--quiet", "topic") |
| 428 | r.file("shared.txt", strings.Replace(r.git("show", "HEAD:shared.txt")+"\n", "topic's line 2", "topic's line 2, edited", 1)) |
| 429 | r.file("new.txt", "new\n") |
| 430 | v3 := r.amend(id, "topic", "--no-edit") |
| 431 | r.git("fetch", "--quiet", "origin") |
| 432 | kind, text = r.compare(id, 3, v3) |
| 433 | mustSay(t, "an edit after a rebase", text, |
| 434 | "wwww v3 at "+short(v3)+": edited since you approved v2 at "+short(v2)+".\n"+ |
| 435 | "Main moved "+short(b1)+" → "+short(b2)+".\n"+ |
| 436 | "1 of 1 commit changed, and 2 files differ from v2:\n"+ |
| 437 | " new.txt not edited on main\n"+ |
| 438 | " shared.txt "+short(again)+" edit line 18 again\n"+ |
| 439 | "\n── git range-diff "+short(b1)+".."+short(v2)+" "+short(b2)+".."+short(v3)+" ──\n", |
| 440 | "1: "+short(v3)+" edit line 2\n", |
| 441 | "\n── v3's diff of these 2 files against main "+short(b2)+" ──\ndiff --git a/new.txt b/new.txt\n", |
| 442 | ) |
| 443 | if kind != compareChanged { |
| 444 | t.Fatalf("an edit after a rebase: %s", kind) |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | // style changes no line's text, styles a comparison's title, headings and |
| 449 | // summary, and leaves a diff's lines as they are; with NO_COLOR set, or |
| 450 | // for no terminal, it copies the page. |
| 451 | func TestStyleKeepsEveryLine(t *testing.T) { |
| 452 | page := "layer heading\n" + |
| 453 | "zzzz v3 at 1234abc: edited since you approved v2 at 5678def.\n" + |
| 454 | "does oc-1234567: a knot\n" + |
| 455 | "Main moved 1111aaa → 2222bbb.\n" + |
| 456 | " shared.txt 3333ccc edit line 18\n" + |
| 457 | "\n── v3's diff of this file against main 2222bbb ──\n" + |
| 458 | "diff --git a/f b/f\n" + |
| 459 | "+edited 4444ddd\n" |
| 460 | style := func(args ...string) string { |
| 461 | var out bytes.Buffer |
| 462 | if err := runStyle(args, strings.NewReader(page), &out); err != nil { |
| 463 | t.Fatal(err) |
| 464 | } |
| 465 | return out.String() |
| 466 | } |
| 467 | t.Setenv("NO_COLOR", "") |
| 468 | styled := style("--paged") |
| 469 | if got := styleEscape.ReplaceAllString(styled, ""); got != page { |
| 470 | t.Fatalf("style changed the text:\n%s", got) |
| 471 | } |
| 472 | for _, want := range []string{ |
| 473 | "layer heading\n", |
| 474 | "\x1b[1mzzzz v3 at 1234abc: \x1b[33medited\x1b[39m since you approved v2 at 5678def.\x1b[22m\n", |
| 475 | "Main moved \x1b[2m1111aaa\x1b[22m → \x1b[2m2222bbb\x1b[22m.\n", |
| 476 | "\x1b[36m── v3's diff of this file against main 2222bbb ──\x1b[39m\n", |
| 477 | "diff --git a/f b/f\n+edited 4444ddd\n", |
| 478 | } { |
| 479 | if !strings.Contains(styled, want) { |
| 480 | t.Fatalf("style does not give %q:\n%q", want, styled) |
| 481 | } |
| 482 | } |
| 483 | if got := style(); got != page { |
| 484 | t.Fatalf("style styled a page for no terminal:\n%q", got) |
| 485 | } |
| 486 | t.Setenv("NO_COLOR", "1") |
| 487 | if got := style("--paged"); got != page { |
| 488 | t.Fatalf("style styled a page with NO_COLOR set:\n%q", got) |
| 489 | } |
| 490 | } |