✨ feat(cli): add shell completion for gmc and wt commands#46
Conversation
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 user experience of the Highlights
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 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.
| 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 | ||
| } |
There was a problem hiding this comment.
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
}| 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 | ||
| } |
There was a problem hiding this comment.
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.
No description provided.