Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch 'jk/submodule-c-credential'
"git -c credential.<var>=<value> submodule" can now be used to
propagate configuration variables related to credential helper
down to the submodules.

* jk/submodule-c-credential:
  git_config_push_parameter: handle empty GIT_CONFIG_PARAMETERS
  git: submodule honor -c credential.* from command line
  quote: implement sq_quotef()
  submodule: fix segmentation fault in submodule--helper clone
  submodule: fix submodule--helper clone usage
  submodule: check argc count for git submodule--helper clone
  submodule: don't pass empty string arguments to submodule--helper clone
  • Loading branch information
Junio C Hamano committed Apr 6, 2016
2 parents aad627e + d1f8849 commit 01e1d54
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 19 deletions.
76 changes: 73 additions & 3 deletions builtin/submodule--helper.c
Expand Up @@ -118,6 +118,55 @@ static int module_name(int argc, const char **argv, const char *prefix)

return 0;
}

/*
* Rules to sanitize configuration variables that are Ok to be passed into
* submodule operations from the parent project using "-c". Should only
* include keys which are both (a) safe and (b) necessary for proper
* operation.
*/
static int submodule_config_ok(const char *var)
{
if (starts_with(var, "credential."))
return 1;
return 0;
}

static int sanitize_submodule_config(const char *var, const char *value, void *data)
{
struct strbuf *out = data;

if (submodule_config_ok(var)) {
if (out->len)
strbuf_addch(out, ' ');

if (value)
sq_quotef(out, "%s=%s", var, value);
else
sq_quote_buf(out, var);
}

return 0;
}

static void prepare_submodule_repo_env(struct argv_array *out)
{
const char * const *var;

for (var = local_repo_env; *var; var++) {
if (!strcmp(*var, CONFIG_DATA_ENVIRONMENT)) {
struct strbuf sanitized_config = STRBUF_INIT;
git_config_from_parameters(sanitize_submodule_config,
&sanitized_config);
argv_array_pushf(out, "%s=%s", *var, sanitized_config.buf);
strbuf_release(&sanitized_config);
} else {
argv_array_push(out, *var);
}
}

}

static int clone_submodule(const char *path, const char *gitdir, const char *url,
const char *depth, const char *reference, int quiet)
{
Expand All @@ -139,7 +188,7 @@ static int clone_submodule(const char *path, const char *gitdir, const char *url
argv_array_push(&cp.args, path);

cp.git_cmd = 1;
cp.env = local_repo_env;
prepare_submodule_repo_env(&cp.env_array);
cp.no_stdin = 1;

return run_command(&cp);
Expand Down Expand Up @@ -180,14 +229,18 @@ static int module_clone(int argc, const char **argv, const char *prefix)

const char *const git_submodule_helper_usage[] = {
N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
"[--reference <repository>] [--name <name>] [--url <url>]"
"[--depth <depth>] [--] [<path>...]"),
"[--reference <repository>] [--name <name>] [--depth <depth>] "
"--url <url> --path <path>"),
NULL
};

argc = parse_options(argc, argv, prefix, module_clone_options,
git_submodule_helper_usage, 0);

if (argc || !url || !path)
usage_with_options(git_submodule_helper_usage,
module_clone_options);

strbuf_addf(&sb, "%s/modules/%s", get_git_dir(), name);
sm_gitdir = strbuf_detach(&sb, NULL);

Expand Down Expand Up @@ -249,6 +302,22 @@ static int module_clone(int argc, const char **argv, const char *prefix)
return 0;
}

static int module_sanitize_config(int argc, const char **argv, const char *prefix)
{
struct strbuf sanitized_config = STRBUF_INIT;

if (argc > 1)
usage(_("git submodule--helper sanitize-config"));

git_config_from_parameters(sanitize_submodule_config, &sanitized_config);
if (sanitized_config.len)
printf("%s\n", sanitized_config.buf);

strbuf_release(&sanitized_config);

return 0;
}

