@matheusbsbs / matheus-bagliano / commits / 5558e55c06
Fork 0

Teach for-each-ref about a little language called Tcl.

Love it or hate it, some people actually still program in Tcl. Some of those programs are meant for interfacing with Git. Programs such as gitk and git-gui. It may be useful to have Tcl-safe output available from for-each-ref, just like shell, Perl and Python already enjoy. Thanks to Sergey Vlasov for pointing out the horrible flaws in the first and second version of this patch, and steering me in the right direction for Tcl value quoting. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Shawn O. Pearce committed Jan 28, 2007 at 02:39 UTC 5558e55c06a1e897f3064f0c8a343d5c9858f6b2
4 files changed +47 -2
Documentation/git-for-each-ref.txt
+2 -2
@@ -7,7 +7,7 @@ git-for-each-ref - Output information on each ref
7
8 SYNOPSIS
9 --------
10 -'git-for-each-ref' [--count=<count>]\* [--shell|--perl|--python] [--sort=<key>]\* [--format=<format>] [<pattern>]
10 +'git-for-each-ref' [--count=<count>]\* [--shell|--perl|--python|--tcl] [--sort=<key>]\* [--format=<format>] [<pattern>]
11
12 DESCRIPTION
13 -----------
@@ -49,7 +49,7 @@ OPTIONS
49 using fnmatch(3). Refs that do not match the pattern
50 are not shown.
51
52 ---shell, --perl, --python::
52 +--shell, --perl, --python, --tcl::
53 If given, strings that substitute `%(fieldname)`
54 placeholders are quoted as string literals suitable for
55 the specified host language. This is meant to produce
builtin-for-each-ref.c
+10
@@ -12,6 +12,7 @@
12 #define QUOTE_SHELL 1
13 #define QUOTE_PERL 2
14 #define QUOTE_PYTHON 3
15 +#define QUOTE_TCL 4
16
17 typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;
18
@@ -723,6 +724,9 @@ static void print_value(struct refinfo *ref, int atom, int quote_style)
724 case QUOTE_PYTHON:
725 python_quote_print(stdout, v->s);
726 break;
727 + case QUOTE_TCL:
728 + tcl_quote_print(stdout, v->s);
729 + break;
730 }
731 }
732
@@ -834,6 +838,12 @@ int cmd_for_each_ref(int ac, const char **av, char *prefix)
838 quote_style = QUOTE_PYTHON;
839 continue;
840 }
841 + if (!strcmp(arg, "--tcl") ) {
842 + if (0 <= quote_style)
843 + die("more than one quoting style?");
844 + quote_style = QUOTE_TCL;
845 + continue;
846 + }
847 if (!strncmp(arg, "--count=", 8)) {
848 if (maxcount)
849 die("more than one --count?");
quote.c
+34
@@ -387,3 +387,37 @@ void python_quote_print(FILE *stream, const char *src)
387 }
388 fputc(sq, stream);
389 }
390 +
391 +void tcl_quote_print(FILE *stream, const char *src)
392 +{
393 + char c;
394 +
395 + fputc('"', stream);
396 + while ((c = *src++)) {
397 + switch (c) {
398 + case '[': case ']':
399 + case '{': case '}':
400 + case '$': case '\\': case '"':
401 + fputc('\\', stream);
402 + default:
403 + fputc(c, stream);
404 + break;
405 + case '\f':
406 + fputs("\\f", stream);
407 + break;
408 + case '\r':
409 + fputs("\\r", stream);
410 + break;
411 + case '\n':
412 + fputs("\\n", stream);
413 + break;
414 + case '\t':
415 + fputs("\\t", stream);
416 + break;
417 + case '\v':
418 + fputs("\\v", stream);
419 + break;
420 + }
421 + }
422 + fputc('"', stream);
423 +}
quote.h
+1
@@ -55,5 +55,6 @@ extern void write_name_quoted(const char *prefix, int prefix_len,
55 /* quoting as a string literal for other languages */
56 extern void perl_quote_print(FILE *stream, const char *src);
57 extern void python_quote_print(FILE *stream, const char *src);
58 +extern void tcl_quote_print(FILE *stream, const char *src);
59
60 #endif