Skip to content

Commit 96415b4

Browse files
jacob-kellergitster
authored andcommitted
name-rev: add support to exclude refs by pattern match
Extend git-name-rev to support excluding refs which match shell patterns using --exclude. These patterns can be used to limit the scope of refs by excluding any ref that matches one of the --exclude patterns. A ref will only be used for naming when it matches at least one --refs pattern but does not match any of the --exclude patterns. Thus, --exclude patterns are given precedence over --refs patterns. For example, suppose you wish to name a series of commits based on an official release tag of the form "v*" but excluding any pre-release tags which match "*rc*". You can use the following to do so: git name-rev --refs="v*" --exclude="*rc*" --all Add tests and update Documentation for this change. Signed-off-by: Jacob Keller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 290be66 commit 96415b4

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

Documentation/git-name-rev.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ OPTIONS
3030
given multiple times, use refs whose names match any of the given shell
3131
patterns. Use `--no-refs` to clear any previous ref patterns given.
3232

33+
--exclude=<pattern>::
34+
Do not use any ref whose name matches a given shell pattern. The
35+
pattern can be one of branch name, tag name or fully qualified ref
36+
name. If given multiple times, a ref will be excluded when it matches
37+
any of the given patterns. When used together with --refs, a ref will
38+
be used as a match only when it matches at least one --refs pattern and
39+
does not match any --exclude patterns. Use `--no-exclude` to clear the
40+
list of exclude patterns.
41+
3342
--all::
3443
List all commits reachable from all refs
3544

builtin/name-rev.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ struct name_ref_data {
109109
int tags_only;
110110
int name_only;
111111
struct string_list ref_filters;
112+
struct string_list exclude_filters;
112113
};
113114

114115
static struct tip_table {
@@ -150,6 +151,15 @@ static int name_ref(const char *path, const struct object_id *oid, int flags, vo
150151
if (data->tags_only && !starts_with(path, "refs/tags/"))
151152
return 0;
152153

154+
if (data->exclude_filters.nr) {
155+
struct string_list_item *item;
156+
157+
for_each_string_list_item(item, &data->exclude_filters) {
158+
if (subpath_matches(path, item->string) >= 0)
159+
return 0;
160+
}
161+
}
162+
153163
if (data->ref_filters.nr) {
154164
struct string_list_item *item;
155165
int matched = 0;
@@ -328,12 +338,14 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
328338
{
329339
struct object_array revs = OBJECT_ARRAY_INIT;
330340
int all = 0, transform_stdin = 0, allow_undefined = 1, always = 0, peel_tag = 0;
331-
struct name_ref_data data = { 0, 0, STRING_LIST_INIT_NODUP };
341+
struct name_ref_data data = { 0, 0, STRING_LIST_INIT_NODUP, STRING_LIST_INIT_NODUP };
332342
struct option opts[] = {
333343
OPT_BOOL(0, "name-only", &data.name_only, N_("print only names (no SHA-1)")),
334344
OPT_BOOL(0, "tags", &data.tags_only, N_("only use tags to name the commits")),
335345
OPT_STRING_LIST(0, "refs", &data.ref_filters, N_("pattern"),
336346
N_("only use refs matching <pattern>")),
347+
OPT_STRING_LIST(0, "exclude", &data.exclude_filters, N_("pattern"),
348+
N_("ignore refs matching <pattern>")),
337349
OPT_GROUP(""),
338350
OPT_BOOL(0, "all", &all, N_("list all commits reachable from all refs")),
339351
OPT_BOOL(0, "stdin", &transform_stdin, N_("read from stdin")),

t/t6007-rev-list-cherry-pick-file.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,18 @@ test_expect_success 'name-rev --refs excludes non-matched patterns' '
118118
test_cmp actual.named expect
119119
'
120120

121+
cat >expect <<EOF
122+
<tags/F
123+
EOF
124+
125+
test_expect_success 'name-rev --exclude excludes matched patterns' '
126+
git rev-list --left-right --right-only --cherry-pick F...E -- bar >>expect &&
127+
git rev-list --left-right --cherry-pick F...E -- bar >actual &&
128+
git name-rev --stdin --name-only --refs="*tags/*" --exclude="*E" \
129+
<actual >actual.named &&
130+
test_cmp actual.named expect
131+
'
132+
121133
test_expect_success 'name-rev --no-refs clears the refs list' '
122134
git rev-list --left-right --cherry-pick F...E -- bar >expect &&
123135
git name-rev --stdin --name-only --refs="*tags/F" --refs="*tags/E" --no-refs --refs="*tags/G" \

0 commit comments

Comments
 (0)