]> git.ipfire.org Git - thirdparty/git.git/commitdiff
ref-filter: add %(rest) atom
authorZheNing Hu <adlternative@gmail.com>
Mon, 26 Jul 2021 03:26:50 +0000 (03:26 +0000)
committerJunio C Hamano <gitster@pobox.com>
Mon, 26 Jul 2021 19:01:26 +0000 (12:01 -0700)
%(rest) is a atom used for cat-file batch mode, which can split
the input lines at the first whitespace boundary, all characters
before that whitespace are considered to be the object name;
characters after that first run of whitespace (i.e., the "rest"
of the line) are output in place of the %(rest) atom.

In order to let "cat-file --batch=%(rest)" use the ref-filter
interface, add %(rest) atom for ref-filter.

Introduce the reject_atom() to reject the atom %(rest) for
"git for-each-ref", "git branch", "git tag" and "git verify-tag".

Reviewed-by: Jacob Keller <jacob.keller@gmail.com>
Suggected-by: Jacob Keller <jacob.keller@gmail.com>
Mentored-by: Christian Couder <christian.couder@gmail.com>
Mentored-by: Hariom Verma <hariom18599@gmail.com>
Signed-off-by: ZheNing Hu <adlternative@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
ref-filter.c
ref-filter.h
t/t3203-branch-output.sh
t/t6300-for-each-ref.sh
t/t7004-tag.sh
t/t7030-verify-tag.sh

index f944160c5ce3f78cd4f21a424a4e85f05db7d1d9..2cacee9e64af1b159a42f868fe347a3d4109e89d 100644 (file)
@@ -157,6 +157,7 @@ enum atom_type {
        ATOM_IF,
        ATOM_THEN,
        ATOM_ELSE,
+       ATOM_REST,
 };
 
 /*
@@ -559,6 +560,15 @@ static int if_atom_parser(struct ref_format *format, struct used_atom *atom,
        return 0;
 }
 
+static int rest_atom_parser(struct ref_format *format, struct used_atom *atom,
+                           const char *arg, struct strbuf *err)
+{
+       if (arg)
+               return strbuf_addf_ret(err, -1, _("%%(rest) does not take arguments"));
+       format->use_rest = 1;
+       return 0;
+}
+
 static int head_atom_parser(struct ref_format *format, struct used_atom *atom,
                            const char *arg, struct strbuf *unused_err)
 {
@@ -615,6 +625,7 @@ static struct {
        [ATOM_IF] = { "if", SOURCE_NONE, FIELD_STR, if_atom_parser },
        [ATOM_THEN] = { "then", SOURCE_NONE },
        [ATOM_ELSE] = { "else", SOURCE_NONE },
+       [ATOM_REST] = { "rest", SOURCE_NONE, FIELD_STR, rest_atom_parser },
        /*
         * Please update $__git_ref_fieldlist in git-completion.bash
         * when you add new atoms
@@ -992,6 +1003,11 @@ static const char *find_next(const char *cp)
        return NULL;
 }
 
+static int reject_atom(enum atom_type atom_type)
+{
+       return atom_type == ATOM_REST;
+}
+
 /*
  * Make sure the format string is well formed, and parse out
  * the used atoms.
@@ -1012,6 +1028,8 @@ int verify_ref_format(struct ref_format *format)
                at = parse_ref_filter_atom(format, sp + 2, ep, &err);
                if (at < 0)
                        die("%s", err.buf);
+               if (reject_atom(used_atom[at].atom_type))
+                       die(_("this command reject atom %%(%.*s)"), (int)(ep - sp - 2), sp + 2);
 
                if ((format->quote_style == QUOTE_PYTHON ||
                     format->quote_style == QUOTE_SHELL ||
@@ -1931,6 +1949,12 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err)
                        v->handler = else_atom_handler;
                        v->s = xstrdup("");
                        continue;
+               } else if (atom_type == ATOM_REST) {
+                       if (ref->rest)
+                               v->s = xstrdup(ref->rest);
+                       else
+                               v->s = xstrdup("");
+                       continue;
                } else
                        continue;
 
@@ -2148,6 +2172,7 @@ static struct ref_array_item *new_ref_array_item(const char *refname,
 
        FLEX_ALLOC_STR(ref, refname, refname);
        oidcpy(&ref->objectname, oid);
+       ref->rest = NULL;
 
        return ref;
 }
index 74fb423fc89f8d93d91ba5636253a8786ae5942a..c15dee8d6b95ba140f4e8f56b0888769d5d3abe6 100644 (file)
@@ -38,6 +38,7 @@ struct ref_sorting {
 
 struct ref_array_item {
        struct object_id objectname;
+       const char *rest;
        int flag;
        unsigned int kind;
        const char *symref;
@@ -76,14 +77,16 @@ struct ref_format {
         * verify_ref_format() afterwards to finalize.
         */
        const char *format;
