Skip to content

Commit

Permalink
Fix integration with git 2.18 shell completions
Browse files Browse the repository at this point in the history
Wrap `git --list-cmds=others` command used by git built-in shell
completions to add a list of hub custom commands.

Fixes #1792
  • Loading branch information
mislav committed Jul 7, 2018
1 parent 698721b commit d7ef572
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
3 changes: 2 additions & 1 deletion commands/args.go
Expand Up @@ -208,6 +208,7 @@ func NewArgs(args []string) *Args {
const (
noopFlag = "--noop"
versionFlag = "--version"
listCmds = "--list-cmds="
helpFlag = "--help"
configFlag = "-c"
chdirFlag = "-C"
Expand All @@ -226,7 +227,7 @@ func slurpGlobalFlags(args *[]string, globalFlags *[]string) {
if slurpNextValue {
commandIndex = i + 1
slurpNextValue = false
} else if arg == versionFlag || arg == helpFlag || !looksLikeFlag(arg) {
} else if arg == versionFlag || arg == helpFlag || strings.HasPrefix(arg, listCmds) || !looksLikeFlag(arg) {
break
} else {
commandIndex = i + 1
Expand Down
25 changes: 25 additions & 0 deletions commands/help.go
Expand Up @@ -45,8 +45,15 @@ hub(1), git-help(1)
`,
}

var cmdListCmds = &Command{
Key: "--list-cmds",
Run: runListCmds,
GitExtension: true,
}

func init() {
CmdRunner.Use(cmdHelp, "--help")
CmdRunner.Use(cmdListCmds)
}

func runHelp(helpCmd *Command, args *Args) {
Expand Down Expand Up @@ -89,6 +96,24 @@ func runHelp(helpCmd *Command, args *Args) {
}
}

func runListCmds(cmd *Command, args *Args) {
listOthers := false
parts := strings.SplitN(args.Command, "=", 2)
for _, kind := range strings.Split(parts[1], ",") {
if kind == "others" {
listOthers = true
break
}
}

if listOthers {
args.AfterFn(func() error {
ui.Println(strings.Join(customCommands(), "\n"))
return nil
})
}
}

func displayManPage(manPage string, args *Args) error {
manProgram, _ := utils.CommandPath("man")
if manProgram == "" {
Expand Down
9 changes: 7 additions & 2 deletions commands/runner.go
Expand Up @@ -84,12 +84,17 @@ func (r *Runner) Execute() ExecError {
forceFail = true
}

cmdName := args.Command
if strings.Contains(cmdName, "=") {
cmdName = strings.SplitN(cmdName, "=", 2)[0]
}

git.GlobalFlags = args.GlobalFlags // preserve git global flags
if !isBuiltInHubCommand(args.Command) {
if !isBuiltInHubCommand(cmdName) {
expandAlias(args)
}

cmd := r.Lookup(args.Command)
cmd := r.Lookup(cmdName)
if cmd != nil && cmd.Runnable() {
execErr := r.Call(cmd, args)
if execErr.ExitCode == 0 && forceFail {
Expand Down
4 changes: 4 additions & 0 deletions features/git_compatibility.feature
Expand Up @@ -5,3 +5,7 @@ Feature: git-hub compatibility
When I successfully run `git config --global alias.branch "branch -a"`
When I run `hub branch`
Then the stdout should contain exactly "* master\n"

Scenario: List commands
When I successfully run `hub --list-cmds=others`
Then the stdout should contain "pull-request"

0 comments on commit d7ef572

Please sign in to comment.