Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

Git Notes

amlog

Message-Id: <1319438176-7304-6-git-send-email-pclouds@gmail.com>

0 notes on commit d688cf0

Please sign in to comment.
Something went wrong with that request. Please try again.