7
*/
8
#include "cache.h"
9
#include "dir.h"
10
+#include "refs.h"
11
12
struct path_simplify {
13
int len;
14
const char *path;
15
};
16
17
+static int read_directory_recursive(struct dir_struct *dir,
18
+ const char *path, const char *base, int baselen,
19
+ int check_only, const struct path_simplify *simplify);
20
+
21
int common_prefix(const char **pathspec)
22
{
23
const char *path, *slash, *next;
291
return ent;
292
}
293
289
-static int dir_exists(const char *dirname, int len)
294
+enum exist_status {
295
+ index_nonexistent = 0,
296
+ index_directory,
297
+ index_gitdir,
298
+};
299
+
300
+/*
301
+ * The index sorts alphabetically by entry name, which
302
+ * means that a gitlink sorts as '\0' at the end, while
303
+ * a directory (which is defined not as an entry, but as
304
+ * the files it contains) will sort with the '/' at the
305
+ * end.
306
+ */
307
+static enum exist_status directory_exists_in_index(const char *dirname, int len)
308
{
309
int pos = cache_name_pos(dirname, len);
292
- if (pos >= 0)
293
- return 1;
294
- pos = -pos-1;
295
- if (pos >= active_nr) /* can't */
296
- return 0;
297
- return !strncmp(active_cache[pos]->name, dirname, len);
310
+ if (pos < 0)
311
+ pos = -pos-1;
312
+ while (pos < active_nr) {
313
+ struct cache_entry *ce = active_cache[pos++];
314
+ unsigned char endchar;
315
+
316
+ if (strncmp(ce->name, dirname, len))
317
+ break;
318
+ endchar = ce->name[len];
319
+ if (endchar > '/')
320
+ break;
321
+ if (endchar == '/')
322
+ return index_directory;
323
+ if (!endchar && S_ISDIRLNK(ntohl(ce->ce_mode)))
324
+ return index_gitdir;
325
+ }
326
+ return index_nonexistent;
327
+}
328
+
329
+/*
330
+ * When we find a directory when traversing the filesystem, we
331
+ * have three distinct cases:
332
+ *
333
+ * - ignore it
334
+ * - see it as a directory
335
+ * - recurse into it
336
+ *
337
+ * and which one we choose depends on a combination of existing
338
+ * git index contents and the flags passed into the directory
339
+ * traversal routine.
340
+ *
341
+ * Case 1: If we *already* have entries in the index under that
342
+ * directory name, we always recurse into the directory to see
343
+ * all the files.
344
+ *
345
+ * Case 2: If we *already* have that directory name as a gitlink,
346
+ * we always continue to see it as a gitlink, regardless of whether
347
+ * there is an actual git directory there or not (it might not
348
+ * be checked out as a subproject!)
349
+ *
350
+ * Case 3: if we didn't have it in the index previously, we
351
+ * have a few sub-cases:
352
+ *
353
+ * (a) if "show_other_directories" is true, we show it as
354
+ * just a directory, unless "hide_empty_directories" is
355
+ * also true and the directory is empty, in which case
356
+ * we just ignore it entirely.
357
+ * (b) if it looks like a git directory, and we don't have
358
+ * 'no_dirlinks' set we treat it as a gitlink, and show it
359
+ * as a directory.
360
+ * (c) otherwise, we recurse into it.
361
+ */
362
+enum directory_treatment {
363
+ show_directory,
364
+ ignore_directory,
365
+ recurse_into_directory,
366
+};
367
+
368
+static enum directory_treatment treat_directory(struct dir_struct *dir,
369
+ const char *dirname, int len,
370
+ const struct path_simplify *simplify)
371
+{
372
+ /* The "len-1" is to strip the final '/' */
373
+ switch (directory_exists_in_index(dirname, len-1)) {
374
+ case index_directory:
375
+ return recurse_into_directory;
376
+
377
+ case index_gitdir:
378
+ return show_directory;
379
+
380
+ case index_nonexistent:
381
+ if (dir->show_other_directories)
382
+ break;
383
+ if (!dir->no_dirlinks) {
384
+ unsigned char sha1[20];
385
+ if (resolve_gitlink_ref(dirname, "HEAD", sha1) == 0)
386
+ return show_directory;
387
+ }
388
+ return recurse_into_directory;
389
+ }
390
+
391
+ /* This is the "show_other_directories" case */
392
+ if (!dir->hide_empty_directories)
393
+ return show_directory;
394
+ if (!read_directory_recursive(dir, dirname, dirname, len, 1, simplify))
395
+ return ignore_directory;
396
+ return show_directory;
397
}
398
399
/*
479
case DT_DIR:
480
memcpy(fullname + baselen + len, "/", 2);
481
len++;
383
- if (dir->show_other_directories &&
384
- !dir_exists(fullname, baselen + len)) {
385
- if (dir->hide_empty_directories &&
386
- !read_directory_recursive(dir,
387
- fullname, fullname,
388
- baselen + len, 1, simplify))
389
- continue;
482
+ switch (treat_directory(dir, fullname, baselen + len, simplify)) {
483
+ case show_directory:
484
break;
485
+ case recurse_into_directory:
486
+ contents += read_directory_recursive(dir,
487
+ fullname, fullname, baselen + len, 0, simplify);
488
+ continue;
489
+ case ignore_directory:
490
+ continue;
491
}
392
-
393
- contents += read_directory_recursive(dir,
394
- fullname, fullname, baselen + len, 0, simplify);
395
- continue;
492
+ break;
493
case DT_REG:
494
case DT_LNK:
495
break;