Skip to content

Commit aacde09

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 aacde09

4 files changed

Lines changed: 64 additions & 13 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
@@ -232,6 +232,26 @@ OPTIONS
232232
error while a misspelled environment variable is silently ignored. Some
233233
options can only be specified as an environment variable.
234234
235+
--add-git-config <string>
236+
Add one git config option. The parsed key and value are passed to
237+
'git config' and must be valid syntax for that command. This flag
238+
can be specified more than once. This is similar to --git-config,
239+
but allows multiple discrete uses of the flag instead of a single
240+
comma-separated string.
241+
242+
Both keys and values can be either quoted or unquoted strings.
243+
Within quoted keys and all values (quoted or not), the following
244+
escape sequences are supported:
245+
'\n' => [newline]
246+
'\t' => [tab]
247+
'\"' => '"'
248+
'\,' => ','
249+
'\\' => '\'
250+
To include a colon within a key (e.g. a URL) the key must be
251+
quoted. Within unquoted values commas must be escaped. Within
252+
quoted values commas may be escaped, but are not required to be.
253+
Any other escape sequence is an error.
254+
235255
--add-user, $GITSYNC_ADD_USER
236256
Add a record to /etc/passwd for the current UID/GID. This is
237257
needed to use SSH with an arbitrary UID. This assumes that
@@ -312,7 +332,8 @@ OPTIONS
312332
--git-config <string>, $GITSYNC_GIT_CONFIG
313333
Additional git config options in a comma-separated 'key:val'
314334
format. The parsed keys and values are passed to 'git config' and
315-
must be valid syntax for that command.
335+
must be valid syntax for that command. This is similar to
336+
--add-git-config, but uses a single 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: 36 additions & 9 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("add-git-config",
304+
nil,
305+
"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); 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); 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 {
@@ -2455,6 +2461,26 @@ OPTIONS
24552461
error while a misspelled environment variable is silently ignored. Some
24562462
options can only be specified as an environment variable.
24572463
2464+
--add-git-config <string>
2465+
Add one git config option. The parsed key and value are passed to
2466+
'git config' and must be valid syntax for that command. This flag
2467+
can be specified more than once. This is similar to --git-config,
2468+
but allows multiple discrete uses of the flag instead of a single
2469+
comma-separated string.
2470+
2471+
Both keys and values can be either quoted or unquoted strings.
2472+
Within quoted keys and all values (quoted or not), the following
2473+
escape sequences are supported:
2474+
'\n' => [newline]
2475+
'\t' => [tab]
2476+
'\"' => '"'
2477+
'\,' => ','
2478+
'\\' => '\'
2479+
To include a colon within a key (e.g. a URL) the key must be
2480+
quoted. Within unquoted values commas must be escaped. Within
2481+
quoted values commas may be escaped, but are not required to be.
2482+
Any other escape sequence is an error.
2483+
24582484
--add-user, $GITSYNC_ADD_USER
24592485
Add a record to /etc/passwd for the current UID/GID. This is
24602486
needed to use SSH with an arbitrary UID. This assumes that
@@ -2535,7 +2561,8 @@ OPTIONS
25352561
--git-config <string>, $GITSYNC_GIT_CONFIG
25362562
Additional git config options in a comma-separated 'key:val'
25372563
format. The parsed keys and values are passed to 'git config' and
2538-
must be valid syntax for that command.
2564+
must be valid syntax for that command. This is similar to
2565+
--add-git-config, but uses a single 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+
--add-git-config='protocol.file.allow:always' \
372+
--add-git-config='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+
--add-git-config='sect.k3:a val' \
3447+
--add-git-config='sect.k4:"a val with quotes"' \
3448+
--add-git-config='"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)