@matheusbsbs / matheus-bagliano / commits / 2c389fc8ec
Fork 0

Move tree_entry_interesting() to tree-walk.c and export it

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 Dec 15, 2010 at 22:02 UTC 2c389fc8ec57722aa1e8f49d316401e2e21d1b05
3 files changed +116 -112
tree-diff.c
-112
@@ -82,118 +82,6 @@ static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2, const
82 return 0;
83 }
84
85 -/*
86 - * Is a tree entry interesting given the pathspec we have?
87 - *
88 - * Pre-condition: baselen == 0 || base[baselen-1] == '/'
89 - *
90 - * Return:
91 - * - 2 for "yes, and all subsequent entries will be"
92 - * - 1 for yes
93 - * - zero for no
94 - * - negative for "no, and no subsequent entries will be either"
95 - */
96 -static int tree_entry_interesting(const struct name_entry *entry, const char *base, int baselen, const struct pathspec *ps)
97 -{
98 - int i;
99 - int pathlen;
100 - int never_interesting = -1;
101 -
102 - if (!ps || !ps->nr)
103 - return 2;
104 -
105 - pathlen = tree_entry_len(entry->path, entry->sha1);
106 -
107 - for (i = 0; i < ps->nr; i++) {
108 - const struct pathspec_item *item = ps->items+i;
109 - const char *match = item->match;
110 - int matchlen = item->len;
111 - int m = -1; /* signals that we haven't called strncmp() */
112 -
113 - if (baselen >= matchlen) {
114 - /* If it doesn't match, move along... */
115 - if (strncmp(base, match, matchlen))
116 - continue;
117 -
118 - /*
119 - * If the base is a subdirectory of a path which
120 - * was specified, all of them are interesting.
121 - */
122 - if (!matchlen ||
123 - base[matchlen] == '/' ||
124 - match[matchlen - 1] == '/')
125 - return 2;
126 -
127 - /* Just a random prefix match */
128 - continue;
129 - }
130 -
131 - /* Does the base match? */
132 - if (strncmp(base, match, baselen))
133 - continue;
134 -
135 - match += baselen;
136 - matchlen -= baselen;
137 -
138 - if (never_interesting) {
139 - /*
140 - * We have not seen any match that sorts later
141 - * than the current path.
142 - */
143 -
144 - /*
145 - * Does match sort strictly earlier than path
146 - * with their common parts?
147 - */
148 - m = strncmp(match, entry->path,
149 - (matchlen < pathlen) ? matchlen : pathlen);
150 - if (m < 0)
151 - continue;
152 -
153 - /*
154 - * If we come here even once, that means there is at
155 - * least one pathspec that would sort equal to or
156 - * later than the path we are currently looking at.
157 - * In other words, if we have never reached this point
158 - * after iterating all pathspecs, it means all
159 - * pathspecs are either outside of base, or inside the
160 - * base but sorts strictly earlier than the current
161 - * one. In either case, they will never match the
162 - * subsequent entries. In such a case, we initialized
163 - * the variable to -1 and that is what will be
164 - * returned, allowing the caller to terminate early.
165 - */
166 - never_interesting = 0;
167 - }
168 -
169 - if (pathlen > matchlen)
170 - continue;
171 -
172 - if (matchlen > pathlen) {
173 - if (match[pathlen] != '/')
174 - continue;
175 - if (!S_ISDIR(entry->mode))
176 - continue;
177 - }
178 -
179 - if (m == -1)
180 - /*
181 - * we cheated and did not do strncmp(), so we do
182 - * that here.
183 - */
184 - m = strncmp(match, entry->path, pathlen);
185 -
186 - /*
187 - * If common part matched earlier then it is a hit,
188 - * because we rejected the case where path is not a
189 - * leading directory and is shorter than match.
190 - */
191 - if (!m)
192 - return 1;
193 - }
194 - return never_interesting; /* No matches */
195 -}
196 -
85 /* A whole sub-tree went away or appeared */
86 static void show_tree(struct diff_options *opt, const char *prefix, struct tree_desc *desc, const char *base, int baselen)
87 {
tree-walk.c
+114
@@ -455,3 +455,117 @@ int get_tree_entry(const unsigned char *tree_sha1, const char *name, unsigned ch
455 free(tree);
456 return retval;
457 }
458 +
459 +/*
460 + * Is a tree entry interesting given the pathspec we have?
461 + *
462 + * Pre-condition: baselen == 0 || base[baselen-1] == '/'
463 + *
464 + * Return:
465 + * - 2 for "yes, and all subsequent entries will be"
466 + * - 1 for yes
467 + * - zero for no
468 + * - negative for "no, and no subsequent entries will be either"
469 + */
470 +int tree_entry_interesting(const struct name_entry *entry,
471 + const char *base, int baselen,
472 + const struct pathspec *ps)
473 +{
474 + int i;
475 + int pathlen;
476 + int never_interesting = -1;
477 +
478 + if (!ps || !ps->nr)
479 + return 2;
480 +
481 + pathlen = tree_entry_len(entry->path, entry->sha1);
482 +
483 + for (i = 0; i < ps->nr; i++) {
484 + const struct pathspec_item *item = ps->items+i;
485 + const char *match = item->match;
486 + int matchlen = item->len;
487 + int m = -1; /* signals that we haven't called strncmp() */
488 +
489 + if (baselen >= matchlen) {
490 + /* If it doesn't match, move along... */
491 + if (strncmp(base, match, matchlen))
492 + continue;
493 +
494 + /*
495 + * If the base is a subdirectory of a path which
496 + * was specified, all of them are interesting.
497 + */
498 + if (!matchlen ||
499 + base[matchlen] == '/' ||
500 + match[matchlen - 1] == '/')
501 + return 2;
502 +
503 + /* Just a random prefix match */
504 + continue;
505 + }
506 +
507 + /* Does the base match? */
508 + if (strncmp(base, match, baselen))
509 + continue;
510 +
511 + match += baselen;
512 + matchlen -= baselen;
513 +
514 + if (never_interesting) {
515 + /*
516 + * We have not seen any match that sorts later
517 + * than the current path.
518 + */
519 +
520 + /*
521 + * Does match sort strictly earlier than path
522 + * with their common parts?
523 + */
524 + m = strncmp(match, entry->path,
525 + (matchlen < pathlen) ? matchlen : pathlen);
526 + if (m < 0)
527 + continue;
528 +
529 + /*
530 + * If we come here even once, that means there is at
531 + * least one pathspec that would sort equal to or
532 + * later than the path we are currently looking at.
533 + * In other words, if we have never reached this point
534 + * after iterating all pathspecs, it means all
535 + * pathspecs are either outside of base, or inside the
536 + * base but sorts strictly earlier than the current
537 + * one. In either case, they will never match the
538 + * subsequent entries. In such a case, we initialized
539 + * the variable to -1 and that is what will be
540 + * returned, allowing the caller to terminate early.
541 + */
542 + never_interesting = 0;
543 + }
544 +
545 + if (pathlen > matchlen)
546 + continue;
547 +
548 + if (matchlen > pathlen) {
549 + if (match[pathlen] != '/')
550 + continue;
551 + if (!S_ISDIR(entry->mode))
552 + continue;
553 + }
554 +
555 + if (m == -1)
556 + /*
557 + * we cheated and did not do strncmp(), so we do
558 + * that here.
559 + */
560 + m = strncmp(match, entry->path, pathlen);
561 +
562 + /*
563 + * If common part matched earlier then it is a hit,
564 + * because we rejected the case where path is not a
565 + * leading directory and is shorter than match.
566 + */
567 + if (!m)
568 + return 1;
569 + }
570 + return never_interesting; /* No matches */
571 +}
tree-walk.h
+2
@@ -60,4 +60,6 @@ static inline int traverse_path_len(const struct traverse_info *info, const stru
60 return info->pathlen + tree_entry_len(n->path, n->sha1);
61 }
62
63 +extern int tree_entry_interesting(const struct name_entry *, const char *, int, const struct pathspec *ps);
64 +
65 #endif