fix: handle restack when branches are checked out in other worktrees#672
fix: handle restack when branches are checked out in other worktrees#672STRRL wants to merge 1 commit intoaviator-co:masterfrom
Conversation
…viator-co#671) When restacking, git rebase fails if the target branch is checked out in another worktree. Detect this case and run the rebase from the worktree directory where the branch is already checked out, omitting the branch argument so git doesn't attempt a checkout.
Current Aviator status
This pull request is currently open (not queued). How to mergeTo merge this PR, comment
See the real-time status of this PR on the
Aviator webapp.
Use the Aviator Chrome Extension
to see the status of your PR within GitHub.
|
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves an issue where Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request effectively addresses the issue of av stack restack failing when branches are checked out in different worktrees. The solution to detect the worktree and execute the rebase command from the correct directory is well-implemented. The addition of tests for this new functionality is also a great inclusion. I've identified a potential race condition in the new runInDir helper function and a few areas where error handling could be made more robust. My detailed feedback is in the comments below.
| saved := r.repoDir | ||
| r.repoDir = dir | ||
| defer func() { r.repoDir = saved }() | ||
| return r.Run(ctx, opts) |
There was a problem hiding this comment.
This implementation of runInDir is not safe for concurrent use. It mutates the repoDir field of the Repo struct, which can lead to race conditions if other goroutines are using the same Repo instance. A safer approach is to create a shallow copy of the Repo object and modify its repoDir for the scope of this call, avoiding mutation of the original object.
| saved := r.repoDir | |
| r.repoDir = dir | |
| defer func() { r.repoDir = saved }() | |
| return r.Run(ctx, opts) | |
| rCopy := *r | |
| rCopy.repoDir = dir | |
| return (&rCopy).Run(ctx, opts) |
| return "", err | ||
| } | ||
|
|
||
| repoDir, _ := filepath.EvalSymlinks(r.repoDir) |
There was a problem hiding this comment.
The error returned by filepath.EvalSymlinks is ignored. If resolving the symlink fails, repoDir will contain the original, unresolved path. This could lead to incorrect comparisons later on, especially on systems like macOS where paths like /var are symlinks to /private/var. The error should be handled to ensure correctness.
repoDir, err := filepath.EvalSymlinks(r.repoDir)
if err != nil {
return "", err
}| if ref, ok := strings.CutPrefix(line, "branch "); ok { | ||
| shortName := strings.TrimPrefix(ref, "refs/heads/") | ||
| if shortName == branch { | ||
| resolved, _ := filepath.EvalSymlinks(currentWorktree) |
There was a problem hiding this comment.
Similar to the earlier comment, the error from filepath.EvalSymlinks is ignored here. This can lead to incorrect behavior if the worktree path cannot be resolved. The error should be handled to ensure the comparison against the main repository directory is reliable.
resolved, err := filepath.EvalSymlinks(currentWorktree)
if err != nil {
return "", err
}| resolvedWt, _ := filepath.EvalSymlinks(wt) | ||
| resolvedExpected, _ := filepath.EvalSymlinks(wtPath) |
There was a problem hiding this comment.
Errors from filepath.EvalSymlinks are being ignored. In tests, it's important to check for errors to ensure the test setup is correct and the function is being tested under the right conditions. If EvalSymlinks were to fail, this test could produce misleading results. Please handle the errors, for example by using require.NoError(t, err).
| resolvedWt, _ := filepath.EvalSymlinks(wt) | |
| resolvedExpected, _ := filepath.EvalSymlinks(wtPath) | |
| resolvedWt, err := filepath.EvalSymlinks(wt) | |
| require.NoError(t, err) | |
| resolvedExpected, err := filepath.EvalSymlinks(wtPath) | |
| require.NoError(t, err) |
Fixes #671
Problem
av stack restackfails when stacked branches are checked out in different worktrees:During restack,
avtries togit checkouteach branch to rebase it. Git refuses because the branch is already checked out in another worktree.Fix
WorktreeForBranch()to detect if a branch is checked out in a worktree (viagit worktree list --porcelain)RebaseParse()to run the rebase from the worktree directory when the target branch is in a different worktree, instead of attempting a checkoutrunInDir()helper to temporarily run git commands from a different directoryTests
TestWorktreeForBranch— verifies detection of branches in worktrees vs main repoTestRebaseInWorktree— verifies rebase succeeds when branch is in another worktreeNote: This fix was developed with AI assistance. I will review the code personally before marking ready for review.