GetUncommittedFileStats returns statistics (additions/deletions) for uncommitted files Returns a map from relative file paths to their FileStats
(repoPath string)
| 17 | // GetUncommittedFileStats returns statistics (additions/deletions) for uncommitted files |
| 18 | // Returns a map from relative file paths to their FileStats |
| 19 | func GetUncommittedFileStats(repoPath string) (map[string]vcs.FileStats, error) { |
| 20 | // Validate the repository path exists |
| 21 | if _, err := os.Stat(repoPath); os.IsNotExist(err) { |
| 22 | return nil, fmt.Errorf("repository path does not exist: %s", repoPath) |
| 23 | } |
| 24 | |
| 25 | // Verify it's a git repository |
| 26 | if !isGitRepository(repoPath) { |
| 27 | return nil, fmt.Errorf("%s is not a git repository", repoPath) |
| 28 | } |
| 29 | |
| 30 | // Get the repository root |
| 31 | repoRoot, err := GetRepositoryRoot(repoPath) |
| 32 | if err != nil { |
| 33 | return nil, fmt.Errorf("failed to get repository root: %w", err) |
| 34 | } |
| 35 | |
| 36 | // Run git diff --numstat to get stats for uncommitted changes |
| 37 | // This includes both staged and unstaged changes |
| 38 | stdout, stderr, err := runGitCommand(repoPath, "diff", "--numstat", "HEAD") |
| 39 | if err != nil { |
| 40 | return nil, gitCommandError(err, stderr) |
| 41 | } |
| 42 | |
| 43 | statusMap, err := getUncommittedFileStatuses(repoPath) |
| 44 | if err != nil { |
| 45 | return nil, err |
| 46 | } |
| 47 | |
| 48 | // Parse the numstat output |
| 49 | stats := make(map[string]vcs.FileStats) |
| 50 | lines := strings.Split(string(stdout), "\n") |
| 51 | for _, line := range lines { |
| 52 | line = strings.TrimSpace(line) |
| 53 | if line == "" { |
| 54 | continue |
| 55 | } |
| 56 | |
| 57 | // Format: additions deletions filename |
| 58 | parts := strings.Fields(line) |
| 59 | if len(parts) < 3 { |
| 60 | continue |
| 61 | } |
| 62 | |
| 63 | additions := 0 |
| 64 | deletions := 0 |
| 65 | |
| 66 | // Parse additions (may be "-" for binary files) |
| 67 | if parts[0] != "-" { |
| 68 | additions, _ = strconv.Atoi(parts[0]) |
| 69 | } |
| 70 | |
| 71 | // Parse deletions (may be "-" for binary files) |
| 72 | if parts[1] != "-" { |
| 73 | deletions, _ = strconv.Atoi(parts[1]) |
| 74 | } |
| 75 | |
| 76 | // Handle renamed files |