Skip to content

Commit 1e9f273

Browse files
pks-tttaylorr
authored andcommitted
revision: introduce struct to handle exclusions
The functions that handle exclusion of refs work on a single string list. We're about to add a second mechanism for excluding refs though, and it makes sense to reuse much of the same architecture for both kinds of exclusion. Introduce a new `struct ref_exclusions` that encapsulates all the logic related to excluding refs and move the `struct string_list` that holds all wildmatch patterns of excluded refs into it. Rename functions that operate on this struct to match its name. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Taylor Blau <[email protected]>
1 parent 05b9425 commit 1e9f273

File tree

3 files changed

+47
-36
lines changed

3 files changed

+47
-36
lines changed

builtin/rev-parse.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static int abbrev_ref_strict;
3939
static int output_sq;
4040

4141
static int stuck_long;
42-
static struct string_list *ref_excludes;
42+
static struct ref_exclusions ref_excludes = REF_EXCLUSIONS_INIT;
4343

4444
/*
4545
* Some arguments are relevant "revision" arguments,
@@ -198,7 +198,7 @@ static int show_default(void)
198198
static int show_reference(const char *refname, const struct object_id *oid,
199199
int flag UNUSED, void *cb_data UNUSED)
200200
{
201-
if (ref_excluded(ref_excludes, refname))
201+
if (ref_excluded(&ref_excludes, refname))
202202
return 0;
203203
show_rev(NORMAL, oid, refname);
204204
return 0;
@@ -585,7 +585,7 @@ static void handle_ref_opt(const char *pattern, const char *prefix)
585585
for_each_glob_ref_in(show_reference, pattern, prefix, NULL);
586586
else
587587
for_each_ref_in(prefix, show_reference, NULL);
588-
clear_ref_exclusion(&ref_excludes);
588+
clear_ref_exclusions(&ref_excludes);
589589
}
590590

591591
enum format_type {
@@ -863,7 +863,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
863863
}
864864
if (!strcmp(arg, "--all")) {
865865
for_each_ref(show_reference, NULL);
866-
clear_ref_exclusion(&ref_excludes);
866+
clear_ref_exclusions(&ref_excludes);
867867
continue;
868868
}
869869
if (skip_prefix(arg, "--disambiguate=", &arg)) {

revision.c

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1517,35 +1517,30 @@ static void add_rev_cmdline_list(struct rev_info *revs,
15171517
}
15181518
}
15191519

1520-
int ref_excluded(struct string_list *ref_excludes, const char *path)
1520+
int ref_excluded(const struct ref_exclusions *exclusions, const char *path)
15211521
{
15221522
struct string_list_item *item;
1523-
1524-
if (!ref_excludes)
1525-
return 0;
1526-
for_each_string_list_item(item, ref_excludes) {
1523+
for_each_string_list_item(item, &exclusions->excluded_refs) {
15271524
if (!wildmatch(item->string, path, 0))
15281525
return 1;
15291526
}
15301527
return 0;
15311528
}
15321529

1533-
void clear_ref_exclusion(struct string_list **ref_excludes_p)
1530+
void init_ref_exclusions(struct ref_exclusions *exclusions)
15341531
{
1535-
if (*ref_excludes_p) {
1536-
string_list_clear(*ref_excludes_p, 0);
1537-
free(*ref_excludes_p);
1538-
}
1539-
*ref_excludes_p = NULL;
1532+
struct ref_exclusions blank = REF_EXCLUSIONS_INIT;
1533+
memcpy(exclusions, &blank, sizeof(*exclusions));
15401534
}
15411535

1542-
void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
1536+
void clear_ref_exclusions(struct ref_exclusions *exclusions)
15431537
{
1544-
if (!*ref_excludes_p) {
1545-
CALLOC_ARRAY(*ref_excludes_p, 1);
1546-
(*ref_excludes_p)->strdup_strings = 1;
1547-
}
1548-
string_list_append(*ref_excludes_p, exclude);
1538+
string_list_clear(&exclusions->excluded_refs, 0);
1539+
}
1540+
1541+
void add_ref_exclusion(struct ref_exclusions *exclusions, const char *exclude)
1542+
{
1543+
string_list_append(&exclusions->excluded_refs, exclude);
15491544
}
15501545

15511546
struct all_refs_cb {
@@ -1563,7 +1558,7 @@ static int handle_one_ref(const char *path, const struct object_id *oid,
15631558
struct all_refs_cb *cb = cb_data;
15641559
struct object *object;
15651560

1566-
if (ref_excluded(cb->all_revs->ref_excludes, path))
1561+
if (ref_excluded(&cb->all_revs->ref_excludes, path))
15671562
return 0;
15681563

15691564
object = get_reference(cb->all_revs, path, oid, cb->all_flags);
@@ -1901,6 +1896,7 @@ void repo_init_revisions(struct repository *r,
19011896

19021897
init_display_notes(&revs->notes_opt);
19031898
list_objects_filter_init(&revs->filter);
1899+
init_ref_exclusions(&revs->ref_excludes);
19041900
}
19051901

19061902
static void add_pending_commit_list(struct rev_info *revs,
@@ -2689,10 +2685,10 @@ static int handle_revision_pseudo_opt(struct rev_info *revs,
26892685
init_all_refs_cb(&cb, revs, *flags);
26902686
other_head_refs(handle_one_ref, &cb);
26912687
}
2692-
clear_ref_exclusion(&revs->ref_excludes);
2688+
clear_ref_exclusions(&revs->ref_excludes);
26932689
} else if (!strcmp(arg, "--branches")) {
26942690
handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
2695-
clear_ref_exclusion(&revs->ref_excludes);
2691+
clear_ref_exclusions(&revs->ref_excludes);
26962692
} else if (!strcmp(arg, "--bisect")) {
26972693
read_bisect_terms(&term_bad, &term_good);
26982694
handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
@@ -2701,15 +2697,15 @@ static int handle_revision_pseudo_opt(struct rev_info *revs,
27012697
revs->bisect = 1;
27022698
} else if (!strcmp(arg, "--tags")) {
27032699
handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
2704-
clear_ref_exclusion(&revs->ref_excludes);
2700+
clear_ref_exclusions(&revs->ref_excludes);
27052701
} else if (!strcmp(arg, "--remotes")) {
27062702
handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
2707-
clear_ref_exclusion(&revs->ref_excludes);
2703+
clear_ref_exclusions(&revs->ref_excludes);
27082704
} else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
27092705
struct all_refs_cb cb;
27102706
init_all_refs_cb(&cb, revs, *flags);
27112707
for_each_glob_ref(handle_one_ref, optarg, &cb);
2712-
clear_ref_exclusion(&revs->ref_excludes);
2708+
clear_ref_exclusions(&revs->ref_excludes);
27132709
return argcount;
27142710
} else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
27152711
add_ref_exclusion(&revs->ref_excludes, optarg);
@@ -2718,17 +2714,17 @@ static int handle_revision_pseudo_opt(struct rev_info *revs,
27182714
struct all_refs_cb cb;
27192715
init_all_refs_cb(&cb, revs, *flags);
27202716
for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb);
2721-
clear_ref_exclusion(&revs->ref_excludes);
2717+
clear_ref_exclusions(&revs->ref_excludes);
27222718
} else if (skip_prefix(arg, "--tags=", &optarg)) {
27232719
struct all_refs_cb cb;
27242720
init_all_refs_cb(&cb, revs, *flags);
27252721
for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb);
2726-
clear_ref_exclusion(&revs->ref_excludes);
2722+
clear_ref_exclusions(&revs->ref_excludes);
27272723
} else if (skip_prefix(arg, "--remotes=", &optarg)) {
27282724
struct all_refs_cb cb;
27292725
init_all_refs_cb(&cb, revs, *flags);
27302726
for_each_glob_ref_in(handle_one_ref, optarg, "refs/remotes/", &cb);
2731-
clear_ref_exclusion(&revs->ref_excludes);
2727+
clear_ref_exclusions(&revs->ref_excludes);
27322728
} else if (!strcmp(arg, "--reflog")) {
27332729
add_reflogs_to_pending(revs, *flags);
27342730
} else if (!strcmp(arg, "--indexed-objects")) {

revision.h

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,21 @@ struct rev_cmdline_info {
8181
} *rev;
8282
};
8383

84+
struct ref_exclusions {
85+
/*
86+
* Excluded refs is a list of wildmatch patterns. If any of the
87+
* patterns matches, the reference will be excluded.
88+
*/
89+
struct string_list excluded_refs;
90+
};
91+
92+
/**
93+
* Initialize a `struct ref_exclusions` with a macro.
94+
*/
95+
#define REF_EXCLUSIONS_INIT { \
96+
.excluded_refs = STRING_LIST_INIT_DUP, \
97+
}
98+
8499
struct oidset;
85100
struct topo_walk_info;
86101

