Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.
JavaScript agent-orchestrationagentic-workflowai-agentsai-codingclaude-codecode-reviewcodebuddycodexcoding-agentscursordeveloper-toolshuman-on-the-loopllmmcpmulti-agentsdlcself-hostedsopworkflow-automationworkflow-engine
refactor(gitwork): split ParseNameStatusNumstat line parsing (#326) (#327)
fx-kurumibd89008c9b6b
diff --git a/internal/gitwork/patch.go b/internal/gitwork/patch.goindex 9614a63d..c79e1c36 100644--- a/internal/gitwork/patch.go+++ b/internal/gitwork/patch.go@@ -261,78 +261,97 @@ func RenderPatchInventory(files []PatchFileSummary) string {return strings.TrimSpace(b.String())}+// ParseNameStatusNumstat merges `git diff --name-status` and+// `git diff --numstat` output into one summary per path, sorted by path.func ParseNameStatusNumstat(nameStatus, numstat []byte) []PatchFileSummary {- type rec struct {- file PatchFileSummary- order int- }- best := map[string]rec{}- next := 0- put := func(path string, update func(*PatchFileSummary)) {- path = filepath.ToSlash(UnquoteLsFilesPath(path))- if path == "" {- return- }- item, ok := best[path]+ files := map[string]*PatchFileSummary{}+ for _, line := range strings.Split(string(nameStatus), "\n") {+ path, status, from, ok := parseNameStatusLine(line)if !ok {- item = rec{file: PatchFileSummary{Path: path, Status: "M"}, order: next}- next+++ continue}- update(&item.file)- item.file.Path = path- if item.file.Status == "" {- item.file.Status = "M"+ if f := patchSummaryFor(files, path); f != nil {+ f.Status = status+ f.RenameFrom = from}- best[path] = item}- for _, line := range strings.Split(string(nameStatus), "\n") {- line = strings.TrimRight(line, "\r")- if strings.TrimSpace(line) == "" {+ for _, line := range strings.Split(string(numstat), "\n") {+ path, added, deleted, binary, ok := parseNumstatLine(line)+ if !ok {continue}- parts := strings.Split(line, "\t")- if len(parts) < 2 || parts[0] == "" {+ f := patchSummaryFor(files, path)+ if f == nil {continue}- letter := string(parts[0][0])- path := parts[len(parts)-1]- from := ""- if (letter == "R" || letter == "C") && len(parts) >= 3 {- from = filepath.ToSlash(UnquoteLsFilesPath(parts[1]))- path = parts[2]- }- put(path, func(f *PatchFileSummary) {- f.Status = letter- f.RenameFrom = from- })- }- for _, line := range strings.Split(string(numstat), "\n") {- line = strings.TrimRight(line, "\r")- parts := strings.Split(line, "\t")- if len(parts) < 3 {+ if binary {+ f.Binary = true+ if f.Status == "M" {+ f.Status = "B"+ }continue}- path := parts[len(parts)-1]- put(path, func(f *PatchFileSummary) {- if parts[0] == "-" || parts[1] == "-" {- f.Binary = true- if f.Status == "M" || f.Status == "" {- f.Status = "B"- }- return- }- f.Added, _ = strconv.Atoi(parts[0])- f.Deleted, _ = strconv.Atoi(parts[1])- })+ f.Added, f.Deleted = added, deleted}- out := make([]PatchFileSummary, 0, len(best))- for _, item := range best {- out = append(out, item.file)+ out := make([]PatchFileSummary, 0, len(files))+ for _, f := range files {+ out = append(out, *f)}sort.Slice(out, func(i, j int) bool { return out[i].Path < out[j].Path })return out}+// patchSummaryFor returns the summary for the normalized path, creating a+// modified ("M") entry on first sight; it returns nil for an empty path.+func patchSummaryFor(files map[string]*PatchFileSummary, rawPath string) *PatchFileSummary {+ path := filepath.ToSlash(UnquoteLsFilesPath(rawPath))+ if path == "" {+ return nil+ }+ f, ok := files[path]+ if !ok {+ f = &PatchFileSummary{Path: path, Status: "M"}+ files[path] = f+ }+ return f+}++// parseNameStatusLine parses one `--name-status` line into its raw path, the+// single-letter status and, for renames/copies, the normalized source path.+func parseNameStatusLine(line string) (path, status, from string, ok bool) {+ line = strings.TrimRight(line, "\r")+ if strings.TrimSpace(line) == "" {+ return "", "", "", false+ }+ parts := strings.Split(line, "\t")+ if len(parts) < 2 || parts[0] == "" {+ return "", "", "", false+ }+ status = string(parts[0][0])+ path = parts[len(parts)-1]+ if (status == "R" || status == "C") && len(parts) >= 3 {+ from = filepath.ToSlash(UnquoteLsFilesPath(parts[1]))+ path = parts[2]+ }+ return path, status, from, true+}++// parseNumstatLine parses one `--numstat` line; binary files report "-" for+// both counts. Unparseable counts read as zero.+func parseNumstatLine(line string) (path string, added, deleted int, binary, ok bool) {+ parts := strings.Split(strings.TrimRight(line, "\r"), "\t")+ if len(parts) < 3 {+ return "", 0, 0, false, false+ }+ path = parts[len(parts)-1]+ if parts[0] == "-" || parts[1] == "-" {+ return path, 0, 0, true, true+ }+ added, _ = strconv.Atoi(parts[0])+ deleted, _ = strconv.Atoi(parts[1])+ return path, added, deleted, false, true+}+var patchWorkspaceLocks sync.Map // cleaned absolute path -> *sync.Mutexfunc lockPatchWorkspace(workspace string) func() {diff --git a/internal/gitwork/patch_summary_parse_test.go b/internal/gitwork/patch_summary_parse_test.gonew file mode 100644index 00000000..299a8638--- /dev/null+++ b/internal/gitwork/patch_summary_parse_test.go@@ -0,0 +1,37 @@+package gitwork++import (+ "reflect"+ "testing"+)++func TestParseNameStatusNumstatMergesAndSorts(t *testing.T) {+ nameStatus := []byte("M\tz.go\r\nR087\told/a.go\tnew/a.go\nC100\tsrc.go\tcopy.go\nA\tadded.png\nD\tgone.go\n\n \nbad\n\tmissing-letter\n")+ numstat := []byte("1\t2\tz.go\n4\t0\tnew/a.go\n-\t-\tadded.png\n-\t-\tonly-numstat.bin\n7\t8\tonly-numstat.go\nshort\t1\n")+ got := ParseNameStatusNumstat(nameStatus, numstat)+ want := []PatchFileSummary{+ {Path: "added.png", Status: "A", Binary: true},+ {Path: "copy.go", Status: "C", RenameFrom: "src.go"},+ {Path: "gone.go", Status: "D"},+ {Path: "new/a.go", Status: "R", Added: 4, RenameFrom: "old/a.go"},+ {Path: "only-numstat.bin", Status: "B", Binary: true},+ {Path: "only-numstat.go", Status: "M", Added: 7, Deleted: 8},+ {Path: "z.go", Status: "M", Added: 1, Deleted: 2},+ }+ if !reflect.DeepEqual(got, want) {+ t.Fatalf("files =\n%#v\nwant\n%#v", got, want)+ }+}++func TestParseNameStatusNumstatUnquotesAndSkipsEmptyPaths(t *testing.T) {+ nameStatus := []byte("M\t\"dir/\\344\\270\\255.go\"\nM\t\"\"\n")+ numstat := []byte("3\t1\t\"dir/\\344\\270\\255.go\"\n1\t1\t\n")+ got := ParseNameStatusNumstat(nameStatus, numstat)+ want := []PatchFileSummary{{Path: "dir/中.go", Status: "M", Added: 3, Deleted: 1}}+ if !reflect.DeepEqual(got, want) {+ t.Fatalf("files = %#v, want %#v", got, want)+ }+ if got := ParseNameStatusNumstat(nil, nil); len(got) != 0 {+ t.Fatalf("empty input = %#v", got)+ }+}