-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.go
More file actions
171 lines (155 loc) · 3.66 KB
/
git.go
File metadata and controls
171 lines (155 loc) · 3.66 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
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"unicode/utf8"
)
type fileEntry struct {
status string
path string
}
func (f fileEntry) Title() string { return f.path }
func (f fileEntry) Description() string { return "" }
func (f fileEntry) FilterValue() string { return f.path }
func (f fileEntry) StatusLabel() string {
return statusLabel(f.status)
}
func getCurrentBranch() string {
out, err := gitCmd("rev-parse", "--abbrev-ref", "HEAD")
if err != nil {
return "?"
}
return strings.TrimSpace(out)
}
func gitCmd(args ...string) (string, error) {
cmd := exec.Command("git", args...)
cmd.Dir = workDir
out, err := cmd.Output()
if err != nil {
return "", err
}
return string(out), nil
}
func getChangedFiles() ([]fileEntry, error) {
// Use -uall to expand untracked directories into individual files
out, err := gitCmd("status", "--porcelain", "-uall")
if err != nil {
return nil, err
}
var files []fileEntry
for _, line := range strings.Split(strings.TrimRight(out, "\n"), "\n") {
if len(line) < 4 {
continue
}
status := strings.TrimSpace(line[:2])
path := strings.TrimSpace(line[3:])
// Handle renames: "R old -> new"
if idx := strings.Index(path, " -> "); idx != -1 {
path = path[idx+4:]
}
files = append(files, fileEntry{status: status, path: path})
}
return files, nil
}
func getAllFiles() ([]fileEntry, error) {
out, err := gitCmd("ls-files")
if err != nil {
return nil, err
}
var files []fileEntry
for _, line := range strings.Split(strings.TrimRight(out, "\n"), "\n") {
if line == "" {
continue
}
files = append(files, fileEntry{status: " ", path: line})
}
return files, nil
}
func getDiff(path string) (string, error) {
// Try staged diff first, then unstaged
out, err := gitCmd("diff", "--cached", "--", path)
if err != nil {
return "", err
}
if strings.TrimSpace(out) != "" {
return out, nil
}
out, err = gitCmd("diff", "--", path)
if err != nil {
return "", err
}
return out, nil
}
// writeFileLine replaces a single line in a file, preserving permissions and line endings.
func writeFileLine(path string, lineNum int, newContent string) error {
full := filepath.Join(workDir, path)
info, err := os.Stat(full)
if err != nil {
return err
}
data, err := os.ReadFile(full)
if err != nil {
return err
}
text := string(data)
// Detect line ending style
eol := "\n"
if strings.Contains(text, "\r\n") {
eol = "\r\n"
}
// Split, replace, rejoin
lines := strings.Split(text, eol)
if lineNum < 0 || lineNum >= len(lines) {
return fmt.Errorf("line %d out of range (file has %d lines)", lineNum, len(lines))
}
lines[lineNum] = newContent
result := strings.Join(lines, eol)
return os.WriteFile(full, []byte(result), info.Mode())
}
func readFile(path string) (string, error) {
full := filepath.Join(workDir, path)
info, err := os.Stat(full)
if err != nil {
return "", err
}
if info.IsDir() {
return listDir(full)
}
out, err := os.ReadFile(full)
if err != nil {
return "", err
}
return string(out), nil
}
func listDir(dir string) (string, error) {
entries, err := os.ReadDir(dir)
if err != nil {
return "", err
}
var b strings.Builder
for _, e := range entries {
name := e.Name()
if e.IsDir() {
name += "/"
}
b.WriteString(name + "\n")
}
return b.String(), nil
}
func isBinary(data string) bool {
// Check first 8KB for null bytes
sample := data
if len(sample) > 8192 {
sample = sample[:8192]
}
if strings.Contains(sample, "\x00") {
return true
}
// Check full string for UTF-8 validity — slicing at 8KB can split
// multi-byte characters, causing false positives after edits shift
// the byte boundary.
return !utf8.ValidString(data)
}