diff --git a/githooks/git/git.go b/githooks/git/git.go index abd34d54..732ea483 100644 --- a/githooks/git/git.go +++ b/githooks/git/git.go @@ -11,7 +11,8 @@ import ( ) const ( - HEAD string = "HEAD" + HEAD string = "HEAD" + GitConfigPrefix string = "githooks." ) // Context defines the context to execute it commands. @@ -45,7 +46,9 @@ func NewCtxSanitized() *Context { return NewCtxSanitizedAt("") } -// SetConfigCache sets the Git config cache to use. +// SetConfigCache sets the Git config cache to use where +// all env. variables are replaced when `envReplacePrefix` matches the +// key. func (c *Context) InitConfigCache(filter func(string) bool) error { cache, err := NewConfigCache(*c, filter) @@ -78,6 +81,11 @@ func (c *Context) GetConfig(key string, scope ConfigScope) (val string) { return "" } + // Expand env. variables in Git config values. + if strings.HasPrefix(val, GitConfigPrefix) { + val = os.ExpandEnv(val) + } + return } diff --git a/githooks/git/gitconfig-cache.go b/githooks/git/gitconfig-cache.go index fa98ba6e..0ed4cd25 100644 --- a/githooks/git/gitconfig-cache.go +++ b/githooks/git/gitconfig-cache.go @@ -2,6 +2,7 @@ package git import ( "bufio" + "os" "regexp" "strings" @@ -123,6 +124,11 @@ func parseConfig(s string, filterFunc func(string) bool) (c ConfigCache, err err return } + // Expand env. variables in Git config values. + if strings.HasPrefix(keyValue[0], GitConfigPrefix) { + keyValue[1] = os.ExpandEnv(keyValue[1]) + } + c.add(keyValue[0], keyValue[0], keyValue[1], scope, false) } diff --git a/githooks/git/gitconfig-cache_test.go b/githooks/git/gitconfig-cache_test.go index 333dcd2f..112ea33d 100644 --- a/githooks/git/gitconfig-cache_test.go +++ b/githooks/git/gitconfig-cache_test.go @@ -1,6 +1,7 @@ package git import ( + "os" "regexp" "testing" @@ -95,3 +96,32 @@ func TestGitConfigCache(t *testing.T) { {Key: "t.t", Value: "a2"}, {Key: "t.t", Value: "a3"}}, kv) } + +func TestGitConfigCacheEnv(t *testing.T) { + + s := "system\x00githooks.a\n${MONKEY}-a" + + "\x00global\x00githooks.b\n$MONKEY-b" + + "\x00global\x00a.c\n$MONKEY-b" + + os.Setenv("MONKEY", "banana") + assert.Equal(t, "banana", os.Getenv("MONKEY")) + + c, err := parseConfig(s, func(string) bool { return true }) + + command := c.scopes[0] + worktree := c.scopes[1] + local := c.scopes[2] + global := c.scopes[3] + system := c.scopes[4] + + assert.Nil(t, err) + assert.Equal(t, 0, len(command)) + assert.Equal(t, 1, len(system)) + assert.Equal(t, 2, len(global)) + assert.Equal(t, 0, len(local)) + assert.Equal(t, 0, len(worktree)) + + assert.Equal(t, "banana-a", system["githooks.a"].values[0]) + assert.Equal(t, "banana-b", global["githooks.b"].values[0]) + assert.Equal(t, "$MONKEY-b", global["a.c"].values[0]) +} diff --git a/githooks/hooks/gitconfig_test.go b/githooks/hooks/gitconfig_test.go new file mode 100644 index 00000000..d9f36c09 --- /dev/null +++ b/githooks/hooks/gitconfig_test.go @@ -0,0 +1,13 @@ +package hooks + +import ( + "strings" + "testing" + + "github.com/gabyx/githooks/githooks/git" + "github.com/stretchr/testify/assert" +) + +func TestGitConfigPrefix(t *testing.T) { + assert.True(t, strings.HasPrefix(GitCKInstallDir, git.GitConfigPrefix)) +} diff --git a/tests/test-alpine-user.sh b/tests/test-alpine-user.sh index 261cd024..b925d62a 100755 --- a/tests/test-alpine-user.sh +++ b/tests/test-alpine-user.sh @@ -25,7 +25,7 @@ RUN if [ -n "$DOCKER_GROUP_ID" ]; then \ else \ echo "Not adding docker since not working with user!" &>2; \ fi -RUN [ -d "$GH_TEST_GIT_CORE/templates/hooks" ] && \ +RUN [ ! -d "$GH_TEST_GIT_CORE/templates/hooks" ] || \ rm -rf "$GH_TEST_GIT_CORE/templates/hooks" RUN mkdir -p "$GH_TEST_REPO" "$GH_TEST_GIT_CORE/templates/hooks" && \ chown -R test:test "$GH_TEST_REPO" "$GH_TEST_GIT_CORE"