Skip to content

Commit 8ff42bf

Browse files
committed
Fix repo override
1 parent 8dd1e12 commit 8ff42bf

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

cmd/gh/main.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,6 @@ func mainRun() exitCode {
169169
return authError
170170
}
171171

172-
// enable `--repo` override
173-
if cmd.Flags().Lookup("repo") != nil {
174-
repoOverride, _ := cmd.Flags().GetString("repo")
175-
cmdFactory.BaseRepo = cmdutil.OverrideBaseRepoFunc(cmdFactory, repoOverride)
176-
}
177-
178172
return nil
179173
}
180174

pkg/cmdutil/repo_override.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,27 @@ import (
77
"github.com/spf13/cobra"
88
)
99

10+
func executeParentHooks(cmd *cobra.Command, args []string) error {
11+
for cmd.HasParent() {
12+
cmd = cmd.Parent()
13+
if cmd.PersistentPreRunE != nil {
14+
return cmd.PersistentPreRunE(cmd, args)
15+
}
16+
}
17+
return nil
18+
}
19+
1020
func EnableRepoOverride(cmd *cobra.Command, f *Factory) {
1121
cmd.PersistentFlags().StringP("repo", "R", "", "Select another repository using the `[HOST/]OWNER/REPO` format")
22+
23+
cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
24+
if err := executeParentHooks(cmd, args); err != nil {
25+
return err
26+
}
27+
repoOverride, _ := cmd.Flags().GetString("repo")
28+
f.BaseRepo = OverrideBaseRepoFunc(f, repoOverride)
29+
return nil
30+
}
1231
}
1332

1433
func OverrideBaseRepoFunc(f *Factory, override string) func() (ghrepo.Interface, error) {

0 commit comments

Comments
 (0)