Jakub Narebski <jnareb@xxxxxxxxx> 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'); } - To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html