+       const char *rest;
        int quote_style;
+       int use_rest;
        int use_color;
 
        /* Internal state to ref-filter */
        int need_color_reset_at_eol;
 };
 
-#define REF_FORMAT_INIT { NULL, 0, -1 }
+#define REF_FORMAT_INIT { .use_color = -1 }
 
 /*  Macros for checking --merged and --no-merged options */
 #define _OPT_MERGED_NO_MERGED(option, filter, h) \
index 5325b9f67a00783974c34b4ac88431b903b3ccec..6e94c6db7b5aa987c29364f32fee8c0d52538adf 100755 (executable)
@@ -340,6 +340,10 @@ test_expect_success 'git branch --format option' '
        test_cmp expect actual
 '
 
+test_expect_success 'git branch with --format=%(rest) must fail' '
+       test_must_fail git branch --format="%(rest)" >actual
+'
+
 test_expect_success 'worktree colors correct' '
        cat >expect <<-EOF &&
        * <GREEN>(HEAD detached from fromtag)<RESET>
index 3d15d0a5360174a52fd67c13a56b213aa10a64a8..0d2e062f791b6d5403077e4be7af45f4f2a12188 100755 (executable)
@@ -1211,6 +1211,10 @@ test_expect_success 'basic atom: head contents:trailers' '
        test_cmp expect actual.clean
 '
 
+test_expect_success 'basic atom: rest must fail' '
+       test_must_fail git for-each-ref --format="%(rest)" refs/heads/main
+'
+
 test_expect_success 'trailer parsing not fooled by --- line' '
        git commit --allow-empty -F - <<-\EOF &&
        this is the subject
index 2f72c5c6883e3e45bbc9602f3e85e1dd9ae3fd01..082be85dffc7b1765471e11d2c580c853cf0965b 100755 (executable)
@@ -1998,6 +1998,10 @@ test_expect_success '--format should list tags as per format given' '
        test_cmp expect actual
 '
 
+test_expect_success 'git tag -l with --format="%(rest)" must fail' '
+       test_must_fail git tag -l --format="%(rest)" "v1*"
+'
+
 test_expect_success "set up color tests" '
        echo "<RED>v1.0<RESET>" >expect.color &&
        echo "v1.0" >expect.bare &&
index 3cefde9602bf73b7be1a457556d0d11713a47e4f..10faa645157ea4521e370abdfd470c84a24f6051 100755 (executable)
@@ -194,6 +194,10 @@ test_expect_success GPG 'verifying tag with --format' '
        test_cmp expect actual
 '
 
+test_expect_success GPG 'verifying tag with --format="%(rest)" must fail' '
+       test_must_fail git verify-tag --format="%(rest)" "fourth-signed"
+'
+
 test_expect_success GPG 'verifying a forged tag with --format should fail silently' '
        test_must_fail git verify-tag --format="tagname : %(tag)" $(cat forged1.tag) >actual-forged &&
        test_must_be_empty actual-forged