Skip to content

Commit 95a6834

Browse files
aspiersgitster
authored andcommitted
Improve documentation and comments regarding directory traversal API
traversal API has a few potentially confusing properties. These comments clarify a few key aspects and will hopefully make it easier to understand for other newcomers in the future. Signed-off-by: Adam Spiers <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f1a7082 commit 95a6834

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

Documentation/technical/api-directory-listing.txt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ Data structure
99
--------------
1010

1111
`struct dir_struct` structure is used to pass directory traversal
12-
options to the library and to record the paths discovered. The notable
13-
options are:
12+
options to the library and to record the paths discovered. A single
13+
`struct dir_struct` is used regardless of whether or not the traversal
14+
recursively descends into subdirectories.
15+
16+
The notable options are:
1417

1518
`exclude_per_dir`::
1619

@@ -39,7 +42,7 @@ options are:
3942
If set, recurse into a directory that looks like a git
4043
directory. Otherwise it is shown as a directory.
4144

42-
The result of the enumeration is left in these fields::
45+
The result of the enumeration is left in these fields:
4346

4447
`entries[]`::
4548

dir.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* This handles recursive filename detection with exclude
33
* files, index knowledge etc..
44
*
5+
* See Documentation/technical/api-directory-listing.txt
6+
*
57
* Copyright (C) Linus Torvalds, 2005-2006
68
* Junio Hamano, 2005-2006
79
*/
@@ -476,6 +478,10 @@ void add_excludes_from_file(struct dir_struct *dir, const char *fname)
476478
die("cannot use %s as an exclude file", fname);
477479
}
478480

481+
/*
482+
* Loads the per-directory exclude list for the substring of base
483+
* which has a char length of baselen.
484+
*/
479485
static void prep_exclude(struct dir_struct *dir, const char *base, int baselen)
480486
{
481487
struct exclude_list *el;
@@ -486,7 +492,7 @@ static void prep_exclude(struct dir_struct *dir, const char *base, int baselen)
486492
(baselen + strlen(dir->exclude_per_dir) >= PATH_MAX))
487493
return; /* too long a path -- ignore */
488494

489-
/* Pop the ones that are not the prefix of the path being checked. */
495+
/* Pop the directories that are not the prefix of the path being checked. */
490496
el = &dir->exclude_list[EXC_DIRS];
491497
while ((stk = dir->exclude_stack) != NULL) {
492498
if (stk->baselen <= baselen &&

dir.h

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef DIR_H
22
#define DIR_H
33

4+
/* See Documentation/technical/api-directory-listing.txt */
5+
46
#include "strbuf.h"
57

68
struct dir_entry {
@@ -13,6 +15,12 @@ struct dir_entry {
1315
#define EXC_FLAG_MUSTBEDIR 8
1416
#define EXC_FLAG_NEGATIVE 16
1517

18+
/*
19+
* Each .gitignore file will be parsed into patterns which are then
20+
* appended to the relevant exclude_list (either EXC_DIRS or
21+
* EXC_FILE). exclude_lists are also used to represent the list of
22+
* --exclude values passed via CLI args (EXC_CMDL).
23+
*/
1624
struct exclude_list {
1725
int nr;
1826
int alloc;
@@ -26,9 +34,15 @@ struct exclude_list {
2634
} **excludes;
2735
};
2836

37+
/*
38+
* The contents of the per-directory exclude files are lazily read on
39+
* demand and then cached in memory, one per exclude_stack struct, in
40+
* order to avoid opening and parsing each one every time that
41+
* directory is traversed.
42+
*/
2943
struct exclude_stack {
30-
struct exclude_stack *prev;
31-
char *filebuf;
44+
struct exclude_stack *prev; /* the struct exclude_stack for the parent directory */
45+
char *filebuf; /* remember pointer to per-directory exclude file contents so we can free() */
3246
int baselen;
3347
int exclude_ix;
3448
};
@@ -59,6 +73,14 @@ struct dir_struct {
5973
#define EXC_DIRS 1
6074
#define EXC_FILE 2
6175

76+
/*
77+
* Temporary variables which are used during loading of the
78+
* per-directory exclude lists.
79+
*
80+
* exclude_stack points to the top of the exclude_stack, and
81+
* basebuf contains the full path to the current
82+
* (sub)directory in the traversal.
83+
*/
6284
struct exclude_stack *exclude_stack;
6385
char basebuf[PATH_MAX];
6486
};

0 commit comments

Comments
 (0)