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

[PATCH] name-rev: introduce the --ref-filter=<regex> option

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Jan 27, 2007, 12:39 UTC
Message-ID
<Pine.LNX.4.63.0701271334410.22628@wbgn013.biozentrum.uni-wuerzburg.de>
In-Reply-To
<7vhcudoxfj.fsf@assigned-by-dhcp.cox.net>

Instead of (or, in addition to) --tags, to use only tags for naming, you can now use --ref-filter=<regex> to specify which refs are used for naming.

Example:
	$ git name-rev --ref-filter='/v1' 33db5f4d
	33db5f4d tags/v1.0rc1^0~1593
Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
---
	On Fri, 26 Jan 2007, Junio C Hamano wrote:
	> Yeah, I found name-rev to be mostly useless since it almost
	> always names relative to my unannotated "anchor" tag that I use
	> to keep track of 'master' I sent the last "What's in" message
	> for.
	How about this?
	Though I think you'd use --ref-filter='tags/v[0-9]'. Or you would 
	alias it:
		git-repo-config alias.findver \
			"name-rev --ref-filter='tags/v[0-9]'"
 Documentation/git-name-rev.txt |   14 ++++++++++++--
 builtin-name-rev.c             |   27 +++++++++++++++++++++------
 2 files changed, 33 insertions(+), 8 deletions(-)
diff --git a/Documentation/git-name-rev.txt b/Documentation/git-name-rev.txt
index 37fbf66..7de4bea 100644
--- a/Documentation/git-name-rev.txt
+++ b/Documentation/git-name-rev.txt
@@ -8,7 +8,7 @@ git-name-rev - Find symbolic names for given revs
 
 SYNOPSIS
 --------
-'git-name-rev' [--tags] ( --all | --stdin | <committish>... )
+'git-name-rev' [--tags | --ref-filter=<regex>] ( --all | --stdin | <committish>... )
 
 DESCRIPTION
 -----------
@@ -22,6 +22,9 @@ OPTIONS
 --tags::
 	Do not use branch names, but only tags to name the commits
 
+--ref-filter=<regex>::
+	Only use refs for naming which match the regexp
+
 --all::
 	List all commits reachable from all refs
 
@@ -29,7 +32,7 @@ OPTIONS
 	Read from stdin, append "(<rev_name>)" to all sha1's of nameable
 	commits, and pass to stdout
 
-EXAMPLE
+EXAMPLES
 -------
 
 Given a commit, find out where it is relative to the local refs. Say somebody
@@ -52,6 +55,13 @@ Another nice thing you can do is:
 % git log | git name-rev --stdin
 ------------
 
+If you want to name a revision only by a subset of the refs you have, use
+a regular expression to specify the subset:
+
+------------
+$ git name-rev --ref-filter='/v1' 33db5f4d
+33db5f4d tags/v1.0rc1^0~1593
+------------
 
 Author
 ------
diff --git a/builtin-name-rev.c b/builtin-name-rev.c
index b4f15cc..89ea95d 100644
--- a/builtin-name-rev.c
+++ b/builtin-name-rev.c
@@ -3,9 +3,10 @@
 #include "commit.h"
 #include "tag.h"
 #include "refs.h"
+#include <regex.h>
 
 static const char name_rev_usage[] =
-	"git-name-rev [--tags] ( --all | --stdin | committish [committish...] )\n";
+	"git-name-rev [--tags | --ref-filter=<regexp>] ( --all | --stdin | committish [committish...] )\n";
 
 typedef struct rev_name {
 	const char *tip_name;
@@ -74,13 +75,21 @@ copy_data:
 	}
 }
 
+struct name_ref_data {
+	int tags_only;
+	regex_t *ref_filter;
+};
+
 static int name_ref(const char *path, const unsigned char *sha1, int flags, void *cb_data)
 {
 	struct object *o = parse_object(sha1);
-	int tags_only = *(int*)cb_data;
+	struct name_ref_data *data = cb_data;
 	int deref = 0;
 
-	if (tags_only && strncmp(path, "refs/tags/", 10))
+	if (data->tags_only && strncmp(path, "refs/tags/", 10))
+		return 0;
+
+	if (data->ref_filter && regexec(data->ref_filter, path, 0, NULL, 0))
 		return 0;
 
 	while (o && o->type == OBJ_TAG) {
@@ -129,7 +138,7 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
 {
 	struct object_array revs = { 0, 0, NULL };
 	int as_is = 0, all = 0, transform_stdin = 0;
-	int tags_only = 0;
+	struct name_ref_data data = { 0, NULL };
 
 	git_config(git_default_config);
 
@@ -146,7 +155,13 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
 				as_is = 1;
 				continue;
 			} else if (!strcmp(*argv, "--tags")) {
-				tags_only = 1;
+				data.tags_only = 1;
+				continue;
+			} else  if (!strncmp(*argv, "--ref-filter=", 13)) {
+				data.ref_filter = xmalloc(sizeof(regex_t));
+				if (regcomp(data.ref_filter, *argv + 13,
+							REG_EXTENDED))
+					die ("invalid regexp: %s", *argv + 13);
 				continue;
 			} else if (!strcmp(*argv, "--all")) {
 				if (argc > 1)
@@ -185,7 +200,7 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
 		add_object_array((struct object *)commit, *argv, &revs);
 	}
 
-	for_each_ref(name_ref, &tags_only);
+	for_each_ref(name_ref, &data);
 
 	if (transform_stdin) {
 		char buffer[2048];
-- 
1.5.0.rc2.g324a-dirty
Previous: Junio C HamanoNext: Johannes Schindelin
Message 7 of 22 in “finding earliest tags descended from a given commit”
  1. J. Bruce FieldsJan 27, 2007
  2. Shawn O. PearceJan 27, 2007
  3. Linus TorvaldsJan 27, 2007
  4. J. Bruce FieldsJan 27, 2007
  5. Shawn O. PearceJan 27, 2007
  6. Junio C HamanoJan 27, 2007
  7. name-rev: introduce the --ref-filter=<regex> optionJohannes Schindelin, Jan 27, 2007
  8. Johannes SchindelinFeb 17, 2007
  9. Jeff KingFeb 17, 2007
  10. Johannes SchindelinFeb 17, 2007
  11. Junio C HamanoFeb 17, 2007
  12. git-name-rev: accept list of refs from userJeff King, Feb 17, 2007
  13. Jeff KingFeb 17, 2007
  14. Junio C HamanoFeb 17, 2007
  15. Jeff KingFeb 17, 2007
  16. Johannes SchindelinFeb 18, 2007
  17. Junio C HamanoFeb 17, 2007
  18. Johannes SchindelinFeb 17, 2007
  19. name-rev: introduce the --ref-filter=<pattern> optionJohannes Schindelin, Feb 17, 2007
  20. Junio C HamanoFeb 17, 2007
  21. Johannes SchindelinFeb 17, 2007
  22. Junio C HamanoFeb 17, 2007

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.