Skip to content

Commit

Permalink
Do not expand aliases which are in-built git and hub commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Goel authored and mislav committed Nov 7, 2016
1 parent 4a7f8cb commit 3792b5e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
16 changes: 14 additions & 2 deletions commands/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ func (r *Runner) Execute() ExecError {
utils.Check(err)

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

cmd := r.Lookup(args.Command)
if cmd != nil && cmd.Runnable() {
Expand Down Expand Up @@ -163,7 +165,8 @@ func executeCommands(cmds []*cmd.Cmd, execFinal bool) error {
func expandAlias(args *Args) {
cmd := args.Command
expandedCmd, err := git.Alias(cmd)
if err == nil && expandedCmd != "" {

if err == nil && expandedCmd != "" && !git.IsBuiltInGitCommand(cmd) {
words, e := splitAliasCmd(expandedCmd)
if e == nil {
args.Command = words[0]
Expand All @@ -172,6 +175,15 @@ func expandAlias(args *Args) {
}
}

func isBuiltInHubCommand(command string) bool {
for hubCommand, _ := range CmdRunner.All() {
if hubCommand == command {
return true
}
}
return false
}

func splitAliasCmd(cmd string) ([]string, error) {
if cmd == "" {
return nil, fmt.Errorf("alias can't be empty")
Expand Down
8 changes: 8 additions & 0 deletions features/ci_status.feature
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,11 @@ Feature: hub ci-status
Given the remote commit state of "git.my.org/michiels/pencilbox" "the_sha" is "success"
When I successfully run `hub ci-status the_sha`
Then the output should contain exactly "success\n"

Scenario: If alias named ci-status exists, it should not be expanded.
Given there is a commit named "the_sha"
Given the remote commit state of "michiels/pencilbox" "the_sha" is "success"
When I successfully run `git config --global alias.ci-status "ci-status -v"`
When I run `hub ci-status the_sha`
Then the output should contain exactly "success\n"
And the exit status should be 0
7 changes: 7 additions & 0 deletions features/git_compatibility.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Feature: git-hub compatibility
Scenario: If alias named branch exists, it should not be expanded.
Given I am in "git://github.com/rtomayko/ronn.git" git repo
And the default branch for "origin" is "master"
When I successfully run `git config --global alias.branch "branch -a"`
When I run `hub branch`
Then the stdout should contain exactly "* master\n"
21 changes: 19 additions & 2 deletions git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ func LocalBranches() ([]string, error) {
if err == nil {
for i, line := range lines {
lines[i] = strings.TrimPrefix(line, "* ")
lines[i] = strings.TrimPrefix(lines[i], " ")
}
}
return lines, err
Expand All @@ -315,8 +316,7 @@ func gitOutput(input ...string) (outputs []string, err error) {

out, err := cmd.CombinedOutput()
for _, line := range strings.Split(out, "\n") {
line = strings.TrimSpace(line)
if line != "" {
if strings.TrimSpace(line) != "" {
outputs = append(outputs, string(line))
}
}
Expand All @@ -337,3 +337,20 @@ func gitCmd(args ...string) *cmd.Cmd {

return cmd
}

func IsBuiltInGitCommand(command string) bool {
helpCommandOutput, err := gitOutput("help", "-a")
if err != nil {
return false
}
for _, helpCommandOutputLine := range helpCommandOutput {
if strings.HasPrefix(helpCommandOutputLine, " ") {
for _, gitCommand := range strings.Split(helpCommandOutputLine, " ") {
if gitCommand == command {
return true
}
}
}
}
return false
}

0 comments on commit 3792b5e

Please sign in to comment.