threads / patch / 59964

patch, 2 partsremove unused "base-offset" from tree-walk codepath

Subject: [PATCH 0/2] remove unused "base-offset" from tree-walk codepath

## tl;dr

3 messages between Jul 7, 2023 and Jul 7, 2023. Diffs are folded; open one to read it.

replies: 2people: 1as markdown or json

Junio C Hamano· Jul 7, 2023, 22:21 UTC · lore

Here are a few bonus patches I came up with while working on the "hmm, attr magic does not seem to work with pathspec patterns?" topic to remove a parameter that is passed around without getting any value other than 0 (hence nobody knows if the code is correct when the parameter is set to non-zero).

It turned out that they do not interact with the main topic in any way, so I made it into a separate and indenendent series.

Junio C Hamano (2):
  tree-walk: lose base_offset that is never used in
    tree_entry_interesting
  tree-walk: drop unused base_offset from do_match()
 builtin/grep.c |  2 +-
 list-objects.c |  2 +-
 tree-diff.c    |  2 +-
 tree-walk.c    | 36 ++++++++++++++++++------------------
 tree-walk.h    |  2 +-
 tree.c         |  2 +-
 6 files changed, 23 insertions(+), 23 deletions(-)
-- 
2.41.0-318-g061c58647e
Junio C Hamano· Jul 7, 2023, 22:21 UTC · re: Junio C Hamano · lore

[PATCH 1/2] tree-walk: lose base_offset that is never used in tree_entry_interesting

The tree_entry_interesting() function takes base_offset, allowing its callers to potentially pass a non-zero number to skip the early part of the path string.

The feature is never exercised and we do not even know what bugs are lurking there, as all callers pass 0 to the parameter.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin/grep.c | 2 +-
 list-objects.c | 2 +-
 tree-diff.c    | 2 +-
 tree-walk.c    | 5 +++--
 tree-walk.h    | 2 +-
 tree.c         | 2 +-
 6 files changed, 8 insertions(+), 7 deletions(-)
Show changes to 6 files +8 −7

builtin/grep.c, list-objects.c, tree-diff.c, tree-walk.c, tree-walk.h, tree.c

diff --git a/builtin/grep.c b/builtin/grep.c
index 22645c6244..5f47317c2f 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -644,7 +644,7 @@ static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
 			strbuf_addstr(&name, base->buf + tn_len);
 			match = tree_entry_interesting(repo->index,
 						       &entry, &name,
-						       0, pathspec);
+						       pathspec);
 			strbuf_setlen(&name, name_base_len);
 
 			if (match == all_entries_not_interesting)
