✨ feat(worktree): add 'wt init' shell integration and 'wt switch' switch worktree#45
✨ feat(worktree): add 'wt init' shell integration and 'wt switch' switch worktree#45
Conversation
…eractive worktree switch Signed-off-by: samzong <samzong.lu@gmail.com>
Summary of ChangesHello @samzong, 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 significantly enhances the 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. 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 introduces two new features for worktree management: wt init for shell integration and wt switch for interactively switching between worktrees. The implementation uses a directive file pattern for shell integration, which is a solid approach. The interactive switching is implemented using github.com/charmbracelet/huh, providing a good user experience. The code is generally well-structured. My feedback includes suggestions to improve error handling, user experience with more descriptive error messages, and code consistency.
| RunE: func(_ *cobra.Command, args []string) error { | ||
| return runWorktreeInit(args[0]) | ||
| }, | ||
| } | ||
|
|
||
| func runWorktreeInit(shellType string) error { | ||
| wrapper := shell.GenerateWrapper(shellType) | ||
| if wrapper == "" { | ||
| return fmt.Errorf("unsupported shell: %s (supported: bash, zsh, fish)", shellType) | ||
| } | ||
| fmt.Print(wrapper) | ||
| return nil | ||
| } |
There was a problem hiding this comment.
To maintain consistency with other commands that use cmd.OutOrStdout() for output, it's better to refactor this to pass the cmd object to runWorktreeInit and use it for printing. This also makes the code more testable as you can redirect output.
| RunE: func(_ *cobra.Command, args []string) error { | |
| return runWorktreeInit(args[0]) | |
| }, | |
| } | |
| func runWorktreeInit(shellType string) error { | |
| wrapper := shell.GenerateWrapper(shellType) | |
| if wrapper == "" { | |
| return fmt.Errorf("unsupported shell: %s (supported: bash, zsh, fish)", shellType) | |
| } | |
| fmt.Print(wrapper) | |
| return nil | |
| } | |
| RunE: func(cmd *cobra.Command, args []string) error { | |
| return runWorktreeInit(cmd, args[0]) | |
| }, | |
| } | |
| func runWorktreeInit(cmd *cobra.Command, shellType string) error { | |
| wrapper := shell.GenerateWrapper(shellType) | |
| if wrapper == "" { | |
| return fmt.Errorf("unsupported shell: %s (supported: bash, zsh, fish)", shellType) | |
| } | |
| fmt.Fprint(cmd.OutOrStdout(), wrapper) | |
| return nil | |
| } |
|
|
||
| func runWorktreeSwitch(wtClient *worktree.Client) error { | ||
| if !wtClient.IsBareWorktree() { | ||
| return errors.New("not in a bare worktree setup") |
There was a problem hiding this comment.
|
|
||
| filtered := filterBareWorktrees(worktrees) | ||
| if len(filtered) == 0 { | ||
| return errors.New("no worktrees found") |
| return errors.New("no worktrees found") | ||
| } | ||
|
|
||
| root, _ := wtClient.GetWorktreeRoot() |
There was a problem hiding this comment.
The error from GetWorktreeRoot() is being ignored. While the logic seems to handle a zero-value root path, it's better practice to explicitly handle the error. If GetWorktreeRoot() fails, it could indicate a problem with the repository setup that should be surfaced to the user.
root, err := wtClient.GetWorktreeRoot()
if err != nil {
return fmt.Errorf("failed to get worktree root: %w", err)
}| if err := huh.NewSelect[string]().Title("Select Worktree").Options(options...).Value(&selected).Run(); err != nil { | ||
| return err | ||
| } |
There was a problem hiding this comment.
When the user cancels the interactive selection (e.g., by pressing Ctrl+C), huh.Run() returns huh.ErrUserAborted. Currently, this is treated as a generic error and will be printed to the user. It would provide a better user experience to handle this case gracefully by exiting without an error message, as it's an intentional user action.
if err := huh.NewSelect[string]().Title("Select Worktree").Options(options...).Value(&selected).Run(); err != nil {
if errors.Is(err, huh.ErrUserAborted) {
return nil // User cancelled, not an error.
}
return err
}
No description provided.