Skip to content

Commit 5e1d2f4

Browse files
committed
Merge branch 'js/http-custom-headers'
Update tests for "http.extraHeaders=<header>" to be portable back to Apache 2.2 (the original depended on <RequireAll/> which is a more recent feature). * js/http-custom-headers: submodule: ensure that -c http.extraheader is heeded t5551: make the test for extra HTTP headers more robust tests: adjust the configuration for Apache 2.2 Rebased-and-signed-off-by: Johannes Schindelin <[email protected]>
2 parents 235777a + 2a2b52b commit 5e1d2f4

16 files changed

+219
-37
lines changed

Documentation/config.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1683,6 +1683,12 @@ http.emptyAuth::
16831683
a username in the URL, as libcurl normally requires a username for
16841684
authentication.
16851685

1686+
http.extraHeader::
1687+
Pass an additional HTTP header when communicating with a server. If
1688+
more than one such entry exists, all of them are added as extra
1689+
headers. To allow overriding the settings inherited from the system
1690+
config, an empty value will reset the extra headers to the empty list.
1691+
16861692
http.cookieFile::
16871693
The pathname of a file containing previously stored cookie lines,
16881694
which should be used

builtin/submodule--helper.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ static int module_name(int argc, const char **argv, const char *prefix)
118118

119119
return 0;
120120
}
121+
121122
static int clone_submodule(const char *path, const char *gitdir, const char *url,
122123
const char *depth, const char *reference, int quiet)
123124
{
@@ -139,7 +140,7 @@ static int clone_submodule(const char *path, const char *gitdir, const char *url
139140
argv_array_push(&cp.args, path);
140141

141142
cp.git_cmd = 1;
142-
cp.env = local_repo_env;
143+
prepare_submodule_repo_env(&cp.env_array);
143144
cp.no_stdin = 1;
144145

145146
return run_command(&cp);
@@ -180,8 +181,8 @@ static int module_clone(int argc, const char **argv, const char *prefix)
180181

181182
const char *const git_submodule_helper_usage[] = {
182183
N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
183-
"[--reference <repository>] [--name <name>] [--url <url>]"
184-
"[--depth <depth>] [--] [<path>...]"),
184+
"[--reference <repository>] [--name <name>] [--depth <depth>] "
185+
"--url <url> --path <path>"),
185186
NULL
186187
};
187188

@@ -191,6 +192,10 @@ static int module_clone(int argc, const char **argv, const char *prefix)
191192
if (!path || !*path)
192193
die(_("submodule--helper: unspecified or empty --path"));
193194

195+
if (argc || !url)
196+
usage_with_options(git_submodule_helper_usage,
197+
module_clone_options);
198+
194199
strbuf_addf(&sb, "%s/modules/%s", get_git_dir(), name);
195200
sm_gitdir = xstrdup(absolute_path(sb.buf));
196201
strbuf_reset(&sb);

