Skip to content

Commit

Permalink
Teach for-each-ref about a little language called Tcl.
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Shawn O. Pearce authored and Junio C Hamano committed Jan 28, 2007
1 parent cace16f commit 5558e55
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Documentation/git-for-each-ref.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ git-for-each-ref - Output information on each ref

SYNOPSIS
--------
'git-for-each-ref' [--count=<count>]\* [--shell|--perl|--python] [--sort=<key>]\* [--format=<format>] [<pattern>]
'git-for-each-ref' [--count=<count>]\* [--shell|--perl|--python|--tcl] [--sort=<key>]\* [--format=<format>] [<pattern>]

DESCRIPTION
-----------
Expand Down Expand Up @@ -49,7 +49,7 @@ OPTIONS
using fnmatch(3). Refs that do not match the pattern
are not shown.

--shell, --perl, --python::
--shell, --perl, --python, --tcl::
If given, strings that substitute `%(fieldname)`
placeholders are quoted as string literals suitable for
the specified host language. This is meant to produce
Expand Down
10 changes: 10 additions & 0 deletions builtin-for-each-ref.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#define QUOTE_SHELL 1
#define QUOTE_PERL 2
#define QUOTE_PYTHON 3
#define QUOTE_TCL 4

typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;

Expand Down Expand Up @@ -723,6 +724,9 @@ static void print_value(struct refinfo *ref, int atom, int quote_style)
case QUOTE_PYTHON:
python_quote_print(stdout, v->s);
break;
case QUOTE_TCL:
tcl_quote_print(stdout, v->s);
break;
}
}

Expand Down Expand Up @@ -834,6 +838,12 @@ int cmd_for_each_ref(int ac, const char **av, char *prefix)
quote_style = QUOTE_PYTHON;
continue;
}
if (!strcmp(arg, "--tcl") ) {
if (0 <= quote_style)
die("more than one quoting style?");
quote_style = QUOTE_TCL;
continue;
}
if (!strncmp(arg, "--count=", 8)) {
if (maxcount)
die("more than one --count?");
Expand Down
34 changes: 34 additions & 0 deletions quote.c
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,37 @@ void python_quote_print(FILE *stream, const char *src)
}
fputc(sq, stream);
}

void tcl_quote_print(FILE *stream, const char *src)
{
char c;

fputc('"', stream);
while ((c = *src++)) {
switch (c) {
case '[': case ']':
case '{': case '}':
case '$': case '\\': case '"':
fputc('\\', stream);
default:
fputc(c, stream);
break;
case '\f':
fputs("\\f", stream);
break;
case '\r':
fputs("\\r", stream);
break;
case '\n':
fputs("\\n", stream);
break;
case '\t':
fputs("\\t", stream);
break;
case '\v':
fputs("\\v", stream);
break;
}
}
fputc('"', stream);
}
1 change: 1 addition & 0 deletions quote.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,6 @@ extern void write_name_quoted(const char *prefix, int prefix_len,
/* quoting as a string literal for other languages */
extern void perl_quote_print(FILE *stream, const char *src);
extern void python_quote_print(FILE *stream, const char *src);
extern void tcl_quote_print(FILE *stream, const char *src);

#endif

0 comments on commit 5558e55

Please sign in to comment.