From: Phillip Wood <phillip.wood123@gmail.com>
To: Derrick Stolee via GitGitGadget <gitgitgadget@gmail.com>,
git@vger.kernel.org
Cc: gitster@pobox.com, me@ttaylorr.com, vdye@github.com,
Jeff King <peff@peff.net>,
Derrick Stolee <derrickstolee@github.com>
Subject: Re: [PATCH v2 1/8] for-each-ref: add --stdin option
Date: Mon, 13 Mar 2023 10:31:44 +0000 [thread overview]
Message-ID: <6badb697-aa74-cc2f-ba43-0bbf54b10973@dunelm.org.uk> (raw)
In-Reply-To: <a1d9e0f6ff6660c9264673be18bc24956f74eb9c.1678468864.git.gitgitgadget@gmail.com>
Hi Stolee
On 10/03/2023 17:20, Derrick Stolee via GitGitGadget wrote:
> From: Derrick Stolee <derrickstolee@github.com>
>
> When a user wishes to input a large list of patterns to 'git
> for-each-ref' (likely a long list of exact refs) there are frequently
> system limits on the number of command-line arguments.
>
> Add a new --stdin option to instead read the patterns from standard
> input. Add tests that check that any unrecognized arguments are
> considered an error when --stdin is provided. Also, an empty pattern
> list is interpreted as the complete ref set.
>
> When reading from stdin, we populate the filter.name_patterns array
> dynamically as opposed to pointing to the 'argv' array directly. This
> requires a careful cast while freeing the individual strings,
> conditioned on the --stdin option.
I think what you've got here is fine, but if you wanted you could
simplify it by using an strvec. Something like
struct strvec vec = STRVEC_INIT;
...
if (from_stdin) {
struct strbuf buf = STRBUF_INIT;
if (argv[0])
die(_("unknown arguments supplied with --stdin"));
while (strbuf_getline(&line, stdin) != EOF)
strvec_push(&vec, buf.buf);
filter.name_patters = vec.v;
} else {
filter.name_patterns = argv;
}
...
strvec_clear(&vec);
gets rid of the manual memory management with ALLOC_GROW() and the need
to cast filter.name_patterns when free()ing. It is not immediately
obvious from the name but struct strvec keeps the array NULL terminated.
Best Wishes
Phillip
> Signed-off-by: Derrick Stolee <derrickstolee@github.com>
> ---
> Documentation/git-for-each-ref.txt | 7 +++++-
> builtin/for-each-ref.c | 29 ++++++++++++++++++++++-
> t/t6300-for-each-ref.sh | 37 ++++++++++++++++++++++++++++++
> 3 files changed, 71 insertions(+), 2 deletions(-)
>
> diff --git a/Documentation/git-for-each-ref.txt b/Documentation/git-for-each-ref.txt
> index 6da899c6296..ccdc2911bb9 100644
> --- a/Documentation/git-for-each-ref.txt
> +++ b/Documentation/git-for-each-ref.txt
> @@ -9,7 +9,8 @@ SYNOPSIS
> --------
> [verse]
> 'git for-each-ref' [--count=<count>] [--shell|--perl|--python|--tcl]
> - [(--sort=<key>)...] [--format=<format>] [<pattern>...]
> + [(--sort=<key>)...] [--format=<format>]
> + [ --stdin | <pattern>... ]
> [--points-at=<object>]
> [--merged[=<object>]] [--no-merged[=<object>]]
> [--contains[=<object>]] [--no-contains[=<object>]]
> @@ -32,6 +33,10 @@ OPTIONS
> literally, in the latter case matching completely or from the
> beginning up to a slash.
>
> +--stdin::
> + If `--stdin` is supplied, then the list of patterns is read from
> + standard input instead of from the argument list.
> +
> --count=<count>::
> By default the command shows all refs that match
> `<pattern>`. This option makes it stop after showing
> diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c
> index 6f62f40d126..e005a7ef3ce 100644
> --- a/builtin/for-each-ref.c
> +++ b/builtin/for-each-ref.c
> @@ -25,6 +25,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
> struct ref_format format = REF_FORMAT_INIT;
> struct strbuf output = STRBUF_INIT;
> struct strbuf err = STRBUF_INIT;
> + int from_stdin = 0;
>
> struct option opts[] = {
> OPT_BIT('s', "shell", &format.quote_style,
> @@ -49,6 +50,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
> OPT_CONTAINS(&filter.with_commit, N_("print only refs which contain the commit")),
> OPT_NO_CONTAINS(&filter.no_commit, N_("print only refs which don't contain the commit")),
> OPT_BOOL(0, "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
> + OPT_BOOL(0, "stdin", &from_stdin, N_("read reference patterns from stdin")),
> OPT_END(),
> };
>
> @@ -75,7 +77,27 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
> ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase);
> filter.ignore_case = icase;
>
> - filter.name_patterns = argv;
> + if (from_stdin) {
> + struct strbuf line = STRBUF_INIT;
> + size_t nr = 0, alloc = 16;
> +
> + if (argv[0])
> + die(_("unknown arguments supplied with --stdin"));
> +
> + CALLOC_ARRAY(filter.name_patterns, alloc);
> +
> + while (strbuf_getline(&line, stdin) != EOF) {
> + ALLOC_GROW(filter.name_patterns, nr + 1, alloc);
> + filter.name_patterns[nr++] = strbuf_detach(&line, NULL);
> + }
> +
> + /* Add a terminating NULL string. */
> + ALLOC_GROW(filter.name_patterns, nr + 1, alloc);
> + filter.name_patterns[nr + 1] = NULL;
> + } else {
> + filter.name_patterns = argv;
> + }
> +
> filter.match_as_path = 1;
> filter_refs(&array, &filter, FILTER_REFS_ALL);
> ref_array_sort(sorting, &array);
> @@ -97,5 +119,10 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
> free_commit_list(filter.with_commit);
> free_commit_list(filter.no_commit);
> ref_sorting_release(sorting);
> + if (from_stdin) {
> + for (size_t i = 0; filter.name_patterns[i]; i++)
> + free((char *)filter.name_patterns[i]);
> + free(filter.name_patterns);
> + }
> return 0;
> }
> diff --git a/t/t6300-for-each-ref.sh b/t/t6300-for-each-ref.sh
> index c466fd989f1..a58053a54c5 100755
> --- a/t/t6300-for-each-ref.sh
> +++ b/t/t6300-for-each-ref.sh
> @@ -1464,4 +1464,41 @@ sig_crlf="$(printf "%s" "$sig" | append_cr; echo dummy)"
> sig_crlf=${sig_crlf%dummy}
> test_atom refs/tags/fake-sig-crlf contents:signature "$sig_crlf"
>
> +test_expect_success 'git for-each-ref --stdin: empty' '
> + >in &&
> + git for-each-ref --format="%(refname)" --stdin <in >actual &&
> + git for-each-ref --format="%(refname)" >expect &&
> + test_cmp expect actual
> +'
> +
> +test_expect_success 'git for-each-ref --stdin: fails if extra args' '
> + >in &&
> + test_must_fail git for-each-ref --format="%(refname)" \
> + --stdin refs/heads/extra <in 2>err &&
> + grep "unknown arguments supplied with --stdin" err
> +'
> +
> +test_expect_success 'git for-each-ref --stdin: matches' '
> + cat >in <<-EOF &&
> + refs/tags/multi*
> + refs/heads/amb*
> + EOF
> +
> + cat >expect <<-EOF &&
> + refs/heads/ambiguous
> + refs/tags/multi-ref1-100000-user1
> + refs/tags/multi-ref1-100000-user2
> + refs/tags/multi-ref1-200000-user1
> + refs/tags/multi-ref1-200000-user2
> + refs/tags/multi-ref2-100000-user1
> + refs/tags/multi-ref2-100000-user2
> + refs/tags/multi-ref2-200000-user1
> + refs/tags/multi-ref2-200000-user2
> + refs/tags/multiline
> + EOF
> +
> + git for-each-ref --format="%(refname)" --stdin <in >actual &&
> + test_cmp expect actual
> +'
> +
> test_done
next prev parent reply other threads:[~2023-03-13 10:31 UTC|newest]
Thread overview: 90+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-06 14:06 [PATCH 0/8] ahead-behind: new builtin for counting multiple commit ranges Derrick Stolee via GitGitGadget
2023-03-06 14:06 ` [PATCH 1/8] ahead-behind: create empty builtin Derrick Stolee via GitGitGadget
2023-03-06 18:48 ` Junio C Hamano
2023-03-07 0:40 ` Taylor Blau
2023-03-08 22:14 ` Derrick Stolee
2023-03-08 22:56 ` Junio C Hamano
2023-03-06 14:06 ` [PATCH 2/8] ahead-behind: parse tip references Derrick Stolee via GitGitGadget
2023-03-07 0:43 ` Taylor Blau
2023-03-06 14:06 ` [PATCH 3/8] ahead-behind: implement --ignore-missing option Derrick Stolee via GitGitGadget
2023-03-07 0:46 ` Taylor Blau
2023-03-06 14:06 ` [PATCH 4/8] commit-graph: combine generation computations Derrick Stolee via GitGitGadget
2023-03-06 14:06 ` [PATCH 5/8] commit-graph: return generation from memory Derrick Stolee via GitGitGadget
2023-03-06 14:06 ` [PATCH 6/8] commit-graph: introduce `ensure_generations_valid()` Taylor Blau via GitGitGadget
2023-03-06 18:52 ` Junio C Hamano
2023-03-07 0:50 ` Taylor Blau
2023-03-06 14:06 ` [PATCH 7/8] ahead-behind: implement ahead_behind() logic Derrick Stolee via GitGitGadget
2023-03-07 1:05 ` Taylor Blau
2023-03-09 17:32 ` Derrick Stolee
2023-03-06 14:06 ` [PATCH 8/8] ahead-behind: add --contains mode Derrick Stolee via GitGitGadget
2023-03-06 18:26 ` [PATCH 0/8] ahead-behind: new builtin for counting multiple commit ranges Junio C Hamano
2023-03-06 20:18 ` Derrick Stolee
2023-03-06 22:24 ` Junio C Hamano
2023-03-07 0:36 ` Taylor Blau
2023-03-09 9:20 ` Jeff King
2023-03-09 21:51 ` Junio C Hamano
2023-03-07 0:33 ` Taylor Blau
2023-03-10 17:20 ` [PATCH v2 0/8] ref-filter: ahead/behind counting, faster --merged option Derrick Stolee via GitGitGadget
2023-03-10 17:20 ` [PATCH v2 1/8] for-each-ref: add --stdin option Derrick Stolee via GitGitGadget
2023-03-10 18:08 ` Junio C Hamano
2023-03-13 10:31 ` Phillip Wood [this message]
2023-03-13 13:33 ` Derrick Stolee
2023-03-13 21:10 ` Taylor Blau
2023-03-15 13:37 ` Ævar Arnfjörð Bjarmason
2023-03-15 17:17 ` Jeff King
2023-03-15 17:49 ` Jeff King
2023-03-15 19:24 ` Junio C Hamano
2023-03-15 19:44 ` Jeff King
2023-03-10 17:20 ` [PATCH v2 2/8] for-each-ref: explicitly test no matches Derrick Stolee via GitGitGadget
2023-03-10 17:20 ` [PATCH v2 3/8] commit-graph: combine generation computations Derrick Stolee via GitGitGadget
2023-03-10 17:20 ` [PATCH v2 4/8] commit-graph: return generation from memory Derrick Stolee via GitGitGadget
2023-03-10 17:21 ` [PATCH v2 5/8] commit-graph: introduce `ensure_generations_valid()` Taylor Blau via GitGitGadget
2023-03-10 17:21 ` [PATCH v2 6/8] commit-reach: implement ahead_behind() logic Derrick Stolee via GitGitGadget
2023-03-15 13:50 ` Ævar Arnfjörð Bjarmason
2023-03-15 16:03 ` Junio C Hamano
2023-03-15 16:13 ` Derrick Stolee
2023-03-10 17:21 ` [PATCH v2 7/8] for-each-ref: add ahead-behind format atom Derrick Stolee via GitGitGadget
2023-03-10 19:09 ` Junio C Hamano
2023-03-15 13:57 ` Ævar Arnfjörð Bjarmason
2023-03-15 16:01 ` Junio C Hamano
2023-03-15 16:12 ` Derrick Stolee
2023-03-15 16:11 ` Derrick Stolee
2023-03-10 17:21 ` [PATCH v2 8/8] commit-reach: add tips_reachable_from_bases() Derrick Stolee via GitGitGadget
2023-03-15 14:13 ` Ævar Arnfjörð Bjarmason
2023-03-15 16:17 ` Derrick Stolee
2023-03-15 16:18 ` Derrick Stolee
2023-03-10 19:16 ` [PATCH v2 0/8] ref-filter: ahead/behind counting, faster --merged option Junio C Hamano
2023-03-10 19:25 ` Derrick Stolee
2023-03-15 17:31 ` Jeff King
2023-03-15 17:44 ` Derrick Stolee
2023-03-15 19:34 ` Junio C Hamano
2023-03-15 13:22 ` Ævar Arnfjörð Bjarmason
2023-03-15 13:54 ` Derrick Stolee
2023-03-15 17:45 ` [PATCH v3 " Derrick Stolee via GitGitGadget
2023-03-15 17:45 ` [PATCH v3 1/8] for-each-ref: add --stdin option Derrick Stolee via GitGitGadget
2023-03-15 18:06 ` Jeff King
2023-03-15 19:14 ` Junio C Hamano
2023-03-15 22:41 ` Jonathan Tan
2023-03-15 17:45 ` [PATCH v3 2/8] for-each-ref: explicitly test no matches Derrick Stolee via GitGitGadget
2023-03-15 17:45 ` [PATCH v3 3/8] commit-graph: combine generation computations Derrick Stolee via GitGitGadget
2023-03-15 22:49 ` Jonathan Tan
2023-03-17 18:30 ` Derrick Stolee
2023-03-15 17:45 ` [PATCH v3 4/8] commit-graph: return generation from memory Derrick Stolee via GitGitGadget
2023-03-15 22:58 ` Jonathan Tan
2023-03-15 17:45 ` [PATCH v3 5/8] commit-graph: introduce `ensure_generations_valid()` Taylor Blau via GitGitGadget
2023-03-15 17:45 ` [PATCH v3 6/8] commit-reach: implement ahead_behind() logic Derrick Stolee via GitGitGadget
2023-03-15 23:28 ` Jonathan Tan
2023-03-17 18:44 ` Derrick Stolee
2023-03-15 17:45 ` [PATCH v3 7/8] for-each-ref: add ahead-behind format atom Derrick Stolee via GitGitGadget
2023-03-15 17:45 ` [PATCH v3 8/8] commit-reach: add tips_reachable_from_bases() Derrick Stolee via GitGitGadget
2023-03-20 11:26 ` [PATCH v4 0/9] ref-filter: ahead/behind counting, faster --merged option Derrick Stolee via GitGitGadget
2023-03-20 11:26 ` [PATCH v4 1/9] for-each-ref: add --stdin option Derrick Stolee via GitGitGadget
2023-03-20 11:26 ` [PATCH v4 2/9] for-each-ref: explicitly test no matches Derrick Stolee via GitGitGadget
2023-03-20 11:26 ` [PATCH v4 3/9] commit-graph: refactor compute_topological_levels() Derrick Stolee via GitGitGadget
2023-03-20 11:26 ` [PATCH v4 4/9] commit-graph: simplify compute_generation_numbers() Derrick Stolee via GitGitGadget
2023-03-20 11:26 ` [PATCH v4 5/9] commit-graph: return generation from memory Derrick Stolee via GitGitGadget
2023-03-20 11:26 ` [PATCH v4 6/9] commit-graph: introduce `ensure_generations_valid()` Taylor Blau via GitGitGadget
2023-03-20 11:26 ` [PATCH v4 7/9] commit-reach: implement ahead_behind() logic Derrick Stolee via GitGitGadget
2023-03-20 20:40 ` Jonathan Tan
2023-03-20 11:26 ` [PATCH v4 8/9] for-each-ref: add ahead-behind format atom Derrick Stolee via GitGitGadget
2023-03-20 11:26 ` [PATCH v4 9/9] commit-reach: add tips_reachable_from_bases() Derrick Stolee via GitGitGadget
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=6badb697-aa74-cc2f-ba43-0bbf54b10973@dunelm.org.uk \
--to=phillip.wood123@gmail.com \
--cc=derrickstolee@github.com \
--cc=git@vger.kernel.org \
--cc=gitgitgadget@gmail.com \
--cc=gitster@pobox.com \
--cc=me@ttaylorr.com \
--cc=peff@peff.net \
--cc=phillip.wood@dunelm.org.uk \
--cc=vdye@github.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).