config.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ void git_config_push_parameter(const char *text)
162162
{
163163
struct strbuf env = STRBUF_INIT;
164164
const char *old = getenv(CONFIG_DATA_ENVIRONMENT);
165-
if (old) {
165+
if (old && *old) {
166166
strbuf_addstr(&env, old);
167167
strbuf_addch(&env, ' ');
168168
}

git-submodule.sh

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,17 @@ isnumber()
192192
n=$(($1 + 0)) 2>/dev/null && test "$n" = "$1"
193193
}
194194

195+
# Sanitize the local git environment for use within a submodule. We
196+
# can't simply use clear_local_git_env since we want to preserve some
197+
# of the settings from GIT_CONFIG_PARAMETERS.
198+
sanitize_submodule_env()
199+
{
200+
save_config=$GIT_CONFIG_PARAMETERS
201+
clear_local_git_env
202+
GIT_CONFIG_PARAMETERS=$save_config
203+
export GIT_CONFIG_PARAMETERS
204+
}
205+
195206
#
196207
# Add a new submodule to the working tree, .gitmodules and the index
197208
#
@@ -347,9 +358,9 @@ Use -f if you really want to add it." >&2
347358
echo "$(eval_gettext "Reactivating local git directory for submodule '\$sm_name'.")"
348359
fi
349360
fi
350-
git submodule--helper clone ${GIT_QUIET:+--quiet} --prefix "$wt_prefix" --path "$sm_path" --name "$sm_name" --url "$realrepo" "$reference" "$depth" || exit
361+
git submodule--helper clone ${GIT_QUIET:+--quiet} --prefix "$wt_prefix" --path "$sm_path" --name "$sm_name" --url "$realrepo" ${reference:+"$reference"} ${depth:+"$depth"} || exit
351362
(
352-
clear_local_git_env
363+
sanitize_submodule_env
353364
cd "$sm_path" &&
354365
# ash fails to wordsplit ${branch:+-b "$branch"...}
355366
case "$branch" in
@@ -418,7 +429,7 @@ cmd_foreach()
418429
name=$(git submodule--helper name "$sm_path")
419430
(
420431
prefix="$prefix$sm_path/"
421-
clear_local_git_env
432+
sanitize_submodule_env
422433
cd "$sm_path" &&
423434
sm_path=$(relative_path "$sm_path") &&
424435
# we make $path available to scripts ...
@@ -601,14 +612,14 @@ cmd_deinit()
601612
}
602613

603614
is_tip_reachable () (
604-
clear_local_git_env
615+
sanitize_submodule_env &&
605616
cd "$1" &&
606617
rev=$(git rev-list -n 1 "$2" --not --all 2>/dev/null) &&
607618
test -z "$rev"
608619
)
609620

610621
fetch_in_submodule () (
611-
clear_local_git_env
622+
sanitize_submodule_env &&
612623
cd "$1" &&
613624
case "$2" in
614625
'')
@@ -736,11 +747,11 @@ Maybe you want to use 'update --init'?")"
736747

737748
if ! test -d "$sm_path"/.git && ! test -f "$sm_path"/.git
738749
then
739-
git submodule--helper clone ${GIT_QUIET:+--quiet} --prefix "$prefix" --path "$sm_path" --name "$name" --url "$url" "$reference" "$depth" || exit
750+
git submodule--helper clone ${GIT_QUIET:+--quiet} --prefix "$prefix" --path "$sm_path" --name "$name" --url "$url" ${reference:+"$reference"} ${depth:+"$depth"} || exit
740751
cloned_modules="$cloned_modules;$name"
741752
subsha1=
742753
else
743-
subsha1=$(clear_local_git_env; cd "$sm_path" &&
754+
subsha1=$(sanitize_submodule_env; cd "$sm_path" &&
744755
git rev-parse --verify HEAD) ||
745756
die "$(eval_gettext "Unable to find current revision in submodule path '\$displaypath'")"
746757
fi
@@ -750,11 +761,11 @@ Maybe you want to use 'update --init'?")"
750761
if test -z "$nofetch"
751762
then
752763
# Fetch remote before determining tracking $sha1
753-
(clear_local_git_env; cd "$sm_path" && git-fetch) ||
764+
(sanitize_submodule_env; cd "$sm_path" && git-fetch) ||
754765
die "$(eval_gettext "Unable to fetch in submodule path '\$sm_path'")"
755766
fi
756-
remote_name=$(clear_local_git_env; cd "$sm_path" && get_default_remote)
757-
sha1=$(clear_local_git_env; cd "$sm_path" &&
767+
remote_name=$(sanitize_submodule_env; cd "$sm_path" && get_default_remote)
768+
sha1=$(sanitize_submodule_env; cd "$sm_path" &&
758769
git rev-parse --verify "${remote_name}/${branch}") ||
759770
die "$(eval_gettext "Unable to find current ${remote_name}/${branch} revision in submodule path '\$sm_path'")"
760771
fi
@@ -819,7 +830,7 @@ Maybe you want to use 'update --init'?")"
819830
die "$(eval_gettext "Invalid update mode '$update_module' for submodule '$name'")"
820831
esac
821832

822-
if (clear_local_git_env; cd "$sm_path" && $command "$sha1")
833+
if (sanitize_submodule_env; cd "$sm_path" && $command "$sha1")
823834
then
824835
say "$say_msg"
825836
elif test -n "$must_die_on_failure"
@@ -835,7 +846,7 @@ Maybe you want to use 'update --init'?")"
835846
then
836847
(
837848
prefix="$prefix$sm_path/"
838-
clear_local_git_env
849+
sanitize_submodule_env
839850
cd "$sm_path" &&
840851
eval cmd_update
841852
)
@@ -873,7 +884,7 @@ Maybe you want to use 'update --init'?")"
873884

874885
set_name_rev () {
875886
revname=$( (
876-
clear_local_git_env
887+
sanitize_submodule_env
877888
cd "$1" && {
878889
git describe "$2" 2>/dev/null ||
879890
git describe --tags "$2" 2>/dev/null ||
@@ -1157,7 +1168,7 @@ cmd_status()
11571168
else
11581169
if test -z "$cached"
11591170
then
1160-
sha1=$(clear_local_git_env; cd "$sm_path" && git rev-parse --verify HEAD)
1171+
sha1=$(sanitize_submodule_env; cd "$sm_path" && git rev-parse --verify HEAD)
11611172
fi
11621173
set_name_rev "$sm_path" "$sha1"
11631174
say "+$sha1 $displaypath$revname"
@@ -1167,7 +1178,7 @@ cmd_status()
11671178
then
11681179
(
11691180
prefix="$displaypath/"
1170-
clear_local_git_env
1181+
sanitize_submodule_env
11711182
wt_prefix=
11721183
cd "$sm_path" &&
11731184
eval cmd_status
@@ -1242,7 +1253,7 @@ cmd_sync()
12421253
if test -e "$sm_path"/.git
12431254
then
12441255
(
1245-
clear_local_git_env
1256+
sanitize_submodule_env
12461257
cd "$sm_path"
12471258
remote=$(get_default_remote)
12481259
git config remote."$remote".url "$sub_origin_url"

http-push.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ static void curl_setup_http(CURL *curl, const char *url,
211211
static struct curl_slist *get_dav_token_headers(struct remote_lock *lock, enum dav_header_flag options)
212212
{
213213
struct strbuf buf = STRBUF_INIT;
214-
struct curl_slist *dav_headers = NULL;
214+
struct curl_slist *dav_headers = http_copy_default_headers();
215215

216216
if (options & DAV_HEADER_IF) {
217217
strbuf_addf(&buf, "If: (<%s>)", lock->token);
@@ -417,7 +417,7 @@ static void start_put(struct transfer_request *request)
417417
static void start_move(struct transfer_request *request)
418418
{
419419
struct active_request_slot *slot;
420-
struct curl_slist *dav_headers = NULL;
420+
struct curl_slist *dav_headers = http_copy_default_headers();
421421

422422
slot = get_active_slot();
423423
slot->callback_func = process_response;
@@ -845,7 +845,7 @@ static struct remote_lock *lock_remote(const char *path, long timeout)
845845
char *ep;
846846
char timeout_header[25];
847847
struct remote_lock *lock = NULL;
848-
struct curl_slist *dav_headers = NULL;
848+
struct curl_slist *dav_headers = http_copy_default_headers();
849849
struct xml_ctx ctx;
850850
char *escaped;
851851

@@ -1126,7 +1126,7 @@ static void remote_ls(const char *path, int flags,
11261126
struct slot_results results;
11271127
struct strbuf in_buffer = STRBUF_INIT;
11281128
struct buffer out_buffer = { STRBUF_INIT, 0 };
1129-
struct curl_slist *dav_headers = NULL;
1129+
struct curl_slist *dav_headers = http_copy_default_headers();
11301130
struct xml_ctx ctx;
11311131
struct remote_ls_ctx ls;
11321132

@@ -1204,7 +1204,7 @@ static int locking_available(void)
12041204
struct slot_results results;
12051205
struct strbuf in_buffer = STRBUF_INIT;
12061206
struct buffer out_buffer = { STRBUF_INIT, 0 };
1207-
struct curl_slist *dav_headers = NULL;
1207+
struct curl_slist *dav_headers = http_copy_default_headers();
12081208
struct xml_ctx ctx;
12091209
int lock_flags = 0;
12101210
char *escaped;

http.c

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ static unsigned long http_auth_methods = CURLAUTH_ANY;
114114

115115
static struct curl_slist *pragma_header;
116116
static struct curl_slist *no_pragma_header;
117+
static struct curl_slist *extra_http_headers;
117118

118119
static struct active_request_slot *active_queue_head;
119120

@@ -323,6 +324,19 @@ static int http_options(const char *var, const char *value, void *cb)
323324
#endif
324325
}
325326

327+
if (!strcmp("http.extraheader", var)) {
328+
if (!value) {
329+
return config_error_nonbool(var);
330+
} else if (!*value) {
331+
curl_slist_free_all(extra_http_headers);
332+
extra_http_headers = NULL;
333+
} else {
334+
extra_http_headers =
335+
curl_slist_append(extra_http_headers, value);
336+
}
337+
return 0;
338+
}
339+
326340
/* Fall back on the default ones */
327341
return git_default_config(var, value, cb);
328342
}
@@ -678,8 +692,10 @@ void http_init(struct remote *remote, const char *url, int proactive_auth)
678692
if (remote)
679693
var_override(&http_proxy_authmethod, remote->http_proxy_authmethod);
680694

681-
pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache");
682-
no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
695+
pragma_header = curl_slist_append(http_copy_default_headers(),
696+
"Pragma: no-cache");
697+
no_pragma_header = curl_slist_append(http_copy_default_headers(),
698+
"Pragma:");
683699

684700
#ifdef USE_CURL_MULTI
685701
{
@@ -765,6 +781,9 @@ void http_cleanup(void)
765781
#endif
766782
curl_global_cleanup();
767783

784+
curl_slist_free_all(extra_http_headers);
785+
extra_http_headers = NULL;
786+
768787
curl_slist_free_all(pragma_header);
769788
pragma_header = NULL;
770789

@@ -1163,6 +1182,16 @@ int run_one_slot(struct active_request_slot *slot,
11631182
return handle_curl_result(results);
11641183
}
11651184

1185+
struct curl_slist *http_copy_default_headers(void)
1186+
{
1187+
struct curl_slist *headers = NULL, *h;
1188+
1189+
for (h = extra_http_headers; h; h = h->next)
1190+
headers = curl_slist_append(headers, h->data);
1191+
1192+
return headers;
1193+
}
1194+
11661195
static CURLcode curlinfo_strbuf(CURL *curl, CURLINFO info, struct strbuf *buf)
11671196
{
11681197
char *ptr;
@@ -1380,7 +1409,7 @@ static int http_request(const char *url,
13801409
{
13811410
struct active_request_slot *slot;
13821411
struct slot_results results;
1383-
struct curl_slist *headers = NULL;
1412+
struct curl_slist *headers = http_copy_default_headers();
13841413
struct strbuf buf = STRBUF_INIT;
13851414
const char *accept_language;
13861415
int ret;

http.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ extern void step_active_slots(void);
106106
extern void http_init(struct remote *remote, const char *url,
107107
int proactive_auth);
108108
extern void http_cleanup(void);
109+
extern struct curl_slist *http_copy_default_headers(void);
109110

110111
extern long int git_curl_ipresolve;
111112
extern int active_requests;

quote.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,19 @@ void sq_quote_buf(struct strbuf *dst, const char *src)
4343
free(to_free);
4444
}
4545

46+
void sq_quotef(struct strbuf *dst, const char *fmt, ...)
47+
{
48+
struct strbuf src = STRBUF_INIT;
49+
50+
va_list ap;
51+
va_start(ap, fmt);
52+
strbuf_vaddf(&src, fmt, ap);
53+
va_end(ap);
54+
55+
sq_quote_buf(dst, src.buf);
56+
strbuf_release(&src);
57+
}
58+
4659
void sq_quote_argv(struct strbuf *dst, const char** argv, size_t maxlen)
4760
{
4861
int i;

quote.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@ struct strbuf;
2525
* sq_quote_buf() writes to an existing buffer of specified size; it
2626
* will return the number of characters that would have been written
2727
* excluding the final null regardless of the buffer size.
28+
*
29+
* sq_quotef() quotes the entire formatted string as a single result.
2830
*/
2931

3032
extern void sq_quote_buf(struct strbuf *, const char *src);
3133
extern void sq_quote_argv(struct strbuf *, const char **argv, size_t maxlen);
34+
extern void sq_quotef(struct strbuf *, const char *fmt, ...);
3235

3336
/* This unwraps what sq_quote() produces in place, but returns
3437
* NULL if the input does not look like what sq_quote would have

remote-curl.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ static int run_slot(struct active_request_slot *slot,
474474
static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
475475
{
476476
struct active_request_slot *slot;
477-
struct curl_slist *headers = NULL;
477+
struct curl_slist *headers = http_copy_default_headers();
478478
struct strbuf buf = STRBUF_INIT;
479479
int err;
480480

@@ -503,7 +503,7 @@ static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
503503
static int post_rpc(struct rpc_state *rpc)
504504
{
505505
struct active_request_slot *slot;
506-
struct curl_slist *headers = NULL;
506+
struct curl_slist *headers = http_copy_default_headers();
507507
int use_gzip = rpc->gzip_request;
508508
char *gzip_body = NULL;
509509
size_t gzip_size = 0;

0 commit comments

Comments
 (0)