-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyohan.go
More file actions
280 lines (263 loc) Β· 7.74 KB
/
Copy pathyohan.go
File metadata and controls
280 lines (263 loc) Β· 7.74 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
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
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"strings"
)
// ANSI color codes
const (
Reset = "\033[0m"
Red = "\033[31m"
Green = "\033[32m"
Yellow = "\033[33m"
Cyan = "\033[36m"
Blue = "\033[34m"
Magenta = "\033[35m"
Bold = "\033[1m"
)
func main() {
if len(os.Args) > 1 {
runCommandLine(os.Args[1:], bufio.NewReader(os.Stdin))
} else {
runInteractiveMenu()
}
}
// Command-line mode
func runCommandLine(args []string, reader *bufio.Reader) {
switch args[0] {
case "commit":
autoCommitAndPush(reader)
case "status":
runGitCommand("status", "π")
case "log":
runGitCommand("log", "--oneline", "-5", "π")
case "branch":
runGitCommand("branch", "πΏ")
case "checkout":
if len(args) < 2 {
fmt.Println(Red + "β οΈ Usage: yohan checkout <branch>" + Reset)
return
}
runGitCommand("checkout", args[1], "π")
case "pull":
runGitCommand("pull", "β¬οΈ")
case "push":
runGitCommand("push", "β¬οΈ")
case "merge":
if len(args) < 2 {
fmt.Println(Red + "β οΈ Usage: yohan merge <branch>" + Reset)
return
}
runGitCommand("merge", args[1], "π")
case "stash":
runGitCommand("stash", "π¦")
case "stash-pop":
runGitCommand("stash", "π€")
case "tag":
if len(args) < 2 {
fmt.Println(Red + "β οΈ Usage: yohan tag <name>" + Reset)
return
}
runGitCommand("tag", args[1], "π·οΈ")
case "diff":
runGitCommand("diff", "π")
case "remote":
runGitCommand("remote", "-v", "π")
case "clone":
if len(args) < 2 {
fmt.Println(Red + "β οΈ Usage: yohan clone <url>" + Reset)
return
}
runGitCommand("clone", args[1], "π₯")
case "reset":
if len(args) < 2 || args[1] != "--hard" {
fmt.Println(Red + "β οΈ Usage: yohan reset --hard" + Reset)
return
}
fmt.Print(Yellow + "β οΈ Are you sure you want to reset --hard? (y/n): " + Reset)
confirm, _ := reader.ReadString('\n')
if strings.TrimSpace(confirm) == "y" {
runGitCommand("reset", "--hard", "π₯")
} else {
fmt.Println(Green + "β
Cancelled reset." + Reset)
}
default:
fmt.Println(Red + "β Unknown command:", args[0] + Reset)
}
}
// Interactive menu mode
func runInteractiveMenu() {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Println(Cyan + Bold + "\nβ¨ YOHAN: All-in-One Git CLI β¨" + Reset)
fmt.Println(Yellow + "Select an option:" + Reset)
fmt.Println("1οΈβ£ Auto commit & push changes")
fmt.Println("2οΈβ£ Git status")
fmt.Println("3οΈβ£ Git log (last 5 commits)")
fmt.Println("4οΈβ£ Git branch")
fmt.Println("5οΈβ£ Git checkout <branch>")
fmt.Println("6οΈβ£ Git pull")
fmt.Println("7οΈβ£ Git push")
fmt.Println("8οΈβ£ Git merge <branch>")
fmt.Println("9οΈβ£ Git stash")
fmt.Println("π Git stash pop")
fmt.Println("1οΈβ£1οΈβ£ Git tag <name>")
fmt.Println("1οΈβ£2οΈβ£ Git diff")
fmt.Println("1οΈβ£3οΈβ£ Git remote -v")
fmt.Println("1οΈβ£4οΈβ£ Git clone <url>")
fmt.Println("1οΈβ£5οΈβ£ Git reset --hard")
fmt.Println("0οΈβ£ Exit")
fmt.Print(Cyan + "\nEnter option: " + Reset)
option, _ := reader.ReadString('\n')
option = strings.TrimSpace(option)
switch option {
case "1":
autoCommitAndPush(reader)
case "2":
runGitCommand("status", "π")
case "3":
runGitCommand("log", "--oneline", "-5", "π")
case "4":
runGitCommand("branch", "πΏ")
case "5":
fmt.Print(Cyan + "Enter branch name: " + Reset)
branch, _ := reader.ReadString('\n')
runGitCommand("checkout", strings.TrimSpace(branch), "π")
case "6":
runGitCommand("pull", "β¬οΈ")
case "7":
runGitCommand("push", "β¬οΈ")
case "8":
fmt.Print(Cyan + "Enter branch to merge: " + Reset)
branch, _ := reader.ReadString('\n')
runGitCommand("merge", strings.TrimSpace(branch), "π")
case "9":
runGitCommand("stash", "π¦")
case "10":
runGitCommand("stash", "π€")
case "11":
fmt.Print(Cyan + "Enter tag name: " + Reset)
tag, _ := reader.ReadString('\n')
runGitCommand("tag", strings.TrimSpace(tag), "π·οΈ")
case "12":
runGitCommand("diff", "π")
case "13":
runGitCommand("remote", "-v", "π")
case "14":
fmt.Print(Cyan + "Enter repository URL: " + Reset)
url, _ := reader.ReadString('\n')
runGitCommand("clone", strings.TrimSpace(url), "π₯")
case "15":
fmt.Print(Yellow + "β οΈ Are you sure you want to reset --hard? (y/n): " + Reset)
confirm, _ := reader.ReadString('\n')
if strings.TrimSpace(confirm) == "y" {
runGitCommand("reset", "--hard", "π₯")
} else {
fmt.Println(Green + "β
Cancelled reset." + Reset)
}
case "0":
fmt.Println(Green + "π Bye!" + Reset)
return
default:
fmt.Println(Red + "β Invalid option." + Reset)
}
}
}
// Run git commands with icon
func runGitCommand(args ...string) {
icon := ""
if len(args) > 0 {
icon = args[len(args)-1] // Last argument can be an icon
args = args[:len(args)-1]
}
fmt.Println(Magenta + "β‘ Running git", strings.Join(args, " "), icon + "..." + Reset)
cmd := exec.Command("git", args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
fmt.Println(Red+"β Error:", err, Reset)
} else {
fmt.Println(Green + "β
Done!" + Reset)
}
}
// Auto commit & push changes
func autoCommitAndPush(reader *bufio.Reader) {
fmt.Println(Blue + "π₯ Staging all changes..." + Reset)
cmdAdd := exec.Command("git", "add", ".")
cmdAdd.Stdout = os.Stdout
cmdAdd.Stderr = os.Stderr
if err := cmdAdd.Run(); err != nil {
fmt.Println(Red+"β Error running git add:", err, Reset)
return
}
fmt.Println(Green + "β
Changes staged successfully!" + Reset)
cmdDiff := exec.Command("git", "diff", "--staged", "--name-status")
diffOut, err := cmdDiff.Output()
if err != nil {
fmt.Println(Red+"β Error getting git diff:", err, Reset)
return
}
diffLines := strings.Split(string(diffOut), "\n")
if len(diffLines) == 0 || (len(diffLines) == 1 && diffLines[0] == "") {
fmt.Println(Yellow + "β οΈ No changes staged for commit." + Reset)
return
}
added, modified, deleted := []string{}, []string{}, []string{}
for _, line := range diffLines {
parts := strings.Fields(line)
if len(parts) < 2 {
continue
}
status, file := parts[0], parts[1]
switch status {
case "A":
added = append(added, file)
case "M":
modified = append(modified, file)
case "D":
deleted = append(deleted, file)
}
}
msgParts := []string{}
if len(added) > 0 {
msgParts = append(msgParts, fmt.Sprintf("β Add %s", strings.Join(added, ", ")))
}
if len(modified) > 0 {
msgParts = append(msgParts, fmt.Sprintf("π Update %s", strings.Join(modified, ", ")))
}
if len(deleted) > 0 {
msgParts = append(msgParts, fmt.Sprintf("β Remove %s", strings.Join(deleted, ", ")))
}
commitMsg := strings.Join(msgParts, "; ")
if commitMsg == "" {
commitMsg = "π Update project"
}
fmt.Println(Cyan + Bold + "\nSuggested Commit Message:" + Reset)
fmt.Println(Yellow + commitMsg + Reset)
fmt.Println(Cyan + "\nEdit commit message or press Enter to keep:" + Reset)
userInput, _ := reader.ReadString('\n')
userInput = strings.TrimSpace(userInput)
if userInput != "" {
commitMsg = userInput
}
fmt.Println(Blue + "\nπΎ Committing changes..." + Reset)
cmdCommit := exec.Command("git", "commit", "-m", commitMsg)
cmdCommit.Stdout = os.Stdout
cmdCommit.Stderr = os.Stderr
if err := cmdCommit.Run(); err != nil {
fmt.Println(Red+"β Error running git commit:", err, Reset)
return
}
fmt.Println(Green + "β
Commit successful!" + Reset)
fmt.Println(Blue + "\nπ Pushing changes to remote..." + Reset)
cmdPush := exec.Command("git", "push")
cmdPush.Stdout = os.Stdout
cmdPush.Stderr = os.Stderr
if err := cmdPush.Run(); err != nil {
fmt.Println(Red+"β Error running git push:", err, Reset)
return
}
fmt.Println(Green + Bold + "\nπ Changes pushed successfully!" + Reset)
}