Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add expansion of env. variables in Git config ⚓ #141

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions githooks/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (
)

const (
HEAD string = "HEAD"
HEAD string = "HEAD"
GitConfigPrefix string = "githooks."
)

// Context defines the context to execute it commands.
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
}

Expand Down
6 changes: 6 additions & 0 deletions githooks/git/gitconfig-cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package git

import (
"bufio"
"os"
"regexp"
"strings"

Expand Down Expand Up @@ -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)
}

Expand Down
30 changes: 30 additions & 0 deletions githooks/git/gitconfig-cache_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package git

import (
"os"
"regexp"
"testing"

Expand Down Expand Up @@ -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])
}
13 changes: 13 additions & 0 deletions githooks/hooks/gitconfig_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
2 changes: 1 addition & 1 deletion tests/test-alpine-user.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down