diff mbox series

[12/30] tree-walk.h users: migrate "p->mode &&" pattern

Message ID 20210308150650.18626-13-avarab@gmail.com (mailing list archive)
State New, archived
Headers show
Series Move the read_tree() function to its only user | expand

Commit Message

Ævar Arnfjörð Bjarmason March 8, 2021, 3:06 p.m. UTC
Change code that dpends on "p->mode" either being a valid mode or zero
to use a p->object_type comparison to "OBJ_NONE".

The object_type() function in cache.h will not return OBJ_NONE, but in
this these API users are implicitly relying on the memzero() that
happens in setup_traverse_info().

Since OBJ_NONE is "0" we can also rely on that being zero'd out here,
along with the rest of the structure. I think this is slightly less
clever than "mode not set", and helps to get rid of more uses of
"mode".

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/merge-tree.c | 9 +++++----
 merge-ort.c          | 2 +-
 unpack-trees.c       | 4 ++--
 3 files changed, 8 insertions(+), 7 deletions(-)

Comments

Elijah Newren March 9, 2021, 5:09 p.m. UTC | #1
On Mon, Mar 8, 2021 at 7:07 AM Ævar Arnfjörð Bjarmason <avarab@gmail.com> wrote:
>
> Change code that dpends on "p->mode" either being a valid mode or zero

s/dpends/depends/

> to use a p->object_type comparison to "OBJ_NONE".
>
> The object_type() function in cache.h will not return OBJ_NONE, but in
> this these API users are implicitly relying on the memzero() that
> happens in setup_traverse_info().

This is a bit hard to parse.  Perhaps just remove "in this"?

