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

Re: [PATCH] Cross-reference the manpages for git-name-rev and git-describe

From
Shawn O. Pearce <spearce@spearce.org>
Date
May 21, 2007, 07:20 UTC
Message-ID
<20070521072025.GO3141@spearce.org>
In-Reply-To
<7vsl9rmkm0.fsf@assigned-by-dhcp.cox.net>
Junio C Hamano <junkio@cox.net> wrote:
> wouldn't this be useful?
> 
> $ git-describe --contains --tags 1b600e659
> v1.5.0-rc3~34
Like this?  ;-)

-->8-- Teach git-describe how to run name-rev

Often users want to know not which tagged version a commit came after, but which tagged version a commit is contained within. This latter task is the job of git-name-rev, but most users are looking to git-describe to do the job.

Junio suggested we make `git describe --contains` run the correct tool, `git name-rev`, and that's exactly what we do here. The output of name-rev was adjusted slightly through the new --name-only option, allowing describe to execv into name-rev and maintain its current output format.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
 Documentation/git-describe.txt |    7 ++++++-
 Documentation/git-name-rev.txt |    7 +++++++
 builtin-describe.c             |   15 +++++++++++++++
 builtin-name-rev.c             |   23 +++++++++++++++++------
 4 files changed, 45 insertions(+), 7 deletions(-)
diff --git a/Documentation/git-describe.txt b/Documentation/git-describe.txt
index 47a583d..dc47b65 100644
--- a/Documentation/git-describe.txt
+++ b/Documentation/git-describe.txt
@@ -8,7 +8,7 @@ git-describe - Show the most recent tag that is reachable from a commit
 
 SYNOPSIS
 --------
-'git-describe' [--all] [--tags] [--abbrev=<n>] <committish>...
+'git-describe' [--all] [--tags] [--contains] [--abbrev=<n>] <committish>...
 
 DESCRIPTION
 -----------
@@ -31,6 +31,11 @@ OPTIONS
 	Instead of using only the annotated tags, use any tag
 	found in `.git/refs/tags`.
 
+--contains::
+	Instead of finding the tag that predates the commit, find
+	the tag that comes after the commit, and thus contains it.
+	Automatically implies --tags.
+
 --abbrev=<n>::
 	Instead of using the default 8 hexadecimal digits as the
 	abbreviated object name, use <n> digits.
diff --git a/Documentation/git-name-rev.txt b/Documentation/git-name-rev.txt
index d6c8bf8..9a1645d 100644
--- a/Documentation/git-name-rev.txt
+++ b/Documentation/git-name-rev.txt
@@ -34,6 +34,13 @@ OPTIONS
 	Read from stdin, append "(<rev_name>)" to all sha1's of nameable
 	commits, and pass to stdout
 
+--name-only::
+	Instead of printing both the SHA-1 and the name, print only
+	the name.  If given with --tags the usual tag prefix of
+	"tags/" is also ommitted from the name, matching the output
+	of gitlink::git-describe[1] more closely.  This option
+	cannot be combined with --stdin.
+
 EXAMPLE
 -------
 
diff --git a/builtin-describe.c b/builtin-describe.c
index 165917e..efbd43f 100644
--- a/builtin-describe.c
+++ b/builtin-describe.c
@@ -3,6 +3,7 @@
 #include "tag.h"
 #include "refs.h"
 #include "builtin.h"
+#include "exec_cmd.h"
 
 #define SEEN		(1u<<0)
 #define MAX_TAGS	(FLAG_BITS - 1)
