@matheusbsbs / matheus-bagliano / commits / 42c8c74c14
Fork 0

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 <blix@sophiasuchtig.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Govind Salinas committed Mar 21, 2008 at 10:05 UTC 42c8c74c1401352b0f388e47a2c9fcd52171c9d3
3 files changed +16 -2
Documentation/pretty-formats.txt
+1
@@ -123,3 +123,4 @@ The placeholders are:
123 - '%Creset': reset color
124 - '%m': left, right or boundary mark
125 - '%n': newline
126 +- '%x00': print a byte from a hex code
log-tree.c
+4 -2
@@ -317,8 +317,10 @@ void show_log(struct rev_info *opt, const char *sep)
317 if (opt->show_log_size)
318 printf("log size %i\n", (int)msgbuf.len);
319
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 + }
324 strbuf_release(&msgbuf);
325 }
326
pretty.c
+11
@@ -457,6 +457,7 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
457 const struct commit *commit = c->commit;
458 const char *msg = commit->buffer;
459 struct commit_list *p;
460 + int h1, h2;
461
462 /* these are independent of the commit */
463 switch (placeholder[0]) {
@@ -478,6 +479,16 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
479 case 'n': /* newline */
480 strbuf_addch(sb, '\n');
481 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;
492 }
493
494 /* these depend on the commit */