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 <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Nguyễn Thái Ngọc Duy 2011-10-24 17:36:10 +11:00 коммит произвёл Junio C Hamano
Родитель 02cb67530e
Коммит d688cf07b1
6 изменённых файлов: 56 добавлений и 44 удалений

Просмотреть файл

@ -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;
}

Просмотреть файл

@ -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)
@ -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;
}

Просмотреть файл

@ -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);
@ -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;
}
@ -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);

Просмотреть файл

@ -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);
@ -614,12 +610,13 @@ 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. */
@ -627,18 +624,18 @@ int tree_entry_interesting(const struct name_entry *entry,
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;
@ -657,7 +654,7 @@ match_wildcards:
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);
@ -666,7 +663,7 @@ match_wildcards:
* later on.
*/
if (ps->recursive && S_ISDIR(entry->mode))
return 1;
return entry_interesting;
}
return never_interesting; /* No matches */
}

Просмотреть файл

@ -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
tree.c
Просмотреть файл

@ -52,7 +52,8 @@ 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;
@ -60,11 +61,11 @@ static int read_tree_1(struct tree *tree, struct strbuf *base,
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;
}