| 1 | // ready, unready and withdraw write a change's record on origin. |
| 2 | // |
| 3 | // `valley ready [--head <sha>] [--change <id>] [--stamp] [<branch>]` marks |
| 4 | // a head of a branch ready, as a new version of its change. The branch is |
| 5 | // the current one unless named. The head is --head when given, and |
| 6 | // otherwise the branch's head on origin; either way it must be on origin |
| 7 | // already. ready creates the change's record at v1, or adds the next |
| 8 | // version to the record there, and pushes the record ref with a lease on |
| 9 | // the value it read. When --change is not given and the head carries no |
| 10 | // change ID, ready reuses the sole live (ready or draft, unlanded) change |
| 11 | // whose active version is on that branch, refusing if more than one is. A |
| 12 | // change that is a draft or withdrawn, readied at the head of its active |
| 13 | // version, is ready again at that version, and gains no new one. When the |
| 14 | // head's merge base is behind origin/main and main touches any path the |
| 15 | // change touches, ready warns on stderr and still proceeds. |
| 16 | // |
| 17 | // --if-active <sha> adds the version only while the change is ready at an |
| 18 | // active version whose head is <sha>. The record ref is pushed leased on |
| 19 | // the record that was read, so nothing can unready the change or ready |
| 20 | // another version between that check and the write. rebase readies the |
| 21 | // head it rebased this way. |
| 22 | // |
| 23 | // `valley unready <change>` makes a ready change a draft: a pause, which |
| 24 | // voids nothing. `valley withdraw <change>` withdraws a ready change or a |
| 25 | // draft, which voids everything outstanding on it. Either is undone by |
| 26 | // readying the change again. The integrator lands no request at a head of |
| 27 | // a change that is not ready, so either stops a pending request. Each says |
| 28 | // when one is pending. The integrator deletes a withdrawn change's |
| 29 | // requests on its next pass, and withdraw warns that a ready before then |
| 30 | // leaves the request standing. |
| 31 | // |
| 32 | // The record names the principal that pushes it as the uploader of the |
| 33 | // version it adds, and as the owner of a change it creates. The host |
| 34 | // derives that principal from the ssh key a push authenticates with, and |
| 35 | // nothing on this side can ask the host for it. So the principal is read |
| 36 | // from git config valley.principal, and the hook refuses a record that |
| 37 | // names any other. |
| 38 | |
| 39 | package main |
| 40 | |
| 41 | import ( |
| 42 | "context" |
| 43 | "fmt" |
| 44 | "io" |
| 45 | "strings" |
| 46 | |
| 47 | "the-valley/change" |
| 48 | "the-valley/derive" |
| 49 | "the-valley/rebase" |
| 50 | ) |
| 51 | |
| 52 | const readyUsage = "usage: valley ready [--head <sha>] [--change <id>] [--if-active <sha>] [--stamp] [<branch>]" |
| 53 | |
| 54 | type readyArgs struct { |
| 55 | head, id, branch, ifActive string |
| 56 | stamp bool |
| 57 | } |
| 58 | |
| 59 | func parseReadyArgs(args []string) (readyArgs, error) { |
| 60 | var a readyArgs |
| 61 | var rest []string |
| 62 | for i := 0; i < len(args); i++ { |
| 63 | arg := args[i] |
| 64 | value := func(flag string) (string, error) { |
| 65 | if v, ok := strings.CutPrefix(arg, flag+"="); ok { |
| 66 | return v, nil |
| 67 | } |
| 68 | if i+1 >= len(args) { |
| 69 | return "", failure(flag + " needs a value; " + readyUsage) |
| 70 | } |
| 71 | i++ |
| 72 | return args[i], nil |
| 73 | } |
| 74 | var err error |
| 75 | switch { |
| 76 | case arg == "--head" || strings.HasPrefix(arg, "--head="): |
| 77 | a.head, err = value("--head") |
| 78 | case arg == "--change" || strings.HasPrefix(arg, "--change="): |
| 79 | a.id, err = value("--change") |
| 80 | case arg == "--if-active" || strings.HasPrefix(arg, "--if-active="): |
| 81 | a.ifActive, err = value("--if-active") |
| 82 | case arg == "--stamp": |
| 83 | a.stamp = true |
| 84 | case strings.HasPrefix(arg, "-"): |
| 85 | err = failure("unknown option " + arg + "; " + readyUsage) |
| 86 | default: |
| 87 | rest = append(rest, arg) |
| 88 | } |
| 89 | if err != nil { |
| 90 | return a, err |
| 91 | } |
| 92 | } |
| 93 | switch { |
| 94 | case len(rest) > 1: |
| 95 | return a, failure(readyUsage) |
| 96 | case len(rest) == 1: |
| 97 | a.branch = strings.TrimPrefix(rest[0], "origin/") |
| 98 | } |
| 99 | return a, nil |
| 100 | } |
| 101 | |
| 102 | func runReady(g git, args []string, stdout, stderr io.Writer) error { |
| 103 | a, err := parseReadyArgs(args) |
| 104 | if err != nil { |
| 105 | return err |
| 106 | } |
| 107 | _, err = ready(g, a, stdout, stderr) |
| 108 | return err |
| 109 | } |
| 110 | |
| 111 | // ready writes the record a asks for, prints the handle and version it |
| 112 | // made ready, warns on stderr if main has moved onto a path the change |
| 113 | // touches, and returns that version's number. |
| 114 | func ready(g git, a readyArgs, stdout, stderr io.Writer) (int, error) { |
| 115 | if !g.succeeds(true, "rev-parse", "--git-dir") { |
| 116 | return 0, failure("not a git repository") |
| 117 | } |
| 118 | principal, err := g.output(true, "config", "valley.principal") |
| 119 | if err != nil || !change.ValidPrincipal(principal) { |
| 120 | return 0, failure("ready records the principal your key pushes as, and git config valley.principal names none; set it with git config valley.principal <name>") |
| 121 | } |
| 122 | if a.branch == "" { |
| 123 | if a.branch, err = g.output(true, "symbolic-ref", "--quiet", "--short", "HEAD"); err != nil { |
| 124 | return 0, failure("HEAD is on no branch; name one: " + readyUsage) |
| 125 | } |
| 126 | } |
| 127 | if !change.ValidBranch(a.branch) { |
| 128 | return 0, failure("not a branch name: " + a.branch) |
| 129 | } |
| 130 | if a.stamp && a.head != "" { |
| 131 | return 0, failure("--stamp amends the branch's own head, so it takes no --head") |
| 132 | } |
| 133 | if a.ifActive != "" && !isObjectID(a.ifActive) { |
| 134 | return 0, failure("--if-active takes the full object ID of a version's head, and " + a.ifActive + " is not one") |
| 135 | } |
| 136 | if a.ifActive != "" && a.stamp { |
| 137 | return 0, failure("--stamp starts a new change, so it takes no --if-active") |
| 138 | } |
| 139 | |
| 140 | patterns := append([]string{"refs/heads/" + recordTarget, "refs/heads/" + a.branch}, recordPatterns...) |
| 141 | if a.id == "" { |
| 142 | patterns = append([]string{"refs/heads/" + recordTarget, "refs/heads/" + a.branch}, inventoryPatterns...) |
| 143 | } |
| 144 | o, err := listOrigin(g, patterns...) |
| 145 | if err != nil { |
| 146 | return 0, err |
| 147 | } |
| 148 | tip, remote := o.refs["refs/heads/"+recordTarget], o.refs["refs/heads/"+a.branch] |
| 149 | if tip == "" { |
| 150 | return 0, failure("origin has no " + recordTarget + " branch, so a change has nothing to target") |
| 151 | } |
| 152 | if err := fetch(g, tip, remote); err != nil { |
| 153 | return 0, err |
| 154 | } |
| 155 | |
| 156 | var head string |
| 157 | switch { |
| 158 | case a.stamp: |
| 159 | // The local branch is read by its exact name, so a branch named |
| 160 | // refs/heads/<branch> is not taken for it. |
| 161 | var found bool |
| 162 | if head, found, err = g.ref("refs/heads/" + a.branch); err != nil || !found { |
| 163 | return 0, failure("--stamp amends the head of your local " + a.branch + ", and there is no such branch") |
| 164 | } |
| 165 | case a.head != "": |
| 166 | if !isHexPrefix(a.head) { |
| 167 | return 0, failure("--head takes a commit's object ID, and " + a.head + " is not one") |
| 168 | } |
| 169 | head, err = g.output(true, "rev-parse", "--verify", "--quiet", a.head+"^{commit}") |
| 170 | if err != nil || remote == "" || !g.succeeds(true, "merge-base", "--is-ancestor", head, remote) { |
| 171 | return 0, failure(a.head + " is not on origin's " + a.branch + "; push it there first") |
| 172 | } |
| 173 | default: |
| 174 | if remote == "" { |
| 175 | return 0, failure(a.branch + " is not on origin; push it there first") |
| 176 | } |
| 177 | head = remote |
| 178 | } |
| 179 | if err := checkRange(g, tip, head); err != nil { |
| 180 | return 0, err |
| 181 | } |
| 182 | id, matched, generated, err := changeID(g, o, head, a.branch, a.id) |
| 183 | if err != nil { |
| 184 | return 0, err |
| 185 | } |
| 186 | |
| 187 | ref := change.Ref(recordTarget, id) |
| 188 | old := o.refs[ref] |
| 189 | var before *change.Record |
| 190 | if matched != nil { |
| 191 | before = matched.record |
| 192 | } else if old != "" { |
| 193 | if err := fetch(g, old); err != nil { |
| 194 | return 0, err |
| 195 | } |
| 196 | if before, err = readRecord(g, old); err != nil { |
| 197 | return 0, err |
| 198 | } |
| 199 | } |
| 200 | handle := o.handle(id) |
| 201 | if a.ifActive != "" { |
| 202 | if err := rebase.CheckActive(handle, before, a.ifActive); err != nil { |
| 203 | return 0, err |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | if a.stamp { |
| 208 | // Who owns a change is the host's to say, from the key a push |
| 209 | // authenticates with, and valley.principal is only what this |
| 210 | // checkout claims. So --stamp, which rewrites a branch before the |
| 211 | // record is pushed, starts new changes alone and never asks whose |
| 212 | // an existing one is. |
| 213 | if before != nil { |
| 214 | return 0, failure(fmt.Sprintf("--stamp marks only a new change's first version, and %s has a record on origin already; ready this head with --change %s instead, without --stamp", handle, id)) |
| 215 | } |
| 216 | if head, err = stamp(g, a.branch, head, remote, tip, id); err != nil { |
| 217 | return 0, err |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | subject, err := g.output(true, "log", "-1", "--format=%s", head) |
| 222 | if err != nil { |
| 223 | return 0, failure("could not read the subject of " + short(head)) |
| 224 | } |
| 225 | version := change.Version{ |
| 226 | Head: change.HeadPrefix + head, Title: change.Title(subject), Branch: a.branch, Uploader: principal, |
| 227 | } |
| 228 | var after *change.Record |
| 229 | var parents []string |
| 230 | switch { |
| 231 | case before == nil: |
| 232 | version.N = 1 |
| 233 | after = &change.Record{ |
| 234 | Schema: change.Schema, ChangeID: id, Target: recordTarget, Owner: principal, |
| 235 | State: change.Ready, Seq: 1, Versions: []change.Version{version}, Active: 1, |
| 236 | } |
| 237 | parents = []string{head} |
| 238 | case before.ActiveVersion().Head == version.Head && before.State != change.Ready: |
| 239 | // A draft or a withdrawn change readied at its active head is |
| 240 | // ready again at the version it had, and the record changes |
| 241 | // nothing else. |
| 242 | if before.Seq >= change.MaxSeq { |
| 243 | return 0, failure(fmt.Sprintf("%s's record has been updated %d times, the most a record may be", handle, before.Seq)) |
| 244 | } |
| 245 | revived := *before |
| 246 | revived.State, revived.Seq = change.Ready, before.Seq+1 |
| 247 | after, version, parents = &revived, before.ActiveVersion(), []string{old} |
| 248 | default: |
| 249 | if after, err = rebase.NextRecord(handle, before, version); err != nil { |
| 250 | return 0, err |
| 251 | } |
| 252 | version, parents = after.ActiveVersion(), []string{old, head} |
| 253 | } |
| 254 | commit, err := writeRecord(g, after, rebase.ReadyMessage(handle, id, version.N, head), parents...) |
| 255 | if err != nil { |
| 256 | return 0, err |
| 257 | } |
| 258 | if err := pushRecord(g, id, old, commit); err != nil { |
| 259 | return 0, err |
| 260 | } |
| 261 | fmt.Fprintf(stdout, "%s %s v%d ready\n", project(g), handle, version.N) |
| 262 | switch { |
| 263 | case generated && !a.stamp: |
| 264 | fmt.Fprintf(stdout, "The change ID %s is new. Ready a later head of this change with --change %s, or carry the ID in the head with --stamp.\n", id, id) |
| 265 | case matched != nil: |
| 266 | fmt.Fprintf(stdout, "Using change %s: branch %s already has it.\n", handle, a.branch) |
| 267 | } |
| 268 | warnStaleBase(g, tip, head, handle, stderr) |
| 269 | return version.N, nil |
| 270 | } |
| 271 | |
| 272 | func runUnready(g git, args []string, stdout, stderr io.Writer) error { |
| 273 | return setState(g, args, stdout, stderr, "unready", change.Draft, "") |
| 274 | } |
| 275 | |
| 276 | func runWithdraw(g git, args []string, stdout, stderr io.Writer) error { |
| 277 | return setState(g, args, stdout, stderr, "withdraw", change.Withdrawn, "") |
| 278 | } |
| 279 | |
| 280 | // setState writes a record that moves a change to the state to, and |
| 281 | // changes nothing else but the seq. unready moves a ready change to draft, |
| 282 | // and withdraw moves a ready change or a draft to withdrawn. Given at, a |
| 283 | // head, it writes only while the change's active version is at it, and |
| 284 | // the push is leased on the record that check read: review's [r]eject |
| 285 | // withdraws the version whose branch it deleted, never one readied since. |
| 286 | func setState(g git, args []string, stdout, stderr io.Writer, verb, to, at string) error { |
| 287 | if len(args) != 1 { |
| 288 | return failure("usage: valley " + verb + " <change>") |
| 289 | } |
| 290 | if !g.succeeds(true, "rev-parse", "--git-dir") { |
| 291 | return failure("not a git repository") |
| 292 | } |
| 293 | name := args[0] |
| 294 | o, err := listOrigin(g, append([]string{"refs/heads/" + strings.TrimPrefix(name, "origin/")}, |
| 295 | inventoryPatterns...)...) |
| 296 | if err != nil { |
| 297 | return err |
| 298 | } |
| 299 | n, found, err := lookup(g, o, name) |
| 300 | if err != nil { |
| 301 | return err |
| 302 | } |
| 303 | if !found { |
| 304 | if n, err = onBranch(g, o, strings.TrimPrefix(name, "origin/")); err != nil { |
| 305 | return err |
| 306 | } |
| 307 | } |
| 308 | handle := o.handle(n.id) |
| 309 | switch from := n.record.State; { |
| 310 | case at != "" && n.record.ActiveVersion().Head != change.HeadPrefix+at: |
| 311 | return failure(fmt.Sprintf("%s is at v%d now, not at %s as it was read", handle, n.record.Active, short(at))) |
| 312 | case from == change.Withdrawn && to == change.Withdrawn: |
| 313 | return failure(fmt.Sprintf("%s is withdrawn already; ready it to bring it back", handle)) |
| 314 | case from == change.Withdrawn: |
| 315 | return failure(fmt.Sprintf("%s is withdrawn, and a withdrawn change is never made a draft; ready it to bring it back", handle)) |
| 316 | case from == change.Draft && to == change.Draft: |
| 317 | return failure(fmt.Sprintf("%s is a draft already; ready it to resume it", handle)) |
| 318 | case n.record.Seq >= change.MaxSeq: |
| 319 | return failure(fmt.Sprintf("%s's record has been updated %d times, the most a record may be", handle, n.record.Seq)) |
| 320 | } |
| 321 | after := *n.record |
| 322 | after.State, after.Seq = to, n.record.Seq+1 |
| 323 | message := fmt.Sprintf("valley %s: %s\n\nChange %s, %s at version %d.", verb, handle, n.id, to, after.Active) |
| 324 | commit, err := writeRecord(g, &after, message, n.commit) |
| 325 | if err != nil { |
| 326 | return err |
| 327 | } |
| 328 | if err := pushRecord(g, n.id, n.commit, commit); err != nil { |
| 329 | return err |
| 330 | } |
| 331 | fmt.Fprintf(stdout, "%s %s v%d %s\n", project(g), handle, after.Active, to) |
| 332 | warnPending(stderr, o, n, handle, to) |
| 333 | return nil |
| 334 | } |
| 335 | |
| 336 | // warnPending says so when origin held an integration request for the |
| 337 | // change as the verb read it. The integrator lands no request at a head of |
| 338 | // a change that is not ready, so the request is held back while the change |
| 339 | // is a draft or withdrawn, and stands for when it is ready again. That is |
| 340 | // what a draft means. A withdrawal voids the request, and the integrator |
| 341 | // deletes it on its next pass. withdraw warns that a ready before then |
| 342 | // leaves it standing, and that only the operator can drop it. The request is named either way approve has named |
| 343 | // a change's: as c-<change ID>, or by a branch, the way approve named one |
| 344 | // before it named a change by its ID. A request filed for an earlier |
| 345 | // version is named by that version's branch, so the branch of every |
| 346 | // version is looked for, each once. |
| 347 | func warnPending(w io.Writer, o origin, n recorded, handle string, to string) { |
| 348 | var refs []string |
| 349 | seen := map[string]bool{} |
| 350 | add := func(ref string) { |
| 351 | if !seen[ref] { |
| 352 | seen[ref] = true |
| 353 | refs = append(refs, ref) |
| 354 | } |
| 355 | } |
| 356 | for _, v := range n.record.Versions { |
| 357 | if v.Branch != "" { |
| 358 | add(requestPrefix + changeName(v.Branch)) |
| 359 | } |
| 360 | } |
| 361 | add(requestPrefix + "c-" + n.id) |
| 362 | kind := "note" |
| 363 | if to == change.Withdrawn { |
| 364 | kind = "warning" |
| 365 | } |
| 366 | pending := false |
| 367 | for _, ref := range refs { |
| 368 | if oid := o.refs[ref]; oid != "" { |
| 369 | fmt.Fprintf(w, "valley: %s: the integration request %s for %s is pending on origin, at %s\n", kind, ref, handle, short(oid)) |
| 370 | pending = true |
| 371 | } |
| 372 | } |
| 373 | switch { |
| 374 | case !pending: |
| 375 | case to == change.Withdrawn: |
| 376 | fmt.Fprintln(w, "valley: withdrawing voids the request, and the integrator deletes it on its next pass while the change is still withdrawn. If the change is readied again before then, the request stands and can land. Only a holder of the approve grant (also accepted as request) may touch a request ref, so the operator must drop it then") |
| 377 | default: |
| 378 | fmt.Fprintln(w, "valley: the integrator lands nothing for a draft. The request stands, and lands once the change is ready again") |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | // onBranch is the change whose active version is on branch, and has not |
| 383 | // landed, for a change named by its branch. Every record on origin is |
| 384 | // read to find it. o must list inventoryPatterns. |
| 385 | func onBranch(g git, o origin, branch string) (recorded, error) { |
| 386 | found, err := unlandedOn(g, o, branch) |
| 387 | if err != nil { |
| 388 | return recorded{}, err |
| 389 | } |
| 390 | switch len(found) { |
| 391 | case 0: |
| 392 | return recorded{}, failure(fmt.Sprintf("no change on origin is named %s or has its active version on that branch", branch)) |
| 393 | case 1: |
| 394 | return found[0], nil |
| 395 | } |
| 396 | var handles []string |
| 397 | for _, n := range found { |
| 398 | handles = append(handles, o.handle(n.id)) |
| 399 | } |
| 400 | return recorded{}, failure(fmt.Sprintf("the changes %s all have their active version on %s; name one by its handle", |
| 401 | strings.Join(handles, ", "), branch)) |
| 402 | } |
| 403 | |
| 404 | // checkRange refuses a change whose commits are not one line, as the |
| 405 | // rebase engine checks a version's head (rebase.CheckRange). |
| 406 | func checkRange(g git, tip, head string) error { |
| 407 | return rebase.CheckRange(context.Background(), g.engine(), tip, head) |
| 408 | } |
| 409 | |
| 410 | // changeID is the change's ID: --change when given, then the head |
| 411 | // commit's change-id header, as jj writes it, then a Change-Id trailer in |
| 412 | // its message, then the sole live (ready or draft, unlanded) change on |
| 413 | // origin whose active version is on branch, and otherwise a new ID. Both |
| 414 | // the header and the trailer are read whatever the source, and either one |
| 415 | // malformed, or the two disagreeing, is refused. More than one live change |
| 416 | // on branch is refused, naming each and asking for --change. IDs on the |
| 417 | // other commits of the change are their own provenance and are not read. |
| 418 | // When the ID came from a live change on branch, matched is that record. |
| 419 | func changeID(g git, o origin, head, branch, flag string) (id string, matched *recorded, generated bool, err error) { |
| 420 | header, trailer, err := headIDs(g, head) |
| 421 | if err != nil { |
| 422 | return "", nil, false, err |
| 423 | } |
| 424 | switch { |
| 425 | case header != "" && trailer != "" && header != trailer: |
| 426 | return "", nil, false, failure(fmt.Sprintf("%s has the change-id header %s and the Change-Id trailer %s, and they must agree", short(head), header, trailer)) |
| 427 | case flag != "": |
| 428 | if !change.ValidID(flag) { |
| 429 | return "", nil, false, failure("--change takes a change ID, 32 letters from k to z, and " + flag + " is not one") |
| 430 | } |
| 431 | return flag, nil, false, nil |
| 432 | case header != "": |
| 433 | return header, nil, false, nil |
| 434 | case trailer != "": |
| 435 | return trailer, nil, false, nil |
| 436 | } |
| 437 | live, err := liveOnBranch(g, o, branch) |
| 438 | if err != nil { |
| 439 | return "", nil, false, err |
| 440 | } |
| 441 | switch len(live) { |
| 442 | case 1: |
| 443 | return live[0].id, &live[0], false, nil |
| 444 | case 0: |
| 445 | id, err = change.NewID() |
| 446 | if err != nil { |
| 447 | return "", nil, false, failure("could not make a change ID: " + err.Error()) |
| 448 | } |
| 449 | return id, nil, true, nil |
| 450 | default: |
| 451 | var names []string |
| 452 | for _, n := range live { |
| 453 | names = append(names, fmt.Sprintf("%s (%s)", o.handle(n.id), n.id)) |
| 454 | } |
| 455 | return "", nil, false, failure(fmt.Sprintf("the changes %s all have their active version on %s; name one with --change <id>", |
| 456 | strings.Join(names, ", "), branch)) |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | // liveOnBranch is every change origin holds a record of whose active |
| 461 | // version is on branch, that has not landed and is not withdrawn (so it is |
| 462 | // ready or a draft). o must list inventoryPatterns. |
| 463 | func liveOnBranch(g git, o origin, branch string) ([]recorded, error) { |
| 464 | found, err := unlandedOn(g, o, branch) |
| 465 | if err != nil { |
| 466 | return nil, err |
| 467 | } |
| 468 | var live []recorded |
| 469 | for _, n := range found { |
| 470 | if n.record.State != change.Withdrawn { |
| 471 | live = append(live, n) |
| 472 | } |
| 473 | } |
| 474 | return live, nil |
| 475 | } |
| 476 | |
| 477 | // warnStaleBase warns on stderr when head's merge base with tip (origin/main |
| 478 | // as fetched) is behind tip and both sides touch any common path, since a |
| 479 | // restack is likely to conflict. A failure to compute the warning is ignored |
| 480 | // so ready still succeeds. |
| 481 | func warnStaleBase(g git, tip, head, handle string, stderr io.Writer) { |
| 482 | base, err := g.output(true, "merge-base", tip, head) |
| 483 | if err != nil || !isObjectID(base) || base == tip { |
| 484 | return |
| 485 | } |
| 486 | ctx := context.Background() |
| 487 | processStarted() |
| 488 | changePaths, err := derive.Diff(ctx, g.at(), g.env, base, head) |
| 489 | if err != nil || len(changePaths) == 0 { |
| 490 | return |
| 491 | } |
| 492 | processStarted() |
| 493 | mainPaths, err := derive.Diff(ctx, g.at(), g.env, base, tip) |
| 494 | if err != nil || len(mainPaths) == 0 { |
| 495 | return |
| 496 | } |
| 497 | overlap := commonPaths(changePaths, mainPaths) |
| 498 | if len(overlap) == 0 { |
| 499 | return |
| 500 | } |
| 501 | fmt.Fprintf(stderr, "valley: warning: base is behind origin/%s (both touch %s); a restack is likely to conflict\n", |
| 502 | recordTarget, formatPathList(overlap)) |
| 503 | fmt.Fprintf(stderr, " run: valley rebase %s (or git rebase origin/%s, push, and valley ready)\n", |
| 504 | handle, recordTarget) |
| 505 | } |
| 506 | |
| 507 | // commonPaths returns the sorted intersection of two sorted path slices. |
| 508 | func commonPaths(a, b []string) []string { |
| 509 | var out []string |
| 510 | for i, j := 0, 0; i < len(a) && j < len(b); { |
| 511 | switch { |
| 512 | case a[i] < b[j]: |
| 513 | i++ |
| 514 | case a[i] > b[j]: |
| 515 | j++ |
| 516 | default: |
| 517 | out = append(out, a[i]) |
| 518 | i++ |
| 519 | j++ |
| 520 | } |
| 521 | } |
| 522 | return out |
| 523 | } |
| 524 | |
| 525 | // formatPathList renders up to three paths for human output, followed by a |
| 526 | // count of the remaining paths when there are more. |
| 527 | func formatPathList(paths []string) string { |
| 528 | const limit = 3 |
| 529 | shown := make([]string, 0, min(limit, len(paths))) |
| 530 | for _, p := range paths[:min(limit, len(paths))] { |
| 531 | shown = append(shown, readable(p)) |
| 532 | } |
| 533 | s := strings.Join(shown, ", ") |
| 534 | if len(paths) > limit { |
| 535 | return fmt.Sprintf("%s and %d more", s, len(paths)-limit) |
| 536 | } |
| 537 | return s |
| 538 | } |
| 539 | |
| 540 | // headIDs reads the change IDs a commit carries: the change-id header in |
| 541 | // the commit object itself, and the Change-Id trailer (rebase.HeadIDs). |
| 542 | func headIDs(g git, head string) (header, trailer string, err error) { |
| 543 | return rebase.HeadIDs(context.Background(), g.engine(), head) |
| 544 | } |
| 545 | |
| 546 | // stamp amends the head of the local branch to carry the change ID as a |
| 547 | // Change-Id trailer, and pushes the branch so that the amended head is on |
| 548 | // origin. It returns the amended head, or the head as it was when it |
| 549 | // carries the trailer already. |
| 550 | // |
| 551 | // Amending rewrites a commit, which is harmless only while nobody else can |
| 552 | // have it. So stamp refuses a head that is on origin already. It refuses a |
| 553 | // local branch that is behind origin's or has diverged from it too, since |
| 554 | // pushing the amended head would drop commits origin holds. Its caller |
| 555 | // refuses a change that has a record already. |
| 556 | func stamp(g git, branch, head, remote, tip, id string) (string, error) { |
| 557 | _, trailer, err := headIDs(g, head) |
| 558 | if err != nil { |
| 559 | return "", err |
| 560 | } |
| 561 | if trailer == id { |
| 562 | return head, nil |
| 563 | } |
| 564 | if trailer != "" { |
| 565 | return "", failure(fmt.Sprintf("%s carries the Change-Id trailer %s already, and --stamp would add %s", short(head), trailer, id)) |
| 566 | } |
| 567 | onBranch := remote != "" && g.succeeds(true, "merge-base", "--is-ancestor", head, remote) |
| 568 | inTarget := g.succeeds(true, "merge-base", "--is-ancestor", head, tip) |
| 569 | switch { |
| 570 | case onBranch && head != remote: |
| 571 | return "", failure(fmt.Sprintf("origin's %s is ahead of yours; bring yours up to date before stamping its head", branch)) |
| 572 | case remote != "" && !onBranch && !g.succeeds(true, "merge-base", "--is-ancestor", remote, head): |
| 573 | return "", failure(fmt.Sprintf("your %s and origin's have diverged; --stamp would drop what origin holds", branch)) |
| 574 | case onBranch || inTarget: |
| 575 | return "", failure(fmt.Sprintf("%s is on origin already, so --stamp will not rewrite it; ready it with --change %s instead", short(head), id)) |
| 576 | } |
| 577 | |
| 578 | raw, err := g.output(true, "cat-file", "commit", head) |
| 579 | if err != nil { |
| 580 | return "", failure("could not read the commit " + short(head)) |
| 581 | } |
| 582 | _, message, _ := strings.Cut(raw, "\n\n") |
| 583 | stamped, err := g.input(true, strings.NewReader(message), "interpret-trailers", "--trailer", "Change-Id: "+id) |
| 584 | if err != nil { |
| 585 | return "", failure("could not add the Change-Id trailer to " + short(head)) |
| 586 | } |
| 587 | // The amended commit keeps the head's tree, parents and author, as |
| 588 | // git commit --amend does. The committer is whoever stamps it. |
| 589 | who, err := g.output(true, "log", "-1", "--date=raw", "--format=%an%x00%ae%x00%ad%x00%T%x00%P", head) |
| 590 | if err != nil { |
| 591 | return "", failure("could not read the commit " + short(head)) |
| 592 | } |
| 593 | f := strings.Split(who, "\x00") |
| 594 | if len(f) != 5 { |
| 595 | return "", failure("could not read the commit " + short(head)) |
| 596 | } |
| 597 | args := []string{"commit-tree", f[3]} |
| 598 | for _, p := range strings.Fields(f[4]) { |
| 599 | args = append(args, "-p", p) |
| 600 | } |
| 601 | amend := g |
| 602 | amend.env = append(append([]string(nil), g.env...), |
| 603 | "GIT_AUTHOR_NAME="+f[0], "GIT_AUTHOR_EMAIL="+f[1], "GIT_AUTHOR_DATE=@"+f[2]) |
| 604 | amended, err := amend.input(false, strings.NewReader(stamped+"\n"), append(args, "-F", "-")...) |
| 605 | if err != nil { |
| 606 | return "", failure("could not write the amended commit") |
| 607 | } |
| 608 | if _, err := g.output(false, "update-ref", "-m", "valley ready --stamp", "refs/heads/"+branch, amended, head); err != nil { |
| 609 | return "", failure("could not move your " + branch + " to the amended head") |
| 610 | } |
| 611 | _, err = g.output(false, "push", "--quiet", "--force-with-lease=refs/heads/"+branch+":"+remote, |
| 612 | "origin", amended+":refs/heads/"+branch) |
| 613 | if err != nil { |
| 614 | return "", failure(fmt.Sprintf("your %s carries the stamped head %s, and origin did not take it", branch, short(amended))) |
| 615 | } |
| 616 | return amended, nil |
| 617 | } |
| 618 | |
| 619 | func isHexPrefix(s string) bool { |
| 620 | if len(s) < 4 || len(s) > 40 { |
| 621 | return false |
| 622 | } |
| 623 | for _, c := range []byte(s) { |
| 624 | if (c < '0' || c > '9') && (c < 'a' || c > 'f') { |
| 625 | return false |
| 626 | } |
| 627 | } |
| 628 | return true |
| 629 | } |