GetUncommittedFileDiffs returns per-line additions and deletions for files with uncommitted changes (staged + unstaged). Line numbers in Additions reference the working-tree (post-image); line numbers in Deletions reference HEAD (pre-image). Both are 1-indexed.
(repoPath string)
| 15 | // reference the working-tree (post-image); line numbers in Deletions |
| 16 | // reference HEAD (pre-image). Both are 1-indexed. |
| 17 | func GetUncommittedFileDiffs(repoPath string) (map[string]vcs.FileDiff, error) { |
| 18 | if _, err := os.Stat(repoPath); os.IsNotExist(err) { |
| 19 | return nil, fmt.Errorf("repository path does not exist: %s", repoPath) |
| 20 | } |
| 21 | if !isGitRepository(repoPath) { |
| 22 | return nil, fmt.Errorf("%s is not a git repository", repoPath) |
| 23 | } |
| 24 | repoRoot, err := GetRepositoryRoot(repoPath) |
| 25 | if err != nil { |
| 26 | return nil, fmt.Errorf("failed to get repository root: %w", err) |
| 27 | } |
| 28 | |
| 29 | stdout, stderr, err := runGitCommand(repoPath, "diff", "--unified=0", "--no-color", "HEAD") |
| 30 | if err != nil { |
| 31 | return nil, gitCommandError(err, stderr) |
| 32 | } |
| 33 | |
| 34 | diffs := parseUnifiedDiff(string(stdout)) |
| 35 | |
| 36 | // Resolve relative paths to absolute, and account for untracked files which |
| 37 | // `git diff HEAD` does not surface. |
| 38 | out := make(map[string]vcs.FileDiff, len(diffs)) |
| 39 | for relPath, fd := range diffs { |
| 40 | out[filepath.Join(repoRoot, relPath)] = fd |
| 41 | } |
| 42 | |
| 43 | statusMap, err := getUncommittedFileStatuses(repoPath) |
| 44 | if err != nil { |
| 45 | return nil, err |
| 46 | } |
| 47 | for relPath, status := range statusMap { |
| 48 | if !isNewStatus(status) { |
| 49 | continue |
| 50 | } |
| 51 | absPath := filepath.Join(repoRoot, relPath) |
| 52 | if _, ok := out[absPath]; ok { |
| 53 | continue |
| 54 | } |
| 55 | lineCount, err := countLinesInFile(absPath) |
| 56 | if err != nil { |
| 57 | continue |
| 58 | } |
| 59 | adds := make([]int, lineCount) |
| 60 | for i := 0; i < lineCount; i++ { |
| 61 | adds[i] = i + 1 |
| 62 | } |
| 63 | out[absPath] = vcs.FileDiff{Additions: adds, IsNew: true} |
| 64 | } |
| 65 | |
| 66 | return out, nil |
| 67 | } |
| 68 | |
| 69 | // GetCommitFileDiffs returns per-line additions and deletions for files |
| 70 | // modified in a single commit, computed against that commit's first parent. |