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 <ps@pks.im> Signed-off-by: Taylor Blau <me@ttaylorr.com>
Patrick Steinhardt committed
Nov 17, 2022 at 06:46 UTC
1e9f273ac06f7826ee3ec5a8da5d03bf07c14389
3 files changed
+47
-36
builtin/rev-parse.c
+4
-4
@@ -39,7 +39,7 @@ static int abbrev_ref_strict;
39
static int output_sq;
40
41
static int stuck_long;
42
-static struct string_list *ref_excludes;
42
+static struct ref_exclusions ref_excludes = REF_EXCLUSIONS_INIT;
43
44
/*
45
* Some arguments are relevant "revision" arguments,
@@ -198,7 +198,7 @@ static int show_default(void)
198
static int show_reference(const char *refname, const struct object_id *oid,
199
int flag UNUSED, void *cb_data UNUSED)
200
{
201
- if (ref_excluded(ref_excludes, refname))
201
+ if (ref_excluded(&ref_excludes, refname))
202
return 0;
203
show_rev(NORMAL, oid, refname);
204
return 0;
@@ -585,7 +585,7 @@ static void handle_ref_opt(const char *pattern, const char *prefix)
585
for_each_glob_ref_in(show_reference, pattern, prefix, NULL);
586
else
587
for_each_ref_in(prefix, show_reference, NULL);
588
- clear_ref_exclusion(&ref_excludes);
588
+ clear_ref_exclusions(&ref_excludes);
589
}
590
591
enum format_type {
@@ -863,7 +863,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
863
}
864
if (!strcmp(arg, "--all")) {
865
for_each_ref(show_reference, NULL);
866
- clear_ref_exclusion(&ref_excludes);
866
+ clear_ref_exclusions(&ref_excludes);
867
continue;
868
}
869
if (skip_prefix(arg, "--disambiguate=", &arg)) {
revision.c
+22
-26
@@ -1517,35 +1517,30 @@ static void add_rev_cmdline_list(struct rev_info *revs,
1517
}
1518
}
1519
1520
-int ref_excluded(struct string_list *ref_excludes, const char *path)
1520
+int ref_excluded(const struct ref_exclusions *exclusions, const char *path)
1521
{
1522
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) {
1524
if (!wildmatch(item->string, path, 0))
1525
return 1;
1526
}
1527
return 0;
1528
}
1529
1533
-void clear_ref_exclusion(struct string_list **ref_excludes_p)
1530
+void init_ref_exclusions(struct ref_exclusions *exclusions)
1531
{
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));
1534
}
1535
1542
-void add_ref_exclusion(struct string_list **ref_excludes_p, const char *exclude)
1536
+void clear_ref_exclusions(struct ref_exclusions *exclusions)
1537
{
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);
1544
}
1545
1546
struct all_refs_cb {
@@ -1563,7 +1558,7 @@ static int handle_one_ref(const char *path, const struct object_id *oid,
1558
struct all_refs_cb *cb = cb_data;
1559
struct object *object;
1560
1566
- if (ref_excluded(cb->all_revs->ref_excludes, path))
1561
+ if (ref_excluded(&cb->all_revs->ref_excludes, path))
1562
return 0;
1563
1564
object = get_reference(cb->all_revs, path, oid, cb->all_flags);
@@ -1901,6 +1896,7 @@ void repo_init_revisions(struct repository *r,
1896
1897
init_display_notes(&revs->notes_opt);
1898
list_objects_filter_init(&revs->filter);
1899
+ init_ref_exclusions(&revs->ref_excludes);
1900
}
1901
1902
static void add_pending_commit_list(struct rev_info *revs,
@@ -2689,10 +2685,10 @@ static int handle_revision_pseudo_opt(struct rev_info *revs,
2685
init_all_refs_cb(&cb, revs, *flags);
2686
other_head_refs(handle_one_ref, &cb);
2687
}
2692
- clear_ref_exclusion(&revs->ref_excludes);
2688
+ clear_ref_exclusions(&revs->ref_excludes);
2689
} else if (!strcmp(arg, "--branches")) {
2690
handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
2695
- clear_ref_exclusion(&revs->ref_excludes);
2691
+ clear_ref_exclusions(&revs->ref_excludes);
2692
} else if (!strcmp(arg, "--bisect")) {
2693
read_bisect_terms(&term_bad, &term_good);
2694
handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
@@ -2701,15 +2697,15 @@ static int handle_revision_pseudo_opt(struct rev_info *revs,
2697
revs->bisect = 1;
2698
} else if (!strcmp(arg, "--tags")) {
2699
handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
2704
- clear_ref_exclusion(&revs->ref_excludes);
2700
+ clear_ref_exclusions(&revs->ref_excludes);
2701
} else if (!strcmp(arg, "--remotes")) {
2702
handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
2707
- clear_ref_exclusion(&revs->ref_excludes);
2703
+ clear_ref_exclusions(&revs->ref_excludes);
2704
} else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2705
struct all_refs_cb cb;
2706
init_all_refs_cb(&cb, revs, *flags);
2707
for_each_glob_ref(handle_one_ref, optarg, &cb);
2712
- clear_ref_exclusion(&revs->ref_excludes);
2708
+ clear_ref_exclusions(&revs->ref_excludes);
2709
return argcount;
2710
} else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2711
add_ref_exclusion(&revs->ref_excludes, optarg);
@@ -2718,17 +2714,17 @@ static int handle_revision_pseudo_opt(struct rev_info *revs,
2714
struct all_refs_cb cb;
2715
init_all_refs_cb(&cb, revs, *flags);
2716
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);
2718
} else if (skip_prefix(arg, "--tags=", &optarg)) {
2719
struct all_refs_cb cb;
2720
init_all_refs_cb(&cb, revs, *flags);
2721
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);
2723
} else if (skip_prefix(arg, "--remotes=", &optarg)) {
2724
struct all_refs_cb cb;
2725
init_all_refs_cb(&cb, revs, *flags);
2726
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);
2728
} else if (!strcmp(arg, "--reflog")) {
2729
add_reflogs_to_pending(revs, *flags);
2730
} else if (!strcmp(arg, "--indexed-objects")) {
revision.h
+21
-6
@@ -81,6 +81,21 @@ struct rev_cmdline_info {
81
} *rev;
82
};
83
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
+
99
struct oidset;
100
struct topo_walk_info;
101
@@ -103,7 +118,7 @@ struct rev_info {
118
struct list_objects_filter_options filter;
119
120
/* excluding from --branches, --refs, etc. expansion */
106
- struct string_list *ref_excludes;
121
+ struct ref_exclusions ref_excludes;
122
123
/* Basic information */
124
const char *prefix;
@@ -439,12 +454,12 @@ void mark_trees_uninteresting_sparse(struct repository *r, struct oidset *trees)
454
void show_object_with_name(FILE *, struct object *, const char *);
455
456
/**
442
- * Helpers to check if a "struct string_list" item matches with
443
- * wildmatch().
457
+ * Helpers to check if a reference should be excluded.
458
*/
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);
463
464
/**
465
* This function can be used if you want to add commit objects as revision