Skip to content

Commit 9fb9e70

Browse files
Phoenix2066bakayu
authored andcommitted
Improve git command error context
1 parent 65e1aac commit 9fb9e70

14 files changed

Lines changed: 93 additions & 30 deletions

File tree

internal/git/clone.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ func (g *GitCommands) CloneRepository(repoURL, directory string) (string, error)
1717

1818
output, _, err := g.executeCommand(args...)
1919
if err != nil {
20-
return string(output), err
20+
return string(output), fmt.Errorf(
21+
"failed to clone repository %s: %w",
22+
repoURL,
23+
err,
24+
)
2125
}
2226

2327
return fmt.Sprintf("Successfully cloned repository: %s", repoURL), nil

internal/git/commit.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ func (g *GitCommands) ShowCommit(commitHash string) (string, error) {
4343

4444
output, _, err := g.executeCommand(args...)
4545
if err != nil {
46-
return string(output), err
46+
return string(output), fmt.Errorf(
47+
"failed to show commit %s: %w",
48+
commitHash,
49+
err,
50+
)
4751
}
4852

4953
return string(output), nil

internal/git/diff.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package git
22

3+
import (
4+
"fmt"
5+
)
6+
37
// DiffOptions specifies the options for the git diff command.
48
type DiffOptions struct {
59
Commit1 string
@@ -36,7 +40,10 @@ func (g *GitCommands) ShowDiff(options DiffOptions) (string, error) {
3640

3741
output, _, err := g.executeCommand(args...)
3842
if err != nil {
39-
return string(output), err
43+
return string(output), fmt.Errorf(
44+
"failed to show diff: %w",
45+
err,
46+
)
4047
}
4148

4249
return string(output), nil

internal/git/files.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ func (g *GitCommands) BlameFile(filePath string) (string, error) {
2525
args := []string{"blame", filePath}
2626
output, _, err := g.executeCommand(args...)
2727
if err != nil {
28-
return string(output), err
28+
return string(output), fmt.Errorf(
29+
"failed to blame file %s: %w",
30+
filePath,
31+
err,
32+
)
2933
}
3034

3135
return string(output), nil

internal/git/init.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ func (g *GitCommands) InitRepository(path string) (string, error) {
1414

1515
output, _, err := g.executeCommand(args...)
1616
if err != nil {
17-
return string(output), err
17+
return string(output), fmt.Errorf(
18+
"failed to initialize repository at %s: %w",
19+
path,
20+
err,
21+
)
1822
}
1923

2024
absPath, _ := filepath.Abs(path)

internal/git/log.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ func (g *GitCommands) ShowLog(options LogOptions) (string, error) {
7474

7575
output, _, err := g.executeCommand(args...)
7676
if err != nil {
77-
return string(output), err
77+
return string(output), fmt.Errorf(
78+
"failed to show log: %w",
79+
err,
80+
)
7881
}
7982

8083
return string(output), nil

internal/git/merge.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ func (g *GitCommands) Rebase(options RebaseOptions) (string, error) {
6464

6565
output, _, err := g.executeCommand(args...)
6666
if err != nil {
67-
return string(output), err
67+
return string(output), fmt.Errorf(
68+
"failed to rebase repository: %w",
69+
err,
70+
)
6871
}
6972

7073
return string(output), nil

internal/git/remote.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ func (g *GitCommands) ManageRemote(options RemoteOptions) (string, error) {
3535

3636
output, _, err := g.executeCommand(args...)
3737
if err != nil {
38-
return string(output), err
38+
return string(output), fmt.Errorf(
39+
"failed to manage git remote: %w",
40+
err,
41+
)
3942
}
4043

4144
return string(output), nil
@@ -55,7 +58,10 @@ func (g *GitCommands) Fetch(remote string, branch string) (string, error) {
5558

5659
output, _, err := g.executeCommand(args...)
5760
if err != nil {
58-
return string(output), err
61+
return string(output), fmt.Errorf(
62+
"failed to fetch from remote: %w",
63+
err,
64+
)
5965
}
6066

6167
return string(output), nil
@@ -86,7 +92,10 @@ func (g *GitCommands) Pull(options PullOptions) (string, error) {
8692

8793
output, _, err := g.executeCommand(args...)
8894
if err != nil {
89-
return string(output), err
95+
return string(output), fmt.Errorf(
96+
"failed to pull repository: %w",
97+
err,
98+
)
9099
}
91100

92101
return string(output), nil
@@ -127,7 +136,10 @@ func (g *GitCommands) Push(options PushOptions) (string, error) {
127136

128137
output, _, err := g.executeCommand(args...)
129138
if err != nil {
130-
return string(output), err
139+
return string(output), fmt.Errorf(
140+
"failed to push to remote: %w",
141+
err,
142+
)
131143
}
132144

133145
return string(output), nil

internal/git/repo.go

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

33
import (
4+
"fmt"
45
"path/filepath"
56
"strings"
67
)
@@ -10,15 +11,21 @@ func (g *GitCommands) GetRepoInfo() (repoName string, branchName string, err err
1011
// Get the root dir of the repo.
1112
repoPath, _, err := g.executeCommand("rev-parse", "--show-toplevel")
1213
if err != nil {
13-
return "", "", err
14+
return "", "", fmt.Errorf(
15+
"failed to get repository root path: %w",
16+
err,
17+
)
1418
}
1519
repoPath = strings.TrimSpace(repoPath)
1620
repoName = filepath.Base(repoPath)
1721

1822
// Get the current branch name.
1923
branchName, _, err = g.executeCommand("rev-parse", "--abbrev-ref", "HEAD")
2024
if err != nil {
21-
return "", "", err
25+
return "", "", fmt.Errorf(
26+
"failed to get current branch name: %w",
27+
err,
28+
)
2229
}
2330
branchName = strings.TrimSpace(branchName)
2431

@@ -28,7 +35,10 @@ func (g *GitCommands) GetRepoInfo() (repoName string, branchName string, err err
2835
func (g *GitCommands) GetGitRepoPath() (repoPath string, err error) {
2936
repoPath, _, err = g.executeCommand("rev-parse", "--git-dir")
3037
if err != nil {
31-
return "", err
38+
return "", fmt.Errorf(
39+
"failed to get git directory path: %w",
40+
err,
41+
)
3242
}
3343
repoPath = strings.TrimSpace(repoPath)
3444
return repoPath, nil
@@ -38,7 +48,10 @@ func (g *GitCommands) GetGitRepoPath() (repoPath string, err error) {
3848
func (g *GitCommands) GetUserName() (string, error) {
3949
userName, _, err := g.executeCommand("config", "user.name")
4050
if err != nil {
41-
return "", err
51+
return "", fmt.Errorf(
52+
"failed to get git user name: %w",
53+
err,
54+
)
4255
}
4356
return strings.TrimSpace(userName), nil
4457
}

internal/git/stage.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func (g *GitCommands) AddFiles(paths []string) (string, string, error) {
1414

1515
output, cmdStr, err := g.executeCommand(args...)
1616
if err != nil {
17-
return string(output), cmdStr, err
17+
return string(output), cmdStr, fmt.Errorf("git add failed for paths %v: %w", paths, err)
1818
}
1919

2020
return string(output), cmdStr, nil
@@ -30,7 +30,7 @@ func (g *GitCommands) ResetFiles(paths []string) (string, string, error) {
3030

3131
output, cmdStr, err := g.executeCommand(args...)
3232
if err != nil {
33-
return string(output), cmdStr, err
33+
return string(output), cmdStr, fmt.Errorf("git reset failed for paths %v: %w", paths, err)
3434
}
3535

3636
return string(output), cmdStr, nil
@@ -52,7 +52,7 @@ func (g *GitCommands) RemoveFiles(paths []string, cached bool) (string, error) {
5252

5353
output, _, err := g.executeCommand(args...)
5454
if err != nil {
55-
return string(output), err
55+
return string(output), fmt.Errorf("git rm failed for paths %v (cached=%v): %w", paths, cached, err)
5656
}
5757

5858
return string(output), nil
@@ -68,7 +68,7 @@ func (g *GitCommands) MoveFile(source, destination string) (string, error) {
6868

6969
output, _, err := g.executeCommand(args...)
7070
if err != nil {
71-
return string(output), err
71+
return string(output), fmt.Errorf("git mv failed from %q to %q: %w", source, destination, err)
7272
}
7373

7474
return string(output), nil
@@ -106,7 +106,7 @@ func (g *GitCommands) Restore(options RestoreOptions) (string, string, error) {
106106

107107
output, cmdStr, err := g.executeCommand(args...)
108108
if err != nil {
109-
return string(output), cmdStr, err
109+
return string(output), cmdStr, fmt.Errorf("git restore failed for paths %v: %w", options.Paths, err)
110110
}
111111

112112
return string(output), cmdStr, nil
@@ -122,7 +122,7 @@ func (g *GitCommands) Revert(commitHash string) (string, string, error) {
122122

123123
output, cmdStr, err := g.executeCommand(args...)
124124
if err != nil {
125-
return string(output), cmdStr, err
125+
return string(output), cmdStr, fmt.Errorf("git revert failed for commit %s: %w", commitHash, err)
126126
}
127127

128128
return string(output), cmdStr, nil
@@ -138,7 +138,7 @@ func (g *GitCommands) ResetToCommit(commitHash string) (string, string, error) {
138138

139139
output, cmdStr, err := g.executeCommand(args...)
140140
if err != nil {
141-
return string(output), cmdStr, err
141+
return string(output), cmdStr, fmt.Errorf("git reset --hard failed for commit %s: %w", commitHash, err)
142142
}
143143

144144
return string(output), cmdStr, nil

0 commit comments

Comments
 (0)