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

Method untrackedFileDiffs

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

Source from the content-addressed store, hash-verified

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