Re: git describe fails without tags
From: Junio C Hamano <hidden>
Date: 2016-06-15 22:42:17
Subsystem:
the rest · Maintainer:
Linus Torvalds
Uwe Zeisberger [off-list ref] writes:
I wonder if it would be sane to assume an implicit tag for the empty repository, s.t. git describe results in <empty>-62ac6c16 (whatever name is choosen for <empty>). Any opinions?
The value of 'describe' is not in the 62ac6c part, but in the tag part, which lets us find out that the rev in question is at least newer than that tag. If the reason you are doing "describe" is to find out an usable abbreviated name, you could feed the first few hexdigits to "git rev-parse --verify", lengthening the prefix longer by one until it says you have a unique prefix [*1*]. In other words, not particularly interested, although it is trivial to implement, like this:
diff --git a/describe.c b/describe.c
index 4866510..aeaf0fb 100644
--- a/describe.c
+++ b/describe.c@@ -137,7 +137,7 @@ static void describe(char *arg, int last return; } } - die("cannot describe '%s'", sha1_to_hex(cmit->object.sha1)); + printf("%s\n", find_unique_abbrev(cmit->object.sha1, abbrev)); } int main(int argc, char **argv)
[Footnote]
*1* If you do this often, we could introduce
$ git rev-parse --abbrev=<n> HEAD
that quacks like --verify (i.e. makes sure there is a single
"extended SHA1 expression" that evaluates to a valid object
name) but outputs the result abbreviated to at least <n>
hexdigits.
This has an added advantage that it would work on a
non-commit object name.
-- >8 --
[PATCH] rev-parse: --abbrev option.
The new option behaves just like --verify, but outputs an
abbreviated object name that is unique within the repository.
Signed-off-by: Junio C Hamano <redacted>
---
rev-parse.c | 14 ++++++++++++++
1 files changed, 14 insertions(+), 0 deletions(-)
3c7c31a913c3c4060e85f2919e2136412928dcecdiff --git a/rev-parse.c b/rev-parse.c
index 0c951af..c1646e4 100644
--- a/rev-parse.c
+++ b/rev-parse.c@@ -20,6 +20,7 @@ static char *def = NULL; #define REVERSED 1 static int show_type = NORMAL; static int symbolic = 0; +static int abbrev = 0; static int output_sq = 0; static int revs_count = 0;
@@ -95,6 +96,8 @@ static void show_rev(int type, const uns putchar('^'); if (symbolic && name) show(name); + else if (abbrev) + show(find_unique_abbrev(sha1, abbrev)); else show(sha1_to_hex(sha1)); }
@@ -195,6 +198,17 @@ int main(int argc, char **argv) verify = 1; continue; } + if (!strcmp(arg, "--abbrev") || + !strncmp(arg, "--abbrev=", 9)) { + filter &= ~(DO_FLAGS|DO_NOREV); + verify = 1; + abbrev = DEFAULT_ABBREV; + if (arg[8] == '=') + abbrev = strtoul(arg + 9, NULL, 10); + if (abbrev < 0 || 40 <= abbrev) + abbrev = DEFAULT_ABBREV; + continue; + } if (!strcmp(arg, "--sq")) { output_sq = 1; continue;
--
1.1.4.g869a