1
1
package git
2
2
3
- import "fmt"
3
+ import (
4
+ "context"
5
+ "errors"
6
+ "fmt"
7
+ "os"
8
+ "os/exec"
9
+ "strings"
10
+
11
+ color "github.com/logrusorgru/aurora"
12
+ )
4
13
5
14
type git struct {}
6
15
@@ -10,82 +19,56 @@ type Git interface {
10
19
Amend (message string )
11
20
}
12
21
22
+ func commit (cmdGit * exec.Cmd , message ... interface {}) {
23
+ fmt .Println ("" )
24
+ fmt .Println (message ... )
25
+ fmt .Println (color .Gray (16 - 1 , "-------------------------------" ))
26
+ fmt .Println ("" )
27
+ cmdGit .Stdout = os .Stdout
28
+ cmdGit .Stderr = os .Stderr
29
+ err := cmdGit .Run ()
30
+ fmt .Println ("" )
31
+ fmt .Println (color .Gray (16 - 1 , "-------------------------------" ))
32
+ if err == nil {
33
+ fmt .Println (color .Green ("Done \U0001F604 " ))
34
+ } else {
35
+ fmt .Println (color .Red ("Something went grong \U0001F92F " ))
36
+ os .Exit (2 )
37
+ }
38
+ }
39
+
13
40
func NewGitWrapper () Git {
14
41
return & git {}
15
42
}
16
43
17
44
func (gitInstance * git ) CheckWorkspaceChanges () {
18
- fmt .Println ("check wrokspace" )
45
+ cmdGit := exec .Command ("git" , "diff" , "--cached" , "--exit-code" )
46
+ _ , err := cmdGit .Output ()
47
+ if err == nil {
48
+ fmt .Println (color .Red (errors .New ("no tracked changes" )))
49
+ fmt .Println (color .Blue ("run git add <file> or . 'to track changes'" ))
50
+ os .Exit (1 )
51
+ }
19
52
}
20
53
21
54
func (gitInstance * git ) Commit (message string ) {
22
- fmt .Println (message )
55
+ ctx := context .Background ()
56
+ cmdGit := exec .CommandContext (ctx , "git" , "commit" , "-m" , message )
57
+ commit (cmdGit , "Committing: " , color .Blue (message ))
23
58
}
24
59
25
60
func (gitInstance * git ) Amend (message string ) {
26
- fmt .Println ("amend" )
61
+ ctx := context .Background ()
62
+ cmdGit := exec .CommandContext (ctx , "git" , "commit" , "--amend" , "-m" , message )
63
+ commit (cmdGit , "Amending: " , color .Blue (message ))
27
64
}
28
65
29
- // func checkErr(err error) {
30
- // if err != nil {
31
- // fmt.Println(color.Red(err))
32
- // os.Exit(1)
33
- // }
34
- // }
35
-
36
- // // CheckTree perform a git tree check
37
- // func CheckTree() {
38
- // cmdGit := exec.Command("git", "diff", "--cached", "--exit-code")
39
- // _, err := cmdGit.Output()
40
- // if err == nil {
41
- // checkErr(errors.New("No changes added to commit"))
42
- // }
43
- // }
44
-
45
- // func commit(cmdGit *exec.Cmd, message ...interface{}) {
46
- // fmt.Println("")
47
- // fmt.Println(message...)
48
- // // fmt.Println(color.Gray("-------------------------------"))
49
- // fmt.Println("")
50
- // cmdGit.Stdout = os.Stdout
51
- // cmdGit.Stderr = os.Stderr
52
- // err := cmdGit.Run()
53
- // fmt.Println("")
54
- // // fmt.Println(color.Gray("-------------------------------"))
55
- // if err == nil {
56
- // fmt.Println(color.Green("Done \U0001F604"))
57
- // } else {
58
- // fmt.Println(color.Red("Something went grong \U0001F92F"))
59
- // }
60
-
61
- // return
62
- // }
66
+ func (gitInstance * git ) BranchName () string {
67
+ cmdGit := exec .Command ("git" , "rev-parse" , "--abbrev-ref" , "HEAD" )
68
+ branchName , err := cmdGit .Output ()
69
+ if err != nil {
70
+ return ""
71
+ }
63
72
64
- // // Commit execute commit
65
- // func Commit(message string) (err error) {
66
- // ctx := context.Background()
67
- // cmdGit := exec.CommandContext(ctx, "git", "commit", "-m", message)
68
- // commit(cmdGit, "Committing: ", color.Blue(message))
69
-
70
- // return
71
- // }
72
-
73
- // // Amend execute commit amend
74
- // func Amend(message string) (err error) {
75
- // ctx := context.Background()
76
- // cmdGit := exec.CommandContext(ctx, "git", "commit", "--amend", "-m", message)
77
- // commit(cmdGit, "Amending: ", color.Blue(message))
78
-
79
- // return
80
- // }
81
-
82
- // // BranchName return current branch name
83
- // func BranchName() string {
84
- // cmdGit := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
85
- // branchName, err := cmdGit.Output()
86
- // if err != nil {
87
- // return ""
88
- // }
89
-
90
- // return strings.TrimSuffix(string(branchName), "\n")
91
- // }
73
+ return strings .TrimSuffix (string (branchName ), "\n " )
74
+ }
0 commit comments