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

Re: git-rev-parse --symbolic-abbrev-name

From
Arnaud Lacombe <lacombar@gmail.com>
Date
Jan 4, 2009, 20:23 UTC
Message-ID
<1a69a9d80901041223r1f3d2956ne05996793bb23e97@mail.gmail.com>
In-Reply-To
<7v63kuyibi.fsf@gitster.siamese.dyndns.org>
Hi,
On Sun, Jan 4, 2009 at 2:36 PM, Junio C Hamano <gitster@pobox.com> wrote:
Show 15 quoted lines
> Karl Chen <quarl@cs.berkeley.edu> writes:
>
>> ... you really think "branchfoo" instead of
>> "refs/heads/branchfoo" is a narrow special case?
>
> Of course it is narrower.  There are namespaces other than "heads" under
> refs, and not everybody is interested in branches.
>
>> obviously all those people posting on blogs don't know about it :)
>
> Yes, and that won't be helped by any new option to the plumbing.
>
> The above two does not necessarily mean that it is useless to add a new
> option to help a narrow special case that is common, though.
>

You'll find hereafter two patches which implements this in git-symbolic-ref and git-rev-parse. Feel free to choose the one you find the best. If you choose to integrate one of these, tells me and I'll do a proper documentation bits and patch submission.

Sample output:

~/git/% ./git-rev-parse --symbolic-short-name HEAD master ~/git/% ./git-symbolic-ref -a HEAD master ~/git/% git checkout v1.6.1 ~/git/% ./git-rev-parse --symbolic-short-name HEAD HEAD ~/git/% ./git-symbolic-ref -a HEAD fatal: ref HEAD is not a symbolic ref ~/git/% ./git-symbolic-ref -qa HEAD ~/git/%

Thanks in advance,
 - Arnaud

ps: I choose --symbolic-short-name as the opposite of --symbolic-full-name for consistency. ps2: sorry for the bogus mime-type

diff --git a/builtin-rev-parse.c b/builtin-rev-parse.c
index 81d5a6f..70f4a33 100644
--- a/builtin-rev-parse.c
+++ b/builtin-rev-parse.c
@@ -24,6 +24,7 @@ static int show_type = NORMAL;
 
 #define SHOW_SYMBOLIC_ASIS 1
 #define SHOW_SYMBOLIC_FULL 2
+#define SHOW_SYMBOLIC_SHORT 3
 static int symbolic;
 static int abbrev;
 static int output_sq;
@@ -110,7 +111,10 @@ static void show_rev(int type, const unsigned char *sha1, const char *name)
 	def = NULL;
 
 	if (symbolic && name) {
-		if (symbolic == SHOW_SYMBOLIC_FULL) {
+		switch (symbolic) {
+		case SHOW_SYMBOLIC_FULL:
+		case SHOW_SYMBOLIC_SHORT:
+			{
 			unsigned char discard[20];
 			char *full;
 
@@ -125,13 +129,20 @@ static void show_rev(int type, const unsigned char *sha1, const char *name)
 				 */
 				break;
 			case 1: /* happy */
+				if (symbolic == SHOW_SYMBOLIC_SHORT) {
+					char *p;
+					p = strrchr(full, (int)'/');
+					if (p != NULL)
+						full = p + 1;
+				}
 				show_with_type(type, full);
 				break;
 			default: /* ambiguous */
 				error("refname '%s' is ambiguous", name);
-				break;
 			}
-		} else {
+			break;
+			}
+		default:
 			show_with_type(type, name);
 		}
 	}
@@ -506,6 +517,10 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
 				symbolic = SHOW_SYMBOLIC_FULL;
 				continue;
 			}
+			if (!strcmp(arg, "--symbolic-short-name")) {
+				symbolic = SHOW_SYMBOLIC_SHORT;
+				continue;
+			}
 			if (!strcmp(arg, "--all")) {
 				for_each_ref(show_reference, NULL);
 				continue;


diff --git a/builtin-symbolic-ref.c b/builtin-symbolic-ref.c
index bfc78bb..ff9ff46 100644
--- a/builtin-symbolic-ref.c
+++ b/builtin-symbolic-ref.c
@@ -8,7 +8,7 @@ static const char * const git_symbolic_ref_usage[] = {
 	NULL
 };
 
-static void check_symref(const char *HEAD, int quiet)
+static void check_symref(const char *HEAD, int quiet, int abbrev)
 {
 	unsigned char sha1[20];
 	int flag;
@@ -22,15 +22,21 @@ static void check_symref(const char *HEAD, int quiet)
 		else
 			exit(1);
 	}
+	if (abbrev) {
+		char *p = strrchr(refs_heads_master, (int)'/');
+		if (p != NULL)
+			refs_heads_master = p + 1;
+	}
 	puts(refs_heads_master);
 }
 
 int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
 {
-	int quiet = 0;
+	int abbrev = 0, quiet = 0;
 	const char *msg = NULL;
 	struct option options[] = {
 		OPT__QUIET(&quiet),
+		OPT_BOOLEAN('a', NULL, &abbrev, "show only branch name"),
 		OPT_STRING('m', NULL, &msg, "reason", "reason of the update"),
 		OPT_END(),
 	};
@@ -41,7 +47,7 @@ int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
 		die("Refusing to perform update with empty message");
 	switch (argc) {
 	case 1:
-		check_symref(argv[0], quiet);
+		check_symref(argv[0], quiet, abbrev);
 		break;
 	case 2:
 		create_symref(argv[0], argv[1], msg);
Previous: Junio C HamanoNext: Miklos Vajna
Message 10 of 28 in “git-branch --print-current”
  1. Karl ChenJan 2, 2009
  2. David AguilarJan 2, 2009
  3. Karl ChenJan 4, 2009
  4. Miklos VajnaJan 4, 2009
  5. Karl ChenJan 4, 2009
  6. Junio C HamanoJan 4, 2009
  7. git-rev-parse --symbolic-abbrev-name [was Re: git-branch --print-current]Karl Chen, Jan 4, 2009
  8. demerphqJan 4, 2009
  9. Junio C HamanoJan 4, 2009
  10. Arnaud LacombeJan 4, 2009
  11. Miklos VajnaJan 4, 2009
  12. Arnaud LacombeJan 5, 2009
  13. Miklos VajnaJan 5, 2009
  14. Junio C HamanoJan 6, 2009
  15. Arnaud LacombeJan 7, 2009
  16. demerphqJan 4, 2009
  17. Junio C HamanoJan 5, 2009
  18. Shawn O. PearceJan 5, 2009
  19. Junio C HamanoJan 5, 2009
  20. Jeff KingJan 5, 2009
  21. Arnaud LacombeJan 4, 2009
  22. Karl ChenJan 4, 2009
  23. demerphqJan 4, 2009
  24. Arnaud LacombeJan 4, 2009
  25. Adeodato SimóJan 4, 2009
  26. Jakub NarebskiJan 4, 2009
  27. Alexandre DulaunoyJan 4, 2009
  28. demerphqJan 4, 2009

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.