Files
pipelines/internal/server/chat_tools_diff_collect.go
T
alan 059eb1bb0d Add show_diff chat tool and diff card
The Pipelines MCP server gains show_diff: it runs Git in a repository
inside the operator's home (default: the chat's directory) and records
the per-file patches. Without base it shows uncommitted work against
HEAD; with base it shows everything since the merge base (a pull
request preview), optionally committed changes only. Bounded at 300
files, 256 KiB per patch and 1 MiB total, with partial diffs labelled;
diff bodies share the per-chat retention budget with HTML views.

The chat draws the item as a card listing each changed file with its
stat; a row opens the existing file diff viewer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 17:08:02 +00:00

147 lines
4.2 KiB
Go

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)
}