Skip to content

Commit 2c1cb2f

Browse files
feat(release): drop an original from one line with run --drop
A fix the rules take for every line can still be wrong for one of them, typically because it builds on code that line does not have. The only ways to keep it out were backport/skip, which keeps it off every line, and --defer, which holds it out of one batch: the next batch tried it again, and a conflict that would never resolve came back every run. run --drop N --reason R records a drop in the line's record, bound to the original's merge commit on main, and every later record carries it, a new batch's included. What the open batch carries of a dropped original comes off it the way --defer takes one off; any other drop changes only the record, so a batch on a line that moved since stays where it is. --undrop withdraws one. Both need an open batch, as every write to the record does. A drop comes after the rules and the carries: backport/skip, rules 2 to 4 or the line itself decide first and the drop stays on record, and backport/include does not lift it. check notes a dropped original, the body lists it, fold refuses it, and a record whose drop names another original's merge stops every command. Each report keeps the record's drops with the merge commit each is bound to and its reason, held on read to the same rules, so an earlier report still says why after an undrop. A reason now travels from a body to terminals, bodies and comments, so a reason or a file name in a record is believed only in the form a run writes it, one clean line, and so is a report's diagnostic. Output that repeats a reason, file names, a report ID or a state GitHub reports escapes it. Assisted-By: LLM Signed-off-by: Myasnikov Daniil <[email protected]>
1 parent 79b335e commit 2c1cb2f

19 files changed

Lines changed: 1217 additions & 81 deletions

‎cmd/backport-batch/README.md‎

Lines changed: 10 additions & 6 deletions
Large diffs are not rendered by default.

