Repository navigation
Fix Git Checkout Failure When Base Branch Has No Local Ref #1426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -174,9 +174,38 @@ func (gm *GitManager) SetDryRun(dryRun bool, dryRunRepoPath string) *GitManager | |||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| func (gm *GitManager) Checkout(branchName string) error { | ||||||||||||||||||||||||||||||
| log.Debug("Running git checkout to branch:", branchName) | ||||||||||||||||||||||||||||||
| if err := gm.createBranchAndCheckout(branchName, false, false); err != nil { | ||||||||||||||||||||||||||||||
| return fmt.Errorf("'git checkout %s' failed with error: %s", branchName, err.Error()) | ||||||||||||||||||||||||||||||
| err := gm.createBranchAndCheckout(branchName, false, false) | ||||||||||||||||||||||||||||||
| if err == nil { | ||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| if errors.Is(err, plumbing.ErrReferenceNotFound) { | ||||||||||||||||||||||||||||||
| if trackErr := gm.checkoutFromRemoteTrackingBranch(branchName); trackErr == nil { | ||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| return fmt.Errorf("'git checkout %s' failed with error: %s", branchName, err.Error()) | ||||||||||||||||||||||||||||||
|
Comment on lines
+181
to
+186
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win 🔎 Supported by static analysis🏁 Script executed: git diff 08a63f760465175c9bc10bc533f919fb0d7aecd6 9595bbdb1ce46bfbb3052bd41425f0e3abf53614 -- utils/git.go utils/git_test.go
rg -n 'Checkout\(|checkoutFromRemoteTrackingBranch|errors.Is|%w' utils/git.go
sed -n '145,225p' utils/git.go
rg -n '\.Checkout\(' --glob '*.go'Repository: jfrog/frogbot Length of output: 9023 🤖 get_repo_knowledge executed:
Length of output: 10852 🏁 Script executed: #!/bin/bash
set -eu
printf '%s\n' '--- caller regions ---'
sed -n '225,270p' scanrepository/scanrepository.go
sed -n '595,645p' scanrepository/scanrepository.go
printf '%s\n' '--- relevant error handling and imports ---'
sed -n '1,45p' utils/git.go
sed -n '250,330p' utils/git.go
printf '%s\n' '--- repository guidance candidates ---'
rg -n -i 'errors\.Is|fmt\.Errorf|%w|wrap|wrapp|error handling|error chain|error propagation|errors\.Join' --glob '*.md' --glob '*.go' --glob '*.yaml' --glob '*.yml' --glob '!vendor/**' . | head -n 240Repository: jfrog/frogbot Length of output: 21489 🏁 Script executed: printf 'awaiting the previously requested inspection results\n'Repository: jfrog/frogbot Length of output: 204 Preserve the recovery error in the checkout failure. When the remote-tracking recovery fails, The existing 🐛 Suggested fix if errors.Is(err, plumbing.ErrReferenceNotFound) {
- if trackErr := gm.checkoutFromRemoteTrackingBranch(branchName); trackErr == nil {
+ trackErr := gm.checkoutFromRemoteTrackingBranch(branchName)
+ if trackErr == nil {
return nil
}
+ return fmt.Errorf("'git checkout %s' failed with error: %s; fallback to remote-tracking branch failed with error: %s", branchName, err.Error(), trackErr.Error())
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we're dropping the original( |
||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // checkoutFromRemoteTrackingBranch creates a local branch named branchName from the corresponding | ||||||||||||||||||||||||||||||
| // remote-tracking ref (e.g. refs/remotes/origin/<branchName>) and checks it out. | ||||||||||||||||||||||||||||||
| func (gm *GitManager) checkoutFromRemoteTrackingBranch(branchName string) error { | ||||||||||||||||||||||||||||||
| remoteRefName := plumbing.NewRemoteReferenceName(gm.remoteName, plumbing.ReferenceName(branchName).Short()) | ||||||||||||||||||||||||||||||
| remoteRef, err := gm.localGitRepository.Reference(remoteRefName, true) | ||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| localRefName := GetFullBranchName(branchName) | ||||||||||||||||||||||||||||||
| if err = gm.localGitRepository.Storer.SetReference(plumbing.NewHashReference(localRefName, remoteRef.Hash())); err != nil { | ||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| worktree, err := gm.localGitRepository.Worktree() | ||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| if err = worktree.Checkout(&git.CheckoutOptions{Branch: localRefName, Force: true}); err != nil { | ||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| log.Debug(fmt.Sprintf("Local branch '%s' was missing, recreated it from remote-tracking ref '%s'", branchName, remoteRefName)) | ||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
sed -n '44,86p' testdata/scanpullrequest/expected_response_multi_dir.md git diff --unified=12 08a63f760465175c9bc10bc533f919fb0d7aecd6 06d850c78108906653af42961e37934d918c8605 -- testdata/scanpullrequest/expected_response_multi_dir.mdRepository: jfrog/frogbot
Length of output: 21128
Use the coefficient for raw character counts.
The table’s
nis the raw count of trailing}characters, not thousands. The measurements givems/n² ≈ 1.7e-6 ms/character². They support quadratic growth, but the measured doublings are approximately—not exactly—4x.Suggested fix
📝 Committable suggestion
🤖 Prompt for AI Agents