]> git.ipfire.org Git - thirdparty/git.git/commitdiff
name-rev: add support to exclude refs by pattern match
authorJacob Keller <jacob.keller@gmail.com>
Wed, 18 Jan 2017 23:06:06 +0000 (15:06 -0800)
committerJunio C Hamano <gitster@pobox.com>
Tue, 24 Jan 2017 02:33:17 +0000 (18:33 -0800)
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 <jacob.keller@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/git-name-rev.txt
builtin/name-rev.c
t/t6007-rev-list-cherry-pick-file.sh

index 7433627db12d311c38b0d8e63cf470b773b88209..e8e68f528cf2fa5705e7cedce8d3d807fa20057d 100644 (file)
@@ -30,6 +30,15 @@ OPTIONS
        given multiple times, use refs whose names match any of the given shell
        patterns. Use `--no-refs` to clear any previous ref patterns given.
 
+--exclude=<pattern>::
+       Do not use any ref whose name matches a given shell pattern. The
+       pattern can be one of branch name, tag name or fully qualified ref
+       name. If given multiple times, a ref will be excluded when it matches
+       any of the given patterns. When used together with --refs, a ref will
+       be used as a match only when it matches at least one --refs pattern and
+       does not match any --exclude patterns. Use `--no-exclude` to clear the
+       list of exclude patterns.
+
 --all::
        List all commits reachable from all refs
 
index 9da027674ca85b5cc0a4c2ceb05f4ad3635a0c76..8bdc3eaa6fa4472d8352e7de10ef9d816e8ca83f 100644 (file)
@@ -109,6 +109,7 @@ struct name_ref_data {
        int tags_only;
        int name_only;
        struct string_list ref_filters;
+       struct string_list exclude_filters;
 };
 
 static struct tip_table {
@@ -150,6 +151,15 @@ static int name_ref(const char *path, const struct object_id *oid, int flags, vo
        if (data->tags_only && !starts_with(path, "refs/tags/"))
                return 0;
 
+       if (data->exclude_filters.nr) {
+               struct string_list_item *item;
+
+               for_each_string_list_item(item, &data->exclude_filters) {
+                       if (subpath_matches(path, item->string) >= 0)
+                               return 0;
+               }
+       }
+
        if (data->ref_filters.nr) {
                struct string_list_item *item;
                int matched = 0;
@@ -328,12 +338,14 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
 {
        struct object_array revs = OBJECT_ARRAY_INIT;
        int all = 0, transform_stdin = 0, allow_undefined = 1, always = 0, peel_tag = 0;
-       struct name_ref_data data = { 0, 0, STRING_LIST_INIT_NODUP };
+       struct name_ref_data data = { 0, 0, STRING_LIST_INIT_NODUP, STRING_LIST_INIT_NODUP };
        struct option opts[] = {
                OPT_BOOL(0, "name-only", &data.name_only, N_("print only names (no SHA-1)")),
                OPT_BOOL(0, "tags", &data.tags_only, N_("only use tags to name the commits")),
                OPT_STRING_LIST(0, "refs", &data.ref_filters, N_("pattern"),
                                   N_("only use refs matching <pattern>")),
+               OPT_STRING_LIST(0, "exclude", &data.exclude_filters, N_("pattern"),
+                                  N_("ignore refs matching <pattern>")),
                OPT_GROUP(""),
                OPT_BOOL(0, "all", &all, N_("list all commits reachable from all refs")),
                OPT_BOOL(0, "stdin", &transform_stdin, N_("read from stdin")),
index d9827a6389a3c468d5c6a8d8ef82bc361993078f..29597451967dd7e4847a5114b79a91399e1b3ab3 100755 (executable)
@@ -118,6 +118,18 @@ test_expect_success 'name-rev --refs excludes non-matched patterns' '
        test_cmp actual.named expect
 '
 
+cat >expect <<EOF
+<tags/F
+EOF
+
+test_expect_success 'name-rev --exclude excludes matched patterns' '
+       git rev-list --left-right --right-only --cherry-pick F...E -- bar >>expect &&
+       git rev-list --left-right --cherry-pick F...E -- bar >actual &&
+       git name-rev --stdin --name-only --refs="*tags/*" --exclude="*E" \
+               <actual >actual.named &&
+       test_cmp actual.named expect
+'
+
 test_expect_success 'name-rev --no-refs clears the refs list' '
        git rev-list --left-right --cherry-pick F...E -- bar >expect &&
        git name-rev --stdin --name-only --refs="*tags/F" --refs="*tags/E" --no-refs --refs="*tags/G" \