threads / patch / 46244

patchsubmodule--helper: teach push-check to handle HEAD

Subject: [PATCH] submodule--helper: teach push-check to handle HEAD

## tl;dr

5 messages between Jun 23, 2017 and Jul 20, 2017. Diffs are folded; open one to read it.

replies: 4people: 3as markdown or json

Brandon Williams· Jun 23, 2017, 20:04 UTC · lore

In 06bf4ad1d (push: propagate remote and refspec with --recurse-submodules) push was taught how to propagate a refspec down to submodules when the '--recurse-submodules' flag is given. The only refspecs that are allowed to be propagated are ones which name a ref which exists in both the superproject and the submodule, with the caveat that 'HEAD' was disallowed.

This patch teaches push-check (the submodule helper which determines if a refspec can be propagated to a submodule) to permit propagating 'HEAD' if and only if the superproject and the submodule both have the same named branch checked out and the submodule is not in a detached head state.

Signed-off-by: Brandon Williams <bmwill@google.com>
---
 builtin/submodule--helper.c    | 57 +++++++++++++++++++++++++++++++-----------
 submodule.c                    | 18 ++++++++++---
 t/t5531-deep-submodule-push.sh | 25 +++++++++++++++++-
 3 files changed, 82 insertions(+), 18 deletions(-)
Show changes to 3 files +82 −18

builtin/submodule--helper.c, submodule.c, t/t5531-deep-submodule-push.sh

diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index 1b4d2b346..fd5020036 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -1107,24 +1107,41 @@ static int resolve_remote_submodule_branch(int argc, const char **argv,
 static int push_check(int argc, const char **argv, const char *prefix)
 {
 	struct remote *remote;
+	const char *superproject_head;
+	char *head;
+	int detached_head = 0;
+	struct object_id head_oid;
 
-	if (argc < 2)
-		die("submodule--helper push-check requires at least 1 argument");
+	if (argc < 3)
+		die("submodule--helper push-check requires at least 2 argument");
+
+	/*
+	 * superproject's resolved head ref.
+	 * if HEAD then the superproject is in a detached head state, otherwise
+	 * it will be the resolved head ref.
+	 */
+	superproject_head = argv[1];
+	/* Get the submodule's head ref and determine if it is detached */
+	head = resolve_refdup("HEAD", 0, head_oid.hash, NULL);
+	if (!head)
+		die(_("Failed to resolve HEAD as a valid ref."));
+	if (!strcmp(head, "HEAD"))
+		detached_head = 1;
 
 	/*
 	 * The remote must be configured.
 	 * This is to avoid pushing to the exact same URL as the parent.
 	 */
-	remote = pushremote_get(argv[1]);
+	remote = pushremote_get(argv[2]);
 	if (!remote || remote->origin == REMOTE_UNCONFIGURED)
-		die("remote '%s' not configured", argv[1]);
+		die("remote '%s' not configured", argv[2]);
 
 	/* Check the refspec */
-	if (argc > 2) {
-		int i, refspec_nr = argc - 2;
+	if (argc > 3) {
+		int i, refspec_nr = argc - 3;
 		struct ref *local_refs = get_local_heads();
 		struct refspec *refspec = parse_push_refspec(refspec_nr,
-							     argv + 2);
+							     argv + 3);
 
 		for (i = 0; i < refspec_nr; i++) {
 			struct refspec *rs = refspec + i;
@@ -1132,18 +1149,30 @@ static int push_check(int argc, const char **argv, const char *prefix)
 			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)
+			/* LHS must match a single ref */
+			switch(count_refspec_match(rs->src, local_refs, NULL)) {
+			case 1:
+				break;
+			case 0:
+				/*
+				 * If LHS matches 'HEAD' then we need to ensure
+				 * that it matches the same named branch
+				 * checked out in the superproject.
+				 */
+				if (!strcmp(rs->src, "HEAD")) {
+					if (!detached_head &&
+					    !strcmp(head, superproject_head))
+						break;
+					die("HEAD does not match the named branch in the superproject");
+				}
+			default:
 				die("src refspec '%s' must name a ref",
 				    rs->src);
+			}
 		}
 		free_refspec(refspec_nr, refspec);
 	}
+	free(head);
 
 	return 0;
 }
