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>

Nguyễn Thái Ngọc Duy committed Oct 24, 2011 at 17:36 UTC d688cf07b15b664a2164c3d92bcb5e8265400a2b
6 files changed +56 -44
builtin/grep.c
+5 -4
@@ -542,18 +542,19 @@ static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec, int
542 static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
543 struct tree_desc *tree, struct strbuf *base, int tn_len)
544 {
545 - int hit = 0, match = 0;
545 + int hit = 0;
546 + enum interesting match = entry_not_interesting;
547 struct name_entry entry;
548 int old_baselen = base->len;
549
550 while (tree_entry(tree, &entry)) {
551 int te_len = tree_entry_len(&entry);
552
552 - if (match != 2) {
553 + if (match != all_entries_interesting) {
554 match = tree_entry_interesting(&entry, base, tn_len, pathspec);
554 - if (match < 0)
555 + if (match == all_entries_not_interesting)
556 break;
556 - if (match == 0)
557 + if (match == entry_not_interesting)
558 continue;
559 }
560
list-objects.c
+5 -4
@@ -71,7 +71,8 @@ static void process_tree(struct rev_info *revs,
71 struct tree_desc desc;
72 struct name_entry entry;
73 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;
76 int baselen = base->len;
77
78 if (!revs->tree_objects)
@@ -97,12 +98,12 @@ static void process_tree(struct rev_info *revs,
98 init_tree_desc(&desc, tree->buffer, tree->size);
99
100 while (tree_entry(&desc, &entry)) {
100 - if (match != 2) {
101 + if (match != all_entries_interesting) {
102 match = tree_entry_interesting(&entry, base, 0,
103 &revs->diffopt.pathspec);
103 - if (match < 0)
104 + if (match == all_entries_not_interesting)
105 break;
105 - if (match == 0)
106 + if (match == entry_not_interesting)
107 continue;
108 }
109
tree-diff.c
+9 -7
@@ -64,14 +64,14 @@ static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2,
64 static void show_tree(struct diff_options *opt, const char *prefix,
65 struct tree_desc *desc, struct strbuf *base)
66 {
67 - int match = 0;
67 + enum interesting match = entry_not_interesting;
68 for (; desc->size; update_tree_entry(desc)) {
69 - if (match != 2) {
69 + if (match != all_entries_interesting) {
70 match = tree_entry_interesting(&desc->entry, base, 0,
71 &opt->pathspec);
72 - if (match < 0)
72 + if (match == all_entries_not_interesting)
73 break;
74 - if (match == 0)
74 + if (match == entry_not_interesting)
75 continue;
76 }
77 show_entry(opt, prefix, desc, base);
@@ -114,12 +114,13 @@ static void show_entry(struct diff_options *opt, const char *prefix,
114 }
115
116 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)
119 {
120 while (t->size) {
121 *match = tree_entry_interesting(&t->entry, base, 0, &opt->pathspec);
122 if (*match) {
122 - if (*match < 0)
123 + if (*match == all_entries_not_interesting)
124 t->size = 0;
125 break;
126 }
@@ -132,7 +133,8 @@ int diff_tree(struct tree_desc *t1, struct tree_desc *t2,
133 {
134 struct strbuf base;
135 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;
138
139 /* Enable recursion indefinitely */
140 opt->pathspec.recursive = DIFF_OPT_TST(opt, RECURSIVE);
tree-walk.c
+21 -24
@@ -577,27 +577,23 @@ static int match_dir_prefix(const char *base,
577 *
578 * Pre-condition: either baselen == base_offset (i.e. empty path)
579 * 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"
580 */
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)
584 {
585 int i;
586 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;
589
590 if (!ps->nr) {
591 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;
597 }
598
599 pathlen = tree_entry_len(entry);
@@ -614,12 +610,13 @@ int tree_entry_interesting(const struct name_entry *entry,
610 goto match_wildcards;
611
612 if (!ps->recursive || ps->max_depth == -1)
617 - return 2;
613 + return all_entries_interesting;
614
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;
620 }
621
622 /* Either there must be no base, or the base must match. */
@@ -627,18 +624,18 @@ int tree_entry_interesting(const struct name_entry *entry,
624 if (match_entry(entry, pathlen,
625 match + baselen, matchlen - baselen,
626 &never_interesting))
630 - return 1;
627 + return entry_interesting;
628
629 if (ps->items[i].use_wildcard) {
630 if (!fnmatch(match + baselen, entry->path, 0))
634 - return 1;
631 + return entry_interesting;
632
633 /*
634 * Match all directories. We'll try to
635 * match files later on.
636 */
637 if (ps->recursive && S_ISDIR(entry->mode))
641 - return 1;
638 + return entry_interesting;
639 }
640
641 continue;
@@ -657,7 +654,7 @@ match_wildcards:
654
655 if (!fnmatch(match, base->buf + base_offset, 0)) {
656 strbuf_setlen(base, base_offset + baselen);
660 - return 1;
657 + return entry_interesting;
658 }
659 strbuf_setlen(base, base_offset + baselen);
660
@@ -666,7 +663,7 @@ match_wildcards:
663 * later on.
664 */
665 if (ps->recursive && S_ISDIR(entry->mode))
669 - return 1;
666 + return entry_interesting;
667 }
668 return never_interesting; /* No matches */
669 }
tree-walk.h
+11 -1
@@ -61,6 +61,16 @@ static inline int traverse_path_len(const struct traverse_info *info, const stru
61 return info->pathlen + tree_entry_len(n);
62 }
63
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);
75
76 #endif
tree.c
+5 -4
@@ -52,7 +52,8 @@ static int read_tree_1(struct tree *tree, struct strbuf *base,
52 struct tree_desc desc;
53 struct name_entry entry;
54 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;
57
58 if (parse_tree(tree))
59 return -1;
@@ -60,11 +61,11 @@ static int read_tree_1(struct tree *tree, struct strbuf *base,
61 init_tree_desc(&desc, tree->buffer, tree->size);
62
63 while (tree_entry(&desc, &entry)) {
63 - if (retval != 2) {
64 + if (retval != all_entries_interesting) {
65 retval = tree_entry_interesting(&entry, base, 0, pathspec);
65 - if (retval < 0)
66 + if (retval == all_entries_not_interesting)
67 break;
67 - if (retval == 0)
68 + if (retval == entry_not_interesting)
69 continue;
70 }
71