-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.go
More file actions
218 lines (189 loc) · 5.22 KB
/
git.go
File metadata and controls
218 lines (189 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
type GitStatus struct {
Path string
Branch string
Files []GitFile
IsRepo bool
HasError bool
Error string
HasRemote bool
NeedsPull bool
RemoteStatus string
}
type GitFile struct {
Path string
Status string
}
func checkGitStatus(repoPath string) GitStatus {
result := GitStatus{
Path: repoPath,
Files: []GitFile{},
IsRepo: false,
}
if !isGitRepository(repoPath) {
result.HasError = true
result.Error = "Not a git repository"
return result
}
result.IsRepo = true
cmd := exec.Command("git", "status", "--porcelain")
cmd.Dir = repoPath
output, err := cmd.Output()
if err != nil {
result.HasError = true
result.Error = err.Error()
return result
}
lines := strings.Split(strings.TrimSpace(string(output)), "\n")
for _, line := range lines {
if line == "" {
continue
}
if len(line) >= 3 {
status := strings.TrimSpace(line[:2])
path := strings.TrimSpace(line[2:])
// Remove quotes if git added them for paths with special characters
if strings.HasPrefix(path, "\"") && strings.HasSuffix(path, "\"") {
path = path[1 : len(path)-1]
}
result.Files = append(result.Files, GitFile{
Path: path,
Status: status,
})
}
}
// Get current branch
branchCmd := exec.Command("git", "branch", "--show-current")
branchCmd.Dir = repoPath
if branchOutput, branchErr := branchCmd.Output(); branchErr == nil {
result.Branch = strings.TrimSpace(string(branchOutput))
}
// Check remote status
checkRemoteStatus(&result)
return result
}
func isGitRepository(path string) bool {
gitPath := filepath.Join(path, ".git")
_, err := os.Stat(gitPath)
return err == nil
}
// isBinary reports whether content appears to be binary by checking
// for null bytes in the first 8KB (same heuristic git uses).
func isBinary(data []byte) bool {
n := len(data)
if n > 8192 {
n = 8192
}
for i := 0; i < n; i++ {
if data[i] == 0 {
return true
}
}
return false
}
func getFileDiff(repoPath, filePath string) (string, error) {
// First try working directory changes
cmd := exec.Command("git", "diff", "HEAD", "--", filePath)
cmd.Dir = repoPath
output, err := cmd.Output()
// If no working directory changes, try staged changes
if err != nil || len(output) == 0 {
cmd = exec.Command("git", "diff", "--cached", "--", filePath)
cmd.Dir = repoPath
output, err = cmd.Output()
// If no staged changes and file is untracked, show file content
if err != nil || len(output) == 0 {
cmd = exec.Command("git", "status", "--porcelain", "--", filePath)
cmd.Dir = repoPath
statusOutput, statusErr := cmd.Output()
if statusErr == nil && strings.HasPrefix(strings.TrimSpace(string(statusOutput)), "??") {
// File is untracked, show its content using os.ReadFile
// Sanitize path to prevent directory traversal
cleanPath := filepath.Join(repoPath, filepath.Clean(filePath))
if strings.HasPrefix(cleanPath, filepath.Clean(repoPath)+string(filepath.Separator)) {
content, contentErr := os.ReadFile(cleanPath)
if contentErr == nil {
if isBinary(content) {
return fmt.Sprintf("Binary file: %s", filePath), nil
}
return fmt.Sprintf("New file: %s\n\n%s", filePath, string(content)), nil
}
}
}
}
}
if err != nil {
return "", err
}
if isBinary(output) {
return fmt.Sprintf("Binary file: %s", filePath), nil
}
return string(output), nil
}
func checkRemoteStatus(status *GitStatus) {
// Check if there's a remote configured
cmd := exec.Command("git", "remote")
cmd.Dir = status.Path
output, err := cmd.Output()
if err != nil || strings.TrimSpace(string(output)) == "" {
status.HasRemote = false
return
}
status.HasRemote = true
// Get current branch
cmd = exec.Command("git", "branch", "--show-current")
cmd.Dir = status.Path
branchOutput, err := cmd.Output()
if err != nil {
status.RemoteStatus = "Unable to get current branch"
return
}
currentBranch := strings.TrimSpace(string(branchOutput))
if currentBranch == "" {
status.RemoteStatus = "No current branch"
return
}
// Check if branch has upstream
cmd = exec.Command("git", "rev-parse", "--abbrev-ref", currentBranch+"@{upstream}")
cmd.Dir = status.Path
upstreamOutput, err := cmd.Output()
if err != nil {
status.RemoteStatus = "No upstream branch"
return
}
upstream := strings.TrimSpace(string(upstreamOutput))
// Skip automatic fetch to avoid performance issues
// Remote status will be based on last fetch time
// Check if local is behind remote
cmd = exec.Command("git", "rev-list", "--count", currentBranch+".."+upstream)
cmd.Dir = status.Path
behindOutput, err := cmd.Output()
if err != nil {
status.RemoteStatus = "Unable to check remote status"
return
}
behindCount := strings.TrimSpace(string(behindOutput))
if behindCount != "0" {
status.NeedsPull = true
if behindCount == "1" {
status.RemoteStatus = "1 commit behind"
} else {
status.RemoteStatus = fmt.Sprintf("%s commits behind", behindCount)
}
} else {
status.NeedsPull = false
status.RemoteStatus = "Up to date"
}
}
func fetchRemoteUpdates(repoPath string) error {
cmd := exec.Command("git", "fetch", "--quiet")
cmd.Dir = repoPath
return cmd.Run()
}