@matheusbsbs / matheus-bagliano / commits / 0a0416a34a
Fork 0

strbuf_expand: convert "%%" to "%"

The only way to safely quote arbitrary text in a pretty-print user format is to replace instances of "%" with "%x25". This is slightly unreadable, and many users would expect "%%" to produce a single "%", as that is what printf format specifiers do. This patch converts "%%" to "%" for all users of strbuf_expand(): (1) git-daemon interpolated paths (2) pretty-print user formats (3) merge driver command lines Case (1) was already doing the conversion itself outside of strbuf_expand(). Case (2) is the intended beneficiary of this patch. Case (3) users probably won't notice, but as this is user-facing behavior, consistently providing the quoting mechanism makes sense. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jan 13, 2010 at 12:35 UTC 0a0416a34a7ef5c64f4e0226371e4cab8c1ba982
5 files changed +18 -1
Documentation/pretty-formats.txt
+1
@@ -134,6 +134,7 @@ The placeholders are:
134 - '%C(...)': color specification, as described in color.branch.* config option
135 - '%m': left, right or boundary mark
136 - '%n': newline
137 +- '%%': a raw '%'
138 - '%x00': print a byte from a hex code
139 - '%w([<w>[,<i1>[,<i2>]]])': switch line wrapping, like the -w option of
140 linkgit:git-shortlog[1].
Documentation/technical/api-strbuf.txt
+4
@@ -199,6 +199,10 @@ character if the letter `n` appears after a `%`. The function returns
199 the length of the placeholder recognized and `strbuf_expand()` skips
200 over it.
201 +
202 +The format `%%` is automatically expanded to a single `%` as a quoting
203 +mechanism; callers do not need to handle the `%` placeholder themselves,
204 +and the callback function will not be invoked for this placeholder.
205 ++
206 All other characters (non-percent and not skipped ones) are copied
207 verbatim to the strbuf. If the callback returned zero, meaning that the
208 placeholder is unknown, then the percent sign is copied, too.
daemon.c
-1
@@ -147,7 +147,6 @@ static char *path_ok(char *directory)
147 { "IP", ip_address },
148 { "P", tcp_port },
149 { "D", directory },
150 - { "%", "%" },
150 { NULL }
151 };
152
strbuf.c
+6
@@ -227,6 +227,12 @@ void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn,
227 break;
228 format = percent + 1;
229
230 + if (*format == '%') {
231 + strbuf_addch(sb, '%');
232 + format++;
233 + continue;
234 + }
235 +
236 consumed = fn(sb, format, context);
237 if (consumed)
238 format += consumed;
t/t6006-rev-list-format.sh
+7
@@ -19,6 +19,13 @@ test_cmp expect.$1 output.$1
19 "
20 }
21
22 +test_format percent %%h <<'EOF'
23 +commit 131a310eb913d107dd3c09a65d1651175898735d
24 +%h
25 +commit 86c75cfd708a0e5868dc876ed5b8bb66c80b4873
26 +%h
27 +EOF
28 +
29 test_format hash %H%n%h <<'EOF'
30 commit 131a310eb913d107dd3c09a65d1651175898735d
31 131a310eb913d107dd3c09a65d1651175898735d