|
| 1 | +package git |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "os/exec" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/dfanso/commit-msg/src/internal/utils" |
| 11 | + "github.com/dfanso/commit-msg/src/types" |
| 12 | +) |
| 13 | + |
| 14 | +// IsRepository checks if a directory is a git repository |
| 15 | +func IsRepository(path string) bool { |
| 16 | + cmd := exec.Command("git", "-C", path, "rev-parse", "--is-inside-work-tree") |
| 17 | + output, err := cmd.CombinedOutput() |
| 18 | + if err != nil { |
| 19 | + return false |
| 20 | + } |
| 21 | + return strings.TrimSpace(string(output)) == "true" |
| 22 | +} |
| 23 | + |
| 24 | +// GetChanges retrieves all Git changes including staged, unstaged, and untracked files |
| 25 | +func GetChanges(config *types.RepoConfig) (string, error) { |
| 26 | + var changes strings.Builder |
| 27 | + |
| 28 | + // 1. Check for unstaged changes |
| 29 | + cmd := exec.Command("git", "-C", config.Path, "diff", "--name-status") |
| 30 | + output, err := cmd.Output() |
| 31 | + if err != nil { |
| 32 | + return "", fmt.Errorf("git diff failed: %v", err) |
| 33 | + } |
| 34 | + |
| 35 | + if len(output) > 0 { |
| 36 | + changes.WriteString("Unstaged changes:\n") |
| 37 | + changes.WriteString(string(output)) |
| 38 | + changes.WriteString("\n\n") |
| 39 | + |
| 40 | + // Get the content of these changes |
| 41 | + diffCmd := exec.Command("git", "-C", config.Path, "diff") |
| 42 | + diffOutput, err := diffCmd.Output() |
| 43 | + if err != nil { |
| 44 | + return "", fmt.Errorf("git diff content failed: %v", err) |
| 45 | + } |
| 46 | + |
| 47 | + changes.WriteString("Unstaged diff content:\n") |
| 48 | + changes.WriteString(string(diffOutput)) |
| 49 | + changes.WriteString("\n\n") |
| 50 | + } |
| 51 | + |
| 52 | + // 2. Check for staged changes |
| 53 | + stagedCmd := exec.Command("git", "-C", config.Path, "diff", "--name-status", "--cached") |
| 54 | + stagedOutput, err := stagedCmd.Output() |
| 55 | + if err != nil { |
| 56 | + return "", fmt.Errorf("git diff --cached failed: %v", err) |
| 57 | + } |
| 58 | + |
| 59 | + if len(stagedOutput) > 0 { |
| 60 | + changes.WriteString("Staged changes:\n") |
| 61 | + changes.WriteString(string(stagedOutput)) |
| 62 | + changes.WriteString("\n\n") |
| 63 | + |
| 64 | + // Get the content of these changes |
| 65 | + stagedDiffCmd := exec.Command("git", "-C", config.Path, "diff", "--cached") |
| 66 | + stagedDiffOutput, err := stagedDiffCmd.Output() |
| 67 | + if err != nil { |
| 68 | + return "", fmt.Errorf("git diff --cached content failed: %v", err) |
| 69 | + } |
| 70 | + |
| 71 | + changes.WriteString("Staged diff content:\n") |
| 72 | + changes.WriteString(string(stagedDiffOutput)) |
| 73 | + changes.WriteString("\n\n") |
| 74 | + } |
| 75 | + |
| 76 | + // 3. Check for untracked files |
| 77 | + untrackedCmd := exec.Command("git", "-C", config.Path, "ls-files", "--others", "--exclude-standard") |
| 78 | + untrackedOutput, err := untrackedCmd.Output() |
| 79 | + if err != nil { |
| 80 | + return "", fmt.Errorf("git ls-files failed: %v", err) |
| 81 | + } |
| 82 | + |
| 83 | + if len(untrackedOutput) > 0 { |
| 84 | + changes.WriteString("Untracked files:\n") |
| 85 | + changes.WriteString(string(untrackedOutput)) |
| 86 | + changes.WriteString("\n\n") |
| 87 | + |
| 88 | + // Try to get content of untracked files (limited to text files and smaller size) |
| 89 | + untrackedFiles := strings.Split(strings.TrimSpace(string(untrackedOutput)), "\n") |
| 90 | + for _, file := range untrackedFiles { |
| 91 | + if file == "" { |
| 92 | + continue |
| 93 | + } |
| 94 | + |
| 95 | + fullPath := filepath.Join(config.Path, file) |
| 96 | + if utils.IsTextFile(fullPath) && utils.IsSmallFile(fullPath) { |
| 97 | + fileContent, err := os.ReadFile(fullPath) |
| 98 | + if err != nil { |
| 99 | + // Log but don't fail - untracked file may have been deleted or is inaccessible |
| 100 | + continue |
| 101 | + } |
| 102 | + changes.WriteString(fmt.Sprintf("Content of new file %s:\n", file)) |
| 103 | + changes.WriteString(string(fileContent)) |
| 104 | + changes.WriteString("\n\n") |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + // 4. Get recent commits for context |
| 110 | + recentCommitsCmd := exec.Command("git", "-C", config.Path, "log", "--oneline", "-n", "3") |
| 111 | + recentCommitsOutput, err := recentCommitsCmd.Output() |
| 112 | + if err == nil && len(recentCommitsOutput) > 0 { |
| 113 | + changes.WriteString("Recent commits for context:\n") |
| 114 | + changes.WriteString(string(recentCommitsOutput)) |
| 115 | + changes.WriteString("\n") |
| 116 | + } |
| 117 | + |
| 118 | + return changes.String(), nil |
| 119 | +} |
0 commit comments