{"thread":{"id":"12793","subject":"[PATCH v3] pretty.c: add %x00 format specifier.","startedAt":"2008-03-21T15:05:06Z","lastAt":"2008-03-23T01:22:54Z","messageCount":4,"participants":["Govind Salinas","Johannes Schindelin"],"isPatch":true,"patchVersion":3,"patchTotal":null},"messages":[{"id":"72597","messageId":"5d46db230803210805j95c6029m19b284836dcb504a@mail.gmail.com","threadId":"12793","inReplyTo":null,"subject":"[PATCH v3] pretty.c: add %x00 format specifier.","fromName":"Govind Salinas","fromEmail":"govind@sophiasuchtig.com","sentAt":"2008-03-21T15:05:06Z","receivedAt":"2008-03-21T15:05:06Z","isPatch":true,"sender":{"key":"govind@sophiasuchtig.com","avatar":null},"body":"This adds a %x00 format which parses the 00 as hex encoding for a byte and\nprints the resulting byte.  This can be used to add null bytes or other bytes\nthat can make machine parsing easier.  It is also necessary to use fwrite to\nprint out the data since printf will terminate if you feed it a null.\n\nJunio supplied the hex decoding.\n\nSigned-off-by: Govind Salinas <blix@sophiasuchtig.com>\n---\n Documentation/pretty-formats.txt |    1 +\n log-tree.c                       |    6 ++++--\n pretty.c                         |   11 +++++++++++\n 3 files changed, 16 insertions(+), 2 deletions(-)\n\ndiff --git a/Documentation/pretty-formats.txt b/Documentation/pretty-formats.txt\nindex 0193c3c..e8bea3e 100644\n--- a/Documentation/pretty-formats.txt\n+++ b/Documentation/pretty-formats.txt\n@@ -123,3 +123,4 @@ The placeholders are:\n - '%Creset': reset color\n - '%m': left, right or boundary mark\n - '%n': newline\n+- '%x00': print a byte from a hex code\ndiff --git a/log-tree.c b/log-tree.c\nindex 608f697..3bb6e49 100644\n--- a/log-tree.c\n+++ b/log-tree.c\n@@ -308,8 +308,10 @@ void show_log(struct rev_info *opt, const char *sep)\n \tif (opt->show_log_size)\n \t\tprintf(\"log size %i\\n\", (int)msgbuf.len);\n\n-\tif (msgbuf.len)\n-\t\tprintf(\"%s%s%s\", msgbuf.buf, extra, sep);\n+\tif (msgbuf.len) {\n+\t\tfwrite(msgbuf.buf, sizeof(char), msgbuf.len, stdout);\n+\t\tprintf(\"%s%s\", extra, sep);\n+\t}\n \tstrbuf_release(&msgbuf);\n }\n\ndiff --git a/pretty.c b/pretty.c\nindex 703f521..cb4944c 100644\n--- a/pretty.c\n+++ b/pretty.c\n@@ -457,6 +457,7 @@ static size_t format_commit_item(struct strbuf\n*sb, const char *placeholder,\n \tconst struct commit *commit = c->commit;\n \tconst char *msg = commit->buffer;\n \tstruct commit_list *p;\n+\tint h1, h2;\n\n \t/* these are independent of the commit */\n \tswitch (placeholder[0]) {\n@@ -478,6 +479,16 @@ static size_t format_commit_item(struct strbuf\n*sb, const char *placeholder,\n \tcase 'n':\t\t/* newline */\n \t\tstrbuf_addch(sb, '\\n');\n \t\treturn 1;\n+\tcase 'x':\n+\t\t/* %x00 == NUL, %x0a == LF, etc. */\n+\t\tif (0 <= (h1 = hexval_table[0xff & placeholder[1]]) &&\n+\t\t    h1 <= 16 &&\n+\t\t    0 <= (h2 = hexval_table[0xff & placeholder[2]]) &&\n+\t\t    h2 <= 16) {\n+\t\t\tstrbuf_addch(sb, (h1<<4)|h2);\n+\t\t\treturn 3;\n+\t\t} else\n+\t\t\treturn 0;\n \t}\n\n \t/* these depend on the commit */\n-- \n1.5.4.4.552.g7113.dirty\n"},{"id":"72599","messageId":"alpine.LSU.1.00.0803211646010.4124@racer.site","threadId":"12793","inReplyTo":"5d46db230803210805j95c6029m19b284836dcb504a@mail.gmail.com","subject":"Re: [PATCH v3] pretty.c: add %x00 format specifier.","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2008-03-21T15:47:12Z","receivedAt":"2008-03-21T15:47:12Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi,\n\nOn Fri, 21 Mar 2008, Govind Salinas wrote:\n\n> This adds a %x00 format which parses the 00 as hex encoding for a byte \n> and prints the resulting byte.  This can be used to add null bytes or \n> other bytes that can make machine parsing easier.  It is also necessary \n> to use fwrite to print out the data since printf will terminate if you \n> feed it a null.\n> \n> Junio supplied the hex decoding.\n\nThis is all nice and well, but if I understood you correctly, your \noriginal aim was to have a pretty-formatted list of the revisions, but \nNUL-delimited for easy parsing.\n\nWhich you can do with \"git log --pretty=format:%s -z\" right now.\n\nCiao,\nDscho\n"},{"id":"72604","messageId":"5d46db230803211045w3bc2f407n76f188b1d77680d7@mail.gmail.com","threadId":"12793","inReplyTo":"alpine.LSU.1.00.0803211646010.4124@racer.site","subject":"Re: [PATCH v3] pretty.c: add %x00 format specifier.","fromName":"Govind Salinas","fromEmail":"govind@sophiasuchtig.com","sentAt":"2008-03-21T17:45:36Z","receivedAt":"2008-03-21T17:45:36Z","isPatch":true,"sender":{"key":"govind@sophiasuchtig.com","avatar":null},"body":"On Fri, Mar 21, 2008 at 10:47 AM, Johannes Schindelin\n<Johannes.Schindelin@gmx.de> wrote:\n> Hi,\n>\n>\n>  On Fri, 21 Mar 2008, Govind Salinas wrote:\n>\n>  > This adds a %x00 format which parses the 00 as hex encoding for a byte\n>  > and prints the resulting byte.  This can be used to add null bytes or\n>  > other bytes that can make machine parsing easier.  It is also necessary\n>  > to use fwrite to print out the data since printf will terminate if you\n>  > feed it a null.\n>  >\n>  > Junio supplied the hex decoding.\n>\n>  This is all nice and well, but if I understood you correctly, your\n>  original aim was to have a pretty-formatted list of the revisions, but\n>  NUL-delimited for easy parsing.\n>\n>  Which you can do with \"git log --pretty=format:%s -z\" right now.\n>\n>  Ciao,\n>  Dscho\n>\n>\n\nUnfortunately that almost work for me.  I am in the following situation\nIf I stick to just using the --pretty=format options then I would be fine\nbut I am also adding --stat and -p to git-log, so now I have 3 multiline\nhunks of data and I need to tell where each one ends deterministically.\nI *could* just look for \"---\" and \"diff  --git\" at every line but I would prefer\nto use a delimiter.\n\nAlternatively, if -z would add a null before the diffstat was printed, that\nwould probably work for me as well.  Even so, using the same delimiter\nis nicer than parsing part with newlines and part with nulls, which I would\nhave to do if I just used -z.  Think\n\"%H%x00%ae%x00%an%x00%s%x00%b%x00 -z -p --stat\". there\neverything splits nicely with nulls.  The alternative (with -z adding a null\nbefore the diffstat is \"%H%n%ae%n%an%n%s%n%b -z -p --stat\" which\nwould work, but I would have to parse the last 3 fields differently.\n\nPerhaps there is a better way to get all this information.  But I do want\nall the information to come from the same call.\n\nThanks,\nGovind.\n"},{"id":"72714","messageId":"5d46db230803221822x29cdfc4t8a0b7ff0e36ff435@mail.gmail.com","threadId":"12793","inReplyTo":"5d46db230803211045w3bc2f407n76f188b1d77680d7@mail.gmail.com","subject":"Re: [PATCH v3] pretty.c: add %x00 format specifier.","fromName":"Govind Salinas","fromEmail":"govind@sophiasuchtig.com","sentAt":"2008-03-23T01:22:54Z","receivedAt":"2008-03-23T01:22:54Z","isPatch":true,"sender":{"key":"govind@sophiasuchtig.com","avatar":null},"body":"On Fri, Mar 21, 2008 at 12:45 PM, Govind Salinas\n<govind@sophiasuchtig.com> wrote:\n>\n> On Fri, Mar 21, 2008 at 10:47 AM, Johannes Schindelin\n>  <Johannes.Schindelin@gmx.de> wrote:\n>  > Hi,\n>  >\n>  >\n>  >  On Fri, 21 Mar 2008, Govind Salinas wrote:\n>  >\n>  >  > This adds a %x00 format which parses the 00 as hex encoding for a byte\n>  >  > and prints the resulting byte.  This can be used to add null bytes or\n>  >  > other bytes that can make machine parsing easier.  It is also necessary\n>  >  > to use fwrite to print out the data since printf will terminate if you\n>  >  > feed it a null.\n>  >  >\n>  >  > Junio supplied the hex decoding.\n>  >\n>  >  This is all nice and well, but if I understood you correctly, your\n>  >  original aim was to have a pretty-formatted list of the revisions, but\n>  >  NUL-delimited for easy parsing.\n>  >\n>  >  Which you can do with \"git log --pretty=format:%s -z\" right now.\n>  >\n>  >  Ciao,\n>  >  Dscho\n>  >\n>  >\n>\n>  Unfortunately that almost work for me.  I am in the following situation\n>  If I stick to just using the --pretty=format options then I would be fine\n>  but I am also adding --stat and -p to git-log, so now I have 3 multiline\n>  hunks of data and I need to tell where each one ends deterministically.\n>  I *could* just look for \"---\" and \"diff  --git\" at every line but I would prefer\n>  to use a delimiter.\n>\n>  Alternatively, if -z would add a null before the diffstat was printed, that\n>  would probably work for me as well.  Even so, using the same delimiter\n>  is nicer than parsing part with newlines and part with nulls, which I would\n>  have to do if I just used -z.  Think\n>  \"%H%x00%ae%x00%an%x00%s%x00%b%x00 -z -p --stat\". there\n>  everything splits nicely with nulls.  The alternative (with -z adding a null\n>  before the diffstat is \"%H%n%ae%n%an%n%s%n%b -z -p --stat\" which\n>  would work, but I would have to parse the last 3 fields differently.\n>\n>  Perhaps there is a better way to get all this information.  But I do want\n>  all the information to come from the same call.\n>\n>  Thanks,\n>  Govind.\n>\n\nOk, how about this instead?  Perhaps it is more palatable.\n\ndiff --git a/log-tree.c b/log-tree.c\nindex 608f697..5f55683 100644\n--- a/log-tree.c\n+++ b/log-tree.c\n@@ -338,7 +338,7 @@ int log_tree_diff_flush(struct rev_info *opt)\n                        int pch = DIFF_FORMAT_DIFFSTAT | DIFF_FORMAT_PATCH;\n                        if ((pch & opt->diffopt.output_format) == pch)\n                                printf(\"---\");\n-                       putchar('\\n');\n+                       putchar(opt->diffopt.line_termination);\n                }\n        }\n        diff_flush(&opt->diffopt);\n"}]}