push: fix --dry-run to not push submodules

Teach push to respect the --dry-run option when configured to recursively push submodules 'on-demand'. This is done by passing the --dry-run flag to the child process which performs a push for a submodules when performing a dry-run. In order to preserve good user experience, the additional check for unpushed submodules is skipped during a dry-run when --recurse-submodules=on-demand. The check is skipped because the submodule pushes were performed as dry-runs and this check would always fail as the submodules would still need to be pushed. Signed-off-by: Brandon Williams <bmwill@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Brandon Williams committed Nov 17, 2016 at 10:46 UTC 0301c821c5cd124733accfbff0ddbf7f0b0ee9fb
4 files changed +18 -10
submodule.c
+8 -5
@@ -683,16 +683,17 @@ int find_unpushed_submodules(struct sha1_array *commits,
683 return needs_pushing->nr;
684 }
685
686 -static int push_submodule(const char *path)
686 +static int push_submodule(const char *path, int dry_run)
687 {
688 if (add_submodule_odb(path))
689 return 1;
690
691 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) {
692 struct child_process cp = CHILD_PROCESS_INIT;
693 - const char *argv[] = {"push", NULL};
693 + argv_array_push(&cp.args, "push");
694 + if (dry_run)
695 + argv_array_push(&cp.args, "--dry-run");
696
695 - cp.argv = argv;
697 prepare_submodule_repo_env(&cp.env_array);
698 cp.git_cmd = 1;
699 cp.no_stdin = 1;
@@ -705,7 +706,9 @@ static int push_submodule(const char *path)
706 return 1;
707 }
708
708 -int push_unpushed_submodules(struct sha1_array *commits, const char *remotes_name)
709 +int push_unpushed_submodules(struct sha1_array *commits,
710 + const char *remotes_name,
711 + int dry_run)
712 {
713 int i, ret = 1;
714 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
@@ -716,7 +719,7 @@ int push_unpushed_submodules(struct sha1_array *commits, const char *remotes_nam
719 for (i = 0; i < needs_pushing.nr; i++) {
720 const char *path = needs_pushing.items[i].string;
721 fprintf(stderr, "Pushing submodule '%s'\n", path);
719 - if (!push_submodule(path)) {
722 + if (!push_submodule(path, dry_run)) {
723 fprintf(stderr, "Unable to push submodule '%s'\n", path);
724 ret = 0;
725 }
submodule.h
+3 -1
@@ -65,7 +65,9 @@ int merge_submodule(unsigned char result[20], const char *path, const unsigned c
65 const unsigned char a[20], const unsigned char b[20], int search);
66 int find_unpushed_submodules(struct sha1_array *commits, const char *remotes_name,
67 struct string_list *needs_pushing);
68 -int push_unpushed_submodules(struct sha1_array *commits, const char *remotes_name);
68 +extern int push_unpushed_submodules(struct sha1_array *commits,
69 + const char *remotes_name,
70 + int dry_run);
71 void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir);
72 int parallel_submodules(void);
73
t/t5531-deep-submodule-push.sh
+1 -1
@@ -431,7 +431,7 @@ test_expect_success 'push unpushable submodule recursively fails' '
431 test_cmp expected actual
432 '
433
434 -test_expect_failure 'push --dry-run does not recursively update submodules' '
434 +test_expect_success 'push --dry-run does not recursively update submodules' '
435 (
436 cd work/gar/bage &&
437 git checkout master &&
transport.c
+6 -3
@@ -921,15 +921,18 @@ int transport_push(struct transport *transport,
921 if (!is_null_oid(&ref->new_oid))
922 sha1_array_append(&commits, ref->new_oid.hash);
923
924 - if (!push_unpushed_submodules(&commits, transport->remote->name)) {
924 + if (!push_unpushed_submodules(&commits,
925 + transport->remote->name,
926 + pretend)) {
927 sha1_array_clear(&commits);
928 die("Failed to push all needed submodules!");
929 }
930 sha1_array_clear(&commits);
931 }
932
931 - if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
932 - TRANSPORT_RECURSE_SUBMODULES_CHECK)) && !is_bare_repository()) {
933 + if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) ||
934 + ((flags & TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND) &&
935 + !pretend)) && !is_bare_repository()) {
936 struct ref *ref = remote_refs;
937 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
938 struct sha1_array commits = SHA1_ARRAY_INIT;