diff --git a/list-objects.c b/list-objects.c
index 672a4cd529..e60a6cd5b4 100644
--- a/list-objects.c
+++ b/list-objects.c
@@ -102,7 +102,7 @@ static void process_tree_contents(struct traversal_context *ctx,
 	while (tree_entry(&desc, &entry)) {
 		if (match != all_entries_interesting) {
 			match = tree_entry_interesting(ctx->revs->repo->index,
-						       &entry, base, 0,
+						       &entry, base,
 						       &ctx->revs->diffopt.pathspec);
 			if (match == all_entries_not_interesting)
 				break;
diff --git a/tree-diff.c b/tree-diff.c
index 966946848a..8fc159b86e 100644
--- a/tree-diff.c
+++ b/tree-diff.c
@@ -317,7 +317,7 @@ static void skip_uninteresting(struct tree_desc *t, struct strbuf *base,
 
 	while (t->size) {
 		match = tree_entry_interesting(opt->repo->index, &t->entry,
-					       base, 0, &opt->pathspec);
+					       base, &opt->pathspec);
 		if (match) {
 			if (match == all_entries_not_interesting)
 				t->size = 0;
diff --git a/tree-walk.c b/tree-walk.c
index 42ed86ef58..1797f2b9c5 100644
--- a/tree-walk.c
+++ b/tree-walk.c
@@ -435,7 +435,7 @@ static inline int prune_traversal(struct index_state *istate,
 	if (still_interesting < 0)
 		return still_interesting;
 	return tree_entry_interesting(istate, e, base,
-				      0, info->pathspec);
+				      info->pathspec);
 }
 
 int traverse_trees(struct index_state *istate,
@@ -1223,10 +1223,11 @@ static enum interesting do_match(struct index_state *istate,
  */
 enum interesting tree_entry_interesting(struct index_state *istate,
 					const struct name_entry *entry,
-					struct strbuf *base, int base_offset,
+					struct strbuf *base,
 					const struct pathspec *ps)
 {
 	enum interesting positive, negative;
+	const int base_offset = 0;
 	positive = do_match(istate, entry, base, base_offset, ps, 0);
 
 	/*
diff --git a/tree-walk.h b/tree-walk.h
index 01a9d8eb44..74cdceb3fe 100644
--- a/tree-walk.h
+++ b/tree-walk.h
@@ -224,7 +224,7 @@ enum interesting {
 
 enum interesting tree_entry_interesting(struct index_state *istate,
 					const struct name_entry *,
-					struct strbuf *, int,
+					struct strbuf *,
 					const struct pathspec *ps);
 
 #endif
diff --git a/tree.c b/tree.c
index e118914a76..c745462f96 100644
--- a/tree.c
+++ b/tree.c
@@ -32,7 +32,7 @@ int read_tree_at(struct repository *r,
 	while (tree_entry(&desc, &entry)) {
 		if (retval != all_entries_interesting) {
 			retval = tree_entry_interesting(r->index, &entry,
-							base, 0, pathspec);
+							base, pathspec);
 			if (retval == all_entries_not_interesting)
 				break;
 			if (retval == entry_not_interesting)
-- 
2.41.0-318-g061c58647e
Junio C Hamano· Jul 7, 2023, 22:21 UTC · re: Junio C Hamano · lore

[PATCH 2/2] tree-walk: drop unused base_offset from do_match()

The tree-walk.c:do_match() function takes base_offset but just like tree_entry_interesting() we dealt with earlier, nobody passes a value other than 0 in it. Get rid of the parameter to avoid having to worry about potential bugs lurking unexercised.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 tree-walk.c | 33 ++++++++++++++++-----------------
 1 file changed, 16 insertions(+), 17 deletions(-)
Show changes to tree-walk.c +16 −17
diff --git a/tree-walk.c b/tree-walk.c
index 1797f2b9c5..f48e8c580e 100644
--- a/tree-walk.c
+++ b/tree-walk.c
@@ -1016,17 +1016,17 @@ static int match_wildcard_base(const struct pathspec_item *item,
 /*
  * Is a tree entry interesting given the pathspec we have?
  *
- * Pre-condition: either baselen == base_offset (i.e. empty path)
+ * Pre-condition: either baselen == 0 (i.e. empty path)
  * or base[baselen-1] == '/' (i.e. with trailing slash).
  */
 static enum interesting do_match(struct index_state *istate,
 				 const struct name_entry *entry,
-				 struct strbuf *base, int base_offset,
+				 struct strbuf *base,
 				 const struct pathspec *ps,
 				 int exclude)
 {
 	int i;
-	int pathlen, baselen = base->len - base_offset;
+	int pathlen, baselen = base->len;
 	enum interesting never_interesting = ps->has_wildcard ?
 		entry_not_interesting : all_entries_not_interesting;
 
@@ -1044,7 +1044,7 @@ static enum interesting do_match(struct index_state *istate,
 		    !(ps->magic & PATHSPEC_MAXDEPTH) ||
 		    ps->max_depth == -1)
 			return all_entries_interesting;
-		return within_depth(base->buf + base_offset, baselen,
+		return within_depth(base->buf, baselen,
 				    !!S_ISDIR(entry->mode),
 				    ps->max_depth) ?
 			entry_interesting : entry_not_interesting;
@@ -1055,7 +1055,7 @@ static enum interesting do_match(struct index_state *istate,
 	for (i = ps->nr - 1; i >= 0; i--) {
 		const struct pathspec_item *item = ps->items+i;
 		const char *match = item->match;
-		const char *base_str = base->buf + base_offset;
+		const char *base_str = base->buf;
 		int matchlen = item->len, matched = 0;
 
 		if ((!exclude &&   item->magic & PATHSPEC_EXCLUDE) ||
@@ -1148,9 +1148,9 @@ static enum interesting do_match(struct index_state *istate,
 
 		strbuf_add(base, entry->path, pathlen);
 
-		if (!git_fnmatch(item, match, base->buf + base_offset,
+		if (!git_fnmatch(item, match, base->buf,
 				 item->nowildcard_len)) {
-			strbuf_setlen(base, base_offset + baselen);
+			strbuf_setlen(base, baselen);
 			goto interesting;
 		}
 
@@ -1162,13 +1162,13 @@ static enum interesting do_match(struct index_state *istate,
 		 * be performed in the submodule itself.
 		 */
 		if (ps->recurse_submodules && S_ISGITLINK(entry->mode) &&
-		    !ps_strncmp(item, match, base->buf + base_offset,
+		    !ps_strncmp(item, match, base->buf,
 				item->nowildcard_len)) {
-			strbuf_setlen(base, base_offset + baselen);
+			strbuf_setlen(base, baselen);
 			goto interesting;
 		}
 
-		strbuf_setlen(base, base_offset + baselen);
+		strbuf_setlen(base, baselen);
 
 		/*
 		 * Match all directories. We'll try to match files
@@ -1204,9 +1204,9 @@ static enum interesting do_match(struct index_state *istate,
 				return entry_interesting;
 
 			strbuf_add(base, entry->path, pathlen);
-			ret = match_pathspec_attrs(istate, base->buf + base_offset,
-						   base->len - base_offset, item);
-			strbuf_setlen(base, base_offset + baselen);
+			ret = match_pathspec_attrs(istate, base->buf,
+						   base->len, item);
+			strbuf_setlen(base, baselen);
 			if (!ret)
 				continue;
 		}
@@ -1218,7 +1218,7 @@ static enum interesting do_match(struct index_state *istate,
 /*
  * Is a tree entry interesting given the pathspec we have?
  *
- * Pre-condition: either baselen == base_offset (i.e. empty path)
+ * Pre-condition: either baselen == 0 (i.e. empty path)
  * or base[baselen-1] == '/' (i.e. with trailing slash).
  */
 enum interesting tree_entry_interesting(struct index_state *istate,
@@ -1227,8 +1227,7 @@ enum interesting tree_entry_interesting(struct index_state *istate,
 					const struct pathspec *ps)
 {
 	enum interesting positive, negative;
-	const int base_offset = 0;
-	positive = do_match(istate, entry, base, base_offset, ps, 0);
+	positive = do_match(istate, entry, base, ps, 0);
 
 	/*
 	 * case | entry | positive | negative | result
@@ -1265,7 +1264,7 @@ enum interesting tree_entry_interesting(struct index_state *istate,
 	    positive <= entry_not_interesting) /* #1, #2, #11, #12 */
 		return positive;
 
-	negative = do_match(istate, entry, base, base_offset, ps, 1);
+	negative = do_match(istate, entry, base, ps, 1);
 
 	/* #8, #18 */
 	if (positive == all_entries_interesting &&
-- 
2.41.0-318-g061c58647e

← back to recent threads