MCPcopy Create free account
hub / github.com/alibaba/open-code-review / untrackedFileDiffs

Method untrackedFileDiffs

internal/diff/git.go:656–704  ·  view source on GitHub ↗
(ctx context.Context)

Source from the content-addressed store, hash-verified

654}
655
656func (p *Provider) untrackedFileDiffs(ctx context.Context) ([]string, error) {
657 files, err := p.untrackedFilesList(ctx)
658 if err != nil {
659 return nil, err
660 }
661
662 var results []string
663 for _, f := range files {
664 content, rerr := readWorkspaceFileForDiffWithLimit(p.repoDir, f, maxUntrackedFileSize)
665 if errors.Is(rerr, errWorkspaceFileTooLarge) {
666 results = append(results, untrackedBinaryDiff(f))
667 continue
668 }
669 if rerr != nil {
670 continue
671 }
672
673 if looksBinary(content) {
674 // Emit git's own binary marker so the parser flags the file and
675 // the selection layer excludes it, matching the tracked path where
676 // `git diff` itself reports "Binary files ... differ".
677 results = append(results, untrackedBinaryDiff(f))
678 continue
679 }
680
681 lineCount := bytes.Count(content, []byte{'\n'})
682 if len(content) > 0 && content[len(content)-1] != '\n' {
683 lineCount++
684 }
685
686 var sb strings.Builder
687 sb.WriteString(fmt.Sprintf("diff --git a/%s b/%s\n", f, f))
688 sb.WriteString("--- /dev/null\n")
689 sb.WriteString(fmt.Sprintf("+++ b/%s\n", f))
690 sb.WriteString(fmt.Sprintf("@@ -0,0 +1,%d @@\n", lineCount))
691
692 lines := bytes.Split(content, []byte{'\n'})
693 if len(lines) > 0 && len(lines[len(lines)-1]) == 0 {
694 lines = lines[:len(lines)-1]
695 }
696 for _, line := range lines {
697 sb.WriteByte('+')
698 sb.Write(line)
699 sb.WriteByte('\n')
700 }
701 results = append(results, sb.String())
702 }
703 return results, nil
704}
705
706func (p *Provider) untrackedFilesList(ctx context.Context) ([]string, error) {
707 // -z, and no trimming of the records it produces. Without it git quotes

Callers 1

GetDiffSetMethod · 0.95

Calls 6

untrackedFilesListMethod · 0.95
untrackedBinaryDiffFunction · 0.85
looksBinaryFunction · 0.85
WriteMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected