threads / patch / 48568

patchsubmodule: do not pass null OID to setup_revisions

Subject: [PATCH] submodule: do not pass null OID to setup_revisions

## tl;dr

3 messages between May 24, 2018 and May 24, 2018. Diffs are folded; open one to read it.

replies: 2people: 2as markdown or json

Jonathan Tan· May 24, 2018, 20:47 UTC · lore

If "git pull --recurse-submodules --rebase" is invoked when the current branch and its corresponding remote-tracking branch have no merge base, a "bad object" fatal error occurs. This issue was introduced with commit a6d7eb2c7a ("pull: optionally rebase submodules (remote submodule changes only)", 2017-06-23), which also introduced this feature.

This is because cmd_pull() in builtin/pull.c thus invokes submodule_touches_in_range() with a null OID as the first parameter. Ensure that this case works, and document what happens in this case.

Signed-off-by: Jonathan Tan <jonathantanmy@google.com>
---
 submodule.c               |  6 ++++--
 submodule.h               |  5 ++++-
 t/t5572-pull-submodule.sh | 21 +++++++++++++++++++++
 3 files changed, 29 insertions(+), 3 deletions(-)
Show changes to 3 files +29 −3

submodule.c, submodule.h, t/t5572-pull-submodule.sh

diff --git a/submodule.c b/submodule.c
index 74d35b2577..49def93dd9 100644
--- a/submodule.c
+++ b/submodule.c
@@ -1169,8 +1169,10 @@ int submodule_touches_in_range(struct object_id *excl_oid,
 
 	argv_array_push(&args, "--"); /* args[0] program name */
 	argv_array_push(&args, oid_to_hex(incl_oid));
-	argv_array_push(&args, "--not");
-	argv_array_push(&args, oid_to_hex(excl_oid));
+	if (!is_null_oid(excl_oid)) {
+		argv_array_push(&args, "--not");
+		argv_array_push(&args, oid_to_hex(excl_oid));
+	}
 
 	collect_changed_submodules(&subs, &args);
 	ret = subs.nr;
diff --git a/submodule.h b/submodule.h
index e5526f6aaa..1fd7111f60 100644
--- a/submodule.h
+++ b/submodule.h
@@ -94,7 +94,10 @@ extern int merge_submodule(struct object_id *result, const char *path,
 			   const struct object_id *a,
 			   const struct object_id *b, int search);
 
-/* Checks if there are submodule changes in a..b. */
+/*
+ * Checks if there are submodule changes in a..b. If a is the null OID,
+ * checks b and all its ancestors instead.
+ */
 extern int submodule_touches_in_range(struct object_id *a,
 				      struct object_id *b);
 extern int find_unpushed_submodules(struct oid_array *commits,
diff --git a/t/t5572-pull-submodule.sh b/t/t5572-pull-submodule.sh
index 321bd37deb..f916729a12 100755
--- a/t/t5572-pull-submodule.sh
+++ b/t/t5572-pull-submodule.sh
@@ -132,4 +132,25 @@ test_expect_success 'pull rebase recursing fails with conflicts' '
 	test_i18ngrep "locally recorded submodule modifications" err
 '
 
+test_expect_success 'branch has no merge base with remote-tracking counterpart' '
+	rm -rf parent child &&
+
+	test_create_repo a-submodule &&
+	test_commit -C a-submodule foo &&
+
+	test_create_repo parent &&
+	git -C parent submodule add "$(pwd)/a-submodule" &&
+	git -C parent commit -m foo &&
+
+	git clone parent child &&
+
+	# Reset master so that it has no merge base with
+	# refs/remotes/origin/master.
+	OTHER=$(git -C child commit-tree -m bar \
+		$(git -C child rev-parse HEAD^{tree})) &&
+	git -C child reset --hard "$OTHER" &&
+
+	git -C child pull --recurse-submodules --rebase
+'
+
 test_done
-- 
2.17.0.768.g1526ddbba1.dirty
Stefan Beller· May 24, 2018, 23:07 UTC · re: Jonathan Tan · lore

Re: [PATCH] submodule: do not pass null OID to setup_revisions

Hi Jonathan,
On Thu, May 24, 2018 at 1:47 PM, Jonathan Tan <jonathantanmy@google.com> wrote:
Show 5 quoted lines
> If "git pull --recurse-submodules --rebase" is invoked when the current
> branch and its corresponding remote-tracking branch have no merge base,
> a "bad object" fatal error occurs. This issue was introduced with commit
> a6d7eb2c7a ("pull: optionally rebase submodules (remote submodule
> changes only)", 2017-06-23), which also introduced this feature.
Ok, what should happen instead?
> This is because cmd_pull() in builtin/pull.c thus invokes
> submodule_touches_in_range() with a null OID as the first parameter.
> Ensure that this case works, and document what happens in this case.

By documenting you mean adding a test, i.e. documenting it for the developers, not the users.

Show 66 quoted lines
>
> Signed-off-by: Jonathan Tan <jonathantanmy@google.com>
> ---
>  submodule.c               |  6 ++++--
>  submodule.h               |  5 ++++-
>  t/t5572-pull-submodule.sh | 21 +++++++++++++++++++++
>  3 files changed, 29 insertions(+), 3 deletions(-)
>
> diff --git a/submodule.c b/submodule.c
> index 74d35b2577..49def93dd9 100644
> --- a/submodule.c
> +++ b/submodule.c
> @@ -1169,8 +1169,10 @@ int submodule_touches_in_range(struct object_id *excl_oid,
>
>         argv_array_push(&args, "--"); /* args[0] program name */
>         argv_array_push(&args, oid_to_hex(incl_oid));
> -       argv_array_push(&args, "--not");
> -       argv_array_push(&args, oid_to_hex(excl_oid));
> +       if (!is_null_oid(excl_oid)) {
> +               argv_array_push(&args, "--not");
> +               argv_array_push(&args, oid_to_hex(excl_oid));
> +       }
>
>         collect_changed_submodules(&subs, &args);
>         ret = subs.nr;
> diff --git a/submodule.h b/submodule.h
> index e5526f6aaa..1fd7111f60 100644
> --- a/submodule.h
> +++ b/submodule.h
> @@ -94,7 +94,10 @@ extern int merge_submodule(struct object_id *result, const char *path,
>                            const struct object_id *a,
>                            const struct object_id *b, int search);
>
> -/* Checks if there are submodule changes in a..b. */
> +/*
> + * Checks if there are submodule changes in a..b. If a is the null OID,
> + * checks b and all its ancestors instead.
> + */
>  extern int submodule_touches_in_range(struct object_id *a,
>                                       struct object_id *b);
>  extern int find_unpushed_submodules(struct oid_array *commits,
> diff --git a/t/t5572-pull-submodule.sh b/t/t5572-pull-submodule.sh
> index 321bd37deb..f916729a12 100755
> --- a/t/t5572-pull-submodule.sh
> +++ b/t/t5572-pull-submodule.sh
> @@ -132,4 +132,25 @@ test_expect_success 'pull rebase recursing fails with conflicts' '
>         test_i18ngrep "locally recorded submodule modifications" err
>  '
>
> +test_expect_success 'branch has no merge base with remote-tracking counterpart' '
> +       rm -rf parent child &&
> +
> +       test_create_repo a-submodule &&
> +       test_commit -C a-submodule foo &&
> +
> +       test_create_repo parent &&
> +       git -C parent submodule add "$(pwd)/a-submodule" &&
> +       git -C parent commit -m foo &&
> +
> +       git clone parent child &&
> +
> +       # Reset master so that it has no merge base with
> +       # refs/remotes/origin/master.
> +       OTHER=$(git -C child commit-tree -m bar \
> +               $(git -C child rev-parse HEAD^{tree})) &&
> +       git -C child reset --hard "$OTHER" &&
I inserted a test_pause here and inspect child:
* the submodule is the same as in parent, so this patch is
  just testing it works with submodules the same?
* No, the submodule is not cloned into the child
  at all. So we do not know what do do with the submodule.

However this patch is about making sure the superproject works out well, without this patch we'd have $ git -C child pull --recurse-submodules --rebase fatal: bad object 0000000000000000000000000000000000000000 which is to be avoided.

Yes I think this is the best way to fix the issue, I thought for some time that could first check if submodules are initialzed or active, but these are checks are done afterwards, so this is ok.

Reviewed-by: Stefan Beller <sbeller@google.com>

Thanks! Stefan

Jonathan Tan· May 24, 2018, 23:30 UTC · re: Stefan Beller · lore

Re: [PATCH] submodule: do not pass null OID to setup_revisions

On Thu, 24 May 2018 16:07:49 -0700 Stefan Beller <sbeller@google.com> wrote:

Show 10 quoted lines
> Hi Jonathan,
> 
> On Thu, May 24, 2018 at 1:47 PM, Jonathan Tan <jonathantanmy@google.com> wrote:
> > If "git pull --recurse-submodules --rebase" is invoked when the current
> > branch and its corresponding remote-tracking branch have no merge base,
> > a "bad object" fatal error occurs. This issue was introduced with commit
> > a6d7eb2c7a ("pull: optionally rebase submodules (remote submodule
> > changes only)", 2017-06-23), which also introduced this feature.
> 
> Ok, what should happen instead?
Just for there not to be this "bad object" error.
Show 6 quoted lines
> > This is because cmd_pull() in builtin/pull.c thus invokes
> > submodule_touches_in_range() with a null OID as the first parameter.
> > Ensure that this case works, and document what happens in this case.
> 
> By documenting you mean adding a test, i.e. documenting it for the
> developers, not the users.

I also updated the submodule.h file, but yes, it is for the developers. I'll change the commit message to make this more clear if I need a reroll.

Show 5 quoted lines
> I inserted a test_pause here and inspect child:
> * the submodule is the same as in parent, so this patch is
>   just testing it works with submodules the same?
> * No, the submodule is not cloned into the child
>   at all. So we do not know what do do with the submodule.

Yes, this test doesn't do much. I just wanted to make sure that submodule_touches_in_range() could be called without encountering this unrelated error.

(Incidentally, we might want to add tests for the "cannot rebase with locally recorded submodule modifications", but I haven't looked into that.)

Show 11 quoted lines
> However this patch is about making sure the superproject
> works out well, without this patch we'd have
> $ git -C child pull --recurse-submodules --rebase
> fatal: bad object 0000000000000000000000000000000000000000
> which is to be avoided.
> 
> Yes I think this is the best way to fix the issue, I thought for some time that
> could first check if submodules are initialzed or active, but these
> are checks are done afterwards, so this is ok.
> 
> Reviewed-by: Stefan Beller <sbeller@google.com>
Thanks!

← back to recent threads