From a9e3e806c2d5535a5133f50abae5478967939f42 Mon Sep 17 00:00:00 2001 From: Rosemarie O'Riorden Date: Wed, 25 Nov 2026 00:44:53 -0500 Subject: [PATCH] gitrepo: Show the conflicts of patches that fail to apply. git am -q prints only "Patch failed at ..." and hints, so reports of patches that fail to apply did not say where the conflict was. Run git am without -q, which names every file with conflicts, turn off its hints, and follow its output with the diff of the conflicts, capped at 100 lines and 10000 characters. The diff ignores the color and external diff settings of the user running the robot, which would garble the report. The cap of 4000 characters now applies to the git am output alone, so that the diff cannot push the conflicts out of the report. Fixes fuzzfactor/leadtestkit#2 Signed-off-by: Rosemarie O'Riorden --- internal/gitrepo/work.go | 52 +++++++++++++++++++++++++-- internal/gitrepo/work_test.go | 67 +++++++++++++++++++++++++++++++++++ internal/pipeline/schedule.go | 4 --- 3 files changed, 116 insertions(+), 7 deletions(-) diff --git a/internal/gitrepo/work.go b/internal/gitrepo/work.go index 1d1d3463..1c3798db 100644 --- a/internal/gitrepo/work.go +++ b/internal/gitrepo/work.go @@ -14,6 +14,7 @@ import ( "regexp" "sort" "strings" + "unicode/utf8" ) // WorkRepo is the local repository used to apply series. It is a @@ -149,7 +150,8 @@ func (w *WorkRepo) ReadFile(ctx context.Context, rev, path string) ([]byte, erro // Apply applies the emails on top of base one by one. It returns the // commit of every applied patch, or "" for a patch whose change is // already there, which git am applies without a commit; on a failure it -// also returns the index of the failing patch and the git am output. +// also returns the index of the failing patch and the git am output, +// followed by the diff of any conflicts. func (w *WorkRepo) Apply( ctx context.Context, base string, mboxes [][]byte, ) ([]string, int, string, error) { @@ -182,13 +184,27 @@ func (w *WorkRepo) Apply( if err != nil { return shas, -1, "", err } - if _, err := g.Run(ctx, "am", "-q", "-3", "--no-gpg-sign", file); err != nil { + // Without -q, git am names the files with conflicts. + if _, err := g.Run(ctx, "-c", "advice.mergeConflict=false", "-c", + "advice.amWorkDir=false", "am", "-3", + "--no-gpg-sign", file); err != nil { var gerr *Error if !errors.As(err, &gerr) { return shas, -1, "", err } + out := strings.TrimSpace(gerr.Stdout + "\n" + gerr.Stderr) + if len(out) > maxAmOutput { + out = "...\n" + out[len(out)-maxAmOutput:] + } + diff, err := w.Git.Out(ctx, "diff", "--no-color", "--no-ext-diff") w.abortAm(ctx) - return shas, i, strings.TrimSpace(gerr.Stdout + "\n" + gerr.Stderr), nil + if err != nil { + return shas, -1, "", err + } + if diff != "" { + out += "\n\n" + diffHeading + "\n" + capDiff(diff) + } + return shas, i, out, nil } sha, err := w.Git.Out(ctx, "rev-parse", "HEAD") if err != nil { @@ -204,6 +220,36 @@ func (w *WorkRepo) Apply( return shas, -1, "", nil } +const ( + maxAmOutput = 4000 + maxDiffLines = 100 + maxDiffChars = 10000 + diffHeading = "Diff of the conflicts:" +) + +// capDiff keeps the start of a diff of at most maxDiffLines lines and +// maxDiffChars bytes, saying so when it drops any. It cuts whole lines +// unless the first line alone is too long. +func capDiff(s string) string { + note := "" + if lines := strings.Split(s, "\n"); len(lines) > maxDiffLines { + s = strings.Join(lines[:maxDiffLines], "\n") + note = fmt.Sprintf("\n... %d more lines.", len(lines)-maxDiffLines) + } + if len(s) > maxDiffChars { + cut := strings.LastIndexByte(s[:maxDiffChars+1], '\n') + if cut < 0 { + cut = maxDiffChars + for cut > 0 && !utf8.RuneStart(s[cut]) { + cut-- + } + } + s = s[:cut] + note = fmt.Sprintf("\n... longer than %d characters.", maxDiffChars) + } + return s + note +} + // SameChange reports whether a commit makes the change of an email, // comparing stable patch IDs, which ignore whitespace and line numbers. func (w *WorkRepo) SameChange(ctx context.Context, sha string, mbox []byte) (bool, error) { diff --git a/internal/gitrepo/work_test.go b/internal/gitrepo/work_test.go index 31b789f8..9b548f4e 100644 --- a/internal/gitrepo/work_test.go +++ b/internal/gitrepo/work_test.go @@ -136,6 +136,10 @@ func TestApplyFailure(t *testing.T) { if err != nil || failed != 0 || len(shas) != 0 || out == "" { t.Fatalf("shas=%v failed=%d out=%q err=%v", shas, failed, out, err) } + // Without a three-way merge there are no conflicts to show. + if strings.Contains(out, diffHeading) { + t.Errorf("out=%q", out) + } // The repository must be usable again after a failure. shas, failed, out, err = w.Apply(ctx, base, patches[:2]) if err != nil || failed != -1 || len(shas) != 2 { @@ -143,6 +147,69 @@ func TestApplyFailure(t *testing.T) { } } +func TestApplyConflict(t *testing.T) { + upstream, test, patches := setup(t) + ctx := context.Background() + w := newWork(t, test) + // Settings of the operator must not change the diff in reports. + config := filepath.Join(t.TempDir(), "gitconfig") + write(t, config, "[color]\n\tui = always\n[diff]\n\texternal = false\n") + w.Git.Env = []string{"GIT_CONFIG_GLOBAL=" + config} + old, _ := w.Resolve(ctx, upstream, "main") + clone := filepath.Join(t.TempDir(), "clone") + run(t, "", "clone", "-q", upstream, clone) + write(t, filepath.Join(clone, "lorem.txt"), "lorem\nipsum\namet\n") + run(t, clone, "commit", "-qam", "lorem: Add amet.") + run(t, clone, "push", "-q", "origin", "main") + base, _ := w.Resolve(ctx, upstream, "main") + shas, failed, out, err := w.Apply(ctx, base, patches[:1]) + if err != nil || failed != 0 || len(shas) != 0 { + t.Fatalf("shas=%v failed=%d out=%q err=%v", shas, failed, out, err) + } + conflict := strings.Index(out, "CONFLICT (content): Merge conflict in lorem.txt") + heading := strings.Index(out, diffHeading) + if conflict < 0 || heading < conflict || !strings.Contains(out[heading:], "<<<<<<<") || + strings.Contains(out, "hint:") || strings.Contains(out, "\x1b[") { + t.Errorf("out=%q", out) + } + // The repository must be usable again after a conflict. + if shas, failed, out, err = w.Apply(ctx, old, patches[:1]); err != nil || + failed != -1 || len(shas) != 1 { + t.Fatalf("second apply: shas=%v failed=%d out=%q err=%v", shas, failed, out, err) + } +} + +func TestCapDiff(t *testing.T) { + lines := func(n int, line string) string { + s := make([]string, n) + for i := range s { + s[i] = line + } + return strings.Join(s, "\n") + } + const tooLong = "\n... longer than 10000 characters." + wide := strings.Repeat("x", 299) + for _, c := range []struct{ name, in, want string }{ + {"100 lines", lines(100, "line"), lines(100, "line")}, + {"101 lines", lines(101, "line"), + lines(100, "line") + "\n... 1 more lines."}, + {"150 lines", lines(150, "line"), lines(100, "line") + "\n... 50 more lines."}, + {"10000 characters", lines(1, strings.Repeat("x", 10000)), + strings.Repeat("x", 10000)}, + // Whole lines are kept: 33 lines of 299 characters and their + // newlines fit in 10000 characters, 34 do not. + {"long lines", lines(50, wide), lines(33, wide) + tooLong}, + {"one long line", strings.Repeat("x", 20000), strings.Repeat("x", 10000) + tooLong}, + // A two-byte rune across the limit is dropped whole. + {"one long line of runes", "x" + strings.Repeat("é", 6000), + "x" + strings.Repeat("é", 4999) + tooLong}, + } { + if got := capDiff(c.in); got != c.want { + t.Errorf("%s: got %d bytes %q...", c.name, len(got), got[max(0, len(got)-80):]) + } + } +} + // git am applies a patch whose change is already there without a // commit, and without an error. func TestApplyAlreadyApplied(t *testing.T) { diff --git a/internal/pipeline/schedule.go b/internal/pipeline/schedule.go index aa13354e..c1b97ff2 100644 --- a/internal/pipeline/schedule.go +++ b/internal/pipeline/schedule.go @@ -669,10 +669,6 @@ func short(sha string) string { } func applyFailure(base *state.Base, output string) string { - const maxDetails = 4000 - if len(output) > maxDetails { - output = "...\n" + output[len(output)-maxDetails:] - } return fmt.Sprintf("Failed to apply on top of %s (%s):\n\n%s", base.Branch, short(base.SHA), output) }