> Since OBJ_NONE is "0" we can also rely on that being zero'd out here,
> along with the rest of the structure. I think this is slightly less
> clever than "mode not set", and helps to get rid of more uses of
> "mode".
>
> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
> ---
>  builtin/merge-tree.c | 9 +++++----
>  merge-ort.c          | 2 +-
>  unpack-trees.c       | 4 ++--
>  3 files changed, 8 insertions(+), 7 deletions(-)
>
> diff --git a/builtin/merge-tree.c b/builtin/merge-tree.c
> index de8520778d2..2de34c2d485 100644
> --- a/builtin/merge-tree.c
> +++ b/builtin/merge-tree.c
> @@ -214,7 +214,7 @@ static void unresolved_directory(const struct traverse_info *info,
>         void *buf0, *buf1, *buf2;
>
>         for (p = n; p < n + 3; p++) {
> -               if (p->mode && S_ISDIR(p->mode))
> +               if (p->object_type == OBJ_TREE)
>                         break;
>         }
>         if (n + 3 <= p)
> @@ -222,7 +222,7 @@ static void unresolved_directory(const struct traverse_info *info,
>
>         newbase = traverse_path(info, p);
>
> -#define ENTRY_OID(e) (((e)->mode && S_ISDIR((e)->mode)) ? &(e)->oid : NULL)
> +#define ENTRY_OID(e) (((e)->object_type == OBJ_TREE) ? &(e)->oid : NULL)
>         buf0 = fill_tree_descriptor(r, t + 0, ENTRY_OID(n + 0));
>         buf1 = fill_tree_descriptor(r, t + 1, ENTRY_OID(n + 1));
>         buf2 = fill_tree_descriptor(r, t + 2, ENTRY_OID(n + 2));
> @@ -242,7 +242,7 @@ static struct merge_list *link_entry(unsigned stage, const struct traverse_info
>         const char *path;
>         struct merge_list *link;
>
> -       if (!n->mode)
> +       if (n->object_type == OBJ_NONE)
>                 return entry;
>         if (entry)
>                 path = entry->path;
> @@ -265,7 +265,8 @@ static void unresolved(const struct traverse_info *info, struct name_entry n[3])
>                  * Treat missing entries as directories so that we return
>                  * after unresolved_directory has handled this.
>                  */
> -               if (!n[i].mode || S_ISDIR(n[i].mode))
> +               if (n[i].object_type == OBJ_NONE ||
> +                   n[i].object_type == OBJ_TREE)
>                         dirmask |= (1 << i);
>         }
>
> diff --git a/merge-ort.c b/merge-ort.c
> index 4075d13aaab..4375027914c 100644
> --- a/merge-ort.c
> +++ b/merge-ort.c
> @@ -668,7 +668,7 @@ static int collect_merge_info_callback(int n,
>          * setup_path_info() for tracking.
>          */
>         p = names;
> -       while (!p->mode)
> +       while (p->object_type == OBJ_NONE)
>                 p++;
>         len = traverse_path_len(info, p->pathlen);
>
> diff --git a/unpack-trees.c b/unpack-trees.c
> index 802f7771d75..92105135522 100644
> --- a/unpack-trees.c
> +++ b/unpack-trees.c
> @@ -859,7 +859,7 @@ static int traverse_trees_recursive(int n, unsigned long dirmask,
>         }
>
>         p = names;
> -       while (!p->mode)
> +       while (p->object_type == OBJ_NONE)
>                 p++;
>
>         newinfo = *info;
> @@ -1239,7 +1239,7 @@ static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, str
>         const struct name_entry *p = names;
>
>         /* Find first entry with a real name (we could use "mask" too) */
> -       while (!p->mode)
> +       while (p->object_type == OBJ_NONE)
>                 p++;
>
>         if (o->debug_unpack)
> --
> 2.31.0.rc0.126.g04f22c5b82
diff mbox series

Patch

diff --git a/builtin/merge-tree.c b/builtin/merge-tree.c
index de8520778d2..2de34c2d485 100644
--- a/builtin/merge-tree.c
+++ b/builtin/merge-tree.c
@@ -214,7 +214,7 @@  static void unresolved_directory(const struct traverse_info *info,
 	void *buf0, *buf1, *buf2;
 
 	for (p = n; p < n + 3; p++) {
-		if (p->mode && S_ISDIR(p->mode))
+		if (p->object_type == OBJ_TREE)
 			break;
 	}
 	if (n + 3 <= p)
@@ -222,7 +222,7 @@  static void unresolved_directory(const struct traverse_info *info,
 
 	newbase = traverse_path(info, p);
 
-#define ENTRY_OID(e) (((e)->mode && S_ISDIR((e)->mode)) ? &(e)->oid : NULL)
+#define ENTRY_OID(e) (((e)->object_type == OBJ_TREE) ? &(e)->oid : NULL)
 	buf0 = fill_tree_descriptor(r, t + 0, ENTRY_OID(n + 0));
 	buf1 = fill_tree_descriptor(r, t + 1, ENTRY_OID(n + 1));
 	buf2 = fill_tree_descriptor(r, t + 2, ENTRY_OID(n + 2));
@@ -242,7 +242,7 @@  static struct merge_list *link_entry(unsigned stage, const struct traverse_info
 	const char *path;
 	struct merge_list *link;
 
-	if (!n->mode)
+	if (n->object_type == OBJ_NONE)
 		return entry;
 	if (entry)
 		path = entry->path;
@@ -265,7 +265,8 @@  static void unresolved(const struct traverse_info *info, struct name_entry n[3])
 		 * Treat missing entries as directories so that we return
 		 * after unresolved_directory has handled this.
 		 */
-		if (!n[i].mode || S_ISDIR(n[i].mode))
+		if (n[i].object_type == OBJ_NONE ||
+		    n[i].object_type == OBJ_TREE)
 			dirmask |= (1 << i);
 	}
 
diff --git a/merge-ort.c b/merge-ort.c
index 4075d13aaab..4375027914c 100644
--- a/merge-ort.c
+++ b/merge-ort.c
@@ -668,7 +668,7 @@  static int collect_merge_info_callback(int n,
 	 * setup_path_info() for tracking.
 	 */
 	p = names;
-	while (!p->mode)
+	while (p->object_type == OBJ_NONE)
 		p++;
 	len = traverse_path_len(info, p->pathlen);
 
diff --git a/unpack-trees.c b/unpack-trees.c
index 802f7771d75..92105135522 100644
--- a/unpack-trees.c
+++ b/unpack-trees.c
@@ -859,7 +859,7 @@  static int traverse_trees_recursive(int n, unsigned long dirmask,
 	}
 
 	p = names;
-	while (!p->mode)
+	while (p->object_type == OBJ_NONE)
 		p++;
 
 	newinfo = *info;
@@ -1239,7 +1239,7 @@  static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, str
 	const struct name_entry *p = names;
 
 	/* Find first entry with a real name (we could use "mask" too) */
-	while (!p->mode)
+	while (p->object_type == OBJ_NONE)
 		p++;
 
 	if (o->debug_unpack)