Skip to content

Commit a46313c

Browse files
authored
refactor: adopt context-aware git operations and upgrade linting workflow (#254)
- Update golangci-lint GitHub Action from v8 to v9 and set its version to v2.6 - Refactor multiple commands to accept context for better cancellation and deadline handling - Update command invocations to pass context for git operations such as diff, commit, hook install/uninstall, and Git directory resolution Signed-off-by: appleboy <appleboy.tw@gmail.com>
1 parent babf890 commit a46313c

File tree

5 files changed

+35
-29
lines changed

5 files changed

+35
-29
lines changed

.github/workflows/testing.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ jobs:
3333
go-version: ${{ matrix.go }}
3434

3535
- name: Setup golangci-lint
36-
uses: golangci/golangci-lint-action@v8
36+
uses: golangci/golangci-lint-action@v9
3737
with:
38-
version: v2.1
38+
version: v2.6
3939
args: --verbose
4040

4141
test:

cmd/commit.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ var commitCmd = &cobra.Command{
8282
git.WithExcludeList(viper.GetStringSlice("git.exclude_list")),
8383
git.WithEnableAmend(commitAmend),
8484
)
85-
diff, err := g.DiffFiles()
85+
diff, err := g.DiffFiles(cmd.Context())
8686
if err != nil {
8787
return err
8888
}
@@ -288,7 +288,7 @@ var commitCmd = &cobra.Command{
288288

289289
outputFile := viper.GetString("output.file")
290290
if outputFile == "" {
291-
out, err := g.GitDir()
291+
out, err := g.GitDir(cmd.Context())
292292
if err != nil {
293293
return err
294294
}
@@ -331,7 +331,7 @@ var commitCmd = &cobra.Command{
331331

332332
// Commit changes to repository
333333
color.Cyan("Recording changes to the repository")
334-
output, err := g.Commit(commitMessage)
334+
output, err := g.Commit(cmd.Context(), commitMessage)
335335
if err != nil {
336336
return err
337337
}

cmd/hook.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var hookInstallCmd = &cobra.Command{
2525
RunE: func(cmd *cobra.Command, args []string) error {
2626
g := git.New()
2727

28-
if err := g.InstallHook(); err != nil {
28+
if err := g.InstallHook(cmd.Context()); err != nil {
2929
return err
3030
}
3131
color.Green("Git hook 'prepare-commit-msg' installed successfully")
@@ -42,7 +42,7 @@ var hookUninstallCmd = &cobra.Command{
4242
RunE: func(cmd *cobra.Command, args []string) error {
4343
g := git.New()
4444

45-
if err := g.UninstallHook(); err != nil {
45+
if err := g.UninstallHook(cmd.Context()); err != nil {
4646
return err
4747
}
4848
color.Green("Git hook 'prepare-commit-msg' removed successfully")

cmd/review.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ var reviewCmd = &cobra.Command{
4444
git.WithExcludeList(viper.GetStringSlice("git.exclude_list")),
4545
git.WithEnableAmend(commitAmend),
4646
)
47-
diff, err := g.DiffFiles()
47+
diff, err := g.DiffFiles(cmd.Context())
4848
if err != nil {
4949
return err
5050
}

git/git.go

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package git
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
67
"os"
@@ -40,7 +41,7 @@ func (c *Command) excludeFiles() []string {
4041

4142
// diffNames generates the git command to list the names of changed files.
4243
// It includes options to handle amended commits and staged changes.
43-
func (c *Command) diffNames() *exec.Cmd {
44+
func (c *Command) diffNames(ctx context.Context) *exec.Cmd {
4445
args := []string{
4546
"diff",
4647
"--name-only",
@@ -55,7 +56,8 @@ func (c *Command) diffNames() *exec.Cmd {
5556
excludedFiles := c.excludeFiles()
5657
args = append(args, excludedFiles...)
5758

58-
return exec.Command(
59+
return exec.CommandContext(
60+
ctx,
5961
"git",
6062
args...,
6163
)
@@ -64,7 +66,7 @@ func (c *Command) diffNames() *exec.Cmd {
6466
// diffFiles generates the git command to show the differences between files.
6567
// It includes options to ignore whitespace changes, use minimal diff algorithm,
6668
// and set the number of context lines.
67-
func (c *Command) diffFiles() *exec.Cmd {
69+
func (c *Command) diffFiles(ctx context.Context) *exec.Cmd {
6870
args := []string{
6971
"diff",
7072
"--ignore-all-space",
@@ -81,44 +83,47 @@ func (c *Command) diffFiles() *exec.Cmd {
8183
excludedFiles := c.excludeFiles()
8284
args = append(args, excludedFiles...)
8385

84-
return exec.Command(
86+
return exec.CommandContext(
87+
ctx,
8588
"git",
8689
args...,
8790
)
8891
}
8992

9093
// hookPath generates the git command to get the path of the hooks directory.
9194
// This is used to locate where git hooks are stored.
92-
func (c *Command) hookPath() *exec.Cmd {
95+
func (c *Command) hookPath(ctx context.Context) *exec.Cmd {
9396
args := []string{
9497
"rev-parse",
9598
"--git-path",
9699
"hooks",
97100
}
98101

99-
return exec.Command(
102+
return exec.CommandContext(
103+
ctx,
100104
"git",
101105
args...,
102106
)
103107
}
104108

105109
// gitDir generates the git command to get the path of the git directory.
106110
// This is used to determine the location of the .git directory.
107-
func (c *Command) gitDir() *exec.Cmd {
111+
func (c *Command) gitDir(ctx context.Context) *exec.Cmd {
108112
args := []string{
109113
"rev-parse",
110114
"--git-dir",
111115
}
112116

113-
return exec.Command(
117+
return exec.CommandContext(
118+
ctx,
114119
"git",
115120
args...,
116121
)
117122
}
118123

119124
// commit generates the git command to create a commit with the provided message.
120125
// It includes options to skip pre-commit hooks, sign off the commit, and handle amendments.
121-
func (c *Command) commit(val string) *exec.Cmd {
126+
func (c *Command) commit(ctx context.Context, val string) *exec.Cmd {
122127
args := []string{
123128
"commit",
124129
"--no-verify",
@@ -130,16 +135,17 @@ func (c *Command) commit(val string) *exec.Cmd {
130135
args = append(args, "--amend")
131136
}
132137

133-
return exec.Command(
138+
return exec.CommandContext(
139+
ctx,
134140
"git",
135141
args...,
136142
)
137143
}
138144

139145
// Commit creates a git commit with the provided message and returns the output or an error.
140146
// It uses the commit method to generate the git command and execute it.
141-
func (c *Command) Commit(val string) (string, error) {
142-
output, err := c.commit(val).Output()
147+
func (c *Command) Commit(ctx context.Context, val string) (string, error) {
148+
output, err := c.commit(ctx, val).Output()
143149
if err != nil {
144150
return "", err
145151
}
@@ -148,8 +154,8 @@ func (c *Command) Commit(val string) (string, error) {
148154
}
149155

150156
// GitDir to show the (by default, absolute) path of the git directory of the working tree.
151-
func (c *Command) GitDir() (string, error) {
152-
output, err := c.gitDir().Output()
157+
func (c *Command) GitDir(ctx context.Context) (string, error) {
158+
output, err := c.gitDir(ctx).Output()
153159
if err != nil {
154160
return "", err
155161
}
@@ -163,16 +169,16 @@ func (c *Command) GitDir() (string, error) {
163169
// DiffFiles compares the differences between two sets of data and returns the differences as a string and an error.
164170
// It first lists the names of changed files and then shows the differences between them.
165171
// If there are no staged changes, it returns an error message.
166-
func (c *Command) DiffFiles() (string, error) {
167-
output, err := c.diffNames().Output()
172+
func (c *Command) DiffFiles(ctx context.Context) (string, error) {
173+
output, err := c.diffNames(ctx).Output()
168174
if err != nil {
169175
return "", err
170176
}
171177
if string(output) == "" {
172178
return "", errors.New("please add your staged changes using git add <files...>")
173179
}
174180

175-
output, err = c.diffFiles().Output()
181+
output, err = c.diffFiles(ctx).Output()
176182
if err != nil {
177183
return "", err
178184
}
@@ -182,8 +188,8 @@ func (c *Command) DiffFiles() (string, error) {
182188

183189
// InstallHook installs the prepare-commit-msg hook if it doesn't already exist.
184190
// It retrieves the hooks directory path, checks if the hook file exists, and writes the hook file with executable permissions.
185-
func (c *Command) InstallHook() error {
186-
hookPath, err := c.hookPath().Output()
191+
func (c *Command) InstallHook(ctx context.Context) error {
192+
hookPath, err := c.hookPath(ctx).Output()
187193
if err != nil {
188194
return err
189195
}
@@ -208,8 +214,8 @@ func (c *Command) InstallHook() error {
208214

209215
// UninstallHook removes the prepare-commit-msg hook if it exists.
210216
// It retrieves the hooks directory path, checks if the hook file exists, and removes the hook file.
211-
func (c *Command) UninstallHook() error {
212-
hookPath, err := c.hookPath().Output()
217+
func (c *Command) UninstallHook(ctx context.Context) error {
218+
hookPath, err := c.hookPath(ctx).Output()
213219
if err != nil {
214220
return err
215221
}

0 commit comments

Comments
 (0)