Compare

b70101c22431bb77ef1d174cb7309f2eaab68c0e..c2a48fdcb56fed3a79bf43b7eac0a4b5ffb32317

t/lib-httpd: document writing concurrency-safe CGI helpers

Update t/lib-httpd.sh to document the fixes applied to
apply-one-time-script.sh and http-429.sh for future developers working
on helper scripts. Add concrete examples of patterns and anti-patterns
that should be considered when handling state management.

Signed-off-by: Michael Montalbo <mmontalbo@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Montalbo <mmontalbo@gmail.com> · 2026-09-01 15:53 UTC · committed by Junio C Hamano

Browse files
12 files changed, +187 −159
modified Documentation/config/worktree.adoc
@@ -5,9 +5,8 @@
5 5 set to true, `worktree add` tries to find a remote-tracking
6 6 branch whose name uniquely matches the new branch name. If
7 7 such a branch exists, it is checked out and set as "upstream"
8 - for the new branch. If multiple matches are found, the command
9 - fails. If no such match can be found, it falls back to
10 - creating a new branch from the current `HEAD`.
8 + for the new branch. If no such match can be found, it falls
9 + back to creating a new branch from the current `HEAD`.
11 10
12 11 `worktree.useRelativePaths`::
13 12 Link worktrees using relative paths (when "`true`") or absolute
modified Documentation/git-worktree.adoc
@@ -219,9 +219,7 @@ To remove a locked worktree, specify `--force` twice.
219 219 of creating a new branch from `HEAD`, if there exists a tracking
220 220 branch in exactly one remote matching the basename of _<path>_,
221 221 base the new branch on the remote-tracking branch, and mark
222 - the remote-tracking branch as "upstream" from the new branch. If
223 - there are multiple matches, the command fails. If there is no
224 - match, the command falls back to creating a new branch from `HEAD`.
222 + the remote-tracking branch as "upstream" from the new branch.
225 223 +
226 224 This can also be set up as the default behaviour by using the
227 225 `worktree.guessRemote` config option.
modified builtin/checkout.c
@@ -1340,51 +1340,13 @@ enum checkout_command {
1340 1340 CHECKOUT_RESTORE = 3,
1341 1341 };
1342 1342
1343 - static void advise_disambiguating_remotes(enum checkout_command which_command,
1344 - const char *branch,
1345 - const struct string_list *matched_remote_names)
1346 - {
1347 - const char *cmdname;
1348 - struct string_list_item *item;
1349 -
1350 - switch (which_command) {
1351 - case CHECKOUT_CHECKOUT:
1352 - cmdname = "checkout";
1353 - break;
1354 - case CHECKOUT_SWITCH:
1355 - cmdname = "switch";
1356 - break;
1357 - default:
1358 - BUG("command <%d> should not reach advise_disambiguating_remotes",
1359 - which_command);
1360 - break;
1361 - }
1362 -
1363 - advise(_("Branch name '%s' appears in multiple remotes:"), branch);
1364 - for_each_string_list_item(item, matched_remote_names) {
1365 - advise(_(" %s"), item->string);
1366 - }
1367 - advise(_("If you meant to check out a remote tracking branch on <remote>,\n"
1368 - "you can do so by fully qualifying the name with the --track option:\n"
1369 - "\n"
1370 - " git %s --track <remote>/%s\n"
1371 - "\n"
1372 - "If you'd like to always have checkouts of an ambiguous name prefer\n"
1373 - "one remote, e.g. the 'origin' remote, consider setting\n"
1374 - "checkout.defaultRemote=origin in your config."),
1375 - cmdname, branch);
1376 - }
1377 -
1378 1343 static char *parse_remote_branch(const char *arg,
1379 1344 struct object_id *rev,
1380 1345 int could_be_checkout_paths,
1381 1346 enum checkout_command which_command)
1382 1347 {
1383 1348 int num_matches = 0;
1384 - struct string_list matched_remote_names = STRING_LIST_INIT_DUP;
1385 -
1386 - char *remote = unique_tracking_name(arg, rev, &num_matches,
1387 - &matched_remote_names);
1349 + char *remote = unique_tracking_name(arg, rev, &num_matches);
1388 1350
1389 1351 if (remote && could_be_checkout_paths) {
1390 1352 die(_("'%s' could be both a local file and a tracking branch.\n"
@@ -1393,15 +1355,37 @@ static char *parse_remote_branch(const char *arg,
1393 1355 }
1394 1356
1395 1357 if (!remote && num_matches > 1) {
1396 - if (advice_enabled(ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME))
1397 - advise_disambiguating_remotes(which_command, arg,
1398 - &matched_remote_names);
1399 - die(_("'%s' matched multiple (%d) remote tracking branches"),
1400 - arg, num_matches);
1358 + if (advice_enabled(ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME)) {
1359 + const char *cmdname;
1360 +
1361 + switch (which_command) {
1362 + case CHECKOUT_CHECKOUT:
1363 + cmdname = "checkout";
1364 + break;
1365 + case CHECKOUT_SWITCH:
1366 + cmdname = "switch";
1367 + break;
1368 + default:
1369 + BUG("command <%d> should not reach parse_remote_branch",
1370 + which_command);
1371 + break;
1372 + }
1373 +
1374 + advise(_("If you meant to check out a remote tracking branch on, e.g. 'origin',\n"
1375 + "you can do so by fully qualifying the name with the --track option:\n"
1376 + "\n"
1377 + " git %s --track origin/<name>\n"
1378 + "\n"
1379 + "If you'd like to always have checkouts of an ambiguous <name> prefer\n"
1380 + "one remote, e.g. the 'origin' remote, consider setting\n"
1381 + "checkout.defaultRemote=origin in your config."),
1382 + cmdname);
1383 + }
1384 +
1385 + die(_("'%s' matched multiple (%d) remote tracking branches"),
1386 + arg, num_matches);
1401 1387 }
1402 1388
1403 - string_list_clear(&matched_remote_names, 0);
1404 -
1405 1389 return remote;
1406 1390 }
1407 1391
modified builtin/worktree.c
@@ -763,26 +763,7 @@ static int dwim_orphan(const struct add_opts *opts, int opt_track, int remote)
763 763 return 1;
764 764 }
765 765
766 - static void advise_disambiguating_remotes(const char *path, const char *branch,
767 - const struct string_list *matched_remote_names)
768 - {
769 - struct string_list_item *item;
770 -
771 - advise(_("Branch name '%s' appears in multiple remotes:"), branch);
772 - for_each_string_list_item(item, matched_remote_names) {
773 - advise(_(" %s"), item->string);
774 - }
775 - advise(_("If you meant to create a worktree from a remote tracking branch on\n"
776 - "<remote>, you can do so by:\n"
777 - "\n"
778 - " git worktree add -b %s %s <remote>/%s\n"
779 - "\n"
780 - "If you'd like to always prefer some remote, e.g. 'origin',\n"
781 - "consider setting checkout.defaultRemote=origin in your config."),
782 - branch, path, branch);
783 - }
784 -
785 - static char *dwim_branch(const struct add_opts *opts, const char *path, char **new_branch)
766 + static char *dwim_branch(const char *path, char **new_branch)
786 767 {
787 768 int n;
788 769 int branch_exists;
@@ -800,21 +781,7 @@ static char *dwim_branch(const struct add_opts *opts, const char *path, char **n
800 781 *new_branch = branchname;
801 782 if (guess_remote) {
802 783 struct object_id oid;
803 - char *remote;
804 - int num_matches = 0;
805 - struct string_list matched_remote_names = STRING_LIST_INIT_DUP;
806 -
807 - remote = unique_tracking_name(*new_branch, &oid, &num_matches,
808 - &matched_remote_names);
809 - if (!remote && num_matches > 1) {
810 - if (!opts->quiet &&
811 - advice_enabled(ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME))
812 - advise_disambiguating_remotes(path, *new_branch,
813 - &matched_remote_names);
814 - die(_("'%s' matched multiple (%d) remote tracking branches"),
815 - *new_branch, num_matches);
816 - }
817 - string_list_clear(&matched_remote_names, 0);
784 + char *remote = unique_tracking_name(*new_branch, &oid, NULL);
818 785 return remote;
819 786 }
820 787 return NULL;
@@ -922,7 +889,7 @@ static int add(int ac, const char **av, const char *prefix,
922 889 opts.orphan = dwim_orphan(&opts, !!opt_track, 0);
923 890 } else if (ac < 2) {
924 891 /* DWIM: Guess branch name from path. */
925 - char *s = dwim_branch(&opts, path, &new_branch_to_free);
892 + char *s = dwim_branch(path, &new_branch_to_free);
926 893 if (s)
927 894 branch = branch_to_free = s;
928 895 new_branch = new_branch_to_free;
@@ -930,29 +897,17 @@ static int add(int ac, const char **av, const char *prefix,
930 897 /* DWIM: Infer --orphan when repo has no refs. */
931 898 opts.orphan = (!s) && dwim_orphan(&opts, !!opt_track, 1);
932 899 } else if (ac == 2) {
900 + struct object_id oid;
933 901 struct commit *commit;
902 + char *remote;
934 903
935 904 commit = lookup_commit_reference_by_name(branch);
936 905 if (!commit) {
937 - struct object_id oid;
938 - char *remote;
939 - int num_matches = 0;
940 - struct string_list matched_remote_names = STRING_LIST_INIT_DUP;
941 -
942 - remote = unique_tracking_name(branch, &oid, &num_matches,
943 - &matched_remote_names);
906 + remote = unique_tracking_name(branch, &oid, NULL);
944 907 if (remote) {
945 908 new_branch = branch;
946 909 branch = new_branch_to_free = remote;
947 - } else if (num_matches > 1) {
948 - if (!opts.quiet &&
949 - advice_enabled(ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME))
950 - advise_disambiguating_remotes(path, branch,
951 - &matched_remote_names);
952 - die(_("'%s' matched multiple (%d) remote tracking branches"),
953 - branch, num_matches);
954 910 }
955 - string_list_clear(&matched_remote_names, 0);
956 911 }
957 912
958 913 if (!strcmp(branch, "HEAD"))
modified checkout.c
@@ -8,7 +8,6 @@
8 8 #include "checkout.h"
9 9 #include "config.h"
10 10 #include "strbuf.h"
11 - #include "string-list.h"
12 11
13 12 struct tracking_name_data {
14 13 /* const */ char *src_ref;
@@ -18,7 +17,6 @@ struct tracking_name_data {
18 17 const char *default_remote;
19 18 char *default_dst_ref;
20 19 struct object_id *default_dst_oid;
21 - struct string_list *remote_names;
22 20 };
23 21
24 22 #define TRACKING_NAME_DATA_INIT { 0 }
@@ -41,8 +39,6 @@ static int check_tracking_name(struct remote *remote, void *cb_data)
41 39 oidcpy(dst, cb->dst_oid);
42 40 cb->default_dst_oid = dst;
43 41 }
44 - if (cb->remote_names)
45 - string_list_append(cb->remote_names, remote->name);
46 42 if (cb->dst_ref) {
47 43 free(query.dst);
48 44 return 0;
@@ -52,19 +48,14 @@ static int check_tracking_name(struct remote *remote, void *cb_data)
52 48 }
53 49
54 50 char *unique_tracking_name(const char *name, struct object_id *oid,
55 - int *dwim_remotes_matched,
56 - struct string_list *dwim_remote_names)
51 + int *dwim_remotes_matched)
57 52 {
58 53 struct tracking_name_data cb_data = TRACKING_NAME_DATA_INIT;
59 54 const char *default_remote = NULL;
60 -
61 - if (!repo_config_get_string_tmp(the_repository,
62 - "checkout.defaultremote",
63 - &default_remote))
55 + if (!repo_config_get_string_tmp(the_repository, "checkout.defaultremote", &default_remote))
64 56 cb_data.default_remote = default_remote;
65 57 cb_data.src_ref = xstrfmt("refs/heads/%s", name);
66 58 cb_data.dst_oid = oid;
67 - cb_data.remote_names = dwim_remote_names;
68 59 for_each_remote(check_tracking_name, &cb_data);
69 60 if (dwim_remotes_matched)
70 61 *dwim_remotes_matched = cb_data.num_matches;
modified checkout.h
@@ -3,8 +3,6 @@
3 3
4 4 #include "hash.h"
5 5
6 - struct string_list;
7 -
8 6 /*
9 7 * Check if the branch name uniquely matches a branch name on a remote
10 8 * tracking branch. Return the name of the remote if such a branch
@@ -12,7 +10,6 @@ struct string_list;
12 10 */
13 11 char *unique_tracking_name(const char *name,
14 12 struct object_id *oid,
15 - int *dwim_remotes_matched,
16 - struct string_list *dwim_remote_names);
13 + int *dwim_remotes_matched);
17 14
18 15 #endif /* CHECKOUT_H */
modified t/lib-httpd.sh
@@ -159,6 +159,18 @@ prepare_httpd() {
159 159 mkdir -p "$HTTPD_DOCUMENT_ROOT_PATH"
160 160 cp "$TEST_PATH"/passwd "$HTTPD_ROOT_PATH"
161 161 cp "$TEST_PATH"/proxy-passwd "$HTTPD_ROOT_PATH"
162 + # Apache can run the following scripts concurrently per request. Make
163 + # sure any state management logic is resilient to race conditions.
164 + #
165 + # For example:
166 + # - use "mkdir dir" to ensure only one request "succeeds" under some
167 + # condition (see http-429.sh).
168 + # - chain (&&) atomic operations like "rm marker" (no -f) with the
169 + # logic that is guarded by the marker instead of relying on a
170 + # separate "test -f" and "rm marker" check
171 + # (see apply-one-time-script.sh).
172 + # - use scratch file names that include the process ID ($$), so
173 + # concurrent requests do not overwrite each other's state.
162 174 install_script incomplete-length-upload-pack-v2-http.sh
163 175 install_script incomplete-body-upload-pack-v2-http.sh
164 176 install_script error-no-report.sh
modified t/lib-httpd/apply-one-time-script.sh
@@ -6,21 +6,31 @@
6 6 #
7 7 # This can be used to simulate the effects of the repository changing in
8 8 # between HTTP request-response pairs.
9 - if test -f one-time-script
10 - then
11 - LC_ALL=C
12 - export LC_ALL
9 + test -f one-time-script || exec "$GIT_EXEC_PATH/git-http-backend"
10 +
11 + LC_ALL=C
12 + export LC_ALL
13 13
14 - "$GIT_EXEC_PATH/git-http-backend" >out
15 - ./one-time-script out >out_modified
14 + out=out.$$
15 + modified=out-modified.$$
16 + "$GIT_EXEC_PATH/git-http-backend" >"$out"
16 17
17 - if cmp -s out out_modified
18 - then
19 - cat out
20 - else
21 - cat out_modified
22 - rm one-time-script
23 - fi
18 + # Since Apache can execute this script for multiple requests
19 + # concurrently, we chain "rm one-time-script" with the logic
20 + # for generating a modified response. If the "rm" ran separately,
21 + # a concurrent request could pass the "test -f" above and
22 + # erroneously result in multiple modified responses or an empty
23 + # body depending on the race state.
24 + #
25 + # We discard stderr for ./one-time-script since it is possible
26 + # ./one-time-script has been removed already, which is expected
27 + # sometimes. In this case, the unmodified response will be returned.
28 + if ./one-time-script "$out" 2>/dev/null >"$modified" &&
29 + ! cmp -s "$out" "$modified" &&
30 + rm one-time-script 2>/dev/null
31 + then
32 + cat "$modified"
24 33 else
25 - "$GIT_EXEC_PATH/git-http-backend"
34 + cat "$out"
26 35 fi
36 + rm -f "$out" "$modified"
modified t/lib-httpd/http-429.sh
@@ -3,7 +3,7 @@
3 3 # Script to return HTTP 429 Too Many Requests responses for testing retry logic.
4 4 # Usage: /http_429/<test-context>/<retry-after-value>/<repo-path>
5 5 #
6 - # The test-context is a unique identifier for each test to isolate state files.
6 + # The test-context is a unique identifier for each test to isolate state directories.
7 7 # The retry-after-value can be:
8 8 # - A number (e.g., "1", "2", "100") - sets Retry-After header to that many seconds
9 9 # - "none" - no Retry-After header
@@ -26,14 +26,16 @@ repo_path="${remaining#*/}" # Get rest (repo path)
26 26 # The repo name is the first component before any "/"
27 27 repo_name="${repo_path%%/*}"
28 28
29 - # Use current directory (HTTPD_ROOT_PATH) for state file
30 - # Create a safe filename from test_context, retry_after and repo_name
31 - # This ensures all requests for the same test context share the same state file
29 + # Use current directory (HTTPD_ROOT_PATH) to hold state directory
30 + # Create a safe directory name from test_context, retry_after and repo_name
31 + # This ensures all requests for the same test context share the same state directory
32 32 safe_name=$(echo "${test_context}-${retry_after}-${repo_name}" | tr '/' '_' | tr -cd 'a-zA-Z0-9_-')
33 - state_file="http-429-state-${safe_name}"
33 + state="http-429-state-${safe_name}"
34 34
35 - # Check if this is the first call (no state file exists)
36 - if test -f "$state_file"
35 + # Check if this is the first call (no state directory exists), or if
36 + # the retry-after-value is "permanent", which indicates a 429 must be
37 + # returned for every request (even if the state directory exists).
38 + if test "$retry_after" != permanent && ! mkdir "$state" 2>/dev/null
37 39 then
38 40 # Already returned 429 once, forward to git-http-backend
39 41 # Set PATH_INFO to just the repo path (without retry-after value)
@@ -52,9 +54,6 @@ then
52 54 exec "$GIT_EXEC_PATH/git-http-backend"
53 55 fi
54 56
55 - # Mark that we've returned 429
56 - touch "$state_file"
57 -
58 57 # Output HTTP 429 response
59 58 printf "Status: 429 Too Many Requests\r\n"
60 59
@@ -67,8 +66,7 @@ case "$retry_after" in
67 66 printf "Retry-After: invalid-format-123abc\r\n"
68 67 ;;
69 68 permanent)
70 - # Always return 429, don't set state file for success
71 - rm -f "$state_file"
69 + # Always return 429
72 70 printf "Retry-After: 1\r\n"
73 71 printf "Content-Type: text/plain\r\n"
74 72 printf "\r\n"
modified t/meson.build
@@ -707,6 +707,7 @@ integration_tests = [
707 707 't5564-http-proxy.sh',
708 708 't5565-push-multiple.sh',
709 709 't5566-push-group.sh',
710 + 't5567-one-time-script.sh',
710 711 't5570-git-daemon.sh',
711 712 't5571-pre-push-hook.sh',
712 713 't5572-pull-submodule.sh',
modified t/t2400-worktree-add.sh
@@ -624,12 +624,12 @@ test_expect_success '"add" <path> <branch> dwims' '
624 624 test_expect_success '"add" <path> <branch> dwims with checkout.defaultRemote' '
625 625 test_when_finished rm -rf repo_upstream repo_dwim foo &&
626 626 setup_remote_repo repo_upstream repo_dwim &&
627 + git init repo_dwim &&
627 628 (
628 629 cd repo_dwim &&
629 630 git remote add repo_upstream2 ../repo_upstream &&
630 631 git fetch repo_upstream2 &&
631 - test_must_fail git worktree add ../foo foo 2>error.actual &&
632 - test_grep "matched multiple (2) remote tracking branches" error.actual &&
632 + test_must_fail git worktree add ../foo foo &&
633 633 git -c checkout.defaultRemote=repo_upstream worktree add ../foo foo &&
634 634 git status -uno --porcelain >status.actual &&
635 635 test_must_be_empty status.actual
@@ -669,19 +669,6 @@ test_expect_success 'git worktree add --guess-remote sets up tracking' '
669 669 test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
670 670 )
671 671 '
672 -
673 - test_expect_success 'git worktree add --guess-remote fails if there are multiple matches' '
674 - test_when_finished rm -rf repo_a repo_b foo &&
675 - setup_remote_repo repo_a repo_b &&
676 - (
677 - cd repo_b &&
678 - git remote add repo_a2 ../repo_a &&
679 - git fetch repo_a2 &&
680 - test_must_fail git worktree add --guess-remote ../foo 2>actual &&
681 - test_grep "matched multiple (2) remote tracking branches" actual
682 - )
683 - '
684 -
685 672 test_expect_success 'git worktree add --guess-remote sets up tracking (quiet)' '
686 673 test_when_finished rm -rf repo_a repo_b foo &&
687 674 setup_remote_repo repo_a repo_b &&
added t/t5567-one-time-script.sh mode 100755
@@ -0,0 +1,96 @@
1 + #!/bin/sh
2 +
3 + test_description='apply-one-time-script CGI helper is safe under concurrent requests'
4 +
5 + . ./test-lib.sh
6 +
7 + HELPER="$TEST_DIRECTORY/lib-httpd/apply-one-time-script.sh"
8 +
9 + test_expect_success PIPE 'helper only serves one rewritten response for concurrent requests' '
10 + mkdir workdir fakebin &&
11 + ENTERED="$PWD/entered" &&
12 + GATE="$PWD/gate" &&
13 + export ENTERED GATE &&
14 + mkfifo "$ENTERED" "$GATE" &&
15 +
16 + # A stub git-http-backend that returns a response based on
17 + # $ROLE. For $ROLE = modify, return the response string
18 + # "packfile", which ends up being modified by the example
19 + # one-time-script below.
20 + #
21 + # Otherwise, run the branch returning a response that
22 + # should be passed through, and block until released
23 + # by "read -r $GATE".
24 + write_script fakebin/git-http-backend <<-\EOF &&
25 + printf "Status: 200 OK\r\n"
26 + printf "Content-Type: application/x-git-result\r\n"
27 + printf "\r\n"
28 + if test "$ROLE" = modify
29 + then
30 + printf "packfile\n"
31 + else
32 + echo entered >"$ENTERED"
33 + read -r released <"$GATE"
34 + printf "refs\n"
35 + fi
36 + EOF
37 +
38 + # An example one-time-script for apply-one-time-script
39 + # to execute. Checks for "packfile" in the response
40 + # that will be returned, and replaces it with a
41 + # modified response. Passes through responses without
42 + # "packfile" in them.
43 + write_script workdir/one-time-script <<-\EOF &&
44 + if grep packfile "$1" >/dev/null
45 + then
46 + sed "/packfile/q" "$1" &&
47 + printf "REPLACED\n"
48 + else
49 + cat "$1"
50 + fi
51 + EOF
52 +
53 + GIT_EXEC_PATH="$PWD/fakebin" &&
54 + export GIT_EXEC_PATH &&
55 +
56 + # Ensure $GATE has a reader so the test does not block indefinitely if
57 + # the helper is buggy and "echo released >&9" below does not unblock
58 + # the unmodified response gate.
59 + exec 9<>"$GATE" &&
60 +
61 + # Launch the passthrough request in the background. Record its pid
62 + # so it can be killed when the test finishes if, for some reason, the
63 + # request stays blocked and would stall a test runner.
64 + { (
65 + cd workdir &&
66 + ROLE=passthrough sh "$HELPER" >../passthrough.out 2>../passthrough.err
67 + ) & } &&
68 + passthrough_pid=$! &&
69 + test_when_finished "kill $passthrough_pid 2>/dev/null || :" &&
70 +
71 + # Wait until the passthrough request is "in-flight" and paused
72 + # mid-response.
73 + read -r entered <"$ENTERED" &&
74 +
75 + # Launch the request for a modified response while the passthrough
76 + # request is concurrently "in-flight" and paused.
77 + (
78 + cd workdir &&
79 + ROLE=modify sh "$HELPER" >../modify.out 2>../modify.err
80 + ) &&
81 +
82 + # Unblock the passthrough request, allowing git-http-backend to
83 + # complete its response.
84 + echo released >&9 &&
85 + { wait "$passthrough_pid" || :; } &&
86 +
87 + test_must_be_empty passthrough.err &&
88 + test_must_be_empty modify.err &&
89 + test_grep "Status: 200 OK" passthrough.out &&
90 + test_grep "Status: 200 OK" modify.out &&
91 + test_grep REPLACED modify.out &&
92 + test_grep ! REPLACED passthrough.out &&
93 + test_grep refs passthrough.out
94 + '
95 +
96 + test_done