Skip to content

✨ feat(cli): add shell completion for gmc and wt commands#46

Merged
samzong merged 1 commit intomainfrom
feat/shell-completion
Feb 3, 2026
Merged

✨ feat(cli): add shell completion for gmc and wt commands#46
samzong merged 1 commit intomainfrom
feat/shell-completion

Conversation

@samzong
Copy link
Owner

@samzong samzong commented Feb 3, 2026

No description provided.

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 user experience of the gmc CLI tool by integrating comprehensive shell completion capabilities for both the main command and its worktree management subcommands. It also centralizes and improves error reporting, ensuring that users receive clear and actionable feedback directly from the CLI, making the tool more intuitive and robust for daily use.

Highlights

  • Shell Completion: Introduced shell completion for the main gmc command and its wt subcommands across various shells (bash, zsh, fish, powershell). This includes dynamic completion for worktree names, branch names, and remote names.
  • Improved Error Handling: Refined the error handling mechanism to provide more user-friendly messages, especially for common scenarios like no changes in the staging area. Errors are now wrapped in a userFacingError type, allowing for clear output while preserving the underlying error context.
  • CLI Command Enhancements: Updated several wt subcommands (remove, promote, add, dup, prune, pr review, share add, sync, init) to leverage the new completion functions and improve argument validation.
Changelog
  • cmd/cmd_test.go
    • Updated TestRootCommand to reflect SilenceErrors being false on the root command.
    • Modified TestHandleErrors to align with the new handleErrors signature and userFacingError type, including tests for error message variations.
  • cmd/completion.go
    • Added a new completion command that generates shell completion scripts for bash, zsh, fish, and powershell.
  • cmd/root.go
    • Changed rootCmd.SilenceErrors to false to allow Cobra to print errors by default.
    • Refactored handleErrors to accept an addAllFlag and introduced a userFacingError type for consistent, user-friendly error output, which now includes conditional hints.
  • cmd/worktree.go
    • Implemented dynamic shell completion for wt subcommands (wt remove, wt promote) and their flags (wt add --base, wt dup --base, wt prune --base, wt pr review --remote).
    • Added new internal completion functions (completeWorktreeNames, completeBranchNames, completeRemoteNames, completeStrategies) to fetch dynamic suggestions.
  • cmd/worktree_init.go
    • Enhanced wt init command with ValidArgs and cobra.MatchAll for improved argument validation and completion.
  • cmd/worktree_share.go
    • Registered completeStrategies for the wt share add --strategy flag, suggesting 'copy' or 'link'.
  • cmd/worktree_sync.go
    • Registered completeBranchNames for the wt sync --base flag.
  • internal/worktree/sync.go
    • Minor adjustment to error message formatting in checkWorktreeReady for simpler string concatenation.
  • internal/worktree/worktree.go
    • Added ListBranches() method to worktree.Client to retrieve local Git branch names.
    • Added ListRemotes() method to worktree.Client to retrieve Git remote names. These methods are used to power dynamic shell completions.
  • main.go
    • Removed direct fmt.Fprintln(os.Stderr, err) call, centralizing error output through the cmd package's handleErrors mechanism.
Activity
  • No specific activity (comments, reviews, etc.) has been recorded for this pull request yet.
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 shell completion for gmc and wt commands, which is a great enhancement for the CLI's usability. The implementation correctly uses cobra's completion features. Additionally, the error handling has been refactored to use a custom error type, which integrates nicely with cobra to provide better user-facing error messages. The changes are well-structured. I have a couple of suggestions for minor improvements regarding code style and maintainability.

Comment on lines +38 to +49
switch args[0] {
case "bash":
return rootCmd.GenBashCompletion(os.Stdout)
case "zsh":
return rootCmd.GenZshCompletion(os.Stdout)
case "fish":
return rootCmd.GenFishCompletion(os.Stdout, true)
case "powershell":
return rootCmd.GenPowerShellCompletionWithDesc(os.Stdout)
}
return nil
}

Choose a reason for hiding this comment

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

medium

The return nil statement at the end of the function is unreachable because the switch statement covers all valid arguments enforced by cobra.OnlyValidArgs. This will likely be flagged by the Go compiler as unreachable code. It's better to add a default case to the switch to make it explicit that all cases are handled and to make the function more robust, even if that case is theoretically unreachable.

	switch args[0] {
	case "bash":
		return rootCmd.GenBashCompletion(os.Stdout)
	case "zsh":
		return rootCmd.GenZshCompletion(os.Stdout)
	case "fish":
		return rootCmd.GenFishCompletion(os.Stdout, true)
	case "powershell":
		return rootCmd.GenPowerShellCompletionWithDesc(os.Stdout)
	default:
		// This is unreachable due to cobra.OnlyValidArgs, but is good practice.
		return nil
	}

Comment on lines +649 to +698
func (c *Client) ListBranches() ([]string, error) {
root, _ := c.GetWorktreeRoot()
repoDir := repoDirForGit(root)

var args []string
if repoDir != "" {
args = []string{"-C", repoDir, "branch", "--format=%(refname:short)"}
} else {
args = []string{"branch", "--format=%(refname:short)"}
}

result, err := c.runner.Run(args...)
if err != nil {
return nil, fmt.Errorf("failed to list branches: %w", err)
}

output := result.StdoutString(true)
if output == "" {
return nil, nil
}

branches := strings.Split(output, "\n")
return branches, nil
}

// ListRemotes returns all remote names
func (c *Client) ListRemotes() ([]string, error) {
root, _ := c.GetWorktreeRoot()
repoDir := repoDirForGit(root)

var args []string
if repoDir != "" {
args = []string{"-C", repoDir, "remote"}
} else {
args = []string{"remote"}
}

result, err := c.runner.Run(args...)
if err != nil {
return nil, fmt.Errorf("failed to list remotes: %w", err)
}

output := result.StdoutString(true)
if output == "" {
return nil, nil
}

remotes := strings.Split(output, "\n")
return remotes, nil
}

Choose a reason for hiding this comment

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

medium

The logic in ListBranches and ListRemotes is very similar. To improve maintainability and reduce code duplication, consider extracting the common logic into a private helper function. This helper could take the git command arguments and an error message string as parameters, returning the list of items or an error.

@samzong samzong merged commit ec1111f into main Feb 3, 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