Skip to content

Commit 42c8c74

Browse files
Govind Salinasgitster
authored andcommitted
pretty.c: add %x00 format specifier.
This adds a %xXX format which inserts two hexdigits after %x as a byte value in the resulting string. This can be used to add a NUL byte or any other byte that can make machine parsing easier. It is also necessary to use fwrite to print out the data since printf will terminate if you feed it a NUL. Signed-off-by: Govind Salinas <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bc61000 commit 42c8c74

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

Documentation/pretty-formats.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,4 @@ The placeholders are:
123123
- '%Creset': reset color
124124
- '%m': left, right or boundary mark
125125
- '%n': newline
126+
- '%x00': print a byte from a hex code

log-tree.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,10 @@ void show_log(struct rev_info *opt, const char *sep)
317317
if (opt->show_log_size)
318318
printf("log size %i\n", (int)msgbuf.len);
319319

320-
if (msgbuf.len)
321-
printf("%s%s%s", msgbuf.buf, extra, sep);
320+
if (msgbuf.len) {
321+
fwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);
322+
printf("%s%s", extra, sep);
323+
}
322324
strbuf_release(&msgbuf);
323325
}
324326

pretty.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,7 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
457457
const struct commit *commit = c->commit;
458458
const char *msg = commit->buffer;
459459
struct commit_list *p;
460+
int h1, h2;
460461

461462
/* these are independent of the commit */
462463
switch (placeholder[0]) {
@@ -478,6 +479,16 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
478479
case 'n': /* newline */
479480
strbuf_addch(sb, '\n');
480481
return 1;
482+
case 'x':
483+
/* %x00 == NUL, %x0a == LF, etc. */
484+
if (0 <= (h1 = hexval_table[0xff & placeholder[1]]) &&
485+
h1 <= 16 &&
486+
0 <= (h2 = hexval_table[0xff & placeholder[2]]) &&
487+
h2 <= 16) {
488+
strbuf_addch(sb, (h1<<4)|h2);
489+
return 3;
490+
} else
491+
return 0;
481492
}
482493

483494
/* these depend on the commit */

0 commit comments

Comments
 (0)