161
istate->cache_changed |= CACHE_TREE_CHANGED;
162
}
163
164
+/*
165
+ * Check whether this_ce and the next entry in the index form a D/F
166
+ * conflict ("path" vs "path/file"). Returns the conflicting "path/..."
167
+ * name when one is found, or NULL otherwise.
168
+ *
169
+ * The cache is sorted, so "path/file" sorts after "path" and the
170
+ * conflict is usually visible as adjacent entries. But other entries
171
+ * can sort between them -- e.g. "path-internal" sits between "path"
172
+ * and "path/file" because '-' (0x2D) precedes '/' (0x2F) -- so when
173
+ * the immediately following entry shares our prefix but starts with a
174
+ * character that sorts before '/', binary search for "path/" instead.
175
+ */
176
+static const char *find_df_conflict(struct index_state *istate,
177
+ const struct cache_entry *this_ce,
178
+ const struct cache_entry *next_ce)
179
+{
180
+ const char *this_name = this_ce->name;
181
+ const char *next_name = next_ce->name;
182
+ int this_len = ce_namelen(this_ce);
183
+ const struct cache_entry *other;
184
+ struct strbuf probe = STRBUF_INIT;
185
+ int pos;
186
+
187
+ if (this_len >= ce_namelen(next_ce) ||
188
+ next_name[this_len] > '/' ||
189
+ strncmp(this_name, next_name, this_len))
190
+ return NULL;
191
+
192
+ if (next_name[this_len] == '/')
193
+ return next_name;
194
+
195
+ strbuf_add(&probe, this_name, this_len);
196
+ strbuf_addch(&probe, '/');
197
+ pos = index_name_pos_sparse(istate, probe.buf, probe.len);
198
+ strbuf_release(&probe);
199
+
200
+ if (pos < 0)
201
+ pos = -pos - 1;
202
+ if (pos >= (int)istate->cache_nr)
203
+ return NULL;
204
+ other = istate->cache[pos];
205
+ if (ce_namelen(other) > this_len &&
206
+ other->name[this_len] == '/' &&
207
+ !strncmp(this_name, other->name, this_len))
208
+ return other->name;
209
+ return NULL;
210
+}
211
+
212
static int verify_cache(struct index_state *istate, int flags)
213
{
214
unsigned i, funny;
238
*/
239
funny = 0;
240
for (i = 0; i + 1 < istate->cache_nr; i++) {
193
- /* path/file always comes after path because of the way
194
- * the cache is sorted. Also path can appear only once,
195
- * which means conflicting one would immediately follow.
196
- */
241
const struct cache_entry *this_ce = istate->cache[i];
242
const struct cache_entry *next_ce = istate->cache[i + 1];
199
- const char *this_name = this_ce->name;
200
- const char *next_name = next_ce->name;
201
- int this_len = ce_namelen(this_ce);
202
- if (this_len < ce_namelen(next_ce) &&
203
- next_name[this_len] == '/' &&
204
- strncmp(this_name, next_name, this_len) == 0) {
243
+ const char *conflict_name;
244
+
245
+ conflict_name = find_df_conflict(istate, this_ce, next_ce);
246
+ if (conflict_name) {
247
if (10 < ++funny) {
248
fprintf(stderr, "...\n");
249
break;
250
}
251
fprintf(stderr, "You have both %s and %s\n",
210
- this_name, next_name);
252
+ this_ce->name, conflict_name);
253
}
254
}
255
if (funny)