Skip to content

Commit ee170bc

Browse files
author
R0n0066
committed
refactor(utils): Use spawn process instead of thirth git libs
1 parent c4c859a commit ee170bc

File tree

1 file changed

+10
-45
lines changed

1 file changed

+10
-45
lines changed

cmd/utils.go

Lines changed: 10 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,21 @@ import (
2424
"errors"
2525
"fmt"
2626
"os"
27+
"os/exec"
2728
"strings"
28-
"time"
2929

3030
"github.com/fatih/color"
3131
. "github.com/logrusorgru/aurora"
3232
"github.com/manifoldco/promptui"
3333
"github.com/spf13/viper"
34-
git "gopkg.in/src-d/go-git.v4"
35-
"gopkg.in/src-d/go-git.v4/plumbing/object"
36-
gitconfig "walmart.com/cfm/src/github.com/tcnksm/go-gitconfig"
3734
)
3835

39-
var w *git.Worktree
40-
var hasStagingFiles bool
41-
4236
func checkErr(err error) {
4337
if err != nil {
4438
color.Set(color.FgMagenta)
4539
defer color.Unset()
4640
fmt.Print(err)
47-
os.Exit(1)
41+
os.Exit(0)
4842
}
4943
}
5044

@@ -109,49 +103,20 @@ func promptList() {
109103
}
110104

111105
func commit(message string) (err error) {
112-
username, err := gitconfig.Username()
113-
email, err := gitconfig.Email()
114-
fmt.Println(Gray("Changes added, Preparing commit..."))
115-
116-
_, err = w.Commit(message, &git.CommitOptions{
117-
Author: &object.Signature{
118-
Name: username,
119-
Email: email,
120-
When: time.Now(),
121-
},
122-
})
123-
checkErr(err)
106+
cmdGit := exec.Command("git", "commit", "-m", message)
124107
lastCommit := "Last commit: " + message
108+
_, err = cmdGit.Output()
109+
checkErr(err)
125110
fmt.Println(Gray(lastCommit))
126111
fmt.Println(Green("Done"))
127112

128113
return
129114
}
130115

131116
func init() {
132-
directory, err := os.Getwd()
133-
checkErr(err)
134-
135-
// Opens an already existent repository.
136-
r, err := git.PlainOpen(directory)
137-
checkErr(err)
138-
139-
go (func() {
140-
w, err = r.Worktree()
141-
checkErr(err)
142-
143-
s, err := w.Status()
144-
checkErr(err)
145-
146-
hasStagingFiles = false
147-
for _, status := range s {
148-
if status.Staging != 32 && status.Staging != 63 {
149-
hasStagingFiles = true
150-
}
151-
}
152-
153-
if !hasStagingFiles {
154-
checkErr(errors.New("No changes added to commit"))
155-
}
156-
})()
117+
cmdGit := exec.Command("git", "diff", "--cached", "--exit-code")
118+
_, err := cmdGit.Output()
119+
if err == nil {
120+
checkErr(errors.New("No changes added to commit"))
121+
}
157122
}

0 commit comments

Comments
 (0)