push: add option to push only submodules

Teach push the --recurse-submodules=only option. This enables push to recursively push all unpushed submodules while leaving the superproject unpushed. This is a desirable feature in a scenario where updates to the superproject are handled automatically by some other means, perhaps a tool like Gerrit code review. In this scenario, a developer could make a change which spans multiple submodules and then push their commits for code review. Upon completion of the code review, their commits can be accepted and applied to their respective submodules while the code review tool can then automatically update the superproject to the most recent SHA1 of each submodule. This would reduce the merge conflicts in the superproject that could occur if multiple people are contributing to the same submodule. Signed-off-by: Brandon Williams <bmwill@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Brandon Williams committed Dec 19, 2016 at 10:25 UTC 225e8bf778d21104da10cfb316e0e2898b24e809
4 files changed +35 -4
builtin/push.c
+2
@@ -565,6 +565,8 @@ int cmd_push(int argc, const char **argv, const char *prefix)
565 flags |= TRANSPORT_RECURSE_SUBMODULES_CHECK;
566 else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
567 flags |= TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND;
568 + else if (recurse_submodules == RECURSE_SUBMODULES_ONLY)
569 + flags |= TRANSPORT_RECURSE_SUBMODULES_ONLY;
570
571 if (tags)
572 add_refspec("refs/tags/*");
t/t5531-deep-submodule-push.sh
+21
@@ -454,4 +454,25 @@ test_expect_success 'push --dry-run does not recursively update submodules' '
454 test_cmp expected_submodule actual_submodule
455 '
456
457 +test_expect_success 'push --dry-run does not recursively update submodules' '
458 + git -C work push --dry-run --recurse-submodules=only ../pub.git master &&
459 +
460 + git -C submodule.git rev-parse master >actual_submodule &&
461 + git -C pub.git rev-parse master >actual_pub &&
462 + test_cmp expected_pub actual_pub &&
463 + test_cmp expected_submodule actual_submodule
464 +'
465 +
466 +test_expect_success 'push only unpushed submodules recursively' '
467 + git -C work/gar/bage rev-parse master >expected_submodule &&
468 + git -C pub.git rev-parse master >expected_pub &&
469 +
470 + git -C work push --recurse-submodules=only ../pub.git master &&
471 +
472 + git -C submodule.git rev-parse master >actual_submodule &&
473 + git -C pub.git rev-parse master >actual_pub &&
474 + test_cmp expected_submodule actual_submodule &&
475 + test_cmp expected_pub actual_pub
476 +'
477 +
478 test_done
transport.c
+11 -4
@@ -947,7 +947,9 @@ int transport_push(struct transport *transport,
947 if (run_pre_push_hook(transport, remote_refs))
948 return -1;
949
950 - if ((flags & TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND) && !is_bare_repository()) {
950 + if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
951 + TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
952 + !is_bare_repository()) {
953 struct ref *ref = remote_refs;
954 struct sha1_array commits = SHA1_ARRAY_INIT;
955
@@ -965,7 +967,8 @@ int transport_push(struct transport *transport,
967 }
968
969 if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) ||
968 - ((flags & TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND) &&
970 + ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND |
971 + TRANSPORT_RECURSE_SUBMODULES_ONLY)) &&
972 !pretend)) && !is_bare_repository()) {
973 struct ref *ref = remote_refs;
974 struct string_list needs_pushing = STRING_LIST_INIT_DUP;
@@ -984,7 +987,10 @@ int transport_push(struct transport *transport,
987 sha1_array_clear(&commits);
988 }
989
987 - push_ret = transport->push_refs(transport, remote_refs, flags);
990 + if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY))
991 + push_ret = transport->push_refs(transport, remote_refs, flags);
992 + else
993 + push_ret = 0;
994 err = push_had_errors(remote_refs);
995 ret = push_ret | err;
996
@@ -996,7 +1002,8 @@ int transport_push(struct transport *transport,
1002 if (flags & TRANSPORT_PUSH_SET_UPSTREAM)
1003 set_upstreams(transport, remote_refs, pretend);
1004
999 - if (!(flags & TRANSPORT_PUSH_DRY_RUN)) {
1005 + if (!(flags & (TRANSPORT_PUSH_DRY_RUN |
1006 + TRANSPORT_RECURSE_SUBMODULES_ONLY))) {
1007 struct ref *ref;
1008 for (ref = remote_refs; ref; ref = ref->next)
1009 transport_update_tracking_ref(transport->remote, ref, verbose);
transport.h
+1
@@ -146,6 +146,7 @@ struct transport {
146 #define TRANSPORT_PUSH_CERT_IF_ASKED (1<<12)
147 #define TRANSPORT_PUSH_ATOMIC (1<<13)
148 #define TRANSPORT_PUSH_OPTIONS (1<<14)
149 +#define TRANSPORT_RECURSE_SUBMODULES_ONLY (1<<15)
150
151 extern int transport_summary_width(const struct ref *refs);
152