/
cgit.go
390 lines (347 loc) · 9.64 KB
/
cgit.go
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package main
import(
"fmt"
"flag"
"strconv"
"os"
"bytes"
"strings"
"os/exec"
fcolor "github.com/fatih/color"
)
var GIT_BIN string = "/usr/bin/git"
var RM_BIN string = "/bin/rm"
var path, _ = os.Getwd()
var committedStr string = "Changes to be committed:"
var notStagedCommitStr string = "Changes not staged for commit:"
var untrackedStr string = "Untracked files:"
func main() {
if len(os.Args) == 1 {
usage()
return
}
if len(os.Args) > 1 {
c := os.Args[1]
if c == "-h" || c == "--help" || c == "help" {
usage()
return
}
}
flag.Parse()
cmd := flag.Arg(0)
execCommand(cmd)
}
func usage() {
help := fmt.Sprintf("Usage: %s [command] [OPTIONS]\n\n", os.Args[0])
help += fmt.Sprintf("command:\n")
help += fmt.Sprintf("\tstatus, st\tshow the working tree status\n")
help += fmt.Sprintf("\tcheckout, co\tcheckout a branch or paths to the working tree\n")
help += fmt.Sprintf("\treset, re\treset current HEAD to the specified state\n")
help += fmt.Sprintf("\tadd\t\tadd file contents to the index\n")
help += fmt.Sprintf("OPTIONS:\n")
help += fmt.Sprintf("\t0,1\t\t0, 1 files will be apply your commmand\n")
help += fmt.Sprintf("\t0-2\t\t0, 1, 2 files will be apply your commmand\n")
help += fmt.Sprintf("\t0-2,3\t\t0, 1, 2, 3 files will be apply your commmand\n")
help += fmt.Sprintf("\tfilename\tfile will be apply your commmand\n")
fmt.Println(help)
}
func execCommand(cmd string) {
var out []byte
switch cmd {
case "status", "st":
out, _, _, _ = execStatus()
case "checkout", "co":
out = execCheckout()
case "reset", "re":
out = execReset()
case "add":
out = execAdd()
case "rm":
out = execRemove()
default:
out = []byte("Unsupported command: " + cmd)
}
fmt.Println(string(out))
}
func execStatus() (status []byte, committed, notStagedCommit, untracked []string) {
out := doExecCommand("status")
var (
desc []string
descs [][]string
idx int
state string
newDesc bool = true
)
ret := strings.Split(string(out), "\n")
for i, line := range ret {
switch line {
case committedStr:
state = "committed"
case notStagedCommitStr:
state = "notStagedCommit"
case untrackedStr:
state = "untracked"
default:
}
if strings.Index(line, "\t") == 0 {
trimLine := strings.TrimSpace(strings.Trim(ret[i], "\t"))
switch state {
case "committed":
committed = append(committed, fmt.Sprintf("\t%d\t%s", idx, trimLine))
idx++
case "notStagedCommit":
notStagedCommit = append(notStagedCommit, fmt.Sprintf("\t%d\t%s", idx, trimLine))
idx++
case "untracked":
untracked = append(untracked, fmt.Sprintf("\t%d\t%s", idx, trimLine))
idx++
}
newDesc = true
continue
}
if newDesc {
if len(desc) > 0 {
descs = append(descs, desc)
}
desc = []string{}
}
desc = append(desc, line)
newDesc = false
}
if len(descs) < 1 && len(desc) > 0 {
descs = append(descs, desc)
}
var str string
for i, _ := range descs {
str += makeDesc(descs[i])
if strings.Contains(strings.Join(descs[i], ""), committedStr) {
str += makeDesc(committed, "yellow")
}
if strings.Contains(strings.Join(descs[i], ""), notStagedCommitStr) {
str += makeDesc(notStagedCommit, "green")
}
if strings.Contains(strings.Join(descs[i], ""), untrackedStr) {
str += makeDesc(untracked, "cyan")
}
}
status = []byte(str)
return
}
func execCheckout() (s []byte) {
args := flag.Args()
idxArr := parseIndexOpts(flag.Arg(1))
if len(idxArr) < 1 {
return doExecCommand(args...)
}
var fileArr []string
_, committed, notStagedCommit, _ := execStatus()
arr := mergeStrArr(committed, notStagedCommit)
for _, line := range arr {
idx, ok := getLineNum(line)
if !ok {
continue
}
if inIntArray(idx, idxArr) {
if file := getFilepath(line); len(file) > 0 {
fileArr = append(fileArr, path + "/" + file)
}
}
}
if len(fileArr) < 1 {
return doExecCommand(args...)
}
for _, file := range fileArr {
doExecCommand("checkout", file)
}
return
}
func execReset() (s []byte) {
if flag.Arg(1) != "HEAD" {
fmt.Println("HEAD miss")
}
args := flag.Args()
idxArr := parseIndexOpts(flag.Arg(2))
if len(idxArr) < 1 {
return doExecCommand(args...)
}
var fileArr []string
_, committed, _, _ := execStatus()
for _, line := range committed {
idx, ok := getLineNum(line)
if !ok {
continue
}
if inIntArray(idx, idxArr) {
if file := getFilepath(line); len(file) > 0 {
fileArr = append(fileArr, path + "/" + file)
}
}
}
fmt.Println(fileArr)
if len(fileArr) < 1 {
return doExecCommand(args...)
}
for _, file := range fileArr {
doExecCommand("reset", "HEAD", file)
}
return
}
func execAdd() (s []byte) {
args := flag.Args()
idxArr := parseIndexOpts(flag.Arg(1))
if len(idxArr) < 1 {
return doExecCommand(args...)
}
var fileArr []string
_, _, notStagedCommit, untracked := execStatus()
arr := mergeStrArr(notStagedCommit, untracked)
for _, line := range arr {
idx, ok := getLineNum(line)
if !ok {
continue
}
if inIntArray(idx, idxArr) {
if file := getFilepath(line); len(file) > 0 {
fileArr = append(fileArr, path + "/" + file)
}
}
}
fmt.Println(fileArr)
if len(fileArr) < 1 {
return doExecCommand(args...)
}
for _, file := range fileArr {
doExecCommand("add", file)
}
return
}
func execRemove() (s []byte) {
args := flag.Args()
idxArr := parseIndexOpts(flag.Arg(1))
if len(idxArr) < 1 {
return doExecCommand(args...)
}
var fileArr []string
_, _, notStagedCommit, untracked := execStatus()
arr := mergeStrArr(notStagedCommit, untracked)
for _, line := range arr {
idx, ok := getLineNum(line)
if !ok {
continue
}
if inIntArray(idx, idxArr) {
if file := getFilepath(line); len(file) > 0 {
fileArr = append(fileArr, path + "/" + file)
}
}
}
fmt.Println(fileArr)
if len(fileArr) < 1 {
return execSysCommand(RM_BIN, args...)
}
for _, file := range fileArr {
execSysCommand(RM_BIN, "-r", file)
}
return
}
func doExecCommand(args ...string) (o []byte) {
return execSysCommand(GIT_BIN, args...)
}
func execSysCommand(cmd string, args ...string) (o []byte) {
command := exec.Command(cmd, args...)
var (
out bytes.Buffer
stderr bytes.Buffer
)
command.Stdout = &out
command.Stderr = &stderr
err := command.Run()
if err != nil {
fmt.Println(string(stderr.Bytes()))
return
}
return out.Bytes()
}
func stringToInt(arg string) int {
i, _ := strconv.ParseInt(arg, 10, 32)
return int(i)
}
func isNumberic(arg string) bool {
_, err := strconv.Atoi(arg)
return err == nil
}
func inIntArray(find int, arr []int) (ok bool) {
for i, _ := range arr {
if find == arr[i] {
return true
}
}
return
}
func parseIndexOpts(opts string) (idxArr []int) {
optsArr := strings.Split(opts, ",")
if len(optsArr) < 1 {
return
}
for i, _ := range optsArr {
arr := strings.Split(optsArr[i], "-")
if len(arr) < 1 {
continue
}
if len(arr) == 1 && isNumberic(optsArr[i]) {
idxArr = append(idxArr, stringToInt(optsArr[i]))
continue
}
if len(arr) == 2 && isNumberic(arr[0]) && isNumberic(arr[1]) {
begin := stringToInt(arr[0])
end := stringToInt(arr[1])
for k := begin; k <= end; k++ {
idxArr = append(idxArr, k)
}
continue
}
}
return
}
func makeDesc(desc []string, colors ...string) (str string) {
var color string
if len(colors) > 0 {
color = colors[0]
}
for i, _ := range desc {
switch color {
case "green":
str += fcolor.GreenString(desc[i]) + "\n"
case "yellow":
str += fcolor.YellowString(desc[i]) + "\n"
case "cyan":
str += fcolor.CyanString(desc[i]) + "\n"
default:
str += desc[i] + "\n"
}
}
return
}
func getFilepath(line string) (file string) {
strArr := strings.Split(strings.TrimSpace(strings.Trim(line, "\t")), "\t")
fileArr := strings.Split(strArr[len(strArr)-1], ":")
file = strings.TrimSpace(fileArr[len(fileArr)-1])
return
}
func getLineNum(line string) (num int, ok bool) {
arr := strings.Split(strings.TrimLeft(line, "\t"), "\t")
if len(arr) < 1 {
return
}
return stringToInt(arr[0]), true
}
func mergeStrArr(a []string, b []string, args ...[]string) (arr []string) {
arr = append(arr, a...)
arr = append(arr, b...)
if len(args) > 0 {
for i, _ := range args {
arr = append(arr, args[i]...)
}
}
return arr
}