Skip to content

Commit 468ce99

Browse files
derrickstoleegitster
authored andcommitted
unpack-trees: rename 'is_excluded_from_list()'
The first consumer of pattern-matching filenames was the .gitignore feature. In that context, storing a list of patterns as a 'struct exclude_list' makes sense. However, the sparse-checkout feature then adopted these structures and methods, but with the opposite meaning: these patterns match the files that should be included! Now that this library is renamed to use 'struct pattern_list' and 'struct pattern', we can now rename the method used by the sparse-checkout feature to determine which paths should appear in the working directory. The method is_excluded_from_list() is only used by the sparse-checkout logic in unpack-trees and list-objects-filter. The confusing part is that it returned 1 for "excluded" (i.e. it matches the list of exclusions) but that really manes that the path matched the list of patterns for _inclusion_ in the working directory. Rename the method to be path_matches_pattern_list() and have it return an explicit 'enum pattern_match_result'. Here, the values MATCHED = 1, UNMATCHED = 0, and UNDECIDED = -1 agree with the previous integer values. This shift allows future consumers to better understand what the retur values mean, and provides more type checking for handling those values. Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 65edd96 commit 468ce99

File tree

4 files changed

+72
-42
lines changed

4 files changed

+72
-42
lines changed

dir.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,19 +1072,28 @@ static struct path_pattern *last_matching_pattern_from_list(const char *pathname
10721072
}
10731073

10741074
/*
1075-
* Scan the list and let the last match determine the fate.
1076-
* Return 1 for exclude, 0 for include and -1 for undecided.
1075+
* Scan the list of patterns to determine if the ordered list
1076+
* of patterns matches on 'pathname'.
1077+
*
1078+
* Return 1 for a match, 0 for not matched and -1 for undecided.
10771079
*/
1078-
int is_excluded_from_list(const char *pathname,
1079-
int pathlen, const char *basename, int *dtype,
1080-
struct pattern_list *pl, struct index_state *istate)
1080+
enum pattern_match_result path_matches_pattern_list(
1081+
const char *pathname, int pathlen,
1082+
const char *basename, int *dtype,
1083+
struct pattern_list *pl,
1084+
struct index_state *istate)
10811085
{
10821086
struct path_pattern *pattern;
10831087
pattern = last_matching_pattern_from_list(pathname, pathlen, basename,
10841088
dtype, pl, istate);
1085-
if (pattern)
1086-
return pattern->flags & PATTERN_FLAG_NEGATIVE ? 0 : 1;
1087-
return -1; /* undecided */
1089+
if (pattern) {
1090+
if (pattern->flags & PATTERN_FLAG_NEGATIVE)
1091+
return NOT_MATCHED;
1092+
else
1093+
return MATCHED;
1094+
}
1095+
1096+
return UNDECIDED;
10881097
}
10891098

