dir: separate public from internal portion of dir_struct
In order to make it clearer to callers what portions of dir_struct are public API, and avoid errors from them setting fields that are meant as internal API, split the fields used for internal implementation reasons into a separate embedded struct. Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Elijah Newren committed
Feb 27, 2023 at 15:28 UTC
5fdf285e6254fff4d9560f72878456f0a53e2e38
2 files changed
+104
-96
dir.c
+58
-56
@@ -1190,7 +1190,7 @@ struct pattern_list *add_pattern_list(struct dir_struct *dir,
1190
struct pattern_list *pl;
1191
struct exclude_list_group *group;
1192
1193
- group = &dir->exclude_list_group[group_type];
1193
+ group = &dir->internal.exclude_list_group[group_type];
1194
ALLOC_GROW(group->pl, group->nr + 1, group->alloc);
1195
pl = &group->pl[group->nr++];
1196
memset(pl, 0, sizeof(*pl));
@@ -1211,7 +1211,7 @@ static void add_patterns_from_file_1(struct dir_struct *dir, const char *fname,
1211
* differently when dir->untracked is non-NULL.
1212
*/
1213
if (!dir->untracked)
1214
- dir->unmanaged_exclude_files++;
1214
+ dir->internal.unmanaged_exclude_files++;
1215
pl = add_pattern_list(dir, EXC_FILE, fname);
1216
if (add_patterns(fname, "", 0, pl, NULL, 0, oid_stat) < 0)
1217
die(_("cannot use %s as an exclude file"), fname);
@@ -1219,7 +1219,7 @@ static void add_patterns_from_file_1(struct dir_struct *dir, const char *fname,
1219
1220
void add_patterns_from_file(struct dir_struct *dir, const char *fname)
1221
{
1222
- dir->unmanaged_exclude_files++; /* see validate_untracked_cache() */
1222
+ dir->internal.unmanaged_exclude_files++; /* see validate_untracked_cache() */
1223
add_patterns_from_file_1(dir, fname, NULL);
1224
}
1225
@@ -1519,7 +1519,7 @@ static struct path_pattern *last_matching_pattern_from_lists(
1519
struct exclude_list_group *group;
1520
struct path_pattern *pattern;
1521
for (i = EXC_CMDL; i <= EXC_FILE; i++) {
1522
- group = &dir->exclude_list_group[i];
1522
+ group = &dir->internal.exclude_list_group[i];
1523
for (j = group->nr - 1; j >= 0; j--) {
1524
pattern = last_matching_pattern_from_list(
1525
pathname, pathlen, basename, dtype_p,
@@ -1545,20 +1545,20 @@ static void prep_exclude(struct dir_struct *dir,
1545
struct untracked_cache_dir *untracked;
1546
int current;
1547
1548
- group = &dir->exclude_list_group[EXC_DIRS];
1548
+ group = &dir->internal.exclude_list_group[EXC_DIRS];
1549
1550
/*
1551
* Pop the exclude lists from the EXCL_DIRS exclude_list_group
1552
* which originate from directories not in the prefix of the
1553
* path being checked.
1554
*/
1555
- while ((stk = dir->exclude_stack) != NULL) {
1555
+ while ((stk = dir->internal.exclude_stack) != NULL) {
1556
if (stk->baselen <= baselen &&
1557
- !strncmp(dir->basebuf.buf, base, stk->baselen))
1557
+ !strncmp(dir->internal.basebuf.buf, base, stk->baselen))
1558
break;
1559
- pl = &group->pl[dir->exclude_stack->exclude_ix];
1560
- dir->exclude_stack = stk->prev;
1561
- dir->pattern = NULL;
1559
+ pl = &group->pl[dir->internal.exclude_stack->exclude_ix];
1560
+ dir->internal.exclude_stack = stk->prev;
1561
+ dir->internal.pattern = NULL;
1562
free((char *)pl->src); /* see strbuf_detach() below */
1563
clear_pattern_list(pl);
1564
free(stk);
@@ -1566,7 +1566,7 @@ static void prep_exclude(struct dir_struct *dir,
1566
}
1567
1568
/* Skip traversing into sub directories if the parent is excluded */
1569
- if (dir->pattern)
1569
+ if (dir->internal.pattern)
1570
return;
1571
1572
/*
@@ -1574,12 +1574,12 @@ static void prep_exclude(struct dir_struct *dir,
1574
* memset(dir, 0, sizeof(*dir)) before use. Changing all of
1575
* them seems lots of work for little benefit.
1576
*/
1577
- if (!dir->basebuf.buf)
1578
- strbuf_init(&dir->basebuf, PATH_MAX);
1577
+ if (!dir->internal.basebuf.buf)
1578
+ strbuf_init(&dir->internal.basebuf, PATH_MAX);
1579
1580
/* Read from the parent directories and push them down. */
1581
current = stk ? stk->baselen : -1;
1582
- strbuf_setlen(&dir->basebuf, current < 0 ? 0 : current);
1582
+ strbuf_setlen(&dir->internal.basebuf, current < 0 ? 0 : current);
1583
if (dir->untracked)
1584
untracked = stk ? stk->ucd : dir->untracked->root;
1585
else
@@ -1599,32 +1599,33 @@ static void prep_exclude(struct dir_struct *dir,
1599
die("oops in prep_exclude");
1600
cp++;
1601
untracked =
1602
- lookup_untracked(dir->untracked, untracked,
1602
+ lookup_untracked(dir->untracked,
1603
+ untracked,
1604
base + current,
1605
cp - base - current);
1606
}
1606
- stk->prev = dir->exclude_stack;
1607
+ stk->prev = dir->internal.exclude_stack;
1608
stk->baselen = cp - base;
1609
stk->exclude_ix = group->nr;
1610
stk->ucd = untracked;
1611
pl = add_pattern_list(dir, EXC_DIRS, NULL);
1611
- strbuf_add(&dir->basebuf, base + current, stk->baselen - current);
1612
- assert(stk->baselen == dir->basebuf.len);
1612
+ strbuf_add(&dir->internal.basebuf, base + current, stk->baselen - current);
1613
+ assert(stk->baselen == dir->internal.basebuf.len);
1614
1615
/* Abort if the directory is excluded */
1616
if (stk->baselen) {
1617
int dt = DT_DIR;
1617
- dir->basebuf.buf[stk->baselen - 1] = 0;
1618
- dir->pattern = last_matching_pattern_from_lists(dir,
1618
+ dir->internal.basebuf.buf[stk->baselen - 1] = 0;
1619
+ dir->internal.pattern = last_matching_pattern_from_lists(dir,
1620
istate,
1620
- dir->basebuf.buf, stk->baselen - 1,
1621
- dir->basebuf.buf + current, &dt);
1622
- dir->basebuf.buf[stk->baselen - 1] = '/';
1623
- if (dir->pattern &&
1624
- dir->pattern->flags & PATTERN_FLAG_NEGATIVE)
1625
- dir->pattern = NULL;
1626
- if (dir->pattern) {
1627
- dir->exclude_stack = stk;
1621
+ dir->internal.basebuf.buf, stk->baselen - 1,
1622
+ dir->internal.basebuf.buf + current, &dt);
1623
+ dir->internal.basebuf.buf[stk->baselen - 1] = '/';
1624
+ if (dir->internal.pattern &&
1625
+ dir->internal.pattern->flags & PATTERN_FLAG_NEGATIVE)
1626
+ dir->internal.pattern = NULL;
1627
+ if (dir->internal.pattern) {
1628
+ dir->internal.exclude_stack = stk;
1629
return;
1630
}
1631
}
@@ -1647,15 +1648,15 @@ static void prep_exclude(struct dir_struct *dir,
1648
*/
1649
!is_null_oid(&untracked->exclude_oid))) {
1650
/*
1650
- * dir->basebuf gets reused by the traversal, but we
1651
- * need fname to remain unchanged to ensure the src
1652
- * member of each struct path_pattern correctly
1651
+ * dir->internal.basebuf gets reused by the traversal,
1652
+ * but we need fname to remain unchanged to ensure the
1653
+ * src member of each struct path_pattern correctly
1654
* back-references its source file. Other invocations
1655
* of add_pattern_list provide stable strings, so we
1656
* strbuf_detach() and free() here in the caller.
1657
*/
1658
struct strbuf sb = STRBUF_INIT;
1658
- strbuf_addbuf(&sb, &dir->basebuf);
1659
+ strbuf_addbuf(&sb, &dir->internal.basebuf);
1660
strbuf_addstr(&sb, dir->exclude_per_dir);
1661
pl->src = strbuf_detach(&sb, NULL);
1662
add_patterns(pl->src, pl->src, stk->baselen, pl, istate,
@@ -1681,10 +1682,10 @@ static void prep_exclude(struct dir_struct *dir,
1682
invalidate_gitignore(dir->untracked, untracked);
1683
oidcpy(&untracked->exclude_oid, &oid_stat.oid);
1684
}
1684
- dir->exclude_stack = stk;
1685
+ dir->internal.exclude_stack = stk;
1686
current = stk->baselen;
1687
}
1687
- strbuf_setlen(&dir->basebuf, baselen);
1688
+ strbuf_setlen(&dir->internal.basebuf, baselen);
1689
}
1690
1691
/*
@@ -1704,8 +1705,8 @@ struct path_pattern *last_matching_pattern(struct dir_struct *dir,
1705
1706
prep_exclude(dir, istate, pathname, basename-pathname);
1707
1707
- if (dir->pattern)
1708
- return dir->pattern;
1708
+ if (dir->internal.pattern)
1709
+ return dir->internal.pattern;
1710
1711
return last_matching_pattern_from_lists(dir, istate, pathname, pathlen,
1712
basename, dtype_p);
@@ -1742,7 +1743,7 @@ static struct dir_entry *dir_add_name(struct dir_struct *dir,
1743
if (index_file_exists(istate, pathname, len, ignore_case))
1744
return NULL;
1745
1745
- ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc);
1746
+ ALLOC_GROW(dir->entries, dir->nr+1, dir->internal.alloc);
1747
return dir->entries[dir->nr++] = dir_entry_new(pathname, len);
1748
}
1749
@@ -1753,7 +1754,7 @@ struct dir_entry *dir_add_ignored(struct dir_struct *dir,
1754
if (!index_name_is_other(istate, pathname, len))
1755
return NULL;
1756
1756
- ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc);
1757
+ ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->internal.ignored_alloc);
1758
return dir->ignored[dir->ignored_nr++] = dir_entry_new(pathname, len);
1759
}
1760
@@ -2569,7 +2570,7 @@ static enum path_treatment read_directory_recursive(struct dir_struct *dir,
2570
2571
if (open_cached_dir(&cdir, dir, untracked, istate, &path, check_only))
2572
goto out;
2572
- dir->visited_directories++;
2573
+ dir->internal.visited_directories++;
2574
2575
if (untracked)
2576
untracked->check_only = !!check_only;
@@ -2578,7 +2579,7 @@ static enum path_treatment read_directory_recursive(struct dir_struct *dir,
2579
/* check how the file or directory should be treated */
2580
state = treat_path(dir, untracked, &cdir, istate, &path,
2581
baselen, pathspec);
2581
- dir->visited_paths++;
2582
+ dir->internal.visited_paths++;
2583
2584
if (state > dir_state)
2585
dir_state = state;
@@ -2586,7 +2587,8 @@ static enum path_treatment read_directory_recursive(struct dir_struct *dir,
2587
/* recurse into subdir if instructed by treat_path */
2588
if (state == path_recurse) {
2589
struct untracked_cache_dir *ud;
2589
- ud = lookup_untracked(dir->untracked, untracked,
2590
+ ud = lookup_untracked(dir->untracked,
2591
+ untracked,
2592
path.buf + baselen,
2593
path.len - baselen);
2594
subdir_state =
@@ -2846,7 +2848,7 @@ static struct untracked_cache_dir *validate_untracked_cache(struct dir_struct *d
2848
* condition also catches running setup_standard_excludes()
2849
* before setting dir->untracked!
2850
*/
2849
- if (dir->unmanaged_exclude_files)
2851
+ if (dir->internal.unmanaged_exclude_files)
2852
return NULL;
2853
2854
/*
@@ -2875,7 +2877,7 @@ static struct untracked_cache_dir *validate_untracked_cache(struct dir_struct *d
2877
* EXC_CMDL is not considered in the cache. If people set it,
2878
* skip the cache.
2879
*/
2878
- if (dir->exclude_list_group[EXC_CMDL].nr)
2880
+ if (dir->internal.exclude_list_group[EXC_CMDL].nr)
2881
return NULL;
2882
2883
if (!ident_in_untracked(dir->untracked)) {
@@ -2935,15 +2937,15 @@ static struct untracked_cache_dir *validate_untracked_cache(struct dir_struct *d
2937
2938
/* Validate $GIT_DIR/info/exclude and core.excludesfile */
2939
root = dir->untracked->root;
2938
- if (!oideq(&dir->ss_info_exclude.oid,
2940
+ if (!oideq(&dir->internal.ss_info_exclude.oid,
2941
&dir->untracked->ss_info_exclude.oid)) {
2942
invalidate_gitignore(dir->untracked, root);
2941
- dir->untracked->ss_info_exclude = dir->ss_info_exclude;
2943
+ dir->untracked->ss_info_exclude = dir->internal.ss_info_exclude;
2944
}
2943
- if (!oideq(&dir->ss_excludes_file.oid,
2945
+ if (!oideq(&dir->internal.ss_excludes_file.oid,
2946
&dir->untracked->ss_excludes_file.oid)) {
2947
invalidate_gitignore(dir->untracked, root);
2946
- dir->untracked->ss_excludes_file = dir->ss_excludes_file;
2948
+ dir->untracked->ss_excludes_file = dir->internal.ss_excludes_file;
2949
}
2950
2951
/* Make sure this directory is not dropped out at saving phase */
@@ -2969,9 +2971,9 @@ static void emit_traversal_statistics(struct dir_struct *dir,
2971
}
2972
2973
trace2_data_intmax("read_directory", repo,
2972
- "directories-visited", dir->visited_directories);
2974
+ "directories-visited", dir->internal.visited_directories);
2975
trace2_data_intmax("read_directory", repo,
2974
- "paths-visited", dir->visited_paths);
2976
+ "paths-visited", dir->internal.visited_paths);
2977
2978
if (!dir->untracked)
2979
return;
@@ -2993,8 +2995,8 @@ int read_directory(struct dir_struct *dir, struct index_state *istate,
2995
struct untracked_cache_dir *untracked;
2996
2997
trace2_region_enter("dir", "read_directory", istate->repo);
2996
- dir->visited_paths = 0;
2997
- dir->visited_directories = 0;
2998
+ dir->internal.visited_paths = 0;
2999
+ dir->internal.visited_directories = 0;
3000
3001
if (has_symlink_leading_path(path, len)) {
3002
trace2_region_leave("dir", "read_directory", istate->repo);
@@ -3342,14 +3344,14 @@ void setup_standard_excludes(struct dir_struct *dir)
3344
excludes_file = xdg_config_home("ignore");
3345
if (excludes_file && !access_or_warn(excludes_file, R_OK, 0))
3346
add_patterns_from_file_1(dir, excludes_file,
3345
- dir->untracked ? &dir->ss_excludes_file : NULL);
3347
+ dir->untracked ? &dir->internal.ss_excludes_file : NULL);
3348
3349
/* per repository user preference */
3350
if (startup_info->have_repository) {
3351
const char *path = git_path_info_exclude();
3352
if (!access_or_warn(path, R_OK, 0))
3353
add_patterns_from_file_1(dir, path,
3352
- dir->untracked ? &dir->ss_info_exclude : NULL);
3354
+ dir->untracked ? &dir->internal.ss_info_exclude : NULL);
3355
}
3356
}
3357
@@ -3405,7 +3407,7 @@ void dir_clear(struct dir_struct *dir)
3407
struct dir_struct new = DIR_INIT;
3408
3409
for (i = EXC_CMDL; i <= EXC_FILE; i++) {
3408
- group = &dir->exclude_list_group[i];
3410
+ group = &dir->internal.exclude_list_group[i];
3411
for (j = 0; j < group->nr; j++) {
3412
pl = &group->pl[j];
3413
if (i == EXC_DIRS)
@@ -3422,13 +3424,13 @@ void dir_clear(struct dir_struct *dir)
3424
free(dir->ignored);
3425
free(dir->entries);
3426
3425
- stk = dir->exclude_stack;
3427
+ stk = dir->internal.exclude_stack;
3428
while (stk) {
3429
struct exclude_stack *prev = stk->prev;
3430
free(stk);
3431
stk = prev;
3432
}
3431
- strbuf_release(&dir->basebuf);
3433
+ strbuf_release(&dir->internal.basebuf);
3434
3435
memcpy(dir, &new, sizeof(*dir));
3436
}
dir.h
+46
-40
@@ -215,14 +215,9 @@ struct dir_struct {
215
/* The number of members in `entries[]` array. */
216
int nr;
217
218
- /* Internal use; keeps track of allocation of `entries[]` array.*/
219
- int alloc;
220
-
218
/* The number of members in `ignored[]` array. */
219
int ignored_nr;
220
224
- int ignored_alloc;
225
-
221
/* bit-field of options */
222
enum {
223
@@ -296,51 +291,62 @@ struct dir_struct {
291
*/
292
struct dir_entry **ignored;
293
294
+ /* Enable/update untracked file cache if set */
295
+ struct untracked_cache *untracked;
296
+
297
/**
298
* The name of the file to be read in each directory for excluded files
299
* (typically `.gitignore`).
300
*/
301
const char *exclude_per_dir;
302
305
- /*
306
- * We maintain three groups of exclude pattern lists:
307
- *
308
- * EXC_CMDL lists patterns explicitly given on the command line.
309
- * EXC_DIRS lists patterns obtained from per-directory ignore files.
310
- * EXC_FILE lists patterns from fallback ignore files, e.g.
311
- * - .git/info/exclude
312
- * - core.excludesfile
313
- *
314
- * Each group contains multiple exclude lists, a single list
315
- * per source.
316
- */
303
+ struct dir_struct_internal {
304
+ /* Keeps track of allocation of `entries[]` array.*/
305
+ int alloc;
306
+
307
+ /* Keeps track of allocation of `ignored[]` array. */
308
+ int ignored_alloc;
309
+
310
+ /*
311
+ * We maintain three groups of exclude pattern lists:
312
+ *
313
+ * EXC_CMDL lists patterns explicitly given on the command line.
314
+ * EXC_DIRS lists patterns obtained from per-directory ignore
315
+ * files.
316
+ * EXC_FILE lists patterns from fallback ignore files, e.g.
317
+ * - .git/info/exclude
318
+ * - core.excludesfile
319
+ *
320
+ * Each group contains multiple exclude lists, a single list
321
+ * per source.
322
+ */
323
#define EXC_CMDL 0
324
#define EXC_DIRS 1
325
#define EXC_FILE 2
320
- struct exclude_list_group exclude_list_group[3];
321
-
322
- /*
323
- * Temporary variables which are used during loading of the
324
- * per-directory exclude lists.
325
- *
326
- * exclude_stack points to the top of the exclude_stack, and
327
- * basebuf contains the full path to the current
328
- * (sub)directory in the traversal. Exclude points to the
329
- * matching exclude struct if the directory is excluded.
330
- */
331
- struct exclude_stack *exclude_stack;
332
- struct path_pattern *pattern;
333
- struct strbuf basebuf;
334
-
335
- /* Enable untracked file cache if set */
336
- struct untracked_cache *untracked;
337
- struct oid_stat ss_info_exclude;
338
- struct oid_stat ss_excludes_file;
339
- unsigned unmanaged_exclude_files;
326
+ struct exclude_list_group exclude_list_group[3];
327
341
- /* Stats about the traversal */
342
- unsigned visited_paths;
343
- unsigned visited_directories;
328
+ /*
329
+ * Temporary variables which are used during loading of the
330
+ * per-directory exclude lists.
331
+ *
332
+ * exclude_stack points to the top of the exclude_stack, and
333
+ * basebuf contains the full path to the current
334
+ * (sub)directory in the traversal. Exclude points to the
335
+ * matching exclude struct if the directory is excluded.
336
+ */
337
+ struct exclude_stack *exclude_stack;
338
+ struct path_pattern *pattern;
339
+ struct strbuf basebuf;
340
+
341
+ /* Additional metadata related to 'untracked' */
342
+ struct oid_stat ss_info_exclude;
343
+ struct oid_stat ss_excludes_file;
344
+ unsigned unmanaged_exclude_files;
345
+
346
+ /* Stats about the traversal */
347
+ unsigned visited_paths;
348
+ unsigned visited_directories;
349
+ } internal;
350
};
351
352
#define DIR_INIT { 0 }