| 1 | package derive |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "os/exec" |
| 9 | "strings" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | // Diff returns every side of the diff from base to head in the repository |
| 14 | // at dir, sorted bytewise and each once. It reads it as `valley checks` |
| 15 | // does: `git diff --no-relative --name-status -M -z`, so renames are |
| 16 | // detected and both sides of one count, and every path is read whole |
| 17 | // whatever bytes it holds (Changed). Replacement refs are off, so no |
| 18 | // refs/replace/* can change which trees are compared. |
| 19 | // |
| 20 | // git runs in env, the caller's choice of environment; nil is this |
| 21 | // process's. A git that fails, and output that is cut short, are errors: |
| 22 | // read as no paths, either would be a change that requires nothing. |
| 23 | func Diff(ctx context.Context, dir string, env []string, base, head string) ([]string, error) { |
| 24 | for _, rev := range []string{base, head} { |
| 25 | if rev == "" || strings.HasPrefix(rev, "-") { |
| 26 | return nil, fmt.Errorf("%q is not a commit to diff", rev) |
| 27 | } |
| 28 | } |
| 29 | cmd := exec.CommandContext(ctx, "git", "--no-replace-objects", "diff", |
| 30 | "--no-relative", "--name-status", "-M", "-z", base, head, "--") |
| 31 | cmd.Dir, cmd.Env = dir, env |
| 32 | cmd.WaitDelay = 5 * time.Second |
| 33 | var out, errb bytes.Buffer |
| 34 | cmd.Stdout, cmd.Stderr = &out, &errb |
| 35 | if err := cmd.Run(); err != nil { |
| 36 | return nil, fmt.Errorf("could not diff %s..%s: %w: %s", base, head, err, strings.TrimSpace(errb.String())) |
| 37 | } |
| 38 | paths, err := Changed(out.Bytes()) |
| 39 | if err != nil { |
| 40 | return nil, err |
| 41 | } |
| 42 | return sortedSet(paths), nil |
| 43 | } |
| 44 | |
| 45 | // Changed reads every side of a diff from the output of `git diff -z |
| 46 | // --name-status`: an added, modified or deleted path, and both the old and |
| 47 | // the new path of a rename or a copy, in the order git gave them. |
| 48 | // |
| 49 | // Each field ends in NUL, so a path is read whole, newlines and bytes that |
| 50 | // are not UTF-8 included. Output that ends partway through a record is |
| 51 | // refused, a record cut inside its status too, and so is a record that is |
| 52 | // not one git writes: an unknown status, or an empty path. |
| 53 | func Changed(out []byte) ([]string, error) { |
| 54 | var paths []string |
| 55 | for len(out) > 0 { |
| 56 | status, after, ok := bytes.Cut(out, []byte{0}) |
| 57 | if !ok { |
| 58 | return nil, errTruncated |
| 59 | } |
| 60 | if !validStatus(status) { |
| 61 | return nil, fmt.Errorf("the diff holds a record whose status is %q, which git does not write", status) |
| 62 | } |
| 63 | out = after |
| 64 | sides := 1 |
| 65 | if status[0] == 'R' || status[0] == 'C' { |
| 66 | sides = 2 |
| 67 | } |
| 68 | for ; sides > 0; sides-- { |
| 69 | path, after, ok := bytes.Cut(out, []byte{0}) |
| 70 | if !ok { |
| 71 | return nil, errTruncated |
| 72 | } |
| 73 | if len(path) == 0 { |
| 74 | return nil, errors.New("the diff holds a record with an empty path, which git does not write") |
| 75 | } |
| 76 | paths = append(paths, string(path)) |
| 77 | out = after |
| 78 | } |
| 79 | } |
| 80 | return paths, nil |
| 81 | } |
| 82 | |
| 83 | var errTruncated = errors.New("truncated diff output") |
| 84 | |
| 85 | // validStatus reports whether s is a status --name-status writes: one |
| 86 | // letter, and for a rename or a copy the similarity after it. |
| 87 | func validStatus(s []byte) bool { |
| 88 | if len(s) == 0 || !strings.ContainsRune("ACDMRTUXB", rune(s[0])) { |
| 89 | return false |
| 90 | } |
| 91 | for _, c := range s[1:] { |
| 92 | if c < '0' || c > '9' { |
| 93 | return false |
| 94 | } |
| 95 | } |
| 96 | return true |
| 97 | } |