diff --git a/submodule.c b/submodule.c
index 1b8a3b575..920b49328 100644
--- a/submodule.c
+++ b/submodule.c
@@ -1003,7 +1003,8 @@ static int push_submodule(const char *path,
  * 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,
+static void submodule_push_check(const char *path, const char *head,
+				 const struct remote *remote,
 				 const char **refspec, int refspec_nr)
 {
 	struct child_process cp = CHILD_PROCESS_INIT;
@@ -1011,6 +1012,7 @@ static void submodule_push_check(const char *path, const struct remote *remote,
 
 	argv_array_push(&cp.args, "submodule--helper");
 	argv_array_push(&cp.args, "push-check");
+	argv_array_push(&cp.args, head);
 	argv_array_push(&cp.args, remote->name);
 
 	for (i = 0; i < refspec_nr; i++)
@@ -1049,10 +1051,20 @@ int push_unpushed_submodules(struct oid_array *commits,
 	 * won't be propagated due to the remote being unconfigured (e.g. a URL
 	 * instead of a remote name).
 	 */
-	if (remote->origin != REMOTE_UNCONFIGURED)
+	if (remote->origin != REMOTE_UNCONFIGURED) {
+		char *head;
+		struct object_id head_oid;
+
+		head = resolve_refdup("HEAD", 0, head_oid.hash, NULL);
+		if (!head)
+			die(_("Failed to resolve HEAD as a valid ref."));
+
 		for (i = 0; i < needs_pushing.nr; i++)
 			submodule_push_check(needs_pushing.items[i].string,
-					     remote, refspec, refspec_nr);
+					     head, remote,
+					     refspec, refspec_nr);
+		free(head);
+	}
 
 	/* Actually push the submodules */
 	for (i = 0; i < needs_pushing.nr; i++) {
diff --git a/t/t5531-deep-submodule-push.sh b/t/t5531-deep-submodule-push.sh
index beff65b8a..0f84a5314 100755
--- a/t/t5531-deep-submodule-push.sh
+++ b/t/t5531-deep-submodule-push.sh
@@ -533,7 +533,8 @@ test_expect_success 'push propagating refspec to a submodule' '
 	# 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
+	# Fails when refspec includes HEAD and parent and submodule do not
+	# have the same named branch checked out
 	test_must_fail git -C work push --recurse-submodules=on-demand origin \
 		HEAD:refs/heads/branch2 &&
 
@@ -548,4 +549,26 @@ test_expect_success 'push propagating refspec to a submodule' '
 	test_cmp expected_pub actual_pub
 '
 
+test_expect_success 'push propagating HEAD refspec to a submodule' '
+	git -C work/gar/bage checkout branch2 &&
+	> work/gar/bage/junk12 &&
+	git -C work/gar/bage add junk12 &&
+	git -C work/gar/bage commit -m "Twelfth junk" &&
+
+	git -C work checkout branch2 &&
+	git -C work add gar/bage &&
+	git -C work commit -m "updating gar/bage in branch2" &&
+
+	# Passes since the superproject and submodules HEAD are both on branch2
+	git -C work push --recurse-submodules=on-demand origin \
+		HEAD:refs/heads/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
-- 
2.13.1.704.gde00cce3c-goog
Stefan Beller· Jun 23, 2017, 20:26 UTC · re: Brandon Williams · lore

Re: [PATCH] submodule--helper: teach push-check to handle HEAD

On Fri, Jun 23, 2017 at 1:04 PM, Brandon Williams <bmwill@google.com> wrote:
Show 12 quoted lines
> In 06bf4ad1d (push: propagate remote and refspec with
> --recurse-submodules) push was taught how to propagate a refspec down to
> submodules when the '--recurse-submodules' flag is given.  The only refspecs
> that are allowed to be propagated are ones which name a ref which exists
> in both the superproject and the submodule, with the caveat that 'HEAD'
> was disallowed.
>
> This patch teaches push-check (the submodule helper which determines if
> a refspec can be propagated to a submodule) to permit propagating 'HEAD'
> if and only if the superproject and the submodule both have the same
> named branch checked out and the submodule is not in a detached head
> state.
cont'd:

We need this use case because Gerrit's documentation ingrains this workflow in its users to use

    git push <remote> HEAD:refs/for/<target-branch>

And when both the submodule as well as the superproject are still on a branch with the same name (and not detached) we'd not be misunderstood by the user on the syntax.

More on the code later.
Junio C Hamano· Jun 23, 2017, 22:51 UTC · re: Brandon Williams · lore

Re: [PATCH] submodule--helper: teach push-check to handle HEAD

Brandon Williams <bmwill@google.com> writes:
Show 37 quoted lines
> In 06bf4ad1d (push: propagate remote and refspec with
> --recurse-submodules) push was taught how to propagate a refspec down to
> submodules when the '--recurse-submodules' flag is given.  The only refspecs
> that are allowed to be propagated are ones which name a ref which exists
> in both the superproject and the submodule, with the caveat that 'HEAD'
> was disallowed.
>
> This patch teaches push-check (the submodule helper which determines if
> a refspec can be propagated to a submodule) to permit propagating 'HEAD'
> if and only if the superproject and the submodule both have the same
> named branch checked out and the submodule is not in a detached head
> state.
>
> Signed-off-by: Brandon Williams <bmwill@google.com>
> ---
>  builtin/submodule--helper.c    | 57 +++++++++++++++++++++++++++++++-----------
>  submodule.c                    | 18 ++++++++++---
>  t/t5531-deep-submodule-push.sh | 25 +++++++++++++++++-
>  3 files changed, 82 insertions(+), 18 deletions(-)
>
> diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
> index 1b4d2b346..fd5020036 100644
> --- a/builtin/submodule--helper.c
> +++ b/builtin/submodule--helper.c
> @@ -1107,24 +1107,41 @@ static int resolve_remote_submodule_branch(int argc, const char **argv,
>  static int push_check(int argc, const char **argv, const char *prefix)
>  {
>  	struct remote *remote;
> +	const char *superproject_head;
> +	char *head;
> +	int detached_head = 0;
> +	struct object_id head_oid;
>  
> -	if (argc < 2)
> -		die("submodule--helper push-check requires at least 1 argument");
> +	if (argc < 3)
> +		die("submodule--helper push-check requires at least 2 argument");
"arguments"?
Show 7 quoted lines
> +
> +	/*
> +	 * superproject's resolved head ref.
> +	 * if HEAD then the superproject is in a detached head state, otherwise
> +	 * it will be the resolved head ref.
> +	 */
> +	superproject_head = argv[1];

The above makes it sound like the caller gives either "HEAD" (when detached) or "refs/heads/branch" (when on 'branch') in argv[1] and you are stashing it away, but ...

Show 6 quoted lines
> +	/* Get the submodule's head ref and determine if it is detached */
> +	head = resolve_refdup("HEAD", 0, head_oid.hash, NULL);
> +	if (!head)
> +		die(_("Failed to resolve HEAD as a valid ref."));
> +	if (!strcmp(head, "HEAD"))
> +		detached_head = 1;

... the work to see which branch we are on and if we are detached is done by this code without consulting argv[1]. I cannot tell what is going on. Is argv[1] assigned to superproject_head a red herring?

Show 19 quoted lines
>  	/*
>  	 * The remote must be configured.
>  	 * This is to avoid pushing to the exact same URL as the parent.
>  	 */
> -	remote = pushremote_get(argv[1]);
> +	remote = pushremote_get(argv[2]);
>  	if (!remote || remote->origin == REMOTE_UNCONFIGURED)
> -		die("remote '%s' not configured", argv[1]);
> +		die("remote '%s' not configured", argv[2]);
>  
>  	/* Check the refspec */
> -	if (argc > 2) {
> -		int i, refspec_nr = argc - 2;
> +	if (argc > 3) {
> +		int i, refspec_nr = argc - 3;
>  		struct ref *local_refs = get_local_heads();
>  		struct refspec *refspec = parse_push_refspec(refspec_nr,
> -							     argv + 2);
> +							     argv + 3);

If you have no need for argv[1] (and you don't, as you have stashed it away in superproject_head), it may be less damage to the code if you shifted argv upfront after grabbing superproject_head.

Show 15 quoted lines
>  		for (i = 0; i < refspec_nr; i++) {
>  			struct refspec *rs = refspec + i;
> @@ -1132,18 +1149,30 @@ static int push_check(int argc, const char **argv, const char *prefix)
>  			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)
> +			/* LHS must match a single ref */
> +			switch(count_refspec_match(rs->src, local_refs, NULL)) {
"switch (count..."
Show 14 quoted lines
> +			case 1:
> +				break;
> +			case 0:
> +				/*
> +				 * If LHS matches 'HEAD' then we need to ensure
> +				 * that it matches the same named branch
> +				 * checked out in the superproject.
> +				 */
> +				if (!strcmp(rs->src, "HEAD")) {
> +					if (!detached_head &&
> +					    !strcmp(head, superproject_head))
> +						break;
> +					die("HEAD does not match the named branch in the superproject");
> +				}

Hmph, so earlier people can "push --recurse-submodules HEAD:$dest" and $dest can be anything, but now we are tightening the rule?

Show 11 quoted lines
> +			default:
>  				die("src refspec '%s' must name a ref",
>  				    rs->src);
> +			}
>  		}
>  		free_refspec(refspec_nr, refspec);
>  	}
> +	free(head);
>  
>  	return 0;
>  }
Brandon Williams· Jun 26, 2017, 18:12 UTC · re: Junio C Hamano · lore

Re: [PATCH] submodule--helper: teach push-check to handle HEAD

On 06/23, Junio C Hamano wrote:
Show 41 quoted lines
> Brandon Williams <bmwill@google.com> writes:
> 
> > In 06bf4ad1d (push: propagate remote and refspec with
> > --recurse-submodules) push was taught how to propagate a refspec down to
> > submodules when the '--recurse-submodules' flag is given.  The only refspecs
> > that are allowed to be propagated are ones which name a ref which exists
> > in both the superproject and the submodule, with the caveat that 'HEAD'
> > was disallowed.
> >
> > This patch teaches push-check (the submodule helper which determines if
> > a refspec can be propagated to a submodule) to permit propagating 'HEAD'
> > if and only if the superproject and the submodule both have the same
> > named branch checked out and the submodule is not in a detached head
> > state.
> >
> > Signed-off-by: Brandon Williams <bmwill@google.com>
> > ---
> >  builtin/submodule--helper.c    | 57 +++++++++++++++++++++++++++++++-----------
> >  submodule.c                    | 18 ++++++++++---
> >  t/t5531-deep-submodule-push.sh | 25 +++++++++++++++++-
> >  3 files changed, 82 insertions(+), 18 deletions(-)
> >
> > diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
> > index 1b4d2b346..fd5020036 100644
> > --- a/builtin/submodule--helper.c
> > +++ b/builtin/submodule--helper.c
> > @@ -1107,24 +1107,41 @@ static int resolve_remote_submodule_branch(int argc, const char **argv,
> >  static int push_check(int argc, const char **argv, const char *prefix)
> >  {
> >  	struct remote *remote;
> > +	const char *superproject_head;
> > +	char *head;
> > +	int detached_head = 0;
> > +	struct object_id head_oid;
> >  
> > -	if (argc < 2)
> > -		die("submodule--helper push-check requires at least 1 argument");
> > +	if (argc < 3)
> > +		die("submodule--helper push-check requires at least 2 argument");
> 
> "arguments"?
 
You're right, I'll fix the typo.
Show 22 quoted lines
> > +
> > +	/*
> > +	 * superproject's resolved head ref.
> > +	 * if HEAD then the superproject is in a detached head state, otherwise
> > +	 * it will be the resolved head ref.
> > +	 */
> > +	superproject_head = argv[1];
> 
> The above makes it sound like the caller gives either "HEAD" (when
> detached) or "refs/heads/branch" (when on 'branch') in argv[1] and
> you are stashing it away, but ...
> 
> > +	/* Get the submodule's head ref and determine if it is detached */
> > +	head = resolve_refdup("HEAD", 0, head_oid.hash, NULL);
> > +	if (!head)
> > +		die(_("Failed to resolve HEAD as a valid ref."));
> > +	if (!strcmp(head, "HEAD"))
> > +		detached_head = 1;
> 
> ... the work to see which branch we are on and if we are detached is
> done by this code without consulting argv[1].  I cannot tell what is
> going on.  Is argv[1] assigned to superproject_head a red herring?

The idea is that 'git submodule--helper push-check' is called by a superproject on every submodule that may be pushed. So this command is invoked on the submodule itself. This change requires knowing what 'HEAD' refers to in the superproject (either detached or a named branch) so the superproject passes that information to the submodule via argv[1]. This snippet of code is responsible for finding what 'HEAD' refers to in the submodule so that later we can compare the superproject's and submodule's 'HEAD' ref to see if they match the same named branch.

Show 60 quoted lines
> 
> >  	/*
> >  	 * The remote must be configured.
> >  	 * This is to avoid pushing to the exact same URL as the parent.
> >  	 */
> > -	remote = pushremote_get(argv[1]);
> > +	remote = pushremote_get(argv[2]);
> >  	if (!remote || remote->origin == REMOTE_UNCONFIGURED)
> > -		die("remote '%s' not configured", argv[1]);
> > +		die("remote '%s' not configured", argv[2]);
> >  
> >  	/* Check the refspec */
> > -	if (argc > 2) {
> > -		int i, refspec_nr = argc - 2;
> > +	if (argc > 3) {
> > +		int i, refspec_nr = argc - 3;
> >  		struct ref *local_refs = get_local_heads();
> >  		struct refspec *refspec = parse_push_refspec(refspec_nr,
> > -							     argv + 2);
> > +							     argv + 3);
> 
> If you have no need for argv[1] (and you don't, as you have stashed
> it away in superproject_head), it may be less damage to the code if
> you shifted argv upfront after grabbing superproject_head.
> 
> >  		for (i = 0; i < refspec_nr; i++) {
> >  			struct refspec *rs = refspec + i;
> > @@ -1132,18 +1149,30 @@ static int push_check(int argc, const char **argv, const char *prefix)
> >  			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)
> > +			/* LHS must match a single ref */
> > +			switch(count_refspec_match(rs->src, local_refs, NULL)) {
> 
> "switch (count..."
> 
> > +			case 1:
> > +				break;
> > +			case 0:
> > +				/*
> > +				 * If LHS matches 'HEAD' then we need to ensure
> > +				 * that it matches the same named branch
> > +				 * checked out in the superproject.
> > +				 */
> > +				if (!strcmp(rs->src, "HEAD")) {
> > +					if (!detached_head &&
> > +					    !strcmp(head, superproject_head))
> > +						break;
> > +					die("HEAD does not match the named branch in the superproject");
> > +				}
> 
> Hmph, so earlier people can "push --recurse-submodules HEAD:$dest"
> and $dest can be anything, but now we are tightening the rule?

I don't believe that anything is changing w.r.t. what $dest can be (unless I'm missing something). This is more about enabling 'HEAD' to be used on the LHS of a refspec as before it wasn't permitted. With this change a user can use 'push --recurse-submodules HEAD:$dest' and the refspec which includes 'HEAD' on the LHS will be propagated to the submodules if and only if 'HEAD' is not detached in the superproject or submodule and 'HEAD' refers to the same named branch.

Show 12 quoted lines
> 
> > +			default:
> >  				die("src refspec '%s' must name a ref",
> >  				    rs->src);
> > +			}
> >  		}
> >  		free_refspec(refspec_nr, refspec);
> >  	}
> > +	free(head);
> >  
> >  	return 0;
> >  }
-- 
Brandon Williams
Brandon Williams· Jul 20, 2017, 17:40 UTC · re: Brandon Williams · lore

[PATCH v2 1/1] submodule--helper: teach push-check to handle HEAD

In 06bf4ad1d (push: propagate remote and refspec with --recurse-submodules) push was taught how to propagate a refspec down to submodules when the '--recurse-submodules' flag is given. The only refspecs that are allowed to be propagated are ones which name a ref which exists in both the superproject and the submodule, with the caveat that 'HEAD' was disallowed.

This patch teaches push-check (the submodule helper which determines if a refspec can be propagated to a submodule) to permit propagating 'HEAD' if and only if the superproject and the submodule both have the same named branch checked out and the submodule is not in a detached head state.

Signed-off-by: Brandon Williams <bmwill@google.com>
---
Changes in V2:
 * fixed a few style issues
 * shifted argv/argc to prevent more damage to the code than is necessary.
 builtin/submodule--helper.c    | 49 ++++++++++++++++++++++++++++++++++--------
 submodule.c                    | 18 +++++++++++++---
 t/t5531-deep-submodule-push.sh | 25 ++++++++++++++++++++-
 3 files changed, 79 insertions(+), 13 deletions(-)
Show changes to 3 files +79 −13

builtin/submodule--helper.c, submodule.c, t/t5531-deep-submodule-push.sh

diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index 6abdad329..0939e3912 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -1108,9 +1108,28 @@ static int resolve_remote_submodule_branch(int argc, const char **argv,
 static int push_check(int argc, const char **argv, const char *prefix)
 {
 	struct remote *remote;
+	const char *superproject_head;
+	char *head;
+	int detached_head = 0;
+	struct object_id head_oid;
 
-	if (argc < 2)
-		die("submodule--helper push-check requires at least 1 argument");
+	if (argc < 3)
+		die("submodule--helper push-check requires at least 2 arguments");
+
+	/*
+	 * superproject's resolved head ref.
+	 * if HEAD then the superproject is in a detached head state, otherwise
+	 * it will be the resolved head ref.
+	 */
+	superproject_head = argv[1];
+	argv++;
+	argc--;
+	/* Get the submodule's head ref and determine if it is detached */
+	head = resolve_refdup("HEAD", 0, head_oid.hash, NULL);
+	if (!head)
+		die(_("Failed to resolve HEAD as a valid ref."));
+	if (!strcmp(head, "HEAD"))
+		detached_head = 1;
 
 	/*
 	 * The remote must be configured.
@@ -1133,18 +1152,30 @@ static int push_check(int argc, const char **argv, const char *prefix)
 			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)
+			/* LHS must match a single ref */
+			switch (count_refspec_match(rs->src, local_refs, NULL)) {
+			case 1:
+				break;
+			case 0:
+				/*
+				 * If LHS matches 'HEAD' then we need to ensure
+				 * that it matches the same named branch
+				 * checked out in the superproject.
+				 */
+				if (!strcmp(rs->src, "HEAD")) {
+					if (!detached_head &&
+					    !strcmp(head, superproject_head))
+						break;
+					die("HEAD does not match the named branch in the superproject");
+				}
+			default:
 				die("src refspec '%s' must name a ref",
 				    rs->src);
+			}
 		}
 		free_refspec(refspec_nr, refspec);
 	}
+	free(head);
 
 	return 0;
 }
diff --git a/submodule.c b/submodule.c
index 6531c5d60..36f45f5a5 100644
--- a/submodule.c
+++ b/submodule.c
@@ -1015,7 +1015,8 @@ static int push_submodule(const char *path,
  * 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,
+static void submodule_push_check(const char *path, const char *head,
+				 const struct remote *remote,
 				 const char **refspec, int refspec_nr)
 {
 	struct child_process cp = CHILD_PROCESS_INIT;
@@ -1023,6 +1024,7 @@ static void submodule_push_check(const char *path, const struct remote *remote,
 
 	argv_array_push(&cp.args, "submodule--helper");
 	argv_array_push(&cp.args, "push-check");
+	argv_array_push(&cp.args, head);
 	argv_array_push(&cp.args, remote->name);
 
 	for (i = 0; i < refspec_nr; i++)
@@ -1061,10 +1063,20 @@ int push_unpushed_submodules(struct oid_array *commits,
 	 * won't be propagated due to the remote being unconfigured (e.g. a URL
 	 * instead of a remote name).
 	 */
-	if (remote->origin != REMOTE_UNCONFIGURED)
+	if (remote->origin != REMOTE_UNCONFIGURED) {
+		char *head;
+		struct object_id head_oid;
+
+		head = resolve_refdup("HEAD", 0, head_oid.hash, NULL);
+		if (!head)
+			die(_("Failed to resolve HEAD as a valid ref."));
+
 		for (i = 0; i < needs_pushing.nr; i++)
 			submodule_push_check(needs_pushing.items[i].string,
-					     remote, refspec, refspec_nr);
+					     head, remote,
+					     refspec, refspec_nr);
+		free(head);
+	}
 
 	/* Actually push the submodules */
 	for (i = 0; i < needs_pushing.nr; i++) {
diff --git a/t/t5531-deep-submodule-push.sh b/t/t5531-deep-submodule-push.sh
index beff65b8a..0f84a5314 100755
--- a/t/t5531-deep-submodule-push.sh
+++ b/t/t5531-deep-submodule-push.sh
@@ -533,7 +533,8 @@ test_expect_success 'push propagating refspec to a submodule' '
 	# 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
+	# Fails when refspec includes HEAD and parent and submodule do not
+	# have the same named branch checked out
 	test_must_fail git -C work push --recurse-submodules=on-demand origin \
 		HEAD:refs/heads/branch2 &&
 
@@ -548,4 +549,26 @@ test_expect_success 'push propagating refspec to a submodule' '
 	test_cmp expected_pub actual_pub
 '
 
+test_expect_success 'push propagating HEAD refspec to a submodule' '
+	git -C work/gar/bage checkout branch2 &&
+	> work/gar/bage/junk12 &&
+	git -C work/gar/bage add junk12 &&
+	git -C work/gar/bage commit -m "Twelfth junk" &&
+
+	git -C work checkout branch2 &&
+	git -C work add gar/bage &&
+	git -C work commit -m "updating gar/bage in branch2" &&
+
+	# Passes since the superproject and submodules HEAD are both on branch2
+	git -C work push --recurse-submodules=on-demand origin \
+		HEAD:refs/heads/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
-- 
2.14.0.rc0.284.gd933b75aa4-goog

← back to recent threads