Skip to content

Commit 036ea17

Browse files
committed
Don't set safe.dir=* in main app
Instead, set it in tests. This requires a new flag `--add-git-config` which is like `--git-config` but uses repeated calls to the flag instead of a comma-separated value.
1 parent 3f823dc commit 036ea17

4 files changed

Lines changed: 66 additions & 15 deletions

File tree

Dockerfile.in

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ RUN rm -rf \
103103
/usr/lib/git-core/git-http-push \
104104
/usr/lib/git-core/git-imap-send \
105105
/usr/lib/git-core/git-instaweb \
106-
/usr/lib/git-core/git-sh-i18n--envsubst \
107106
/usr/lib/git-core/scalar \
108107
/usr/lib/openssh/ssh-keysign \
109108
/usr/lib/openssh/ssh-pkcs11-helper \

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,28 @@ OPTIONS
312312
--git-config <string>, $GITSYNC_GIT_CONFIG
313313
Additional git config options in a comma-separated 'key:val'
314314
format. The parsed keys and values are passed to 'git config' and
315-
must be valid syntax for that command.
315+
must be valid syntax for that command. This is similar to
316+
--git-config-add, but uses a single comma-separated string.
317+
318+
Both keys and values can be either quoted or unquoted strings.
319+
Within quoted keys and all values (quoted or not), the following
320+
escape sequences are supported:
321+
'\n' => [newline]
322+
'\t' => [tab]
323+
'\"' => '"'
324+
'\,' => ','
325+
'\\' => '\'
326+
To include a colon within a key (e.g. a URL) the key must be
327+
quoted. Within unquoted values commas must be escaped. Within
328+
quoted values commas may be escaped, but are not required to be.
329+
Any other escape sequence is an error.
330+
331+
--git-config-add <string>
332+
Add one git config option. The parsed key and value are passed to
333+
'git config' and must be valid syntax for that command. This flag
334+
can be specified more than once. This is similar to --git-config,
335+
but allows multiple discrete uses of the flag instead of a single
336+
comma-separated string.
316337
317338
Both keys and values can be either quoted or unquoted strings.
318339
Within quoted keys and all values (quoted or not), the following

