Skip to content

Commit

Permalink
tree_entry_interesting(): give meaningful names to return values
Browse files Browse the repository at this point in the history
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 <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Nguyễn Thái Ngọc Duy authored and Junio C Hamano committed Oct 27, 2011
1 parent 02cb675 commit d688cf0
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 44 deletions.
9 changes: 5 additions & 4 deletions builtin/grep.c
Original file line number Diff line number Diff line change
Expand Up @@ -542,18 +542,19 @@ static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec, int
static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
struct tree_desc *tree, struct strbuf *base, int tn_len)
{
int hit = 0, match = 0;
int hit = 0;
enum interesting match = entry_not_interesting;
struct name_entry entry;
int old_baselen = base->len;

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

if (match != 2) {
if (match != all_entries_interesting) {
match = tree_entry_interesting(&entry, base, tn_len, pathspec);
if (match < 0)
if (match == all_entries_not_interesting)
break;
if (match == 0)
if (match == entry_not_interesting)
continue;
}

Expand Down
9 changes: 5 additions & 4 deletions list-objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ static void process_tree(struct rev_info *revs,
struct tree_desc desc;
struct name_entry entry;
struct name_path me;
int match = revs->diffopt.pathspec.nr == 0 ? 2 : 0;
enum interesting match = revs->diffopt.pathspec.nr == 0 ?
all_entries_interesting: entry_not_interesting;
int baselen = base->len;

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

while (tree_entry(&desc, &entry)) {
if (match != 2) {
if (match != all_entries_interesting) {
match = tree_entry_interesting(&entry, base, 0,
&revs->diffopt.pathspec);
if (match < 0)
if (match == all_entries_not_interesting)
break;
if (match == 0)
if (match == entry_not_interesting)
continue;
}

Expand Down
16 changes: 9 additions & 7 deletions tree-diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2,
static void show_tree(struct diff_options *opt, const char *prefix,
struct tree_desc *desc, struct strbuf *base)
{
int match = 0;
enum interesting match = entry_not_interesting;
for (; desc->size; update_tree_entry(desc)) {
if (match != 2) {
if (match != all_entries_interesting) {
match = tree_entry_interesting(&desc->entry, base, 0,
&opt->pathspec);
if (match < 0)
if (match == all_entries_not_interesting)
break;
if (match == 0)
if (match == entry_not_interesting)
continue;
}
show_entry(opt, prefix, desc, base);
Expand Down Expand Up @@ -114,12 +114,13 @@ static void show_entry(struct diff_options *opt, const char *prefix,
}

static void skip_uninteresting(struct tree_desc *t, struct strbuf *base,
struct diff_options *opt, int *match)
struct diff_options *opt,
enum interesting *match)
{
while (t->size) {
*match = tree_entry_interesting(&t->entry, base, 0, &opt->pathspec);
if (*match) {
if (*match < 0)
if (*match == all_entries_not_interesting)
t->size = 0;
break;
}
Expand All @@ -132,7 +133,8 @@ int diff_tree(struct tree_desc *t1, struct tree_desc *t2,
{
struct strbuf base;
int baselen = strlen(base_str);
int t1_match = 0, t2_match = 0;
enum interesting t1_match = entry_not_interesting;
enum interesting t2_match = entry_not_interesting;

/* Enable recursion indefinitely */
opt->pathspec.recursive = DIFF_OPT_TST(opt, RECURSIVE);
Expand Down
45 changes: 21 additions & 24 deletions tree-walk.c
Original file line number Diff line number Diff line change
Expand Up @@ -577,27 +577,23 @@ static int match_dir_prefix(const char *base,
*
* Pre-condition: either baselen == base_offset (i.e. empty path)
* or base[baselen-1] == '/' (i.e. with trailing slash).
*
* Return:
* - 2 for "yes, and all subsequent entries will be"
* - 1 for yes
* - zero for no
* - negative for "no, and no subsequent entries will be either"
*/
int tree_entry_interesting(const struct name_entry *entry,
struct strbuf *base, int base_offset,
const struct pathspec *ps)
enum interesting tree_entry_interesting(const struct name_entry *entry,
struct strbuf *base, int base_offset,
const struct pathspec *ps)
{
int i;
int pathlen, baselen = base->len - base_offset;
int never_interesting = ps->has_wildcard ? 0 : -1;
int never_interesting = ps->has_wildcard ?
entry_not_interesting : all_entries_not_interesting;

if (!ps->nr) {
if (!ps->recursive || ps->max_depth == -1)
return 2;
return !!within_depth(base->buf + base_offset, baselen,
!!S_ISDIR(entry->mode),
ps->max_depth);
return all_entries_interesting;
return within_depth(base->buf + base_offset, baselen,
!!S_ISDIR(entry->mode),
ps->max_depth) ?
entry_interesting : entry_not_interesting;
}

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

if (!ps->recursive || ps->max_depth == -1)
return 2;
return all_entries_interesting;

return !!within_depth(base_str + matchlen + 1,
baselen - matchlen - 1,
!!S_ISDIR(entry->mode),
ps->max_depth);
return within_depth(base_str + matchlen + 1,
baselen - matchlen - 1,
!!S_ISDIR(entry->mode),
ps->max_depth) ?
entry_interesting : entry_not_interesting;
}

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

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

/*
* Match all directories. We'll try to
* match files later on.
*/
if (ps->recursive && S_ISDIR(entry->mode))
return 1;
return entry_interesting;
}

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

if (!fnmatch(match, base->buf + base_offset, 0)) {
strbuf_setlen(base, base_offset + baselen);
return 1;
return entry_interesting;
}
strbuf_setlen(base, base_offset + baselen);

Expand All @@ -666,7 +663,7 @@ int tree_entry_interesting(const struct name_entry *entry,
* later on.
*/
if (ps->recursive && S_ISDIR(entry->mode))
return 1;
return entry_interesting;
}
return never_interesting; /* No matches */
}
12 changes: 11 additions & 1 deletion tree-walk.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ static inline int traverse_path_len(const struct traverse_info *info, const stru
return info->pathlen + tree_entry_len(n);
}

extern int tree_entry_interesting(const struct name_entry *, struct strbuf *, int, const struct pathspec *ps);
/* in general, positive means "kind of interesting" */
enum interesting {
all_entries_not_interesting = -1, /* no, and no subsequent entries will be either */
entry_not_interesting = 0,
entry_interesting = 1,
all_entries_interesting = 2 /* yes, and all subsequent entries will be */
};

extern enum interesting tree_entry_interesting(const struct name_entry *,
struct strbuf *, int,
const struct pathspec *ps);

#endif
9 changes: 5 additions & 4 deletions tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,20 @@ static int read_tree_1(struct tree *tree, struct strbuf *base,
struct tree_desc desc;
struct name_entry entry;
unsigned char sha1[20];
int len, retval = 0, oldlen = base->len;
int len, oldlen = base->len;
enum interesting retval = entry_not_interesting;

if (parse_tree(tree))
return -1;

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

while (tree_entry(&desc, &entry)) {
if (retval != 2) {
if (retval != all_entries_interesting) {
retval = tree_entry_interesting(&entry, base, 0, pathspec);
if (retval < 0)
if (retval == all_entries_not_interesting)
break;
if (retval == 0)
if (retval == entry_not_interesting)
continue;
}

Expand Down

0 comments on commit d688cf0

Please sign in to comment.