git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <junkio@cox.net>
To: Jakub Narebski <jnareb@gmail.com>
Cc: git@vger.kernel.org
Subject: Re: Add "git show-ref" builtin command
Date: Fri, 15 Sep 2006 15:54:36 -0700	[thread overview]
Message-ID: <7vy7skeo5v.fsf@assigned-by-dhcp.cox.net> (raw)
In-Reply-To: <eef5m8$euj$1@sea.gmane.org> (Jakub Narebski's message of "Fri, 15 Sep 2006 23:24:56 +0200")

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');
 }
 

  reply	other threads:[~2006-09-15 22:54 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-09-15 18:19 Add "git show-ref" builtin command Linus Torvalds
2006-09-15 21:00 ` Junio C Hamano
2006-09-15 21:24   ` Jakub Narebski
2006-09-15 22:54     ` Junio C Hamano [this message]
2006-09-15 21:56 ` Teach "git checkout" to use git-show-ref Linus Torvalds
2006-09-15 22:11   ` Jakub Narebski
2006-09-16  9:33   ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7vy7skeo5v.fsf@assigned-by-dhcp.cox.net \
    --to=junkio@cox.net \
    --cc=git@vger.kernel.org \
    --cc=jnareb@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).