git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[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
Previous: Greg PriceNext: Greg Price
Message 10 of 24 in “rebase: command "ref" and options --rewrite-{refs,heads,tags}”
  1. 0/6 rebase: command "ref" and options --rewrite-{refs,heads,tags}Greg Price, Jun 27, 2011
  2. 1/6 rebase -i: Add the "ref" commandGreg Price, Oct 10, 2009
  3. Junio C HamanoJun 27, 2011
  4. Greg PriceJun 28, 2011
  5. 2/6 pretty: Add %D for script-friendly decorationGreg Price, Nov 18, 2009
  6. Junio C HamanoJun 27, 2011
  7. 4/6 rebase: --rewrite-{refs,heads,tags} to pull refs along with branchGreg Price, Nov 18, 2009
  8. Phil HordJun 27, 2011
  9. Greg PriceJun 28, 2011
  10. 3/6 for-each-ref: --stdin to match specified refs against patternGreg Price, Jan 7, 2010
  11. 5/6 t/lib-rebase.sh: pass through ref commandsGreg Price, Jan 25, 2010
  12. 6/6 rebase --rewrite-refs: testsGreg Price, Jan 25, 2010
  13. Phil HordJun 27, 2011
  14. Greg PriceJun 28, 2011
  15. Junio C HamanoJun 27, 2011
  16. Greg PriceJun 28, 2011
  17. Junio C HamanoJun 27, 2011
  18. Greg PriceJun 28, 2011
  19. Greg PriceJun 28, 2011
  20. Ramkumar RamachandraJun 30, 2011
  21. Sverre RabbelierAug 3, 2011
  22. Greg PriceAug 3, 2011
  23. Junio C HamanoJun 27, 2011
  24. Greg PriceJun 28, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.