‎cmd/backport-batch/assemble.go‎

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,10 @@ func (a *app) run(args []string) int {
7575
cont := fs.Bool("continue", false, "continue the open batch pull request, or start a batch when none is open")
7676
dateArg := fs.String("date", "", "batch date, YYYY-MM-DD (default: today, UTC)")
7777
deferArg := fs.String("defer", "", "originals to take out of the open batch, N[,N]; rebuilds it without them")
78+
dropArg := fs.String("drop", "", "originals to drop from the line, N[,N]; recorded on the open batch, and taken off it")
79+
undropArg := fs.String("undrop", "", "originals whose drop to withdraw, N[,N]")
7880
noWorkflows := fs.Bool("no-workflow-files", false, "leave an original that changes "+workflowsDir+" to a maintainer, for a token that cannot push one")
79-
reasonArg := fs.String("reason", "", "why --defer takes them out, recorded in the batch body")
81+
reasonArg := fs.String("reason", "", "why --defer takes them out or --drop drops them, recorded in the batch body")
8082
push := fs.Bool("push", false, "push the branch and open or update the draft batch PR")
8183
if err := parse(fs, args); err != nil {
8284
return 2
@@ -96,9 +98,30 @@ func (a *app) run(args []string) int {
9698
return a.fail(fmt.Errorf("--onto: %w", err))
9799
}
98100
}
99-
var deferred []int
100-
reason := ""
101+
var deferred, drops []int
102+
reason, ops := "", 0
103+
for _, op := range []string{*deferArg, *dropArg, *undropArg} {
104+
if op != "" {
105+
ops++
106+
}
107+
}
101108
switch {
109+
case ops > 1:
110+
return a.fail(errors.New("--defer, --drop and --undrop each change the open batch pull request on their own; give one"))
111+
case *dropArg != "" || *undropArg != "":
112+
if *onto != "" || *cont || *dateArg != "" {
113+
return a.fail(errors.New("--drop and --undrop change the open batch pull request; each takes no --onto, --continue or --date"))
114+
}
115+
if *undropArg != "" && *reasonArg != "" {
116+
return a.fail(errors.New("--undrop takes no --reason"))
117+
}
118+
list := *dropArg + *undropArg
119+
if drops, err = parseNumbers(map[bool]string{true: "--drop", false: "--undrop"}[*dropArg != ""], list); err != nil {
120+
return a.fail(err)
121+
}
122+
if reason = capped(oneLine(clean(*reasonArg, false)), titleLimit); *dropArg != "" && reason == "" {
123+
return a.fail(errors.New("--drop needs a --reason, which the line's record keeps"))
124+
}
102125
case *deferArg != "" || *reasonArg != "":
103126
if deferred, reason, err = parseDefer(*deferArg, *reasonArg); err != nil {
104127
return a.fail(err)
@@ -119,9 +142,12 @@ func (a *app) run(args []string) int {
119142
if err != nil {
120143
return a.fail(err)
121144
}
122-
if goOn, code := a.resumeClose(b, *push, deferred != nil); !goOn {
145+
if goOn, code := a.resumeClose(b, *push, deferred != nil || drops != nil); !goOn {
123146
return code
124147
}
148+
if drops != nil {
149+
return a.redrop(b, drops, reason, *undropArg != "", *push)
150+
}
125151
if deferred != nil {
126152
return a.rebuild(b, deferred, reason, *push)
127153
}
@@ -162,6 +188,9 @@ func (a *app) run(args []string) int {
162188

163189
r := newReport(b)
164190
r.opts = runOptions{NoWorkflowFiles: *noWorkflows, Mode: modeNew}
191+
// A drop is a decision about the line, not about one batch: a new batch
192+
// keeps the ones the closed batch recorded.
193+
r.dropped = b.src.rec.dropped()
165194
if carried != nil {
166195
r.opts.Mode = modeContinue
167196
if r.stale, err = keptOut(b, carried); err != nil {
@@ -221,12 +250,12 @@ func (a *app) run(args []string) int {
221250
return a.finish(b, r, branch, date, *push, false)
222251
}
223252

224-
// keptOut lists the originals with backport/skip that the batch picked before
225-
// the label came: a run never takes a commit off the batch, --defer does.
253+
// keptOut lists the originals with backport/skip, or dropped, that the batch
254+
// still carries: a run never takes a commit off the batch, --defer does.
226255
func keptOut(b *batch, carried *history) ([]*verdict, error) {
227256
var out []*verdict
228257
for _, v := range b.verdicts {
229-
if v.label != labelSkip {
258+
if v.rule != ruleSkipLabel && v.rule != ruleDropped {
230259
continue
231260
}
232261
if err := b.load(v); err != nil {
@@ -331,7 +360,12 @@ func (a *app) finish(b *batch, r *report, branch string, date time.Time, push, r
331360
// act on, with the full classification left to the report it names.
332361
func (r *report) lineRecord(doc reportDoc) lineRecord {
333362
built := doc.Built
334-
return lineRecord{Version: 2, Line: doc.Line, Built: &built, Report: doc.ID, Closing: r.closing, Deferred: slices.Clone(doc.Primary.Deferred), Dropped: []droppedEntry{}, Stale: slices.Clone(doc.Overlays.Stale)}
363+
dropped := slices.Clone(r.dropped)
364+
if dropped == nil {
365+
dropped = []droppedEntry{}
366+
}
367+
slices.SortFunc(dropped, func(a, b droppedEntry) int { return a.Original - b.Original })
368+
return lineRecord{Version: 2, Line: doc.Line, Built: &built, Report: doc.ID, Closing: r.closing, Deferred: slices.Clone(doc.Primary.Deferred), Dropped: dropped, Stale: slices.Clone(doc.Overlays.Stale)}
335369
}
336370

337371
// branchDate is the date a batch branch was opened under.
@@ -686,7 +720,7 @@ func (a *app) finishClose(l line, url string, c closing) error {
686720
return fmt.Errorf("closing #%d, reading it again: %w", c.PR, err)
687721
}
688722
if st := prState(&pr); st != "open" || pr.Head.SHA != c.Head {
689-
return fmt.Errorf("closing #%d: it is %s at %.12s, not open at %.12s, which its record froze", c.PR, st, pr.Head.SHA, c.Head)
723+
return fmt.Errorf("closing #%d: it is %s at %.12s, not open at %.12s, which its record froze", c.PR, st, terminalSafe(pr.Head.SHA), c.Head)
690724
}
691725
heads, err := remoteBatchHeads(l)
692726
if err != nil {

‎cmd/backport-batch/body.go‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ func prTitle(l line, date string) string {
221221
}
222222

223223
// skipRules are the rules a skipped original is reported under, in order.
224-
var skipRules = []int{ruleBeforeStart, ruleOnLine, ruleType, ruleBreaking, ruleTouches, ruleNewer}
224+
var skipRules = []int{ruleBeforeStart, ruleOnLine, ruleType, ruleBreaking, ruleTouches, ruleNewer, ruleDropped}
225225

226226
// report is what one run put on the batch branch and what it left out.
227227
type report struct {
@@ -251,6 +251,8 @@ type report struct {
251251
// closing is the close of the open batch this run leaves carrying
252252
// nothing.
253253
closing *closing
254+
// dropped are the maintainers' drops the record keeps.
255+
dropped []droppedEntry
254256
}
255257

256258
// ruleName is how output names what kept an original out: the policy's
@@ -261,10 +263,20 @@ func ruleName(rule int) string {
261263
return labelSkip
262264
case ruleBeforeStart:
263265
return "rule 1, before the line's start"
266+
case ruleDropped:
267+
return "a maintainer's drop"
264268
}
265269
return "rule " + strconv.Itoa(rule)
266270
}
267271

272+
// listTitle titles the list of what a rule skipped, a drop among them.
273+
func (r *report) listTitle(rule int) string {
274+
if rule == ruleDropped {
275+
return "Dropped from " + codeSpan(r.line.branch()) + " by a maintainer"
276+
}
277+
return r.ruleTitle(rule)
278+
}
279+
268280
func (r *report) ruleTitle(rule int) string {
269281
switch rule {
270282
case ruleBeforeStart:
@@ -350,15 +362,15 @@ func (r *report) what(details map[int]bool) string {
350362
if !details[rule] {
351363
left = ", too many to list here; the run's output lists them"
352364
}
353-
fmt.Fprintf(&b, "- %s: %d%s\n", r.ruleTitle(rule), len(items), left)
365+
fmt.Fprintf(&b, "- %s: %d%s\n", r.listTitle(rule), len(items), left)
354366
}
355367
if r.other > 0 {
356368
fmt.Fprintf(&b, "- Commits on `main` that merged no pull request: %d\n", r.other)
357369
}
358370
for _, rule := range skipRules {
359371
items := r.skipped[rule]
360372
if len(items) > 0 && details[rule] {
361-
fmt.Fprintf(&b, "\n<details><summary>%s</summary>\n\n", r.ruleTitle(rule))
373+
fmt.Fprintf(&b, "\n<details><summary>%s</summary>\n\n", r.listTitle(rule))
362374
for _, v := range items {
363375
fmt.Fprintf(&b, "- %s %s, %s\n", prLink(v.o.number), titleSpan(v.o.title), titleSpan(v.reason))
364376
}
@@ -372,6 +384,8 @@ func (r *report) what(details map[int]bool) string {
372384
// outcome is what became of an original a label overrode.
373385
func (r *report) outcome(v *verdict) string {
374386
switch {
387+
case slices.Contains(r.stale, v) && v.rule == ruleDropped:
388+
return "dropped, but still on this branch; take it out with `backport-batch run --defer`"
375389
case slices.Contains(r.stale, v):
376390
return "still on this branch, picked before the label; take it out with `backport-batch run --defer`"
377391
case v.label == labelSkip:

‎cmd/backport-batch/check.go‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ func (b *batch) dispositions(cut bool) (map[*verdict]disposition, error) {
165165
switch {
166166
case slices.Contains(stale, v):
167167
out[v] = disposition{outcomeWarning, fmt.Sprintf("#%d is still on batch #%d; take it out with backport-batch run --defer: %s", v.o.number, b.pr.Number, named)}
168+
case v.rule == ruleDropped:
169+
d := b.drops[v.o.number]
170+
out[v] = disposition{outcomeNote, fmt.Sprintf("#%d is dropped from %s on #%d (%s): %s", v.o.number, b.line.branch(), d.On, terminalSafe(d.Reason), named)}
168171
case v.rule != 0:
169172
case v.onBatch != "":
170173
if cut {
@@ -250,7 +253,7 @@ func refuseFold(b *batch, n int, commit string) (string, error) {
250253
case v.rule == ruleSkipLabel:
251254
return fmt.Sprintf("#%d carries %s", n, labelSkip), nil
252255
case v.rule != 0:
253-
return fmt.Sprintf("#%d is not one the batch takes: %s, %s", n, ruleName(v.rule), v.reason), nil
256+
return fmt.Sprintf("#%d is not one the batch takes: %s, %s", n, ruleName(v.rule), terminalSafe(v.reason)), nil
254257
case b.pr == nil:
255258
return fmt.Sprintf("no batch pull request is open on %s", b.line.branch()), nil
256259
case v.onBatch != "":

‎cmd/backport-batch/check_test.go‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,20 @@ func TestOutputNamesNoInternalRule(t *testing.T) {
396396
}
397397
}
398398

399+
// A refusal fold prints is one line with no control sequence, whatever the
400+
// reason it repeats says: a drop's reason comes from a pull request body, a
401+
// rule 4 reason from file names an original's author chose.
402+
func TestFoldRefusalPrintsNothingThatDrivesATerminal(t *testing.T) {
403+
l, _ := parseLine("release-1.1")
404+
for _, reason := range []string{"dropped on #90: x\n\x1b[2Jrefused: forged", "touches \"app/a\x1b]0;x\x07\nok\""} {
405+
b := &batch{line: l, verdicts: []*verdict{{o: &original{number: 5}, rule: ruleDropped, reason: reason}}}
406+
why, err := refuseFold(b, 5, strings.Repeat("a", 40))
407+
if err != nil || why == "" || strings.ContainsFunc(why, func(r rune) bool { return unicode.IsControl(r) || unicode.Is(unicode.Cf, r) }) {
408+
t.Errorf("refuseFold = %q, %v; want one clean line", why, err)
409+
}
410+
}
411+
}
412+
399413
type errString string
400414

401415
func (e errString) Error() string { return string(e) }

‎cmd/backport-batch/defer.go‎

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,29 @@ import (
2727
// parseDefer reads --defer N[,N] and the --reason that has to come with it.
2828
func parseDefer(list, reason string) ([]int, string, error) {
2929
if list == "" {
30-
return nil, "", errors.New("--reason goes with --defer")
30+
return nil, "", errors.New("--reason goes with --defer or --drop")
3131
}
3232
reason = capped(oneLine(clean(reason, false)), titleLimit)
3333
if reason == "" {
3434
return nil, "", errors.New("--defer needs a --reason, which the batch body records")
3535
}
36+
numbers, err := parseNumbers("--defer", list)
37+
return numbers, reason, err
38+
}
39+
40+
// parseNumbers reads the N[,N] list a flag takes, each once.
41+
func parseNumbers(flag, list string) ([]int, error) {
3642
var numbers []int
3743
for _, s := range strings.Split(list, ",") {
3844
n, err := strconv.Atoi(strings.TrimPrefix(strings.TrimSpace(s), "#"))
3945
if err != nil || n <= 0 {
40-
return nil, "", fmt.Errorf("--defer %q is not a list of pull request numbers", list)
46+
return nil, fmt.Errorf("%s %q is not a list of pull request numbers", flag, list)
4147
}
4248
if !slices.Contains(numbers, n) {
4349
numbers = append(numbers, n)
4450
}
4551
}
46-
return numbers, reason, nil
52+
return numbers, nil
4753
}
4854

4955
// rebuild takes the originals in numbers out of the open batch: it builds the
@@ -54,17 +60,8 @@ func (a *app) rebuild(b *batch, numbers []int, reason string, push bool) int {
5460
if b.pr == nil {
5561
return a.fail(fmt.Errorf("no batch pull request is open on %s; --defer rebuilds the open one", b.line.branch()))
5662
}
57-
branch := b.pr.Head.Ref
58-
date, err := branchDate(branch)
59-
if err != nil {
60-
return a.fail(err)
61-
}
62-
recorded := map[int]deferredEntry{}
63-
if b.src.open {
64-
recorded = b.src.rec.deferred()
65-
}
6663
hold := map[int]string{}
67-
for n, e := range recorded {
64+
for n, e := range b.recorded() {
6865
if e.Reason != "" {
6966
hold[n] = e.Reason
7067
}
@@ -86,7 +83,15 @@ func (a *app) rebuild(b *batch, numbers []int, reason string, push bool) int {
8683
}
8784
hold[n] = reason
8885
}
89-
start, replay, err := rebuildPlan(b, hold)
86+
return a.reassemble(b, hold, hold, b.src.rec.dropped(), modeDefer, push)
87+
}
88+
89+
// reassemble builds the open batch again off the line's head without the
90+
// units of the originals in out, and reports it with hold deferred for their
91+
// reasons and drops kept.
92+
func (a *app) reassemble(b *batch, out, hold map[int]string, drops []droppedEntry, mode string, push bool) int {
93+
branch := b.pr.Head.Ref
94+
start, replay, err := rebuildPlan(b, out)
9095
if err != nil {
9196
return a.fail(err)
9297
}
@@ -102,7 +107,7 @@ func (a *app) rebuild(b *batch, numbers []int, reason string, push bool) int {
102107
case err != nil:
103108
return a.fail(err)
104109
case failed != "":
105-
return a.fail(fmt.Errorf("without what is deferred, %.12s%s no longer applies (%s); defer it as well", failed, originName(b, u), strings.Join(files, ", ")))
110+
return a.fail(fmt.Errorf("without what is deferred, %.12s%s no longer applies (%s); defer it as well", failed, originName(b, u), terminalSafe(strings.Join(files, ", "))))
106111
}
107112
}
108113
head, err := resolve("HEAD")
@@ -113,10 +118,23 @@ func (a *app) rebuild(b *batch, numbers []int, reason string, push bool) int {
113118
if err != nil {
114119
return a.fail(err)
115120
}
121+
return a.reclassify(b, picks, hold, drops, mode, push)
122+
}
123+
124+
// reclassify reports the open batch as picks shows it, after an operation
125+
// that adds no original: what the record held stays held, and an original
126+
// the batch neither carries nor records is missing until a run tries it.
127+
func (a *app) reclassify(b *batch, picks *history, hold map[int]string, drops []droppedEntry, mode string, push bool) int {
128+
date, err := branchDate(b.pr.Head.Ref)
129+
if err != nil {
130+
return a.fail(err)
131+
}
132+
recorded := b.recorded()
116133
r := newReport(b)
117-
// Taking an original out changes nothing about how the batch is
118-
// assembled, so the record keeps the flag the batch was assembled with.
119-
r.opts = runOptions{NoWorkflowFiles: b.src.savedNoWorkflowFiles(), Mode: modeDefer}
134+
// None of these changes how the batch is assembled, so the record keeps
135+
// the flag the batch was assembled with.
136+
r.opts = runOptions{NoWorkflowFiles: b.src.savedNoWorkflowFiles(), Mode: mode}
137+
r.dropped = drops
120138
if r.stale, err = keptOut(b, picks); err != nil {
121139
return a.fail(err)
122140
}
@@ -137,7 +155,16 @@ func (a *app) rebuild(b *batch, numbers []int, reason string, push bool) int {
137155
r.missing = append(r.missing, v)
138156
}
139157
}
140-
return a.finish(b, r, branch, date, push, true)
158+
return a.finish(b, r, b.pr.Head.Ref, date, push, true)
159+
}
160+
161+
// recorded is what the open batch's record defers; a closed batch's record
162+
// holds nothing back on this one.
163+
func (b *batch) recorded() map[int]deferredEntry {
164+
if !b.src.open {
165+
return map[int]deferredEntry{}
166+
}
167+
return b.src.rec.deferred()
141168
}
142169

143170
// rebuildPlan is where the rebuild of the open batch without the originals in

‎cmd/backport-batch/defer_test.go‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,3 +684,26 @@ func TestRefusalNamesTheMergedBatchsBranch(t *testing.T) {
684684
t.Errorf("run --push = %d, want a refusal naming #90:\n%s", code, stderr)
685685
}
686686
}
687+
688+
// A rebuild that stops names the files that no longer apply on the line of
689+
// its refusal, whatever the files are called.
690+
func TestDeferRefusalNamesFilesOnOneLine(t *testing.T) {
691+
f := newFixture(t)
692+
f.lines()
693+
name := "app/x\nwarning: forged.txt"
694+
f.pr(5, "fix(app): five", "", map[string]string{name: "five\n"})
695+
f.pr(6, "fix(app): six", "", map[string]string{name: "five and six\n"})
696+
gh := &fakeGH{}
697+
openBatch(t, f, gh)
698+
f.onLine("release-1.1", "fix(chart): a line-only value", map[string]string{"chart/values.yaml": "replicas: 3\nimage: app:1\n"})
699+
code, _, stderr := f.exec(gh, "run", "--line", "release-1.1", "--defer", "5", "--reason", "x", "--push")
700+
f.git("switch", "-q", "main")
701+
if code != 2 || !strings.Contains(stderr, "no longer applies") {
702+
t.Fatalf("run --defer 5 = %d, want the refusal:\n%s", code, stderr)
703+
}
704+
for _, ln := range strings.Split(stderr, "\n") {
705+
if strings.HasPrefix(ln, "warning: forged") {
706+
t.Errorf("the refusal split onto a line of its own: %q", ln)
707+
}
708+
}
709+
}

0 commit comments

Comments
 (0)