@@ -242,12 +243,15 @@ static void describe(const char *arg, int last_one)
 int cmd_describe(int argc, const char **argv, const char *prefix)
 {
 	int i;
+	int contains = 0;
 
 	for (i = 1; i < argc; i++) {
 		const char *arg = argv[i];
 
 		if (*arg != '-')
 			break;
+		else if (!strcmp(arg, "--contains"))
+			contains = 1;
 		else if (!strcmp(arg, "--debug"))
 			debug = 1;
 		else if (!strcmp(arg, "--all"))
@@ -272,6 +276,17 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
 
 	save_commit_buffer = 0;
 
+	if (contains) {
+		const char **nr = xmalloc((4 + argc - i) * sizeof(char*));
+		nr[0] = "name-rev";
+		nr[1] = "--name-only";
+		nr[2] = "--tags";
+		memcpy(nr + 3, argv + i, (argc - i) * sizeof(char*));
+		nr[3 + argc - i] = NULL;
+		execv_git_cmd(nr);
+		die("unable to start %s", nr[0]);
+	}
+
 	if (argc <= i)
 		describe("HEAD", 1);
 	else
diff --git a/builtin-name-rev.c b/builtin-name-rev.c
index ef16385..a639e2f 100644
--- a/builtin-name-rev.c
+++ b/builtin-name-rev.c
@@ -83,6 +83,7 @@ copy_data:
 
 struct name_ref_data {
 	int tags_only;
+	int name_only;
 	const char *ref_filter;
 };
 
@@ -110,6 +111,10 @@ static int name_ref(const char *path, const unsigned char *sha1, int flags, void
 
 		if (!prefixcmp(path, "refs/heads/"))
 			path = path + 11;
+		else if (data->tags_only
+		    && data->name_only
+		    && !prefixcmp(path, "refs/tags/"))
+			path = path + 10;
 		else if (!prefixcmp(path, "refs/"))
 			path = path + 5;
 
@@ -149,7 +154,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;
-	struct name_ref_data data = { 0, NULL };
+	struct name_ref_data data = { 0, 0, NULL };
 
 	git_config(git_default_config);
 
@@ -165,6 +170,9 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
 			if (!strcmp(*argv, "--")) {
 				as_is = 1;
 				continue;
+			} else if (!strcmp(*argv, "--name-only")) {
+				data.name_only = 1;
+				continue;
 			} else if (!strcmp(*argv, "--tags")) {
 				data.tags_only = 1;
 				continue;
@@ -263,14 +271,17 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
 			struct object * obj = get_indexed_object(i);
 			if (!obj)
 				continue;
-			printf("%s %s\n", sha1_to_hex(obj->sha1), get_rev_name(obj));
+			if (!data.name_only)
+				printf("%s ", sha1_to_hex(obj->sha1));
+			printf("%s\n", get_rev_name(obj));
 		}
 	} else {
 		int i;
-		for (i = 0; i < revs.nr; i++)
-			printf("%s %s\n",
-				revs.objects[i].name,
-				get_rev_name(revs.objects[i].item));
+		for (i = 0; i < revs.nr; i++) {
+			if (!data.name_only)
+				printf("%s ", revs.objects[i].name);
+			printf("%s\n", get_rev_name(revs.objects[i].item));
+		}
 	}
 
 	return 0;
-- 
1.5.2.14.g45bde-dirty

-- 
Shawn.
Previous: Matthieu MoyNext: Junio C Hamano
Message 12 of 17 in “Commits gone AWOL, but not reported by git-fsck --unreachable”
  1. Robin H. JohnsonMay 19, 2007
  2. Jeff KingMay 19, 2007
  3. Robin H. JohnsonMay 19, 2007
  4. Jeff KingMay 19, 2007
  5. Anand KumriaMay 19, 2007
  6. Matthieu MoyMay 19, 2007
  7. Brian GernhardtMay 19, 2007
  8. Matthieu MoyMay 19, 2007
  9. Brian GernhardtMay 19, 2007
  10. Junio C HamanoMay 19, 2007
  11. Cross-reference the manpages for git-name-rev and git-describe (was Re: Commits gone AWOL, but not reported by git-fsck --unreachable)Matthieu Moy, May 20, 2007
  12. Shawn O. PearceMay 21, 2007
  13. Junio C HamanoMay 21, 2007
  14. Shawn O. PearceMay 21, 2007
  15. Matthieu MoyMay 21, 2007
  16. Junio C HamanoMay 22, 2007
  17. Shawn O. PearceMay 22, 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.