|
| 1 | +package git |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + |
| 9 | + color "github.com/logrusorgru/aurora" |
| 10 | +) |
| 11 | + |
| 12 | +func checkErr(err error) { |
| 13 | + if err != nil { |
| 14 | + fmt.Println(color.Red(err)) |
| 15 | + os.Exit(1) |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +// CheckTree perform a git tree check |
| 20 | +func CheckTree() { |
| 21 | + cmdGit := exec.Command("git", "diff", "--cached", "--exit-code") |
| 22 | + _, err := cmdGit.Output() |
| 23 | + if err == nil { |
| 24 | + checkErr(errors.New("No changes added to commit")) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +func commit(cmdGit *exec.Cmd, message ...interface{}) { |
| 29 | + output, err := cmdGit.Output() |
| 30 | + checkErr(err) |
| 31 | + |
| 32 | + fmt.Println("") |
| 33 | + fmt.Println("Committing: ", color.Blue(message)) |
| 34 | + fmt.Println(color.Gray("-------------------------------")) |
| 35 | + fmt.Println("") |
| 36 | + fmt.Println(color.Gray(string(output))) |
| 37 | + fmt.Println(color.Gray("-------------------------------")) |
| 38 | + fmt.Println(color.Green("Done \U0001F604")) |
| 39 | + |
| 40 | + return |
| 41 | +} |
| 42 | + |
| 43 | +// Commit execute commit |
| 44 | +func Commit(message string) (err error) { |
| 45 | + cmdGit := exec.Command("git", "commit", "-m", message) |
| 46 | + commit(cmdGit, "Committing: ", color.Blue(message)) |
| 47 | + |
| 48 | + return |
| 49 | +} |
| 50 | + |
| 51 | +// Amend execute commit amend |
| 52 | +func Amend(message string) (err error) { |
| 53 | + cmdGit := exec.Command("git", "commit", "--amend", "-m", message) |
| 54 | + commit(cmdGit, "Amending: ", color.Blue(message)) |
| 55 | + |
| 56 | + return |
| 57 | +} |
0 commit comments