Merge branch 'bw/push-options-recursively-to-submodules'

"git push --recurse-submodules --push-option=<string>" learned to
propagate the push option recursively down to pushes in submodules.

* bw/push-options-recursively-to-submodules:
  push: propagate remote and refspec with --recurse-submodules
  submodule--helper: add push-check subcommand
  remote: expose parse_push_refspec function
  push: propagate push-options with --recurse-submodules
  push: unmark a local variable as static
This commit is contained in:
Junio C Hamano 2017-04-19 21:37:14 -07:00
commit 872e2cf00a
9 changed files with 219 additions and 9 deletions

View File

@ -510,8 +510,8 @@ int cmd_push(int argc, const char **argv, const char *prefix)
int push_cert = -1;
int rc;
const char *repo = NULL; /* default repository */
static struct string_list push_options = STRING_LIST_INIT_DUP;
static struct string_list_item *item;
struct string_list push_options = STRING_LIST_INIT_DUP;
const struct string_list_item *item;
struct option options[] = {
OPT__VERBOSITY(&verbosity),
@ -584,6 +584,7 @@ int cmd_push(int argc, const char **argv, const char *prefix)
die(_("push options must not have new line characters"));
rc = do_push(repo, flags, &push_options);
string_list_clear(&push_options, 0);
if (rc == -1)
usage_with_options(push_usage, options);
else

View File

@ -1105,6 +1105,50 @@ static int resolve_remote_submodule_branch(int argc, const char **argv,
return 0;
}
static int push_check(int argc, const char **argv, const char *prefix)
{
struct remote *remote;
if (argc < 2)
die("submodule--helper push-check requires at least 1 argument");
/*
* The remote must be configured.
* This is to avoid pushing to the exact same URL as the parent.
*/
remote = pushremote_get(argv[1]);
if (!remote || remote->origin == REMOTE_UNCONFIGURED)
die("remote '%s' not configured", argv[1]);
/* Check the refspec */
if (argc > 2) {
int i, refspec_nr = argc - 2;
struct ref *local_refs = get_local_heads();
struct refspec *refspec = parse_push_refspec(refspec_nr,
argv + 2);
for (i = 0; i < refspec_nr; i++) {
struct refspec *rs = refspec + i;
if (rs->pattern || rs->matching)
continue;
/*
* LHS must match a single ref
* NEEDSWORK: add logic to special case 'HEAD' once
* working with submodules in a detached head state
* ceases to be the norm.
*/
if (count_refspec_match(rs->src, local_refs, NULL) != 1)
die("src refspec '%s' must name a ref",
rs->src);
}
free_refspec(refspec_nr, refspec);
}
return 0;
}
static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
{
int i;
@ -1170,6 +1214,7 @@ static struct cmd_struct commands[] = {
{"resolve-relative-url-test", resolve_relative_url_test, 0},
{"init", module_init, SUPPORT_SUPER_PREFIX},
{"remote-branch", resolve_remote_submodule_branch, 0},
{"push-check", push_check, 0},
{"absorb-git-dirs", absorb_git_dirs, SUPPORT_SUPER_PREFIX},
{"is-active", is_active, 0},
};

View File

@ -630,7 +630,7 @@ struct refspec *parse_fetch_refspec(int nr_refspec, const char **refspec)
return parse_refspec_internal(nr_refspec, refspec, 1, 0);
}
static struct refspec *parse_push_refspec(int nr_refspec, const char **refspec)
struct refspec *parse_push_refspec(int nr_refspec, const char **refspec)
{
return parse_refspec_internal(nr_refspec, refspec, 0, 0);
}

View File

@ -169,6 +169,7 @@ struct ref *ref_remove_duplicates(struct ref *ref_map);
int valid_fetch_refspec(const char *refspec);
struct refspec *parse_fetch_refspec(int nr_refspec, const char **refspec);
extern struct refspec *parse_push_refspec(int nr_refspec, const char **refspec);
void free_refspec(int nr_refspec, struct refspec *refspec);

View File

@ -14,6 +14,7 @@
#include "blob.h"
#include "thread-utils.h"
#include "quote.h"
#include "remote.h"
#include "worktree.h"
static int config_fetch_recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND;
@ -783,7 +784,11 @@ int find_unpushed_submodules(struct oid_array *commits,
return needs_pushing->nr;
}
static int push_submodule(const char *path, int dry_run)
static int push_submodule(const char *path,
const struct remote *remote,
const char **refspec, int refspec_nr,
const struct string_list *push_options,
int dry_run)
{
if (add_submodule_odb(path))
return 1;
@ -794,6 +799,20 @@ static int push_submodule(const char *path, int dry_run)
if (dry_run)
argv_array_push(&cp.args, "--dry-run");
if (push_options && push_options->nr) {
const struct string_list_item *item;
for_each_string_list_item(item, push_options)
argv_array_pushf(&cp.args, "--push-option=%s",
item->string);
}
if (remote->origin != REMOTE_UNCONFIGURED) {
int i;
argv_array_push(&cp.args, remote->name);
for (i = 0; i < refspec_nr; i++)
argv_array_push(&cp.args, refspec[i]);
}
prepare_submodule_repo_env(&cp.env_array);
cp.git_cmd = 1;
cp.no_stdin = 1;
@ -806,20 +825,67 @@ static int push_submodule(const char *path, int dry_run)
return 1;
}
/*
* Perform a check in the submodule to see if the remote and refspec work.
* Die if the submodule can't be pushed.
*/
static void submodule_push_check(const char *path, const struct remote *remote,
const char **refspec, int refspec_nr)
{
struct child_process cp = CHILD_PROCESS_INIT;
int i;
argv_array_push(&cp.args, "submodule--helper");
argv_array_push(&cp.args, "push-check");
argv_array_push(&cp.args, remote->name);
for (i = 0; i < refspec_nr; i++)
argv_array_push(&cp.args, refspec[i]);
prepare_submodule_repo_env(&cp.env_array);
cp.git_cmd = 1;
cp.no_stdin = 1;
cp.no_stdout = 1;
cp.dir = path;
/*
* Simply indicate if 'submodule--helper push-check' failed.
* More detailed error information will be provided by the
* child process.
*/
if (run_command(&cp))
die("process for submodule '%s' failed", path);
}
int push_unpushed_submodules(struct oid_array *commits,
const char *remotes_name,
const struct remote *remote,
const char **refspec, int refspec_nr,
const struct string_list *push_options,
int dry_run)
{
int i, ret = 1;
struct string_list needs_pushing = STRING_LIST_INIT_DUP;
if (!find_unpushed_submodules(commits, remotes_name, &needs_pushing))
if (!find_unpushed_submodules(commits, remote->name, &needs_pushing))
return 1;
/*
* Verify that the remote and refspec can be propagated to all
* submodules. This check can be skipped if the remote and refspec
* won't be propagated due to the remote being unconfigured (e.g. a URL
* instead of a remote name).
*/
if (remote->origin != REMOTE_UNCONFIGURED)
for (i = 0; i < needs_pushing.nr; i++)
submodule_push_check(needs_pushing.items[i].string,
remote, refspec, refspec_nr);
/* Actually push the submodules */
for (i = 0; i < needs_pushing.nr; i++) {
const char *path = needs_pushing.items[i].string;
fprintf(stderr, "Pushing submodule '%s'\n", path);
if (!push_submodule(path, dry_run)) {
if (!push_submodule(path, remote, refspec, refspec_nr,
push_options, dry_run)) {
fprintf(stderr, "Unable to push submodule '%s'\n", path);
ret = 0;
}

View File

@ -4,6 +4,7 @@
struct diff_options;
struct argv_array;
struct oid_array;
struct remote;
enum {
RECURSE_SUBMODULES_ONLY = -5,
@ -91,7 +92,9 @@ extern int find_unpushed_submodules(struct oid_array *commits,
const char *remotes_name,
struct string_list *needs_pushing);
extern int push_unpushed_submodules(struct oid_array *commits,
const char *remotes_name,
const struct remote *remote,
const char **refspec, int refspec_nr,
const struct string_list *push_options,
int dry_run);
extern void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir);
extern int parallel_submodules(void);

View File

@ -475,4 +475,56 @@ test_expect_success 'push only unpushed submodules recursively' '
test_cmp expected_pub actual_pub
'
test_expect_success 'push propagating the remotes name to a submodule' '
git -C work remote add origin ../pub.git &&
git -C work remote add pub ../pub.git &&
> work/gar/bage/junk10 &&
git -C work/gar/bage add junk10 &&
git -C work/gar/bage commit -m "Tenth junk" &&
git -C work add gar/bage &&
git -C work commit -m "Tenth junk added to gar/bage" &&
# Fails when submodule does not have a matching remote
test_must_fail git -C work push --recurse-submodules=on-demand pub master &&
# Succeeds when submodules has matching remote and refspec
git -C work push --recurse-submodules=on-demand origin master &&
git -C submodule.git rev-parse master >actual_submodule &&
git -C pub.git rev-parse master >actual_pub &&
git -C work/gar/bage rev-parse master >expected_submodule &&
git -C work rev-parse master >expected_pub &&
test_cmp expected_submodule actual_submodule &&
test_cmp expected_pub actual_pub
'
test_expect_success 'push propagating refspec to a submodule' '
> work/gar/bage/junk11 &&
git -C work/gar/bage add junk11 &&
git -C work/gar/bage commit -m "Eleventh junk" &&
git -C work checkout branch2 &&
git -C work add gar/bage &&
git -C work commit -m "updating gar/bage in branch2" &&
# Fails when submodule does not have a matching branch
test_must_fail git -C work push --recurse-submodules=on-demand origin branch2 &&
# Fails when refspec includes an object id
test_must_fail git -C work push --recurse-submodules=on-demand origin \
"$(git -C work rev-parse branch2):refs/heads/branch2" &&
# Fails when refspec includes 'HEAD' as it is unsupported at this time
test_must_fail git -C work push --recurse-submodules=on-demand origin \
HEAD:refs/heads/branch2 &&
git -C work/gar/bage branch branch2 master &&
git -C work push --recurse-submodules=on-demand origin branch2 &&
git -C submodule.git rev-parse branch2 >actual_submodule &&
git -C pub.git rev-parse branch2 >actual_pub &&
git -C work/gar/bage rev-parse branch2 >expected_submodule &&
git -C work rev-parse branch2 >expected_pub &&
test_cmp expected_submodule actual_submodule &&
test_cmp expected_pub actual_pub
'
test_done

View File

@ -142,6 +142,46 @@ test_expect_success 'push options work properly across http' '
test_cmp expect actual
'
test_expect_success 'push options and submodules' '
test_when_finished "rm -rf parent" &&
test_when_finished "rm -rf parent_upstream" &&
mk_repo_pair &&
git -C upstream config receive.advertisePushOptions true &&
cp -r upstream parent_upstream &&
test_commit -C upstream one &&
test_create_repo parent &&
git -C parent remote add up ../parent_upstream &&
test_commit -C parent one &&
git -C parent push --mirror up &&
git -C parent submodule add ../upstream workbench &&
git -C parent/workbench remote add up ../../upstream &&
git -C parent commit -m "add submoule" &&
test_commit -C parent/workbench two &&
git -C parent add workbench &&
git -C parent commit -m "update workbench" &&
git -C parent push \
--push-option=asdf --push-option="more structured text" \
--recurse-submodules=on-demand up master &&
git -C upstream rev-parse --verify master >expect &&
git -C parent/workbench rev-parse --verify master >actual &&
test_cmp expect actual &&
git -C parent_upstream rev-parse --verify master >expect &&
git -C parent rev-parse --verify master >actual &&
test_cmp expect actual &&
printf "asdf\nmore structured text\n" >expect &&
test_cmp expect upstream/.git/hooks/pre-receive.push_options &&
test_cmp expect upstream/.git/hooks/post-receive.push_options &&
test_cmp expect parent_upstream/.git/hooks/pre-receive.push_options &&
test_cmp expect parent_upstream/.git/hooks/post-receive.push_options
'
stop_httpd
test_done

View File

@ -1031,7 +1031,9 @@ int transport_push(struct transport *transport,
&ref->new_oid);
if (!push_unpushed_submodules(&commits,
transport->remote->name,
transport->remote,
refspec, refspec_nr,
transport->push_options,
pretend)) {
oid_array_clear(&commits);
die("Failed to push all needed submodules!");