Skip to content

Commit 0aa21f3

Browse files
committed
chore(git): Refactor git commands and functions.
- Replace `git.Diff()` with `git.New().DiffFiles()` - Remove `--unified=1` argument from `diffFiles()` function - Rename `excludeFiles()` function to `Command.excludeFiles()` - Rename `diffNames()` and `hookPath()` functions to `Command.diffNames()` and `Command.hookPath()` - Add `Command` struct and `New()` function
1 parent 18ae561 commit 0aa21f3

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

cmd/commit.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ var commitCmd = &cobra.Command{
3838
return errors.New("To use CodeGPT, you must have git on your PATH")
3939
}
4040

41-
diff, err := git.Diff()
41+
g := git.New()
42+
diff, err := g.DiffFiles()
4243
if err != nil {
4344
return err
4445
}

git/git.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ var excludeFromDiff = []string{
1414
"go.sum",
1515
}
1616

17-
func excludeFiles() []string {
17+
type Command struct {
18+
ignoreLists []string
19+
}
20+
21+
func (c *Command) excludeFiles() []string {
1822
newFileLists := []string{}
1923
for _, f := range excludeFromDiff {
2024
newFileLists = append(newFileLists, ":(exclude)"+f)
@@ -23,40 +27,39 @@ func excludeFiles() []string {
2327
return newFileLists
2428
}
2529

26-
func diffNames() *exec.Cmd {
30+
func (c *Command) diffNames() *exec.Cmd {
2731
args := []string{
2832
"diff",
2933
"--staged",
3034
"--name-only",
3135
}
3236

33-
args = append(args, excludeFiles()...)
37+
args = append(args, c.excludeFiles()...)
3438

3539
return exec.Command(
3640
"git",
3741
args...,
3842
)
3943
}
4044

41-
func diffFiles() *exec.Cmd {
45+
func (c *Command) diffFiles() *exec.Cmd {
4246
args := []string{
4347
"diff",
4448
"--staged",
4549
"--ignore-all-space",
4650
"--diff-algorithm=minimal",
4751
"--function-context",
48-
"--unified=1",
4952
}
5053

51-
args = append(args, excludeFiles()...)
54+
args = append(args, c.excludeFiles()...)
5255

5356
return exec.Command(
5457
"git",
5558
args...,
5659
)
5760
}
5861

59-
func hookPath() *exec.Cmd {
62+
func (c *Command) hookPath() *exec.Cmd {
6063
args := []string{
6164
"rev-parse",
6265
"--git-path",
@@ -72,16 +75,16 @@ func hookPath() *exec.Cmd {
7275
// Diff compares the differences between two sets of data.
7376
// It returns a string representing the differences and an error.
7477
// If there are no differences, it returns an empty string and an error.
75-
func Diff() (string, error) {
76-
output, err := diffNames().Output()
78+
func (c *Command) DiffFiles() (string, error) {
79+
output, err := c.diffNames().Output()
7780
if err != nil {
7881
log.Fatal(err)
7982
}
8083
if string(output) == "" {
8184
return "", errors.New("please add your staged changes using git add <files...>")
8285
}
8386

84-
output, err = diffFiles().Output()
87+
output, err = c.diffFiles().Output()
8588
if err != nil {
8689
log.Fatal(err)
8790
}
@@ -90,7 +93,11 @@ func Diff() (string, error) {
9093
}
9194

9295
// Hook to show git hook path
93-
func Hook() (string, error) {
94-
output, err := hookPath().Output()
96+
func (c *Command) HookPath() (string, error) {
97+
output, err := c.hookPath().Output()
9598
return string(output), err
9699
}
100+
101+
func New() *Command {
102+
return &Command{}
103+
}

0 commit comments

Comments
 (0)