10901099
static struct path_pattern *last_matching_pattern_from_lists(

dir.h

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,23 @@ int read_directory(struct dir_struct *, struct index_state *istate,
230230
const char *path, int len,
231231
const struct pathspec *pathspec);
232232

233-
int is_excluded_from_list(const char *pathname, int pathlen,
234-
const char *basename, int *dtype,
235-
struct pattern_list *pl,
236-
struct index_state *istate);
233+
enum pattern_match_result {
234+
UNDECIDED = -1,
235+
NOT_MATCHED = 0,
236+
MATCHED = 1,
237+
};
238+
239+
/*
240+
* Scan the list of patterns to determine if the ordered list
241+
* of patterns matches on 'pathname'.
242+
*
243+
* Return 1 for a match, 0 for not matched and -1 for undecided.
244+
*/
245+
enum pattern_match_result path_matches_pattern_list(const char *pathname,
246+
int pathlen,
247+
const char *basename, int *dtype,
248+
struct pattern_list *pl,
249+
struct index_state *istate);
237250
struct dir_entry *dir_add_ignored(struct dir_struct *dir,
238251
struct index_state *istate,
239252
const char *pathname, int len);

list-objects-filter.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -328,12 +328,12 @@ static void filter_blobs_limit__init(
328328
*/
329329
struct frame {
330330
/*
331-
* defval is the usual default include/exclude value that
331+
* default_match is the usual default include/exclude value that
332332
* should be inherited as we recurse into directories based
333333
* upon pattern matching of the directory itself or of a
334334
* containing directory.
335335
*/
336-
int defval;
336+
enum pattern_match_result default_match;
337337

338338
/*
339339
* 1 if the directory (recursively) contains any provisionally
@@ -363,8 +363,9 @@ static enum list_objects_filter_result filter_sparse(
363363
void *filter_data_)
364364
{
365365
struct filter_sparse_data *filter_data = filter_data_;
366-
int val, dtype;
366+
int dtype;
367367
struct frame *frame;
368+
enum pattern_match_result match;
368369

369370
switch (filter_situation) {
370371
default:
@@ -373,15 +374,15 @@ static enum list_objects_filter_result filter_sparse(
373374
case LOFS_BEGIN_TREE:
374375
assert(obj->type == OBJ_TREE);
375376
dtype = DT_DIR;
376-
val = is_excluded_from_list(pathname, strlen(pathname),
377-
filename, &dtype, &filter_data->pl,
378-
r->index);
379-
if (val < 0)
380-
val = filter_data->array_frame[filter_data->nr - 1].defval;
377+
match = path_matches_pattern_list(pathname, strlen(pathname),
378+
filename, &dtype, &filter_data->pl,
379+
r->index);
380+
if (match == UNDECIDED)
381+
match = filter_data->array_frame[filter_data->nr - 1].default_match;
381382

382383
ALLOC_GROW(filter_data->array_frame, filter_data->nr + 1,
383384
filter_data->alloc);
384-
filter_data->array_frame[filter_data->nr].defval = val;
385+
filter_data->array_frame[filter_data->nr].default_match = match;
385386
filter_data->array_frame[filter_data->nr].child_prov_omit = 0;
386387
filter_data->nr++;
387388

@@ -435,12 +436,12 @@ static enum list_objects_filter_result filter_sparse(
435436
frame = &filter_data->array_frame[filter_data->nr - 1];
436437

437438
dtype = DT_REG;
438-
val = is_excluded_from_list(pathname, strlen(pathname),
439+
match = path_matches_pattern_list(pathname, strlen(pathname),
439440
filename, &dtype, &filter_data->pl,
440441
r->index);
441-
if (val < 0)
442-
val = frame->defval;
443-
if (val > 0) {
442+
if (match == UNDECIDED)
443+
match = frame->default_match;
444+
if (match == MATCHED) {
444445
if (omits)
445446
oidset_remove(omits, &obj->oid);
446447
return LOFR_MARK_SEEN | LOFR_DO_SHOW;
@@ -487,7 +488,7 @@ static void filter_sparse_oid__init(
487488
die("could not load filter specification");
488489

489490
ALLOC_GROW(d->array_frame, d->nr + 1, d->alloc);
490-
d->array_frame[d->nr].defval = 0; /* default to include */
491+
d->array_frame[d->nr].default_match = 0; /* default to include */
491492
d->array_frame[d->nr].child_prov_omit = 0;
492493
d->nr++;
493494

unpack-trees.c

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,27 +1265,30 @@ static int clear_ce_flags_1(struct index_state *istate,
12651265
struct cache_entry **cache, int nr,
12661266
struct strbuf *prefix,
12671267
int select_mask, int clear_mask,
1268-
struct pattern_list *pl, int defval);
1268+
struct pattern_list *pl,
1269+
enum pattern_match_result default_match);
12691270

12701271
/* Whole directory matching */
12711272
static int clear_ce_flags_dir(struct index_state *istate,
12721273
struct cache_entry **cache, int nr,
12731274
struct strbuf *prefix,
12741275
char *basename,
12751276
int select_mask, int clear_mask,
1276-
struct pattern_list *pl, int defval)
1277+
struct pattern_list *pl,
1278+
enum pattern_match_result default_match)
12771279
{
12781280
struct cache_entry **cache_end;
12791281
int dtype = DT_DIR;
1280-
int ret = is_excluded_from_list(prefix->buf, prefix->len,
1281-
basename, &dtype, pl, istate);
12821282
int rc;
1283+
enum pattern_match_result ret;
1284+
ret = path_matches_pattern_list(prefix->buf, prefix->len,
1285+
basename, &dtype, pl, istate);
12831286

12841287
strbuf_addch(prefix, '/');
12851288

12861289
/* If undecided, use matching result of parent dir in defval */
1287-
if (ret < 0)
1288-
ret = defval;
1290+
if (ret == UNDECIDED)
1291+
ret = default_match;
12891292

12901293
for (cache_end = cache; cache_end != cache + nr; cache_end++) {
12911294
struct cache_entry *ce = *cache_end;
@@ -1298,7 +1301,7 @@ static int clear_ce_flags_dir(struct index_state *istate,
12981301
* with ret (iow, we know in advance the incl/excl
12991302
* decision for the entire directory), clear flag here without
13001303
* calling clear_ce_flags_1(). That function will call
1301-
* the expensive is_excluded_from_list() on every entry.
1304+
* the expensive path_matches_pattern_list() on every entry.
13021305
*/
13031306
rc = clear_ce_flags_1(istate, cache, cache_end - cache,
13041307
prefix,
@@ -1327,7 +1330,8 @@ static int clear_ce_flags_1(struct index_state *istate,
13271330
struct cache_entry **cache, int nr,
13281331
struct strbuf *prefix,
13291332
int select_mask, int clear_mask,
1330-
struct pattern_list *pl, int defval)
1333+
struct pattern_list *pl,
1334+
enum pattern_match_result default_match)
13311335
{
13321336
struct cache_entry **cache_end = cache + nr;
13331337

@@ -1338,7 +1342,8 @@ static int clear_ce_flags_1(struct index_state *istate,
13381342
while(cache != cache_end) {
13391343
struct cache_entry *ce = *cache;
13401344
const char *name, *slash;
1341-
int len, dtype, ret;
1345+
int len, dtype;
1346+
enum pattern_match_result ret;
13421347

13431348
if (select_mask && !(ce->ce_flags & select_mask)) {
13441349
cache++;
@@ -1362,7 +1367,7 @@ static int clear_ce_flags_1(struct index_state *istate,
13621367
prefix,
13631368
prefix->buf + prefix->len - len,
13641369
select_mask, clear_mask,
1365-
pl, defval);
1370+
pl, default_match);
13661371

13671372
/* clear_c_f_dir eats a whole dir already? */
13681373
if (processed) {
@@ -1374,18 +1379,20 @@ static int clear_ce_flags_1(struct index_state *istate,
13741379
strbuf_addch(prefix, '/');
13751380
cache += clear_ce_flags_1(istate, cache, cache_end - cache,
13761381
prefix,
1377-
select_mask, clear_mask, pl, defval);
1382+
select_mask, clear_mask, pl,
1383+
default_match);
13781384
strbuf_setlen(prefix, prefix->len - len - 1);
13791385
continue;
13801386
}
13811387

13821388
/* Non-directory */
13831389
dtype = ce_to_dtype(ce);
1384-
ret = is_excluded_from_list(ce->name, ce_namelen(ce),
1385-
name, &dtype, pl, istate);
1386-
if (ret < 0)
1387-
ret = defval;
1388-
if (ret > 0)
1390+
ret = path_matches_pattern_list(ce->name,
1391+
ce_namelen(ce),
1392+
name, &dtype, pl, istate);
1393+
if (ret == UNDECIDED)
1394+
ret = default_match;
1395+
if (ret == MATCHED)
13891396
ce->ce_flags &= ~clear_mask;
13901397
cache++;
13911398
}

0 commit comments

Comments
 (0)