@matheusbsbs / matheus-bagliano / commits / 23615708e2
Fork 0

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> Signed-off-by: Junio C Hamano <junkio@cox.net>

Shawn O. Pearce committed May 21, 2007 at 03:20 UTC 23615708e2abef5c293ddee9335950161a038133
4 files changed +44 -7
Documentation/git-describe.txt
+6 -1
@@ -8,7 +8,7 @@ git-describe - Show the most recent tag that is reachable from a commit
8
9 SYNOPSIS
10 --------
11 -'git-describe' [--all] [--tags] [--abbrev=<n>] <committish>...
11 +'git-describe' [--all] [--tags] [--contains] [--abbrev=<n>] <committish>...
12
13 DESCRIPTION
14 -----------
@@ -31,6 +31,11 @@ OPTIONS
31 Instead of using only the annotated tags, use any tag
32 found in `.git/refs/tags`.
33
34 +--contains::
35 + Instead of finding the tag that predates the commit, find
36 + the tag that comes after the commit, and thus contains it.
37 + Automatically implies --tags.
38 +
39 --abbrev=<n>::
40 Instead of using the default 8 hexadecimal digits as the
41 abbreviated object name, use <n> digits.
Documentation/git-name-rev.txt
+7
@@ -34,6 +34,13 @@ OPTIONS
34 Read from stdin, append "(<rev_name>)" to all sha1's of nameable
35 commits, and pass to stdout
36
37 +--name-only::
38 + Instead of printing both the SHA-1 and the name, print only
39 + the name. If given with --tags the usual tag prefix of
40 + "tags/" is also ommitted from the name, matching the output
41 + of gitlink::git-describe[1] more closely. This option
42 + cannot be combined with --stdin.
43 +
44 EXAMPLE
45 -------
46
builtin-describe.c
+14
@@ -3,6 +3,7 @@
3 #include "tag.h"
4 #include "refs.h"
5 #include "builtin.h"
6 +#include "exec_cmd.h"
7
8 #define SEEN (1u<<0)
9 #define MAX_TAGS (FLAG_BITS - 1)
@@ -242,12 +243,15 @@ static void describe(const char *arg, int last_one)
243 int cmd_describe(int argc, const char **argv, const char *prefix)
244 {
245 int i;
246 + int contains = 0;
247
248 for (i = 1; i < argc; i++) {
249 const char *arg = argv[i];
250
251 if (*arg != '-')
252 break;
253 + else if (!strcmp(arg, "--contains"))
254 + contains = 1;
255 else if (!strcmp(arg, "--debug"))
256 debug = 1;
257 else if (!strcmp(arg, "--all"))
@@ -272,6 +276,16 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
276
277 save_commit_buffer = 0;
278
279 + if (contains) {
280 + const char **args = xmalloc((4 + argc - i) * sizeof(char*));
281 + args[0] = "name-rev";
282 + args[1] = "--name-only";
283 + args[2] = "--tags";
284 + memcpy(args + 3, argv + i, (argc - i) * sizeof(char*));
285 + args[3 + argc - i] = NULL;
286 + return cmd_name_rev(3 + argc - i, args, prefix);
287 + }
288 +
289 if (argc <= i)
290 describe("HEAD", 1);
291 else
builtin-name-rev.c
+17 -6
@@ -83,6 +83,7 @@ copy_data:
83
84 struct name_ref_data {
85 int tags_only;
86 + int name_only;
87 const char *ref_filter;
88 };
89
@@ -110,6 +111,10 @@ static int name_ref(const char *path, const unsigned char *sha1, int flags, void
111
112 if (!prefixcmp(path, "refs/heads/"))
113 path = path + 11;
114 + else if (data->tags_only
115 + && data->name_only
116 + && !prefixcmp(path, "refs/tags/"))
117 + path = path + 10;
118 else if (!prefixcmp(path, "refs/"))
119 path = path + 5;
120
@@ -149,7 +154,7 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
154 {
155 struct object_array revs = { 0, 0, NULL };
156 int as_is = 0, all = 0, transform_stdin = 0;
152 - struct name_ref_data data = { 0, NULL };
157 + struct name_ref_data data = { 0, 0, NULL };
158
159 git_config(git_default_config);
160
@@ -165,6 +170,9 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
170 if (!strcmp(*argv, "--")) {
171 as_is = 1;
172 continue;
173 + } else if (!strcmp(*argv, "--name-only")) {
174 + data.name_only = 1;
175 + continue;
176 } else if (!strcmp(*argv, "--tags")) {
177 data.tags_only = 1;
178 continue;
@@ -263,14 +271,17 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
271 struct object * obj = get_indexed_object(i);
272 if (!obj)
273 continue;
266 - printf("%s %s\n", sha1_to_hex(obj->sha1), get_rev_name(obj));
274 + if (!data.name_only)
275 + printf("%s ", sha1_to_hex(obj->sha1));
276 + printf("%s\n", get_rev_name(obj));
277 }
278 } else {
279 int i;
270 - for (i = 0; i < revs.nr; i++)
271 - printf("%s %s\n",
272 - revs.objects[i].name,
273 - get_rev_name(revs.objects[i].item));
280 + for (i = 0; i < revs.nr; i++) {
281 + if (!data.name_only)
282 + printf("%s ", revs.objects[i].name);
283 + printf("%s\n", get_rev_name(revs.objects[i].item));
284 + }
285 }
286
287 return 0;