Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 49 additions & 3 deletions internal/gitrepo/work.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"regexp"
"sort"
"strings"
"unicode/utf8"
)

// WorkRepo is the local repository used to apply series. It is a
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down
67 changes: 67 additions & 0 deletions internal/gitrepo/work_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,80 @@ 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 {
t.Fatalf("second apply: shas=%v failed=%d out=%q err=%v", shas, failed, out, err)
}
}

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) {
Expand Down
4 changes: 0 additions & 4 deletions internal/pipeline/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down