@matheusbsbs / matheus-bagliano / commits / 7d2f667b12
Fork 0

Teach tree_entry_interesting() that the tree entries are sorted.

When we are looking at a tree entry with pathspecs, if all the pathspecs sort strictly earlier than the entry we are currently looking at, there is no way later entries in the same tree would match our pathspecs, because the entries are sorted. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Mar 21, 2007 at 09:51 UTC 7d2f667b12334cea68087eb7da717efd7b3d80f6
1 file changed +35 -6
tree-diff.c
+35 -6
@@ -81,6 +81,7 @@ static int tree_entry_interesting(struct tree_desc *desc, const char *base, int
81 unsigned mode;
82 int i;
83 int pathlen;
84 + int never_interesting = -1;
85
86 if (!opt->nr_paths)
87 return 1;
@@ -89,9 +90,10 @@ static int tree_entry_interesting(struct tree_desc *desc, const char *base, int
90
91 pathlen = tree_entry_len(path, sha1);
92
92 - for (i=0; i < opt->nr_paths; i++) {
93 + for (i = 0; i < opt->nr_paths; i++) {
94 const char *match = opt->paths[i];
95 int matchlen = opt->pathlens[i];
96 + int m;
97
98 if (baselen >= matchlen) {
99 /* If it doesn't match, move along... */
@@ -109,6 +111,30 @@ static int tree_entry_interesting(struct tree_desc *desc, const char *base, int
111 match += baselen;
112 matchlen -= baselen;
113
114 + /*
115 + * Does match sort strictly earlier than path with their
116 + * common parts?
117 + */
118 + m = strncmp(match, path,
119 + (matchlen < pathlen) ? matchlen : pathlen);
120 + if (m < 0)
121 + continue;
122 +
123 + /*
124 + * If we come here even once, that means there is at
125 + * least one pathspec that would sort equal to or
126 + * later than the path we are currently looking at.
127 + * In other words, if we have never reached this point
128 + * after iterating all pathspecs, it means all
129 + * pathspecs are either outside of base, or inside the
130 + * base but sorts strictly earlier than the current
131 + * one. In either case, they will never match the
132 + * subsequent entries. In such a case, we initialized
133 + * the variable to -1 and that is what will be
134 + * returned, allowing the caller to terminate early.
135 + */
136 + never_interesting = 0;
137 +
138 if (pathlen > matchlen)
139 continue;
140
@@ -119,12 +145,15 @@ static int tree_entry_interesting(struct tree_desc *desc, const char *base, int
145 continue;
146 }
147
122 - if (strncmp(path, match, pathlen))
123 - continue;
124 -
125 - return 1;
148 + /*
149 + * If common part matched earlier then it is a hit,
150 + * because we rejected the case where path is not a
151 + * leading directory and is shorter than match.
152 + */
153 + if (!m)
154 + return 1;
155 }
127 - return 0; /* No matches */
156 + return never_interesting; /* No matches */
157 }
158
159 /* A whole sub-tree went away or appeared */