main.go

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,12 @@ func main() {
297297
flGitCmd := pflag.String("git",
298298
envString("git", "GITSYNC_GIT", "GIT_SYNC_GIT"),
299299
"the git command to run (subject to PATH search, mostly for testing)")
300-
flGitConfig := pflag.String("git-config",
300+
flGitConfigString := pflag.String("git-config",
301301
envString("", "GITSYNC_GIT_CONFIG", "GIT_SYNC_GIT_CONFIG"),
302302
"additional git config options in 'section.var1:val1,\"section.sub.var2\":\"val2\"' format")
303+
flGitConfigList := pflag.StringArray("git-config-add", // Array vs. Slice to avoid CSV parsing
304+
nil,
305+
"add one additional git config option in 'section.var1:val1' format; can be repeated")
303306
flGitGC := pflag.String("git-gc",
304307
envString("always", "GITSYNC_GIT_GC", "GIT_SYNC_GIT_GC"),
305308
"git garbage collection behavior: one of 'auto', 'always', 'aggressive', or 'off'")
@@ -786,9 +789,16 @@ func main() {
786789
}
787790

788791
// This needs to be after all other git-related config flags.
789-
if *flGitConfig != "" {
790-
if err := git.SetupExtraGitConfigs(ctx, *flGitConfig); err != nil {
791-
log.Error(err, "can't set additional git configs", "configs", *flGitConfig)
792+
if *flGitConfigString != "" {
793+
if err := git.SetupExtraGitConfigs(ctx, *flGitConfigString, "--git-config"); err != nil {
794+
log.Error(err, "can't set additional git configs", "configs", *flGitConfigString)
795+
os.Exit(1)
796+
}
797+
}
798+
if len(*flGitConfigList) > 0 {
799+
str := strings.Join(*flGitConfigList, ",")
800+
if err := git.SetupExtraGitConfigs(ctx, str, "--git-config-add"); err != nil {
801+
log.Error(err, "can't set additional git configs", "configs", str)
792802
os.Exit(1)
793803
}
794804
}
@@ -2164,10 +2174,6 @@ func (git *repoSync) SetupDefaultGitConfigs(ctx context.Context) error {
21642174
// Never prompt for a password.
21652175
key: "core.askPass",
21662176
val: "true",
2167-
}, {
2168-
// Mark repos as safe (avoid a "dubious ownership" error).
2169-
key: "safe.directory",
2170-
val: "*",
21712177
}}
21722178

21732179
for _, kv := range configs {
@@ -2180,10 +2186,10 @@ func (git *repoSync) SetupDefaultGitConfigs(ctx context.Context) error {
21802186

21812187
// SetupExtraGitConfigs configures the global git environment with user-provided
21822188
// override settings.
2183-
func (git *repoSync) SetupExtraGitConfigs(ctx context.Context, configsFlag string) error {
2189+
func (git *repoSync) SetupExtraGitConfigs(ctx context.Context, configsFlag string, flagName string) error {
21842190
configs, err := parseGitConfigs(configsFlag)
21852191
if err != nil {
2186-
return fmt.Errorf("can't parse --git-config flag: %w", err)
2192+
return fmt.Errorf("can't parse %s flag: %w", flagName, err)
21872193
}
21882194
git.log.V(1).Info("setting additional git configs", "configs", configs)
21892195
for _, kv := range configs {
@@ -2535,7 +2541,28 @@ OPTIONS
25352541
--git-config <string>, $GITSYNC_GIT_CONFIG
25362542
Additional git config options in a comma-separated 'key:val'
25372543
format. The parsed keys and values are passed to 'git config' and
2538-
must be valid syntax for that command.
2544+
must be valid syntax for that command. This is similar to
2545+
--git-config-add, but uses a single comma-separated string.
2546+
2547+
Both keys and values can be either quoted or unquoted strings.
2548+
Within quoted keys and all values (quoted or not), the following
2549+
escape sequences are supported:
2550+
'\n' => [newline]
2551+
'\t' => [tab]
2552+
'\"' => '"'
2553+
'\,' => ','
2554+
'\\' => '\'
2555+
To include a colon within a key (e.g. a URL) the key must be
2556+
quoted. Within unquoted values commas must be escaped. Within
2557+
quoted values commas may be escaped, but are not required to be.
2558+
Any other escape sequence is an error.
2559+
2560+
--git-config-add <string>
2561+
Add one git config option. The parsed key and value are passed to
2562+
'git config' and must be valid syntax for that command. This flag
2563+
can be specified more than once. This is similar to --git-config,
2564+
but allows multiple discrete uses of the flag instead of a single
2565+
comma-separated string.
25392566
25402567
Both keys and values can be either quoted or unquoted strings.
25412568
Within quoted keys and all values (quoted or not), the following

test_e2e.sh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,8 @@ function GIT_SYNC() {
368368
--add-user \
369369
--group-write \
370370
--touch-file="$INTERLOCK" \
371-
--git-config='protocol.file.allow:always' \
371+
--git-config-add='protocol.file.allow:always' \
372+
--git-config-add='safe.directory:*' \
372373
--http-bind=":$HTTP_PORT" \
373374
--http-metrics \
374375
--http-pprof \
@@ -3441,7 +3442,10 @@ function e2e::additional_git_configs() {
34413442
--repo="file://$REPO" \
34423443
--root="$ROOT" \
34433444
--link="link" \
3444-
--git-config='http.postBuffer:10485760,sect.k1:"a val",sect.k2:another val'
3445+
--git-config='http.postBuffer:10485760,sect.k1:"a val",sect.k2:another val' \
3446+
--git-config-add='sect.k3:a val' \
3447+
--git-config-add='sect.k4:"a val with quotes"' \
3448+
--git-config-add='"sect.k5":"quoted_all"'
34453449
assert_link_exists "$ROOT/link"
34463450
assert_file_exists "$ROOT/link/file"
34473451
assert_file_eq "$ROOT/link/file" "${FUNCNAME[0]}"

0 commit comments

Comments
 (0)