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

Re: Add "git show-ref" builtin command

From
Junio C Hamano <junkio@cox.net>
Date
Sep 15, 2006, 22:54 UTC
Message-ID
<7vy7skeo5v.fsf@assigned-by-dhcp.cox.net>
In-Reply-To
<eef5m8$euj$1@sea.gmane.org>
Jakub Narebski <jnareb@gmail.com> writes:
> Wouldn't it be better to be able to use (or be able to enable, like echo -e
> option) interpretation of the  backslash-escaped characters, like 
> \t, \n, \0?

I've been thinking about letting the --format to specify embedding arbitrary byte value in the output.

This option however is mostly to help Porcelain written in languages other than C (and that is where the language specific quoting styles come in) to allow a template of a scriptlet to be specified, as you have probably seen in the examples in the documentation page, so I think it is more user friendly to leave backslash as just a literal character.

My current thinking is to allow you to say %XX (a per-cent followed by exactly two hexadecimal digits) to do embed a literal byte value. Then a Porcelain written in Perl that does not want to eval output can do something like this:

	my $fmt = 'r%(refname)%00o%(objectname)%00%00';
	open R, '-|', 'git-for-each-ref', "--format=$fmt";
	my $all = join('', <R>);
	close R;        	        
        for (split(/\0\0/, $all)) {
		/r(.*?)\0o(.*)/ &&
                print "ref = $1, obj = $2\n";
        }

Another thing is that originally I picked %(name) syntax because I thought we might want to do fancier "%20(column)d" like Python does with its string formatting operator. But I now think it makes more sense to output whatever is asked as string literals and have host language worry about formatting. So in that sense, using %() as our formatting specifier will get in the way for people who writes in Python. Maybe I should change it to something like %{name} instead (not ${name} -- that would interfere with the shell and Perl).

Anyhow, on top of the previous one, this will let you say %00 to embed a NUL in your string.

diff --git a/builtin-for-each-ref.c b/builtin-for-each-ref.c
index f064e7e..698618b 100644
--- a/builtin-for-each-ref.c
+++ b/builtin-for-each-ref.c
@@ -710,12 +710,39 @@ static void print_value(struct refinfo *
 	}
 }
 
+static int hex1(char ch)
+{
+	if ('0' <= ch && ch <= '9')
+		return ch - '0';
+	else if ('a' <= ch && ch <= 'f')
+		return ch - 'a' + 10;
+	else if ('A' <= ch && ch <= 'F')
+		return ch - 'A' + 10;
+	return -1;
+}
+static int hex2(const char *cp)
+{
+	if (cp[0] && cp[1])
+		return (hex1(cp[0]) << 4) | hex1(cp[1]);
+	else
+		return -1;
+}
+
 static void emit(const char *cp, const char *ep)
 {
 	while (*cp && (!ep || cp < ep)) {
-		if (*cp == '%')
+		if (*cp == '%') {
 			if (cp[1] == '%')
 				cp++;
+			else {
+				int ch = hex2(cp + 1);
+				if (0 <= ch) {
+					putchar(ch);
+					cp += 3;
+					continue;
+				}
+			}
+		}
 		putchar(*cp);
 		cp++;
 	}
@@ -731,8 +758,10 @@ static void show_ref(struct refinfo *inf
 			emit(cp, sp);
 		print_value(info, parse_atom(sp + 2, ep), quote_style);
 	}
-	if (*cp)
-		fputs(cp, stdout);
+	if (*cp) {
+		sp = cp + strlen(cp);
+		emit(cp, sp);
+	}
 	putchar('\n');
 }
 
Previous: Jakub NarebskiNext: Linus Torvalds
Message 4 of 7 in “Add "git show-ref" builtin command”
  1. Linus TorvaldsSep 15, 2006
  2. Junio C HamanoSep 15, 2006
  3. Jakub NarebskiSep 15, 2006
  4. Junio C HamanoSep 15, 2006
  5. Teach "git checkout" to use git-show-refLinus Torvalds, Sep 15, 2006
  6. Jakub NarebskiSep 15, 2006
  7. Junio C HamanoSep 16, 2006

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.