Skip to content

Commit d688cf0

Browse files
pcloudsgitster
authored andcommitted
tree_entry_interesting(): give meaningful names to return values
It is a basic code hygiene to avoid magic constants that are unnamed. Besides, this helps extending the value later on for "interesting, but cannot decide if the entry truely matches yet" (ie. prefix matches) Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 02cb675 commit d688cf0

File tree

6 files changed

+56
-44
lines changed

6 files changed

+56
-44
lines changed

builtin/grep.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -542,18 +542,19 @@ static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec, int
542542
static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
543543
struct tree_desc *tree, struct strbuf *base, int tn_len)
544544
{
545-
int hit = 0, match = 0;
545+
int hit = 0;
546+
enum interesting match = entry_not_interesting;
546547
struct name_entry entry;
547548
int old_baselen = base->len;
548549

549550
while (tree_entry(tree, &entry)) {
550551
int te_len = tree_entry_len(&entry);
551552

552-
if (match != 2) {
553+
if (match != all_entries_interesting) {
553554
match = tree_entry_interesting(&entry, base, tn_len, pathspec);
554-
if (match < 0)
555+
if (match == all_entries_not_interesting)
555556
break;
556-
if (match == 0)
557+
if (match == entry_not_interesting)
557558
continue;
558559
}
559560

list-objects.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ static void process_tree(struct rev_info *revs,
7171
struct tree_desc desc;
7272
struct name_entry entry;
7373
struct name_path me;
74-
int match = revs->diffopt.pathspec.nr == 0 ? 2 : 0;
74+
enum interesting match = revs->diffopt.pathspec.nr == 0 ?
75+
all_entries_interesting: entry_not_interesting;
7576
int baselen = base->len;
7677

7778
if (!revs->tree_objects)
@@ -97,12 +98,12 @@ static void process_tree(struct rev_info *revs,
9798
init_tree_desc(&desc, tree->buffer, tree->size);
9899

99100
while (tree_entry(&desc, &entry)) {
100-
if (match != 2) {
101+
if (match != all_entries_interesting) {
101102
match = tree_entry_interesting(&entry, base, 0,
102103
&revs->diffopt.pathspec);
103-
if (match < 0)
104+
if (match == all_entries_not_interesting)
104105
break;
105-
if (match == 0)
106+
if (match == entry_not_interesting)
106107
continue;
107108
}
108109

tree-diff.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2,
6464
static void show_tree(struct diff_options *opt, const char *prefix,
6565
struct tree_desc *desc, struct strbuf *base)
6666
{
67-
int match = 0;
67+
enum interesting match = entry_not_interesting;
6868
for (; desc->size; update_tree_entry(desc)) {
69-
if (match != 2) {
69+
if (match != all_entries_interesting) {
7070
match = tree_entry_interesting(&desc->entry, base, 0,
7171
&opt->pathspec);
72-
if (match < 0)
72+
if (match == all_entries_not_interesting)
7373
break;
74-
if (match == 0)
74+
if (match == entry_not_interesting)
7575
continue;
7676
}
7777
show_entry(opt, prefix, desc, base);
@@ -114,12 +114,13 @@ static void show_entry(struct diff_options *opt, const char *prefix,
114114
}
115115

116116
static void skip_uninteresting(struct tree_desc *t, struct strbuf *base,
117-
struct diff_options *opt, int *match)
117+
struct diff_options *opt,
118+
enum interesting *match)
118119
{
119120
while (t->size) {
120121
*match = tree_entry_interesting(&t->entry, base, 0, &opt->pathspec);
121122
if (*match) {
122-
if (*match < 0)
123+
if (*match == all_entries_not_interesting)
123124
t->size = 0;
124125
break;
125126
}
@@ -132,7 +133,8 @@ int diff_tree(struct tree_desc *t1, struct tree_desc *t2,
132133
{
133134
struct strbuf base;
134135
int baselen = strlen(base_str);
135-
int t1_match = 0, t2_match = 0;
136+
enum interesting t1_match = entry_not_interesting;
137+
enum interesting t2_match = entry_not_interesting;
136138

137139
/* Enable recursion indefinitely */
138140
opt->pathspec.recursive = DIFF_OPT_TST(opt, RECURSIVE);

tree-walk.c

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -577,27 +577,23 @@ static int match_dir_prefix(const char *base,
577577
*
578578
* Pre-condition: either baselen == base_offset (i.e. empty path)
579579
* or base[baselen-1] == '/' (i.e. with trailing slash).
580-
*
581-
* Return:
582-
* - 2 for "yes, and all subsequent entries will be"
583-
* - 1 for yes
584-
* - zero for no
585-
* - negative for "no, and no subsequent entries will be either"
586580
*/
587-
int tree_entry_interesting(const struct name_entry *entry,
588-
struct strbuf *base, int base_offset,
589-
const struct pathspec *ps)
581+
enum interesting tree_entry_interesting(const struct name_entry *entry,
582+
struct strbuf *base, int base_offset,
583+
const struct pathspec *ps)
590584
{
591585
int i;
592586
int pathlen, baselen = base->len - base_offset;
593-
int never_interesting = ps->has_wildcard ? 0 : -1;
587+
int never_interesting = ps->has_wildcard ?
588+
entry_not_interesting : all_entries_not_interesting;
594589

595590
if (!ps->nr) {
596591
if (!ps->recursive || ps->max_depth == -1)
597-
return 2;
598-
return !!within_depth(base->buf + base_offset, baselen,
599-
!!S_ISDIR(entry->mode),
600-
ps->max_depth);
592+
return all_entries_interesting;
593+
return within_depth(base->buf + base_offset, baselen,
594+
!!S_ISDIR(entry->mode),
595+
ps->max_depth) ?
596+
entry_interesting : entry_not_interesting;
601597
}
602598

603599
pathlen = tree_entry_len(entry);
@@ -614,31 +610,32 @@ int tree_entry_interesting(const struct name_entry *entry,
614610
goto match_wildcards;
615611

616612
if (!ps->recursive || ps->max_depth == -1)
617-
return 2;
613+
return all_entries_interesting;
618614

619-
return !!within_depth(base_str + matchlen + 1,
620-
baselen - matchlen - 1,
621-
!!S_ISDIR(entry->mode),
622-
ps->max_depth);
615+
return within_depth(base_str + matchlen + 1,
616+
baselen - matchlen - 1,
617+
!!S_ISDIR(entry->mode),
618+
ps->max_depth) ?
619+
entry_interesting : entry_not_interesting;
623620
}
624621

625622
/* Either there must be no base, or the base must match. */
626623
if (baselen == 0 || !strncmp(base_str, match, baselen)) {
627624
if (match_entry(entry, pathlen,
628625
match + baselen, matchlen - baselen,
629626
&never_interesting))
630-
return 1;
627+
return entry_interesting;
631628

632629
if (ps->items[i].use_wildcard) {
633630
if (!fnmatch(match + baselen, entry->path, 0))
634-
return 1;
631+
return entry_interesting;
635632

636633
/*
637634
* Match all directories. We'll try to
638635
* match files later on.
639636
*/
640637
if (ps->recursive && S_ISDIR(entry->mode))
641-
return 1;
638+
return entry_interesting;
642639
}
643640

644641
continue;
@@ -657,7 +654,7 @@ int tree_entry_interesting(const struct name_entry *entry,
657654

658655
if (!fnmatch(match, base->buf + base_offset, 0)) {
659656
strbuf_setlen(base, base_offset + baselen);
660-
return 1;
657+
return entry_interesting;
661658
}
662659
strbuf_setlen(base, base_offset + baselen);
663660

@@ -666,7 +663,7 @@ int tree_entry_interesting(const struct name_entry *entry,
666663
* later on.
667664
*/
668665
if (ps->recursive && S_ISDIR(entry->mode))
669-
return 1;
666+
return entry_interesting;
670667
}
671668
return never_interesting; /* No matches */
672669
}

tree-walk.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ static inline int traverse_path_len(const struct traverse_info *info, const stru
6161
return info->pathlen + tree_entry_len(n);
6262
}
6363

64-
extern int tree_entry_interesting(const struct name_entry *, struct strbuf *, int, const struct pathspec *ps);
64+
/* in general, positive means "kind of interesting" */
65+
enum interesting {
66+
all_entries_not_interesting = -1, /* no, and no subsequent entries will be either */
67+
entry_not_interesting = 0,
68+
entry_interesting = 1,
69+
all_entries_interesting = 2 /* yes, and all subsequent entries will be */
70+
};
71+
72+
extern enum interesting tree_entry_interesting(const struct name_entry *,
73+
struct strbuf *, int,
74+
const struct pathspec *ps);
6575

6676
#endif

tree.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,20 @@ static int read_tree_1(struct tree *tree, struct strbuf *base,
5252
struct tree_desc desc;
5353
struct name_entry entry;
5454
unsigned char sha1[20];
55-
int len, retval = 0, oldlen = base->len;
55+
int len, oldlen = base->len;
56+
enum interesting retval = entry_not_interesting;
5657

5758
if (parse_tree(tree))
5859
return -1;
5960

6061
init_tree_desc(&desc, tree->buffer, tree->size);
6162

6263
while (tree_entry(&desc, &entry)) {
63-
if (retval != 2) {
64+
if (retval != all_entries_interesting) {
6465
retval = tree_entry_interesting(&entry, base, 0, pathspec);
65-
if (retval < 0)
66+
if (retval == all_entries_not_interesting)
6667
break;
67-
if (retval == 0)
68+
if (retval == entry_not_interesting)
6869
continue;
6970
}
7071

0 commit comments

Comments
 (0)