Skip to content

✨ feat(worktree): add 'wt init' shell integration and 'wt switch' switch worktree#45

Merged
samzong merged 1 commit intomainfrom
feat/wt-switch
Jan 23, 2026
Merged

✨ feat(worktree): add 'wt init' shell integration and 'wt switch' switch worktree#45
samzong merged 1 commit intomainfrom
feat/wt-switch

Conversation

@samzong
Copy link
Owner

@samzong samzong commented Jan 23, 2026

No description provided.

…eractive worktree switch

Signed-off-by: samzong <samzong.lu@gmail.com>
@gemini-code-assist
Copy link

Summary of Changes

Hello @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 gmc tool's worktree management capabilities by introducing shell integration and an interactive worktree switching mechanism. The new gmc wt init command sets up the necessary shell environment, while gmc wt switch provides a user-friendly way to navigate between Git worktrees, making the workflow more efficient.

Highlights

  • Shell Integration Command (gmc wt init): A new command gmc wt init <shell> is introduced to generate shell-specific integration scripts (for bash, zsh, and fish). This enables gmc to modify the parent shell's environment, specifically for changing directories.
  • Interactive Worktree Switch (gmc wt switch): A new command gmc wt switch allows users to interactively select and switch to a different Git worktree. This command leverages the new shell integration to change the current working directory.
  • Directive File Pattern: A new internal shell package implements a "directive file" pattern, allowing gmc to communicate commands (like cd) back to the parent shell via a temporary file that the shell wrapper then sources.
  • Interactive UI with Charmbracelet: The gmc wt switch command utilizes the charmbracelet/huh library to provide an interactive, terminal-based user interface for selecting worktrees.

🧠 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 Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +28 to +40
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
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This error message is correct, but could be more helpful to the user. Consider suggesting how they can create a bare worktree setup.

Suggested change
return errors.New("not in a bare worktree setup")
return errors.New("not in a bare worktree setup. Use 'gmc wt clone' to create one.")


filtered := filterBareWorktrees(worktrees)
if len(filtered) == 0 {
return errors.New("no worktrees found")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This error message is correct, but could be more helpful. Consider suggesting how to create a worktree.

Suggested change
return errors.New("no worktrees found")
return errors.New("no worktrees found. Use 'gmc wt add <name>' to create a new worktree.")

return errors.New("no worktrees found")
}

root, _ := wtClient.GetWorktreeRoot()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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)
	}

Comment on lines +56 to +58
if err := huh.NewSelect[string]().Title("Select Worktree").Options(options...).Value(&selected).Run(); err != nil {
return err
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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
	}

@samzong samzong merged commit b492deb into main Jan 23, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant