| 1 | package rebase |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | |
| 12 | "the-valley/change" |
| 13 | ) |
| 14 | |
| 15 | // world is a bare origin and a clone of it to work in, with git's |
| 16 | // configuration its own. |
| 17 | type world struct { |
| 18 | t *testing.T |
| 19 | origin, work string |
| 20 | env []string |
| 21 | g Git |
| 22 | } |
| 23 | |
| 24 | func newWorld(t *testing.T) *world { |
| 25 | t.Helper() |
| 26 | dir := t.TempDir() |
| 27 | w := &world{t: t, origin: filepath.Join(dir, "origin.git"), work: filepath.Join(dir, "work")} |
| 28 | w.env = append(os.Environ(), "HOME="+dir, "XDG_CONFIG_HOME="+filepath.Join(dir, "config"), "GIT_CONFIG_NOSYSTEM=1", |
| 29 | "GIT_AUTHOR_NAME=rebase-test", "GIT_AUTHOR_EMAIL=rebase-test@localhost", |
| 30 | "GIT_COMMITTER_NAME=rebase-test", "GIT_COMMITTER_EMAIL=rebase-test@localhost") |
| 31 | w.run(dir, "init", "--quiet", "--bare", "--initial-branch=main", w.origin) |
| 32 | w.run(dir, "clone", "--quiet", w.origin, w.work) |
| 33 | w.g = Git{Dir: w.work, Env: w.env} |
| 34 | w.git("checkout", "--quiet", "-b", "main") |
| 35 | w.commit("base", "base.txt", "base\n") |
| 36 | w.git("push", "--quiet", "origin", "main") |
| 37 | return w |
| 38 | } |
| 39 | |
| 40 | func (w *world) run(dir string, args ...string) string { |
| 41 | w.t.Helper() |
| 42 | cmd := exec.Command("git", args...) |
| 43 | cmd.Dir, cmd.Env = dir, w.env |
| 44 | out, err := cmd.CombinedOutput() |
| 45 | if err != nil { |
| 46 | w.t.Fatalf("git %s: %v\n%s", strings.Join(args, " "), err, out) |
| 47 | } |
| 48 | return strings.TrimSpace(string(out)) |
| 49 | } |
| 50 | |
| 51 | func (w *world) git(args ...string) string { w.t.Helper(); return w.run(w.work, args...) } |
| 52 | |
| 53 | // commit writes one file on the current branch and commits it. |
| 54 | func (w *world) commit(subject, file, content string) string { |
| 55 | w.t.Helper() |
| 56 | if err := os.WriteFile(filepath.Join(w.work, file), []byte(content), 0o644); err != nil { |
| 57 | w.t.Fatal(err) |
| 58 | } |
| 59 | w.git("add", file) |
| 60 | w.git("commit", "--quiet", "-m", subject) |
| 61 | return w.git("rev-parse", "HEAD") |
| 62 | } |
| 63 | |
| 64 | // layer makes a branch on from with one commit of its own, pushes it, and |
| 65 | // returns its head. |
| 66 | func (w *world) layer(branch, from string) string { |
| 67 | w.t.Helper() |
| 68 | w.git("checkout", "--quiet", "-b", branch, from) |
| 69 | head := w.commit("layer "+branch, branch+".txt", branch+"\n") |
| 70 | w.git("push", "--quiet", "origin", branch) |
| 71 | return head |
| 72 | } |
| 73 | |
| 74 | // moveMain moves origin's main on by a commit of its own. |
| 75 | func (w *world) moveMain(file, content string) string { |
| 76 | w.t.Helper() |
| 77 | w.git("checkout", "--quiet", "main") |
| 78 | head := w.commit("main moves on: "+file, file, content) |
| 79 | w.git("push", "--quiet", "origin", "main") |
| 80 | return head |
| 81 | } |
| 82 | |
| 83 | // ready writes a ready record for a change at v1, on branch at its head on |
| 84 | // origin, through the engine's own write path. |
| 85 | func (w *world) ready(id, branch string) { |
| 86 | w.t.Helper() |
| 87 | head := w.at("refs/heads/" + branch) |
| 88 | r := &change.Record{ |
| 89 | Schema: change.Schema, ChangeID: id, Target: Target, Owner: "author", State: change.Ready, Seq: 1, Active: 1, |
| 90 | Versions: []change.Version{{N: 1, Head: change.HeadPrefix + head, Title: "layer " + branch, Branch: branch, Uploader: "author"}}, |
| 91 | } |
| 92 | w.write(id, r, "", head) |
| 93 | } |
| 94 | |
| 95 | // setState writes the change's record again in another state. |
| 96 | func (w *world) setState(id, state string) { |
| 97 | w.t.Helper() |
| 98 | old := w.at(change.Ref(Target, id)) |
| 99 | r := w.record(id) |
| 100 | r.State, r.Seq = state, r.Seq+1 |
| 101 | w.write(id, r, old, old) |
| 102 | } |
| 103 | |
| 104 | func (w *world) write(id string, r *change.Record, old string, parents ...string) { |
| 105 | w.t.Helper() |
| 106 | ctx := context.Background() |
| 107 | commit, err := WriteRecord(ctx, w.g, r, "test record", parents...) |
| 108 | if err != nil { |
| 109 | w.t.Fatal(err) |
| 110 | } |
| 111 | if got := PushRecord(ctx, w.g, id, old, commit); got.Push != Pushed { |
| 112 | w.t.Fatalf("the record of %s was not pushed: %+v", id, got) |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // at is a ref on origin, or "" for none. |
| 117 | func (w *world) at(ref string) string { |
| 118 | w.t.Helper() |
| 119 | cmd := exec.Command("git", "rev-parse", "--verify", "--quiet", ref) |
| 120 | cmd.Dir, cmd.Env = w.origin, w.env |
| 121 | out, _ := cmd.Output() |
| 122 | return strings.TrimSpace(string(out)) |
| 123 | } |
| 124 | |
| 125 | func (w *world) record(id string) *change.Record { |
| 126 | w.t.Helper() |
| 127 | data := w.run(w.origin, "cat-file", "blob", change.Ref(Target, id)+":record.json") |
| 128 | r, err := change.Parse([]byte(data + "\n")) |
| 129 | if err != nil { |
| 130 | w.t.Fatal(err) |
| 131 | } |
| 132 | return r |
| 133 | } |
| 134 | |
| 135 | // refs is every branch and record on origin, to tell that nothing moved. |
| 136 | func (w *world) refs() string { |
| 137 | w.t.Helper() |
| 138 | return w.run(w.origin, "for-each-ref", "--format=%(objectname) %(refname)", "refs/heads/", change.RefPrefix) |
| 139 | } |
| 140 | |
| 141 | // capture reads a snapshot and the family a selection names. |
| 142 | func (w *world) capture(sel Selection) *Family { |
| 143 | w.t.Helper() |
| 144 | s, err := ReadSnapshot(context.Background(), w.g, Verifier{Attest: "attest"}) |
| 145 | if err != nil { |
| 146 | w.t.Fatal(err) |
| 147 | } |
| 148 | f, err := Capture(s, sel) |
| 149 | if err != nil { |
| 150 | w.t.Fatal(err) |
| 151 | } |
| 152 | return f |
| 153 | } |
| 154 | |
| 155 | // publish prepares, pushes and appends a family as the principal rebaser. |
| 156 | func (w *world) publish(f *Family) *Publication { |
| 157 | w.t.Helper() |
| 158 | ctx := context.Background() |
| 159 | p, err := Prepare(ctx, w.g, f) |
| 160 | if err != nil { |
| 161 | w.t.Fatalf("prepare: %v", err) |
| 162 | } |
| 163 | defer p.Release(ctx, w.g) |
| 164 | pub, err := Publish(ctx, w.g, p) |
| 165 | if err != nil { |
| 166 | w.t.Fatalf("publish: %v", err) |
| 167 | } |
| 168 | pub.AppendAll(ctx, w.g, "rebaser") |
| 169 | return pub |
| 170 | } |
| 171 | |
| 172 | // sameDelta holds that a layer's diff from its base is what it was. |
| 173 | func (w *world) sameDelta(oldBase, oldHead, newBase, newHead string) { |
| 174 | w.t.Helper() |
| 175 | if a, b := w.run(w.origin, "diff", oldBase, oldHead), w.run(w.origin, "diff", newBase, newHead); a != b { |
| 176 | w.t.Errorf("the diff of %s changed in the rebase:\n%s\nnow:\n%s", oldHead, a, b) |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | func (w *world) ancestor(a, b string) bool { |
| 181 | cmd := exec.Command("git", "merge-base", "--is-ancestor", a, b) |
| 182 | cmd.Dir, cmd.Env = w.origin, w.env |
| 183 | return cmd.Run() == nil |
| 184 | } |
| 185 | |
| 186 | func names(layers []*Layer) string { |
| 187 | var n []string |
| 188 | for _, l := range layers { |
| 189 | n = append(n, l.Name) |
| 190 | } |
| 191 | return strings.Join(n, " ") |
| 192 | } |
| 193 | |
| 194 | var ( |
| 195 | idA = strings.Repeat("k", 32) |
| 196 | idB = strings.Repeat("m", 32) |
| 197 | idC = strings.Repeat("n", 32) |
| 198 | ) |
| 199 | |
| 200 | // A chain A → B → C moves whole onto main: each layer onto the rebased |
| 201 | // layer below, each keeping its own diff, one push for every branch, and |
| 202 | // each change's rebased head added as v2 with the rebaser as its uploader. |
| 203 | func TestChainMovesWhole(t *testing.T) { |
| 204 | w := newWorld(t) |
| 205 | base := w.at("refs/heads/main") |
| 206 | a := w.layer("a", "main") |
| 207 | b := w.layer("b", "a") |
| 208 | c := w.layer("c", "b") |
| 209 | w.ready(idA, "a") |
| 210 | w.ready(idB, "b") |
| 211 | w.ready(idC, "c") |
| 212 | main := w.moveMain("main.txt", "main\n") |
| 213 | |
| 214 | f := w.capture(Selection{ID: idC}) |
| 215 | if got := names(f.Layers); got != "kkkk mmmm nnnn" { |
| 216 | t.Fatalf("the chain under c is %q", got) |
| 217 | } |
| 218 | pub := w.publish(f) |
| 219 | if pub.Push != Pushed { |
| 220 | t.Fatalf("the branches were not pushed: %v %v", pub.Push, pub.Refused) |
| 221 | } |
| 222 | na, nb, nc := w.at("refs/heads/a"), w.at("refs/heads/b"), w.at("refs/heads/c") |
| 223 | if !w.ancestor(main, na) || !w.ancestor(na, nb) || !w.ancestor(nb, nc) { |
| 224 | t.Fatal("the rebased chain is not main, a, b, c") |
| 225 | } |
| 226 | w.sameDelta(base, a, main, na) |
| 227 | w.sameDelta(a, b, na, nb) |
| 228 | w.sameDelta(b, c, nb, nc) |
| 229 | for id, head := range map[string]string{idA: na, idB: nb, idC: nc} { |
| 230 | r := w.record(id) |
| 231 | v := r.ActiveVersion() |
| 232 | if r.State != change.Ready || v.N != 2 || HeadOf(v) != head || v.Uploader != "rebaser" { |
| 233 | t.Errorf("change %s: %+v", id[:4], r) |
| 234 | } |
| 235 | } |
| 236 | for _, res := range pub.Results { |
| 237 | if res.Branch != Pushed || res.Appended == nil || res.Appended.Version != 2 || res.Unfinished() { |
| 238 | t.Errorf("%s: %+v", res.Layer.Name, res) |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | // A shared lower layer, A → B and A → C, is prepared once: A gains one |
| 244 | // version, both children are rebased onto its new head, and one push moves |
| 245 | // all three branches. The family is the same from either child. |
| 246 | func TestSharedLowerLayerIsRebasedOnce(t *testing.T) { |
| 247 | w := newWorld(t) |
| 248 | a := w.layer("a", "main") |
| 249 | b := w.layer("b", "a") |
| 250 | c := w.layer("c", "a") |
| 251 | w.ready(idA, "a") |
| 252 | w.ready(idB, "b") |
| 253 | w.ready(idC, "c") |
| 254 | main := w.moveMain("main.txt", "main\n") |
| 255 | |
| 256 | if got := names(w.capture(Selection{ID: idC, Whole: true}).Layers); got != "kkkk mmmm nnnn" { |
| 257 | t.Fatalf("the family of c is %q", got) |
| 258 | } |
| 259 | f := w.capture(Selection{ID: idB, Whole: true}) |
| 260 | if got := names(f.Layers); got != "kkkk mmmm nnnn" { |
| 261 | t.Fatalf("the family of b is %q", got) |
| 262 | } |
| 263 | if len(f.Tops()) != 2 || f.Bottom().ID != idA { |
| 264 | t.Fatalf("the family's tops are %q", names(f.Tops())) |
| 265 | } |
| 266 | pub := w.publish(f) |
| 267 | if pub.Push != Pushed { |
| 268 | t.Fatalf("the branches were not pushed: %v %v", pub.Push, pub.Refused) |
| 269 | } |
| 270 | na, nb, nc := w.at("refs/heads/a"), w.at("refs/heads/b"), w.at("refs/heads/c") |
| 271 | if w.run(w.origin, "rev-parse", nb+"^") != na || w.run(w.origin, "rev-parse", nc+"^") != na { |
| 272 | t.Fatal("b and c are not both on the new head of a") |
| 273 | } |
| 274 | if !w.ancestor(main, na) { |
| 275 | t.Fatal("a is not on main") |
| 276 | } |
| 277 | w.sameDelta(a, b, na, nb) |
| 278 | w.sameDelta(a, c, na, nc) |
| 279 | if r := w.record(idA); len(r.Versions) != 2 || HeadOf(r.ActiveVersion()) != na { |
| 280 | t.Errorf("a gained other than one version: %+v", r) |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | // A conflict in one child holds the whole family: no branch moves, and no |
| 285 | // record gains a version, the siblings' included. The refusal names the |
| 286 | // layer and the path. |
| 287 | func TestConflictMovesNothing(t *testing.T) { |
| 288 | w := newWorld(t) |
| 289 | w.layer("a", "main") |
| 290 | w.layer("b", "a") |
| 291 | w.layer("c", "a") |
| 292 | w.ready(idA, "a") |
| 293 | w.ready(idB, "b") |
| 294 | w.ready(idC, "c") |
| 295 | w.moveMain("c.txt", "main has its own\n") |
| 296 | before := w.refs() |
| 297 | |
| 298 | f := w.capture(Selection{ID: idA, Whole: true}) |
| 299 | _, err := Prepare(context.Background(), w.g, f) |
| 300 | var r *Refusal |
| 301 | if !errors.As(err, &r) || r.Kind != Conflict || r.Layer.ID != idC || r.Below != "kkkk as rebased" { |
| 302 | t.Fatalf("prepare: %v", err) |
| 303 | } |
| 304 | if strings.Join(r.Paths, " ") != "c.txt" { |
| 305 | t.Errorf("the conflict names %q", r.Paths) |
| 306 | } |
| 307 | if after := w.refs(); after != before { |
| 308 | t.Errorf("a conflict moved something:\n%s\nnow:\n%s", before, after) |
| 309 | } |
| 310 | if out := w.git("worktree", "list", "--porcelain"); strings.Count(out, "worktree ") != 1 { |
| 311 | t.Errorf("the rebase left a worktree:\n%s", out) |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | // A layer whose commits are on main already rebases to nothing, and is |
| 316 | // refused rather than retired. |
| 317 | func TestEmptyLayerIsRefused(t *testing.T) { |
| 318 | w := newWorld(t) |
| 319 | w.layer("a", "main") |
| 320 | w.ready(idA, "a") |
| 321 | w.moveMain("a.txt", "a\n") |
| 322 | before := w.refs() |
| 323 | |
| 324 | _, err := Prepare(context.Background(), w.g, w.capture(Selection{ID: idA})) |
| 325 | var r *Refusal |
| 326 | if !errors.As(err, &r) || r.Kind != EmptyLayer || r.Layer.ID != idA || r.Below != Target { |
| 327 | t.Fatalf("prepare: %v", err) |
| 328 | } |
| 329 | if after := w.refs(); after != before { |
| 330 | t.Errorf("an empty layer moved something") |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | // A branch moved on origin between the capture and the push loses its |
| 335 | // lease: the atomic push moves no branch, says which was refused, and no |
| 336 | // version is appended. |
| 337 | func TestMovedBranchLosesItsLease(t *testing.T) { |
| 338 | w := newWorld(t) |
| 339 | w.layer("a", "main") |
| 340 | w.layer("b", "a") |
| 341 | w.ready(idA, "a") |
| 342 | w.ready(idB, "b") |
| 343 | w.moveMain("main.txt", "main\n") |
| 344 | ctx := context.Background() |
| 345 | |
| 346 | p, err := Prepare(ctx, w.g, w.capture(Selection{ID: idB})) |
| 347 | if err != nil { |
| 348 | t.Fatal(err) |
| 349 | } |
| 350 | defer p.Release(ctx, w.g) |
| 351 | elsewhere := w.layer("elsewhere", "b") |
| 352 | w.git("push", "--quiet", "--force", "origin", elsewhere+":refs/heads/b") |
| 353 | before := w.refs() |
| 354 | |
| 355 | pub, err := Publish(ctx, w.g, p) |
| 356 | if err != nil { |
| 357 | t.Fatal(err) |
| 358 | } |
| 359 | pub.AppendAll(ctx, w.g, "rebaser") |
| 360 | stale := false |
| 361 | for _, ref := range pub.Refused { |
| 362 | stale = stale || ref.Ref == "refs/heads/b" && strings.Contains(ref.Why, "stale info") |
| 363 | } |
| 364 | if pub.Push != Refused || !stale { |
| 365 | t.Fatalf("the push was %v, refusing %+v", pub.Push, pub.Refused) |
| 366 | } |
| 367 | if after := w.refs(); after != before { |
| 368 | t.Errorf("a refused push moved something:\n%s\nnow:\n%s", before, after) |
| 369 | } |
| 370 | for _, res := range pub.Results { |
| 371 | if res.Appended != nil || res.Unfinished() { |
| 372 | t.Errorf("%s: %+v", res.Layer.Name, res.Appended) |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | // After the branches move, each append stands alone. A change unreadied |
| 378 | // meanwhile gains no version and writes no record; one whose record push |
| 379 | // the hook refuses keeps the record commit it tried; one whose push |
| 380 | // response is lost is unknown, though origin took it; and the rest are |
| 381 | // appended. |
| 382 | func TestAppendsAfterThePushStandAlone(t *testing.T) { |
| 383 | w := newWorld(t) |
| 384 | w.layer("a", "main") |
| 385 | w.layer("b", "a") |
| 386 | w.layer("c", "b") |
| 387 | w.layer("d", "c") |
| 388 | idD := strings.Repeat("p", 32) |
| 389 | w.ready(idA, "a") |
| 390 | w.ready(idB, "b") |
| 391 | w.ready(idC, "c") |
| 392 | w.ready(idD, "d") |
| 393 | w.moveMain("main.txt", "main\n") |
| 394 | ctx := context.Background() |
| 395 | |
| 396 | p, err := Prepare(ctx, w.g, w.capture(Selection{ID: idD})) |
| 397 | if err != nil { |
| 398 | t.Fatal(err) |
| 399 | } |
| 400 | defer p.Release(ctx, w.g) |
| 401 | pub, err := Publish(ctx, w.g, p) |
| 402 | if err != nil || pub.Push != Pushed { |
| 403 | t.Fatalf("publish: %v %v", err, pub.Push) |
| 404 | } |
| 405 | w.setState(idB, change.Draft) |
| 406 | hook := filepath.Join(w.origin, "hooks", "pre-receive") |
| 407 | refuse := "#!/bin/sh\nwhile read old new ref; do [ \"$ref\" = " + change.Ref(Target, idC) + " ] && exit 1; done\nexit 0\n" |
| 408 | if err := os.WriteFile(hook, []byte(refuse), 0o755); err != nil { |
| 409 | t.Fatal(err) |
| 410 | } |
| 411 | // A receive-pack that takes the push and then fails, as when the |
| 412 | // connection drops before the client reads the result. |
| 413 | lossy := filepath.Join(t.TempDir(), "lossy-receive-pack") |
| 414 | if err := os.WriteFile(lossy, []byte("#!/bin/sh\ngit-receive-pack \"$@\"\nexit 1\n"), 0o755); err != nil { |
| 415 | t.Fatal(err) |
| 416 | } |
| 417 | |
| 418 | for _, res := range pub.Results { |
| 419 | if res.Layer.ID == idD { |
| 420 | w.git("config", "remote.origin.receivepack", lossy) |
| 421 | } |
| 422 | pub.Append(ctx, w.g, "rebaser", res) |
| 423 | } |
| 424 | w.git("config", "--unset", "remote.origin.receivepack") |
| 425 | byID := map[string]*Appended{} |
| 426 | for _, res := range pub.Results { |
| 427 | byID[res.Layer.ID] = res.Appended |
| 428 | } |
| 429 | if ap := byID[idA]; ap.Err != nil || ap.Version != 2 || ap.Write.Push != Pushed { |
| 430 | t.Errorf("a: %+v", ap) |
| 431 | } |
| 432 | if ap := byID[idB]; ap.Err == nil || ap.Version != 0 || ap.Write.Commit != "" || ap.Write.Push != Unpushed { |
| 433 | t.Errorf("b, unreadied: %+v", ap) |
| 434 | } |
| 435 | if ap := byID[idC]; ap.Err == nil || ap.Write.Push != Refused || ap.Write.Commit == "" || ap.Write.Why == "" || |
| 436 | w.at(change.Ref(Target, idC)) != ap.Write.Old { |
| 437 | t.Errorf("c, refused by the hook: %+v", ap) |
| 438 | } |
| 439 | if ap := byID[idD]; ap.Err == nil || ap.Write.Push != Unknown || w.at(change.Ref(Target, idD)) != ap.Write.Commit { |
| 440 | t.Errorf("d, its response lost: %+v", ap) |
| 441 | } |
| 442 | if r := w.record(idB); r.State != change.Draft || len(r.Versions) != 1 { |
| 443 | t.Errorf("b gained a version while a draft: %+v", r) |
| 444 | } |
| 445 | var unfinished []string |
| 446 | for _, res := range pub.Results { |
| 447 | if res.Unfinished() { |
| 448 | unfinished = append(unfinished, res.Layer.Name) |
| 449 | } |
| 450 | } |
| 451 | if got := strings.Join(unfinished, " "); got != "mmmm nnnn pppp" { |
| 452 | t.Errorf("the unfinished layers are %q", got) |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | // A cancelled context publishes nothing: Prepare under one leaves no |
| 457 | // worktree and no ref behind, and Publish under one pushes nothing, the |
| 458 | // prepared heads kept until they are released. |
| 459 | func TestCancelledBeforePublishPublishesNothing(t *testing.T) { |
| 460 | w := newWorld(t) |
| 461 | w.layer("a", "main") |
| 462 | w.ready(idA, "a") |
| 463 | w.moveMain("main.txt", "main\n") |
| 464 | before := w.refs() |
| 465 | |
| 466 | cancelled, cancel := context.WithCancel(context.Background()) |
| 467 | cancel() |
| 468 | if _, err := Prepare(cancelled, w.g, w.capture(Selection{ID: idA})); !errors.Is(err, context.Canceled) { |
| 469 | t.Fatalf("prepare under a cancelled context: %v", err) |
| 470 | } |
| 471 | ctx, cancel := context.WithCancel(context.Background()) |
| 472 | p, err := Prepare(ctx, w.g, w.capture(Selection{ID: idA})) |
| 473 | if err != nil { |
| 474 | t.Fatal(err) |
| 475 | } |
| 476 | if kept := w.git("for-each-ref", "--format=%(objectname)", keepPrefix); kept != p.Heads[p.Family.Bottom()] { |
| 477 | t.Errorf("the prepared head is not kept: %q", kept) |
| 478 | } |
| 479 | cancel() |
| 480 | pub, err := Publish(ctx, w.g, p) |
| 481 | if !errors.Is(err, context.Canceled) || pub.Push != Unpushed { |
| 482 | t.Fatalf("publish under a cancelled context: %v, %v", err, pub.Push) |
| 483 | } |
| 484 | if after := w.refs(); after != before { |
| 485 | t.Errorf("a cancelled rebase moved something") |
| 486 | } |
| 487 | if err := p.Release(ctx, w.g); err != nil { |
| 488 | t.Fatal(err) |
| 489 | } |
| 490 | if kept := w.git("for-each-ref", keepPrefix); kept != "" || strings.Count(w.git("worktree", "list", "--porcelain"), "worktree ") != 1 { |
| 491 | t.Errorf("the rebase left a ref or a worktree behind: %q", kept) |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | // Capture reports each layer's state and refuses nothing a caller could |
| 496 | // hold instead: a draft and a plain branch are layers, which Check then |
| 497 | // refuses as the manual rebase does. |
| 498 | func TestCaptureReportsStatesForTheCallerToHold(t *testing.T) { |
| 499 | w := newWorld(t) |
| 500 | w.layer("a", "main") |
| 501 | w.layer("b", "a") |
| 502 | w.layer("c", "b") |
| 503 | w.ready(idB, "b") |
| 504 | w.ready(idC, "c") |
| 505 | w.setState(idB, change.Draft) |
| 506 | w.moveMain("main.txt", "main\n") |
| 507 | |
| 508 | f := w.capture(Selection{ID: idC}) |
| 509 | var states []string |
| 510 | for _, l := range f.Layers { |
| 511 | states = append(states, string(l.State())) |
| 512 | } |
| 513 | if got := strings.Join(states, " "); got != "branch draft ready" { |
| 514 | t.Fatalf("the states are %q", got) |
| 515 | } |
| 516 | var r *Refusal |
| 517 | if err := f.Check(); !errors.As(err, &r) || r.Kind != NotReady || r.Layer.ID != idB { |
| 518 | t.Fatalf("check: %v", err) |
| 519 | } |
| 520 | } |