From d8973922ac6d3bee82255e2b9703ad8c9fa68f71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Sun, 14 Apr 2019 15:08:21 +0200 Subject: [PATCH] Propagate global git arguments to Before/After chains This fixes `hub -C mydir merge ` and other commands that might be invoked with git global arguments by ensuring that those global arguments are also fowarded to any accompanying `git` commands within `Before()` and `After()` chains. --- commands/args.go | 19 +++++++++++++++++-- commands/args_test.go | 13 +++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/commands/args.go b/commands/args.go index 94720b11..8536e362 100644 --- a/commands/args.go +++ b/commands/args.go @@ -59,13 +59,28 @@ func (a *Args) Replace(executable, command string, params ...string) { } func (a *Args) Commands() []*cmd.Cmd { - result := a.beforeChain + result := []*cmd.Cmd{} + appendFromChain := func(c *cmd.Cmd) { + if c.Name == "git" { + ga := []string{c.Name} + ga = append(ga, a.GlobalFlags...) + ga = append(ga, c.Args...) + result = append(result, cmd.NewWithArray(ga)) + } else { + result = append(result, c) + } + } + for _, c := range a.beforeChain { + appendFromChain(c) + } if !a.noForward { result = append(result, a.ToCmd()) } + for _, c := range a.afterChain { + appendFromChain(c) + } - result = append(result, a.afterChain...) return result } diff --git a/commands/args_test.go b/commands/args_test.go index 3801012f..7164a413 100644 --- a/commands/args_test.go +++ b/commands/args_test.go @@ -119,3 +119,16 @@ func TestArgs_GlobalFlags_Replaced(t *testing.T) { assert.Equal(t, "open", cmd.Name) assert.Equal(t, []string{"-a", "http://example.com"}, cmd.Args) } + +func TestArgs_GlobalFlags_BeforeAfterChain(t *testing.T) { + args := NewArgs([]string{"-c", "key=value", "-C", "dir", "status"}) + args.Before("git", "remote", "add") + args.After("git", "clean") + args.After("echo", "done!") + cmds := args.Commands() + assert.Equal(t, 4, len(cmds)) + assert.Equal(t, "git -c key=value -C dir remote add", cmds[0].String()) + assert.Equal(t, "git -c key=value -C dir status", cmds[1].String()) + assert.Equal(t, "git -c key=value -C dir clean", cmds[2].String()) + assert.Equal(t, "echo done!", cmds[3].String()) +}