Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Steinhardt <psteinhardt@gitlab.com>2022-05-12 12:28:40 +0300
committerPatrick Steinhardt <psteinhardt@gitlab.com>2022-05-17 08:54:18 +0300
commit0e106be9a12614015c3486dc3d08ae4c1a9d58cd (patch)
tree392b9d72e56f27b5971938b64c812ab9c7277994
parent8959200e3ebd37963a1c896aa65e521aff666d41 (diff)
git: Extract helper function to convert config pairs to envvars
We'll need to convert config pairs into Git configuration environment variables so that we can inject them into the Ruby sidecar. Extract the code that does this from `WithConfigEnv()` into a separate helper to prepare for this change.
-rw-r--r--internal/git/command_options.go27
-rw-r--r--internal/git/command_options_test.go47
2 files changed, 63 insertions, 11 deletions
diff --git a/internal/git/command_options.go b/internal/git/command_options.go
index 6b9f8dbb7..ca31b88ce 100644
--- a/internal/git/command_options.go
+++ b/internal/git/command_options.go
@@ -82,6 +82,21 @@ func (cp ConfigPair) GlobalArgs() ([]string, error) {
return []string{"-c", fmt.Sprintf("%s=%s", cp.Key, cp.Value)}, nil
}
+// ConfigPairsToGitEnvironment converts the given config pairs into a set of environment variables
+// that can be injected into a Git executable.
+func ConfigPairsToGitEnvironment(configPairs []ConfigPair) []string {
+ env := make([]string, 0, len(configPairs)*2+1)
+
+ for i, configPair := range configPairs {
+ env = append(env,
+ fmt.Sprintf("GIT_CONFIG_KEY_%d=%s", i, configPair.Key),
+ fmt.Sprintf("GIT_CONFIG_VALUE_%d=%s", i, configPair.Value),
+ )
+ }
+
+ return append(env, fmt.Sprintf("GIT_CONFIG_COUNT=%d", len(configPairs)))
+}
+
// Flag is a single token optional command line argument that enables or
// disables functionality (e.g. "-L")
type Flag struct {
@@ -210,17 +225,7 @@ func WithConfig(configPairs ...ConfigPair) CmdOpt {
// via the process's command line.
func WithConfigEnv(configPairs ...ConfigPair) CmdOpt {
return func(_ context.Context, _ config.Cfg, _ CommandFactory, c *cmdCfg) error {
- env := make([]string, 0, len(configPairs)*2+1)
-
- for i, configPair := range configPairs {
- env = append(env,
- fmt.Sprintf("GIT_CONFIG_KEY_%d=%s", i, configPair.Key),
- fmt.Sprintf("GIT_CONFIG_VALUE_%d=%s", i, configPair.Value),
- )
- }
- env = append(env, fmt.Sprintf("GIT_CONFIG_COUNT=%d", len(configPairs)))
-
- c.env = append(c.env, env...)
+ c.env = append(c.env, ConfigPairsToGitEnvironment(configPairs)...)
return nil
}
}
diff --git a/internal/git/command_options_test.go b/internal/git/command_options_test.go
index 9a8dfa1d8..95d874384 100644
--- a/internal/git/command_options_test.go
+++ b/internal/git/command_options_test.go
@@ -465,3 +465,50 @@ func TestWithInternalFetch(t *testing.T) {
})
}
}
+
+func TestConfigPairsToEnvironment(t *testing.T) {
+ for _, tc := range []struct {
+ desc string
+ configPairs []ConfigPair
+ expectedEnv []string
+ }{
+ {
+ desc: "no pairs",
+ expectedEnv: []string{
+ "GIT_CONFIG_COUNT=0",
+ },
+ },
+ {
+ desc: "single pair",
+ configPairs: []ConfigPair{
+ {Key: "foo.bar", Value: "baz"},
+ },
+ expectedEnv: []string{
+ "GIT_CONFIG_KEY_0=foo.bar",
+ "GIT_CONFIG_VALUE_0=baz",
+ "GIT_CONFIG_COUNT=1",
+ },
+ },
+ {
+ desc: "multiple pairs",
+ configPairs: []ConfigPair{
+ {Key: "duplicate.key", Value: "first"},
+ {Key: "foo.bar", Value: "baz"},
+ {Key: "duplicate.key", Value: "second"},
+ },
+ expectedEnv: []string{
+ "GIT_CONFIG_KEY_0=duplicate.key",
+ "GIT_CONFIG_VALUE_0=first",
+ "GIT_CONFIG_KEY_1=foo.bar",
+ "GIT_CONFIG_VALUE_1=baz",
+ "GIT_CONFIG_KEY_2=duplicate.key",
+ "GIT_CONFIG_VALUE_2=second",
+ "GIT_CONFIG_COUNT=3",
+ },
+ },
+ } {
+ t.Run(tc.desc, func(t *testing.T) {
+ require.Equal(t, tc.expectedEnv, ConfigPairsToGitEnvironment(tc.configPairs))
+ })
+ }
+}