@@ -103,7 +118,7 @@ struct rev_info {
103118
struct list_objects_filter_options filter;
104119

105120
/* excluding from --branches, --refs, etc. expansion */
106-
struct string_list *ref_excludes;
121+
struct ref_exclusions ref_excludes;
107122

108123
/* Basic information */
109124
const char *prefix;
@@ -439,12 +454,12 @@ void mark_trees_uninteresting_sparse(struct repository *r, struct oidset *trees)
439454
void show_object_with_name(FILE *, struct object *, const char *);
440455

441456
/**
442-
* Helpers to check if a "struct string_list" item matches with
443-
* wildmatch().
457+
* Helpers to check if a reference should be excluded.
444458
*/
445-
int ref_excluded(struct string_list *, const char *path);
446-
void clear_ref_exclusion(struct string_list **);
447-
void add_ref_exclusion(struct string_list **, const char *exclude);
459+
int ref_excluded(const struct ref_exclusions *exclusions, const char *path);
460+
void init_ref_exclusions(struct ref_exclusions *);
461+
void clear_ref_exclusions(struct ref_exclusions *);
462+
void add_ref_exclusion(struct ref_exclusions *, const char *exclude);
448463

449464
/**
450465
* This function can be used if you want to add commit objects as revision

0 commit comments

Comments
 (0)