Skip to content

Commit e01ef93

Browse files
feat(git): add missing implementations of methods (commit, amend and get branch name)
1 parent 9c20d19 commit e01ef93

File tree

1 file changed

+49
-66
lines changed

1 file changed

+49
-66
lines changed

git/git.go

Lines changed: 49 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
package git
22

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+
)
413

514
type git struct{}
615

@@ -10,82 +19,56 @@ type Git interface {
1019
Amend(message string)
1120
}
1221

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+
1340
func NewGitWrapper() Git {
1441
return &git{}
1542
}
1643

1744
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+
}
1952
}
2053

2154
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))
2358
}
2459

2560
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))
2764
}
2865

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+
}
6372

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

Comments
 (0)