package server import ( "fmt" "os" "path/filepath" "sort" "strings" "git.a-wan.net/alan/pipelines/internal/protocol" ) type gitDiffEntry struct { path string kind string untracked bool } // collectGitDiff lists the files changed between revisions (one revision: the // work tree against it; two: those commits) plus, when includeUntracked, the // untracked files Git does not ignore, and reads each file's patch within the // show_diff bounds. func collectGitDiff(git gitRunner, revisions []string, includeUntracked bool) (*protocol.StageFileChanges, error) { nameArgs := append([]string{"diff", "--name-status", "-z", "--no-renames"}, revisions...) names, err := git.output(append(nameArgs, "--")...) if err != nil { return nil, err } entries := parseNameStatus(names) if includeUntracked { others, err := git.output("ls-files", "--others", "--exclude-standard", "-z") if err != nil { return nil, err } for _, path := range strings.Split(others, "\x00") { if path != "" { entries = append(entries, gitDiffEntry{path: path, kind: "added", untracked: true}) } } } sort.SliceStable(entries, func(i, j int) bool { return entries[i].path < entries[j].path }) changes := &protocol.StageFileChanges{Status: "complete", Files: []protocol.FileChange{}} used := 0 var partial []string for i, entry := range entries { if i >= showDiffMaxFiles { changes.Omitted = len(entries) - showDiffMaxFiles partial = append(partial, fmt.Sprintf("%d more files are not listed (the limit is %d)", changes.Omitted, showDiffMaxFiles)) break } file := protocol.FileChange{Path: entry.path, Kind: entry.kind} patch, reason, err := readEntryPatch(git, revisions, entry) if err != nil { return nil, err } switch { case reason != "": case isBinaryPatch(patch): file.Binary, reason = true, "binary" case len(patch) > showDiffMaxFileBytes: reason = "file_size" case used+len(patch) > showDiffMaxBytes: reason = "budget" } if reason == "" { file.Patch = patch used += len(patch) } else { file.NoPatch, file.NoPatchReason = true, reason file.Truncated = reason != "binary" } if reason == "file_size" || reason == "budget" { partial = appendOnce(partial, "some patches were left out by the size limit") } changes.Files = append(changes.Files, file) } if len(partial) > 0 { changes.Status = "partial" changes.Diagnostic = strings.Join(partial, "; ") + "." } return changes, nil } // readEntryPatch returns one file's patch, or the no-patch reason when its // content is not read. func readEntryPatch(git gitRunner, revisions []string, entry gitDiffEntry) (string, string, error) { flags := []string{"diff", "--no-color", "--no-ext-diff", "--no-textconv", "--no-renames"} if entry.untracked { info, err := os.Lstat(filepath.Join(git.directory, entry.path)) if err != nil { return "", "git_status", nil } if info.Mode().IsRegular() && info.Size() > showDiffMaxFileBytes { return "", "file_size", nil } patch, err := git.patch(true, append(flags, "--no-index", "--", os.DevNull, entry.path)...) return patch, "", err } args := append(append(flags, revisions...), "--", ":(literal)"+entry.path) patch, err := git.patch(false, args...) return patch, "", err } // parseNameStatus reads `git diff --name-status -z --no-renames`: a status // letter, then the path, each NUL-terminated. func parseNameStatus(out string) []gitDiffEntry { fields := strings.Split(out, "\x00") var entries []gitDiffEntry for i := 0; i+1 < len(fields); i += 2 { status, path := fields[i], fields[i+1] if status == "" || path == "" { continue } kind := "modified" switch status[0] { case 'A': kind = "added" case 'D': kind = "deleted" } entries = append(entries, gitDiffEntry{path: path, kind: kind}) } return entries } func isBinaryPatch(patch string) bool { for _, line := range strings.Split(patch, "\n") { if strings.HasPrefix(line, "@@") { return false } if strings.HasPrefix(line, "Binary files ") || line == "GIT binary patch" { return true } } return false } func appendOnce(list []string, value string) []string { for _, existing := range list { if existing == value { return list } } return append(list, value) }