Merge branch 'bw/push-options-recursively-to-submodules'
authorJunio C Hamano <gitster@pobox.com>
Thu, 20 Apr 2017 04:37:14 +0000 (21:37 -0700)
committerJunio C Hamano <gitster@pobox.com>
Thu, 20 Apr 2017 04:37:14 +0000 (21:37 -0700)
"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

builtin/push.c
builtin/submodule--helper.c
remote.c
remote.h
submodule.c
submodule.h
t/t5531-deep-submodule-push.sh
t/t5545-push-options.sh
transport.c
index 5c22e9f2e56b7d8049890e44f4c6fb63b16f8357..a597759d8fac205f41c406d0a0e32f9240941190 100644 (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
index 85aafe46a463c651eb09d540ea9ffa0cfcd4a796..6ee962207c4e38f3135f26ff8e0fdca98fa81fa1 100644 (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},
 };
index af72727d760d37e511ba662148029c5d0eb52259..801137c72ebb2edf196811a8031db603c61f376d 100644 (file)
--- a/remote.c
+++ b/remote.c
@@ -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);
 }
index 949badc4eea89f32f6839233d658e457721b43af..6c28cd3e4bfe2e8be058485d0b963b23622b999f 100644 (file)
--- a/remote.h
+++ b/remote.h
@@ -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);
 
index 6368d5fa6afe001d38a63908a5a62cfe498ceaea..cdbecfbeaf8c16af192845c95477985e659bcd20 100644 (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;
                }
index a86f757a06720e1bc85d7e9ecad51ffbc6610dbe..486371d2c34a9397d9f075f052cbae774ad662e7 100644 (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);
index f55137f76ffac4c6f8333444b444aef65da8b3fd..57ba3226288cadbbe8af569d13217f5b2c17af90 100755 (executable)
@@ -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
index 97065e62b870c732e7c197dfda054775546289cd..f9232f5d0ffff996359a27d5727f645f8fcff6d3 100755 (executable)
@@ -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
index c83f3b71bebffee19daa88cfba76aa8d16938b2f..4d33138a7525310af3f9f73b699360f159516ad1 100644 (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!");