Skip to content

Autocomplete branch flags#6031

Merged
mislav merged 5 commits intocli:trunkfrom
bchadwic:autocomplete-branch-flags
Mar 29, 2023
Merged

Autocomplete branch flags#6031
mislav merged 5 commits intocli:trunkfrom
bchadwic:autocomplete-branch-flags

Conversation

@bchadwic
Copy link
Contributor

@bchadwic bchadwic commented Aug 4, 2022

Looks like there might be others looking for this?
#2877, #2992

This happens to me often enough to make a pr:
$ gh pr create -B ..."what version is this merging into again?"...^C
*Proceeds to run a git branch just to type out the above again with a branch named "release/some-version"

Having an autocomplete on flags that accept a branch name helps me in two ways:

  1. I don't have to remember as much
  2. I don't have to type out as much

I didn't want to import the git package into edit.go. I'm wondering if the branch resolver should live elsewhere?

Ref: #360

@bchadwic bchadwic requested a review from a team as a code owner August 4, 2022 01:29
@bchadwic bchadwic requested review from samcoe and removed request for a team August 4, 2022 01:29
@cliAutomation cliAutomation added the external pull request originating outside of the CLI core team label Aug 4, 2022
Copy link
Contributor

@mislav mislav left a comment

Choose a reason for hiding this comment

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

This looks great so far!

  1. Note that some other commands also take in branch names via flags, for example gh pr create;
  2. Note that most commands take in a -R/--repo flag or the GH_REPO environment variable to override which repository is the "current" one. In those cases, we don't want to be reading branch information from the local git repository because it will likely not match the repository the user has explicitly selected.

@bchadwic bchadwic force-pushed the autocomplete-branch-flags branch from b6356b7 to 9474540 Compare August 5, 2022 05:33
@bchadwic
Copy link
Contributor Author

bchadwic commented Aug 5, 2022

we don't want to be reading branch information from the local git repository because it will likely not match the repository the user has explicitly selected

I would think a lot of use cases for -R/--repo would involve users trying to interact with an upstream or forked repository. In which case could we assume it might still be handy to see branch completions since forks usually have little branch discrepancy from upstream repositories?

I'm in my local git repository which has a base repo of bchadwic/cli. I'm about to open a pr:
$ gh pr create -R cli/cli -H bchadwic:autocomplete-branch-flags -B..."is default main or trunk?"

If you don't think this is a frequent scenario, do you possibly have a suggestion as to how to prevent autocompletion on scenario # 2?

_ = cmd.RegisterFlagCompletionFunc(name, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
      if cmd.Flag("repo").Changed {
	      return []string{"AUTOCOMPLETE NOT ALLOWED WITH REPO FLAG"}, cobra.ShellCompDirectiveDefault
      }
      return options, cobra.ShellCompDirectiveNoFileComp
})

I tried this but I don't think the repo flag is set until the user runs the command. Would there be another approach you would take?

Thank you 🦖

@bchadwic bchadwic requested a review from mislav August 5, 2022 06:34
Copy link
Contributor

@mislav mislav left a comment

Choose a reason for hiding this comment

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

I have taken another pass at this and added --repo flag handling. Thanks!!

@mislav mislav enabled auto-merge (squash) March 29, 2023 14:50
@mislav mislav merged commit 8266268 into cli:trunk Mar 29, 2023
@bchadwic bchadwic deleted the autocomplete-branch-flags branch March 29, 2023 17:05
jtpetty pushed a commit that referenced this pull request May 22, 2023
Co-authored-by: Mislav Marohnić <mislav@github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external pull request originating outside of the CLI core team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants