-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
95 lines (80 loc) · 1.95 KB
/
main.go
File metadata and controls
95 lines (80 loc) · 1.95 KB
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
89
90
91
92
93
94
95
package main
import (
_ "embed"
"fmt"
"log"
"os"
"os/exec"
tea "github.com/charmbracelet/bubbletea"
"me.kryptk.overcommit/components"
"me.kryptk.overcommit/utils"
)
//go:embed config.toml
var config string
func main() {
_, err := os.ReadDir(os.ExpandEnv("$PWD/.git"))
if err != nil {
fmt.Println("not a git repository")
return
}
if len(os.Args) > 1 {
switch os.Args[1] {
case "-i", "--init":
initRepo()
return
case "--alias":
exec.Command("git", "config", "--global", "alias.c", "!overcommit").Run()
fmt.Println("done. use: git c")
return
}
}
runTUI()
}
func initRepo() {
hookDir := ".githooks"
os.MkdirAll(hookDir, 0755)
hook := `#!/bin/sh
msg=$(head -1 "$1")
if ! echo "$msg" | grep -qE '^(feat|fix|docs|style|refactor|test|chore)(\(.+\))?: .+'; then
echo "bad commit message: $msg"
echo ""
echo "expected: type(scope): message"
echo "types: feat|fix|docs|style|refactor|test|chore"
echo ""
echo "use 'overcommit' for easy conventional commits"
exit 1
fi
`
os.WriteFile(hookDir+"/commit-msg", []byte(hook), 0755)
exec.Command("git", "config", "core.hooksPath", hookDir).Run()
fmt.Println("done. commit .githooks/ to enforce for team")
}
func runTUI() {
c, err := utils.LoadConfig(config)
if err != nil {
log.Fatal(err)
}
selector := components.NewTypeSelector(c.Keys)
scopeSelector := components.NewScopeSelector(utils.GetScopes())
committer := components.NewCommitView(c.Lint.MaxSubjectLength, c.LLM)
m := components.PageView{
Page: components.SELECTION,
Selector: &selector,
ScopeSelector: &scopeSelector,
Committer: &committer,
Template: c.Template,
}
finalModel, err := tea.NewProgram(m).Run()
if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}
result := finalModel.(components.PageView)
if result.FinalMessage == "" {
return
}
cmd := exec.Command("git", "commit", "-m", result.FinalMessage)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
}