[PATCH 3/6] for-each-ref: --stdin to match specified refs against pattern
- From
Greg Price <price@mit.edu>
- Date
- Jan 7, 2010, 23:08 UTC
- Message-ID
- <47cee601ac5a3b8938b902b173cbaf4146e0d657.1309133817.git.greg@quora.com>
- In-Reply-To
- <cover.1309133817.git.greg@quora.com>
The pattern syntax of for-each-ref is handy for users, but a bit subtle: a pattern matches a ref if it matches as an fnmatch pattern, or literally, or literally against a prefix of the refname up to a slash. Expose the for-each-ref pattern-matcher for scripts to use, so that they can provide a consistent user interface without duplicating the implementation.
Signed-off-by: Greg Price <price@mit.edu> --- builtin/for-each-ref.c | 22 ++++++++++++++++++++-- 1 files changed, 20 insertions(+), 2 deletions(-)
diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c index 89e75c6..0413ec0 100644 --- a/builtin/for-each-ref.c +++ b/builtin/for-each-ref.c @@ -779,6 +779,20 @@ static int grab_single_ref(const char *refname, const unsigned char *sha1, int f return 0; } +static void for_each_ref_stdin(each_ref_fn fn, void *cb_data) +{ + struct strbuf buf; + unsigned char sha1[20]; + + strbuf_init(&buf, 0); + while (strbuf_getline(&buf, stdin, '\n') != EOF) { + if (read_ref(buf.buf, sha1)) + continue; + fn(buf.buf, sha1, 0, cb_data); + } + strbuf_release(&buf); +} + static int cmp_ref_sort(struct ref_sort *s, struct refinfo *a, struct refinfo *b) { struct atom_value *va, *vb; @@ -943,7 +957,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix) int i, num_refs; const char *format = "%(objectname) %(objecttype)\t%(refname)"; struct ref_sort *sort = NULL, **sort_tail = &sort; - int maxcount = 0, quote_style = 0; + int maxcount = 0, quote_style = 0, stdin_refs = 0; struct refinfo **refs; struct grab_ref_cbdata cbdata; @@ -962,6 +976,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix) OPT_STRING( 0 , "format", &format, "format", "format to use for the output"), OPT_CALLBACK(0 , "sort", sort_tail, "key", "field name to sort on", &opt_parse_sort), + OPT_BOOLEAN( 0 , "stdin", &stdin_refs, "read candidate refs from stdin, rather than all refs"), OPT_END(), }; @@ -986,7 +1001,10 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix) memset(&cbdata, 0, sizeof(cbdata)); cbdata.grab_pattern = argv; - for_each_rawref(grab_single_ref, &cbdata); + if (stdin_refs) + for_each_ref_stdin(grab_single_ref, &cbdata); + else + for_each_rawref(grab_single_ref, &cbdata); refs = cbdata.grab_array; num_refs = cbdata.grab_cnt;
-- 1.7.5.4