@@ -2,6 +2,8 @@ package cmdutil
22
33import (
44 "os"
5+ "sort"
6+ "strings"
57
68 "github.com/cli/cli/internal/ghrepo"
79 "github.com/spf13/cobra"
@@ -19,6 +21,34 @@ func executeParentHooks(cmd *cobra.Command, args []string) error {
1921
2022func EnableRepoOverride (cmd * cobra.Command , f * Factory ) {
2123 cmd .PersistentFlags ().StringP ("repo" , "R" , "" , "Select another repository using the `[HOST/]OWNER/REPO` format" )
24+ _ = cmd .RegisterFlagCompletionFunc ("repo" , func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
25+ remotes , err := f .Remotes ()
26+ if err != nil {
27+ return nil , cobra .ShellCompDirectiveError
28+ }
29+
30+ config , err := f .Config ()
31+ if err != nil {
32+ return nil , cobra .ShellCompDirectiveError
33+ }
34+ defaultHost , err := config .DefaultHost ()
35+ if err != nil {
36+ return nil , cobra .ShellCompDirectiveError
37+ }
38+
39+ var results []string
40+ for _ , remote := range remotes {
41+ repo := remote .RepoOwner () + "/" + remote .RepoName ()
42+ if ! strings .EqualFold (remote .RepoHost (), defaultHost ) {
43+ repo = remote .RepoHost () + "/" + repo
44+ }
45+ if strings .HasPrefix (repo , toComplete ) {
46+ results = append (results , repo )
47+ }
48+ }
49+ sort .Strings (results )
50+ return results , cobra .ShellCompDirectiveNoFileComp
51+ })
2252
2353 cmd .PersistentPreRunE = func (cmd * cobra.Command , args []string ) error {
2454 if err := executeParentHooks (cmd , args ); err != nil {
0 commit comments