struct submodule_update_clone {
/* index into 'list', the list of submodules to look into for cloning */
int current;
Expand Down Expand Up @@ -509,6 +578,7 @@ static struct cmd_struct commands[] = {
{"list", module_list},
{"name", module_name},
{"clone", module_clone},
{"sanitize-config", module_sanitize_config},
{"update-clone", update_clone}
};

Expand Down
2 changes: 1 addition & 1 deletion config.c
Expand Up @@ -162,7 +162,7 @@ void git_config_push_parameter(const char *text)
{
struct strbuf env = STRBUF_INIT;
const char *old = getenv(CONFIG_DATA_ENVIRONMENT);
if (old) {
if (old && *old) {
strbuf_addstr(&env, old);
strbuf_addch(&env, ' ');
}
Expand Down
40 changes: 25 additions & 15 deletions git-submodule.sh
Expand Up @@ -192,6 +192,16 @@ isnumber()
n=$(($1 + 0)) 2>/dev/null && test "$n" = "$1"
}

# Sanitize the local git environment for use within a submodule. We
# can't simply use clear_local_git_env since we want to preserve some
# of the settings from GIT_CONFIG_PARAMETERS.
sanitize_submodule_env()
{
sanitized_config=$(git submodule--helper sanitize-config)
clear_local_git_env
GIT_CONFIG_PARAMETERS=$sanitized_config
}

#
# Add a new submodule to the working tree, .gitmodules and the index
#
Expand Down Expand Up @@ -347,9 +357,9 @@ Use -f if you really want to add it." >&2
echo "$(eval_gettext "Reactivating local git directory for submodule '\$sm_name'.")"
fi
fi
git submodule--helper clone ${GIT_QUIET:+--quiet} --prefix "$wt_prefix" --path "$sm_path" --name "$sm_name" --url "$realrepo" "$reference" "$depth" || exit
git submodule--helper clone ${GIT_QUIET:+--quiet} --prefix "$wt_prefix" --path "$sm_path" --name "$sm_name" --url "$realrepo" ${reference:+"$reference"} ${depth:+"$depth"} || exit
(
clear_local_git_env
sanitize_submodule_env
cd "$sm_path" &&
# ash fails to wordsplit ${branch:+-b "$branch"...}
case "$branch" in
Expand Down Expand Up @@ -418,7 +428,7 @@ cmd_foreach()
name=$(git submodule--helper name "$sm_path")
(
prefix="$prefix$sm_path/"
clear_local_git_env
sanitize_submodule_env
cd "$sm_path" &&
sm_path=$(relative_path "$sm_path") &&
# we make $path available to scripts ...
Expand Down Expand Up @@ -592,14 +602,14 @@ cmd_deinit()
}

is_tip_reachable () (
clear_local_git_env
sanitize_submodule_env &&
cd "$1" &&
rev=$(git rev-list -n 1 "$2" --not --all 2>/dev/null) &&
test -z "$rev"
)

fetch_in_submodule () (
clear_local_git_env
sanitize_submodule_env &&
cd "$1" &&
case "$2" in
'')
Expand Down Expand Up @@ -726,7 +736,7 @@ cmd_update()
subsha1=
update_module=checkout
else
subsha1=$(clear_local_git_env; cd "$sm_path" &&
subsha1=$(sanitize_submodule_env; cd "$sm_path" &&
git rev-parse --verify HEAD) ||
die "$(eval_gettext "Unable to find current revision in submodule path '\$displaypath'")"
fi
Expand All @@ -736,11 +746,11 @@ cmd_update()
if test -z "$nofetch"
then
# Fetch remote before determining tracking $sha1
(clear_local_git_env; cd "$sm_path" && git-fetch) ||
(sanitize_submodule_env; cd "$sm_path" && git-fetch) ||
die "$(eval_gettext "Unable to fetch in submodule path '\$sm_path'")"
fi
remote_name=$(clear_local_git_env; cd "$sm_path" && get_default_remote)
sha1=$(clear_local_git_env; cd "$sm_path" &&
remote_name=$(sanitize_submodule_env; cd "$sm_path" && get_default_remote)
sha1=$(sanitize_submodule_env; cd "$sm_path" &&
git rev-parse --verify "${remote_name}/${branch}") ||
die "$(eval_gettext "Unable to find current ${remote_name}/${branch} revision in submodule path '\$sm_path'")"
fi
Expand Down Expand Up @@ -798,7 +808,7 @@ cmd_update()
die "$(eval_gettext "Invalid update mode '$update_module' for submodule '$name'")"
esac

if (clear_local_git_env; cd "$sm_path" && $command "$sha1")
if (sanitize_submodule_env; cd "$sm_path" && $command "$sha1")
then
say "$say_msg"
elif test -n "$must_die_on_failure"
Expand All @@ -814,7 +824,7 @@ cmd_update()
then
(
prefix="$prefix$sm_path/"
clear_local_git_env
sanitize_submodule_env
cd "$sm_path" &&
eval cmd_update
)
Expand Down Expand Up @@ -852,7 +862,7 @@ cmd_update()

set_name_rev () {
revname=$( (
clear_local_git_env
sanitize_submodule_env
cd "$1" && {
git describe "$2" 2>/dev/null ||
git describe --tags "$2" 2>/dev/null ||
Expand Down Expand Up @@ -1136,7 +1146,7 @@ cmd_status()
else
if test -z "$cached"
then
sha1=$(clear_local_git_env; cd "$sm_path" && git rev-parse --verify HEAD)
sha1=$(sanitize_submodule_env; cd "$sm_path" && git rev-parse --verify HEAD)
fi
set_name_rev "$sm_path" "$sha1"
say "+$sha1 $displaypath$revname"
Expand All @@ -1146,7 +1156,7 @@ cmd_status()
then
(
prefix="$displaypath/"
clear_local_git_env
sanitize_submodule_env
cd "$sm_path" &&
eval cmd_status
) ||
Expand Down Expand Up @@ -1220,7 +1230,7 @@ cmd_sync()
if test -e "$sm_path"/.git
then
(
clear_local_git_env
sanitize_submodule_env
cd "$sm_path"
remote=$(get_default_remote)
git config remote."$remote".url "$sub_origin_url"
Expand Down
13 changes: 13 additions & 0 deletions quote.c
Expand Up @@ -43,6 +43,19 @@ void sq_quote_buf(struct strbuf *dst, const char *src)
free(to_free);
}

void sq_quotef(struct strbuf *dst, const char *fmt, ...)
{
struct strbuf src = STRBUF_INIT;

va_list ap;
va_start(ap, fmt);
strbuf_vaddf(&src, fmt, ap);
va_end(ap);

sq_quote_buf(dst, src.buf);
strbuf_release(&src);
}

void sq_quote_argv(struct strbuf *dst, const char** argv, size_t maxlen)
{
int i;
Expand Down
3 changes: 3 additions & 0 deletions quote.h
Expand Up @@ -25,10 +25,13 @@ struct strbuf;
* sq_quote_buf() writes to an existing buffer of specified size; it
* will return the number of characters that would have been written
* excluding the final null regardless of the buffer size.
*
* sq_quotef() quotes the entire formatted string as a single result.
*/

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

/* This unwraps what sq_quote() produces in place, but returns
* NULL if the input does not look like what sq_quote would have
Expand Down
14 changes: 14 additions & 0 deletions t/t1300-repo-config.sh
Expand Up @@ -1087,6 +1087,20 @@ test_expect_success 'git -c complains about empty key and value' '
test_must_fail git -c "" rev-parse
'

test_expect_success 'multiple git -c appends config' '
test_config alias.x "!git -c x.two=2 config --get-regexp ^x\.*" &&
cat >expect <<-\EOF &&
x.one 1
x.two 2
EOF
git -c x.one=1 x >actual &&
test_cmp expect actual
'

test_expect_success 'git -c is not confused by empty environment' '
GIT_CONFIG_PARAMETERS="" git -c x.one=1 config --list
'

test_expect_success 'git config --edit works' '
git config -f tmp test.value no &&
echo test.value=yes >expect &&
Expand Down
17 changes: 17 additions & 0 deletions t/t5550-http-fetch-dumb.sh
Expand Up @@ -91,6 +91,23 @@ test_expect_success 'configured username does not override URL' '
expect_askpass pass user@host
'

test_expect_success 'cmdline credential config passes into submodules' '
git init super &&
set_askpass user@host pass@host &&
(
cd super &&
git submodule add "$HTTPD_URL/auth/dumb/repo.git" sub &&
git commit -m "add submodule"
) &&
set_askpass wrong pass@host &&
test_must_fail git clone --recursive super super-clone &&
rm -rf super-clone &&
set_askpass wrong pass@host &&
git -c "credential.$HTTP_URL.username=user@host" \
clone --recursive super super-clone &&
expect_askpass pass user@host
'

test_expect_success 'fetch changes via http' '
echo content >>file &&
git commit -a -m two &&
Expand Down
26 changes: 26 additions & 0 deletions t/t7412-submodule--helper.sh
@@ -0,0 +1,26 @@
#!/bin/sh
#
# Copyright (c) 2016 Jacob Keller
#

test_description='Basic plumbing support of submodule--helper
This test verifies the submodule--helper plumbing command used to implement
git-submodule.
'

. ./test-lib.sh

test_expect_success 'sanitize-config clears configuration' '
git -c user.name="Some User" submodule--helper sanitize-config >actual &&
test_must_be_empty actual
'

sq="'"
test_expect_success 'sanitize-config keeps credential.helper' '
git -c credential.helper=helper submodule--helper sanitize-config >actual &&
echo "${sq}credential.helper=helper${sq}" >expect &&
test_cmp expect actual
'

test_done

0 comments on commit 01e1d54

Please sign in to comment.