Skip to content

Commit 0e10e02

Browse files
committed
improve git command error logging in workspace git service
1 parent 540c9de commit 0e10e02

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

internal/workspace/git_service.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,25 @@ func (g *gitService) CloneRepository(repoURL, clonePath, branch string, createNe
4747
if createNewBranch {
4848
// Clone the default branch first, then create new branch
4949
cmd = exec.Command("git", "clone", "--depth", "50", repoURL, clonePath)
50+
log.Infof("Executing Git command: %s", cmd.String())
5051
} else {
5152
// Try to clone specific branch directly
5253
cmd = exec.Command("git", "clone", "--depth", "50", "--branch", branch, repoURL, clonePath)
54+
log.Infof("Executing Git command: %s", cmd.String())
5355
}
5456

5557
output, err := cmd.CombinedOutput()
5658
if err != nil {
59+
log.Errorf("Git command failed: %s, output: %s, error: %v", cmd.String(), string(output), err)
5760
if !createNewBranch {
5861
// If direct branch clone failed, try cloning default branch first
5962
log.Warnf("Failed to clone specific branch %s directly, cloning default branch: %v", branch, err)
6063
cmd = exec.Command("git", "clone", "--depth", "50", repoURL, clonePath)
64+
log.Infof("Executing fallback Git command: %s", cmd.String())
6165
output, err = cmd.CombinedOutput()
66+
if err != nil {
67+
log.Errorf("Git command failed: %s, output: %s, error: %v", cmd.String(), string(output), err)
68+
}
6269
}
6370
if err != nil {
6471
return GitError("clone", clonePath, fmt.Errorf("%s: %w", string(output), err))
@@ -104,8 +111,10 @@ func (g *gitService) CloneRepository(repoURL, clonePath, branch string, createNe
104111
func (g *gitService) GetRemoteURL(repoPath string) (string, error) {
105112
cmd := exec.Command("git", "remote", "get-url", "origin")
106113
cmd.Dir = repoPath
114+
log.Infof("Executing Git command: %s", cmd.String())
107115
output, err := cmd.Output()
108116
if err != nil {
117+
log.Errorf("Git command failed: %s, output: %s, error: %v", cmd.String(), string(output), err)
109118
return "", GitError("get_remote_url", repoPath, err)
110119
}
111120
return strings.TrimSpace(string(output)), nil
@@ -115,8 +124,10 @@ func (g *gitService) GetRemoteURL(repoPath string) (string, error) {
115124
func (g *gitService) GetCurrentBranch(repoPath string) (string, error) {
116125
cmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
117126
cmd.Dir = repoPath
127+
log.Infof("Executing Git command: %s", cmd.String())
118128
output, err := cmd.Output()
119129
if err != nil {
130+
log.Errorf("Git command failed: %s, output: %s, error: %v", cmd.String(), string(output), err)
120131
return "", GitError("get_current_branch", repoPath, err)
121132
}
122133
return strings.TrimSpace(string(output)), nil
@@ -126,8 +137,10 @@ func (g *gitService) GetCurrentBranch(repoPath string) (string, error) {
126137
func (g *gitService) GetCurrentCommit(repoPath string) (string, error) {
127138
cmd := exec.Command("git", "rev-parse", "HEAD")
128139
cmd.Dir = repoPath
140+
log.Infof("Executing Git command: %s", cmd.String())
129141
output, err := cmd.Output()
130142
if err != nil {
143+
log.Errorf("Git command failed: %s, output: %s, error: %v", cmd.String(), string(output), err)
131144
return "", fmt.Errorf("failed to get current commit: %w", err)
132145
}
133146
return strings.TrimSpace(string(output)), nil
@@ -137,8 +150,10 @@ func (g *gitService) GetCurrentCommit(repoPath string) (string, error) {
137150
func (g *gitService) GetBranchCommit(repoPath, branch string) (string, error) {
138151
cmd := exec.Command("git", "rev-parse", fmt.Sprintf("origin/%s", branch))
139152
cmd.Dir = repoPath
153+
log.Infof("Executing Git command: %s", cmd.String())
140154
output, err := cmd.Output()
141155
if err != nil {
156+
log.Errorf("Git command failed: %s, output: %s, error: %v", cmd.String(), string(output), err)
142157
return "", fmt.Errorf("failed to get branch commit for %s: %w", branch, err)
143158
}
144159
return strings.TrimSpace(string(output)), nil
@@ -184,7 +199,9 @@ func (g *gitService) ValidateBranch(repoPath, expectedBranch string) bool {
184199
func (g *gitService) ConfigureSafeDirectory(repoPath string) error {
185200
cmd := exec.Command("git", "config", "--local", "--add", "safe.directory", repoPath)
186201
cmd.Dir = repoPath
202+
log.Infof("Executing Git command: %s", cmd.String())
187203
if output, err := cmd.CombinedOutput(); err != nil {
204+
log.Errorf("Git command failed: %s, output: %s, error: %v", cmd.String(), string(output), err)
188205
return GitError("config_safe_directory", repoPath, fmt.Errorf("%s: %w", string(output), err))
189206
}
190207
return nil
@@ -194,7 +211,9 @@ func (g *gitService) ConfigureSafeDirectory(repoPath string) error {
194211
func (g *gitService) ConfigurePullStrategy(repoPath string) error {
195212
cmd := exec.Command("git", "config", "--local", "pull.rebase", "true")
196213
cmd.Dir = repoPath
214+
log.Infof("Executing Git command: %s", cmd.String())
197215
if output, err := cmd.CombinedOutput(); err != nil {
216+
log.Errorf("Git command failed: %s, output: %s, error: %v", cmd.String(), string(output), err)
198217
return fmt.Errorf("failed to configure pull strategy: %w, output: %s", err, string(output))
199218
}
200219
return nil
@@ -204,7 +223,9 @@ func (g *gitService) ConfigurePullStrategy(repoPath string) error {
204223
func (g *gitService) CreateAndCheckoutBranch(repoPath, branchName string) error {
205224
cmd := exec.Command("git", "checkout", "-b", branchName)
206225
cmd.Dir = repoPath
226+
log.Infof("Executing Git command: %s", cmd.String())
207227
if output, err := cmd.CombinedOutput(); err != nil {
228+
log.Errorf("Git command failed: %s, output: %s, error: %v", cmd.String(), string(output), err)
208229
return fmt.Errorf("failed to create new branch %s: %w, output: %s", branchName, err, string(output))
209230
}
210231
return nil
@@ -214,7 +235,9 @@ func (g *gitService) CreateAndCheckoutBranch(repoPath, branchName string) error
214235
func (g *gitService) CheckoutBranch(repoPath, branchName string) error {
215236
cmd := exec.Command("git", "checkout", branchName)
216237
cmd.Dir = repoPath
238+
log.Infof("Executing Git command: %s", cmd.String())
217239
if output, err := cmd.CombinedOutput(); err != nil {
240+
log.Errorf("Git command failed: %s, output: %s, error: %v", cmd.String(), string(output), err)
218241
return fmt.Errorf("failed to checkout branch %s: %w, output: %s", branchName, err, string(output))
219242
}
220243
return nil
@@ -224,7 +247,9 @@ func (g *gitService) CheckoutBranch(repoPath, branchName string) error {
224247
func (g *gitService) CreateTrackingBranch(repoPath, branchName string) error {
225248
cmd := exec.Command("git", "checkout", "-b", branchName, fmt.Sprintf("origin/%s", branchName))
226249
cmd.Dir = repoPath
250+
log.Infof("Executing Git command: %s", cmd.String())
227251
if output, err := cmd.CombinedOutput(); err != nil {
252+
log.Errorf("Git command failed: %s, output: %s, error: %v", cmd.String(), string(output), err)
228253
return fmt.Errorf("failed to create tracking branch %s: %w, output: %s", branchName, err, string(output))
229254
}
230255
return nil
@@ -245,14 +270,18 @@ func (g *gitService) FetchAndCheckoutPR(repoPath string, prNumber int) error {
245270
// Step 1: First fetch the PR content to FETCH_HEAD without creating/updating local branch
246271
fetchCmd := exec.Command("git", "fetch", "origin", fmt.Sprintf("pull/%d/head", prNumber))
247272
fetchCmd.Dir = repoPath
273+
log.Infof("Executing Git command: %s", fetchCmd.String())
248274
if output, err := fetchCmd.CombinedOutput(); err != nil {
275+
log.Errorf("Git command failed: %s, output: %s, error: %v", fetchCmd.String(), string(output), err)
249276
return fmt.Errorf("failed to fetch PR #%d content: %w, output: %s", prNumber, err, string(output))
250277
}
251278

252279
// Step 2: Reset current branch to the fetched content
253280
resetCmd := exec.Command("git", "reset", "--hard", "FETCH_HEAD")
254281
resetCmd.Dir = repoPath
282+
log.Infof("Executing Git command: %s", resetCmd.String())
255283
if output, err := resetCmd.CombinedOutput(); err != nil {
284+
log.Errorf("Git command failed: %s, output: %s, error: %v", resetCmd.String(), string(output), err)
256285
return fmt.Errorf("failed to reset PR branch to latest content: %w, output: %s", err, string(output))
257286
}
258287

@@ -262,13 +291,17 @@ func (g *gitService) FetchAndCheckoutPR(repoPath string, prNumber int) error {
262291

263292
fetchCmd := exec.Command("git", "fetch", "origin", fmt.Sprintf("pull/%d/head:%s", prNumber, prBranchName), "--force")
264293
fetchCmd.Dir = repoPath
294+
log.Infof("Executing Git command: %s", fetchCmd.String())
265295
if output, err := fetchCmd.CombinedOutput(); err != nil {
296+
log.Errorf("Git command failed: %s, output: %s, error: %v", fetchCmd.String(), string(output), err)
266297
return fmt.Errorf("failed to fetch PR #%d: %w, output: %s", prNumber, err, string(output))
267298
}
268299

269300
checkoutCmd := exec.Command("git", "checkout", prBranchName)
270301
checkoutCmd.Dir = repoPath
302+
log.Infof("Executing Git command: %s", checkoutCmd.String())
271303
if output, err := checkoutCmd.CombinedOutput(); err != nil {
304+
log.Errorf("Git command failed: %s, output: %s, error: %v", checkoutCmd.String(), string(output), err)
272305
return fmt.Errorf("failed to checkout PR branch %s: %w, output: %s", prBranchName, err, string(output))
273306
}
274307

0 commit comments

Comments
 (0)