-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit.go
88 lines (78 loc) · 2.38 KB
/
git.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
package library
import (
"bufio"
"bytes"
"fmt"
"os/exec"
"strings"
)
// Initiate Git Function
func InitiateGitFunction(flags Flag) {
// Commit and Push
// - Equivalent to : `git commit -am "{message}" && git push origin HEAD`
if *flags.Git && *flags.Message != "" {
fmt.Println("📟 Commit and Push")
cnp := fmt.Sprintf("git commit -am '%s'; git push origin HEAD", *flags.Message)
cmd := [...]string{"bash", "-c", cnp}
fmt.Println(ExecCommand(cmd[:]...))
}
// Reset all changes (both staged and unstaged) in your working directory
// Remove all untracked files and directories
// - Equivalent to : `git reset --hard && git clean -df`
if *flags.Git && *flags.Reset {
fmt.Println("🔥 Reset")
cmd := [...]string{"bash", "-c", "git reset --hard && git clean -df"}
fmt.Println(ExecCommand(cmd[:]...))
}
// Reset Cache
// - Equivalent to : `git rm -rf cached . && git add .`
if *flags.Git && *flags.ResetCache {
fmt.Println("🏓 Re-staged")
cmd := [...]string{"bash", "-c", "git rm -rf --cached . && git add ."}
fmt.Println(ExecCommand(cmd[:]...))
}
// Git Gone
if *flags.Git && *flags.Gone {
GitGone()
}
}
// Git Gone Implementation in Go
func GitGone() {
fmt.Println("🧽 Git Gone")
// Run the "git fetch --all --prune" command
fetchOut, err := exec.Command("git", "fetch", "--all", "--prune").Output()
fmt.Println(string(fetchOut))
if err != nil {
fmt.Println("❌ Error fetching branches:", err)
return
}
// Run the "git branch -vv" command
branchCmd := exec.Command("git", "branch", "-vv")
branches, err := branchCmd.CombinedOutput()
if err != nil {
fmt.Printf("❌ Error running 'git branch': %v\n", err)
return
}
// Parse the output of "git branch -vv"
scanner := bufio.NewScanner(bytes.NewReader(branches))
var branchesToDelete []string
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, ": gone") {
branchesToDelete = append(branchesToDelete, strings.Fields(line)[0])
}
}
// Delete branches with upstream tracking information "gone]"
if len(branchesToDelete) > 0 {
for _, branch := range branchesToDelete {
_, err := exec.Command("git", "branch", "-D", branch).Output()
if err != nil {
fmt.Println("❌ Error deleting branch:", branch)
} else {
fmt.Println("🧹 Successfully delete branch:", branch)
}
}
} else {
fmt.Println("🙏 No branches with upstream tracking information 'gone' found.")
}
}