diff mbox series

[v3,7/8] submodule--helper: remove update_data.suboid

Message ID 4e402b67145c6e33c13826f1daf1883a66cd9cd4.1666988096.git.gitgitgadget@gmail.com (mailing list archive)
State New, archived
Headers show
Series clone, submodule update: check out submodule branches | expand

Commit Message

Glen Choo Oct. 28, 2022, 8:14 p.m. UTC
From: Glen Choo <chooglen@google.com>

update_data.suboid's value is only used in update_submodule()'s call
chain, where it represents the OID of the submodule's HEAD. If the
submodule is newly cloned, it is set to null_oid().

Instead of checking for the null OID, just check if the submodule is
newly cloned. This makes update_submodule() the only function where
update_data.suboid is used, so replace it with a local variable.

As a result, the submodule_up_to_date check is more explicit, which
makes the next commit slightly easier to reason about.

Signed-off-by: Glen Choo <chooglen@google.com>
---
 builtin/submodule--helper.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)

Comments

Jonathan Tan Nov. 14, 2022, 11:45 p.m. UTC | #1
Rearranging the diffs...

"Glen Choo via GitGitGadget" <gitgitgadget@gmail.com> writes:
> @@ -2532,10 +2532,8 @@ static int update_submodule(struct update_data *update_data)
>  	if (ret)
>  		return ret;
>  
> -	if (update_data->just_cloned)
> -		oidcpy(&update_data->suboid, null_oid());
> -	else if (resolve_gitlink_ref(update_data->sm_path, "HEAD",
> -				     &update_data->suboid, NULL))
> +	if (!update_data->just_cloned &&
> +	    resolve_gitlink_ref(update_data->sm_path, "HEAD", &suboid, NULL))
>  		return die_message(_("Unable to find current revision in submodule path '%s'"),
>  				   update_data->displaypath);
>  

Here, we only set suboid if !update_data->just_cloned...

> @@ -2570,7 +2568,8 @@ static int update_submodule(struct update_data *update_data)
>  		free(remote_ref);
>  	}
>  
> -	submodule_up_to_date = oideq(&update_data->oid, &update_data->suboid);
> +	submodule_up_to_date = !update_data->just_cloned &&
> +		oideq(&update_data->oid, &suboid);
>  	if (!submodule_up_to_date || update_data->force) {
>  		ret = run_update_procedure(update_data);
>  		if (ret)

...and here, we read suboid if !update_data->just_cloned.

> @@ -2523,6 +2522,7 @@ static int update_submodule(struct update_data *update_data)
>  {
>  	int submodule_up_to_date;
>  	int ret;
> +	struct object_id suboid;

And here we declare it with no initializer. This is safe for now since we only
read it when !update_data->just_cloned, which is also the condition for setting
it, but may be error-prone in the future. Probably best to initialize it to
something.
 
Other than that everything looks good up to and including this patch. This is
definitely an improvement over using a null OID to signal something, so thanks.
diff mbox series

Patch

diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index 894be133b3f..ef76a111c7f 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -1918,7 +1918,6 @@  struct update_data {
 	const char *prefix;
 	char *displaypath;
 	enum submodule_update_type update_default;
-	struct object_id suboid;
 	struct string_list references;
 	struct submodule_update_strategy update_strategy;
 	struct list_objects_filter_options *filter_options;
@@ -2346,7 +2345,7 @@  static int run_update_command(const struct update_data *ud, int subforce)
 
 static int run_update_procedure(const struct update_data *ud)
 {
-	int subforce = is_null_oid(&ud->suboid) || ud->force;
+	int subforce = ud->just_cloned || ud->force;
 
 	if (!ud->nofetch) {
 		/*
@@ -2523,6 +2522,7 @@  static int update_submodule(struct update_data *update_data)
 {
 	int submodule_up_to_date;
 	int ret;
+	struct object_id suboid;
 
 	ret = determine_submodule_update_strategy(the_repository,
 						  update_data->just_cloned,
@@ -2532,10 +2532,8 @@  static int update_submodule(struct update_data *update_data)
 	if (ret)
 		return ret;
 
-	if (update_data->just_cloned)
-		oidcpy(&update_data->suboid, null_oid());
-	else if (resolve_gitlink_ref(update_data->sm_path, "HEAD",
-				     &update_data->suboid, NULL))
+	if (!update_data->just_cloned &&
+	    resolve_gitlink_ref(update_data->sm_path, "HEAD", &suboid, NULL))
 		return die_message(_("Unable to find current revision in submodule path '%s'"),
 				   update_data->displaypath);
 
@@ -2570,7 +2568,8 @@  static int update_submodule(struct update_data *update_data)
 		free(remote_ref);
 	}
 
-	submodule_up_to_date = oideq(&update_data->oid, &update_data->suboid);
+	submodule_up_to_date = !update_data->just_cloned &&
+		oideq(&update_data->oid, &suboid);
 	if (!submodule_up_to_date || update_data->force) {
 		ret = run_update_procedure(update_data);
 		if (ret)
@@ -2583,7 +2582,6 @@  static int update_submodule(struct update_data *update_data)
 
 		next.prefix = NULL;
 		oidcpy(&next.oid, null_oid());
-		oidcpy(&next.suboid, null_oid());
 
 		cp.dir = update_data->sm_path;
 		cp.git_cmd = 1;