{"thread":{"id":"34024","subject":"[PATCH 00/15] Towards a more awesome git-branch","startedAt":"2013-06-04T12:35:22Z","lastAt":"2013-06-06T00:07:44Z","messageCount":30,"participants":["Ramkumar Ramachandra","Duy Nguyen","Eric Sunshine","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":15},"messages":[{"id":"219348","messageId":"1370349337-20938-1-git-send-email-artagnon@gmail.com","threadId":"34024","inReplyTo":null,"subject":"[PATCH 00/15] Towards a more awesome git-branch","fromName":"Ramkumar Ramachandra","fromEmail":"artagnon@gmail.com","sentAt":"2013-06-04T12:35:22Z","receivedAt":"2013-06-04T12:35:22Z","isPatch":true,"sender":{"key":"r@artagnon.com","avatar":"https://avatars.githubusercontent.com/u/37226?v=4"},"body":"Hi,\n\nDuy and I have been working on this topic for some time now.  Here's a\nreview candidate.  Duy did most of the chunky work, and I mostly\ndid review/documentation.  The key patches are:\n\n[5/15]  is a brilliant patch that made this entire thing possible.\n[10/15] is another brilliant patch to auto-calculate widths.\n\nDuy is currently writing code to factor out -v[v] and adding --sort,\n--count to git-branch, but I'm having a lot of difficulty working with\na series of this size.  I'm leaning towards getting this merged before\ntackling branch (who wants to review a 40-part series?).  Especially\nafter [14/15] and [15/15], git for-each-ref should be usable in its\nown right.  I currently have:\n\n[pretty]\nhot = %C(red)%(HEAD)%C(reset) %C(green)%(refname:short)%C(reset)%(upstream:trackshort)\n[alias]\nhot = for-each-ref --pretty=hot --count 10 --sort='-committerdate' refs/heads\n\nWhich is really useful and manageable until we port these options to\nbranch and get nice configuration variables.\n\nNguyễn Thái Ngọc Duy (8):\n  for-each-ref, quote: convert *_quote_print -> *_quote_buf\n  for-each-ref: don't print out elements directly\n  pretty: extend pretty_print_context with callback\n  pretty: allow passing NULL commit to format_commit_message()\n  for-each-ref: get --pretty using format_commit_message\n  for-each-ref: teach verify_format() about pretty's syntax\n  for-each-ref: introduce format specifier %>(*) and %<(*)\n  for-each-ref: improve responsiveness of %(upstream:track)\n\nRamkumar Ramachandra (7):\n  tar-tree: remove dependency on sq_quote_print()\n  quote: remove sq_quote_print()\n  pretty: limit recursion in format_commit_one()\n  for-each-ref: introduce %(HEAD) marker\n  for-each-ref: introduce %(upstream:track[short])\n  pretty: introduce get_pretty_userformat\n  for-each-ref: use get_pretty_userformat in --pretty\n\n Documentation/git-for-each-ref.txt |  42 +++++-\n builtin/for-each-ref.c             | 274 ++++++++++++++++++++++++++++++-------\n builtin/tar-tree.c                 |  11 +-\n commit.h                           |   9 ++\n pretty.c                           |  77 ++++++++++-\n quote.c                            |  61 +++------\n quote.h                            |   8 +-\n 7 files changed, 372 insertions(+), 110 deletions(-)\n\n-- \n1.8.3.GIT\n"},{"id":"219350","messageId":"1370349337-20938-2-git-send-email-artagnon@gmail.com","threadId":"34024","inReplyTo":"1370349337-20938-1-git-send-email-artagnon@gmail.com","subject":"[PATCH 01/15] for-each-ref, quote: convert *_quote_print -> *_quote_buf","fromName":"Ramkumar Ramachandra","fromEmail":"artagnon@gmail.com","sentAt":"2013-06-04T12:35:23Z","receivedAt":"2013-06-04T12:35:23Z","isPatch":true,"sender":{"key":"r@artagnon.com","avatar":"https://avatars.githubusercontent.com/u/37226?v=4"},"body":"From: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n\nfor-each-ref.c:print_value() currently prints values to stdout\nimmediately using {sq|perl|python|tcl}_quote_print, giving us no\nopportunity to do any further processing.  In preparation for getting\nprint_value() to accept an additional strbuf argument to write to,\nconvert the *_quote_print functions and callers to *_quote_buf.\n\n[rr: commit message, minor modifications]\n\nSigned-off-by: Ramkumar Ramachandra <artagnon@gmail.com>\n---\n builtin/for-each-ref.c | 13 +++++++++----\n quote.c                | 44 ++++++++++++++++++++++----------------------\n quote.h                |  6 +++---\n 3 files changed, 34 insertions(+), 29 deletions(-)\n\ndiff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c\nindex 7f059c3..1d4083c 100644\n--- a/builtin/for-each-ref.c\n+++ b/builtin/for-each-ref.c\n@@ -867,24 +867,29 @@ static void sort_refs(struct ref_sort *sort, struct refinfo **refs, int num_refs\n static void print_value(struct refinfo *ref, int atom, int quote_style)\n {\n \tstruct atom_value *v;\n+\tstruct strbuf sb = STRBUF_INIT;\n \tget_value(ref, atom, &v);\n \tswitch (quote_style) {\n \tcase QUOTE_NONE:\n \t\tfputs(v->s, stdout);\n \t\tbreak;\n \tcase QUOTE_SHELL:\n-\t\tsq_quote_print(stdout, v->s);\n+\t\tsq_quote_buf(&sb, v->s);\n \t\tbreak;\n \tcase QUOTE_PERL:\n-\t\tperl_quote_print(stdout, v->s);\n+\t\tperl_quote_buf(&sb, v->s);\n \t\tbreak;\n \tcase QUOTE_PYTHON:\n-\t\tpython_quote_print(stdout, v->s);\n+\t\tpython_quote_buf(&sb, v->s);\n \t\tbreak;\n \tcase QUOTE_TCL:\n-\t\ttcl_quote_print(stdout, v->s);\n+\t\ttcl_quote_buf(&sb, v->s);\n \t\tbreak;\n \t}\n+\tif (quote_style != QUOTE_NONE) {\n+\t\tfputs(sb.buf, stdout);\n+\t\tstrbuf_release(&sb);\n+\t}\n }\n \n static int hex1(char ch)\ndiff --git a/quote.c b/quote.c\nindex 911229f..8c294df 100644\n--- a/quote.c\n+++ b/quote.c\n@@ -463,72 +463,72 @@ int unquote_c_style(struct strbuf *sb, const char *quoted, const char **endp)\n \n /* quoting as a string literal for other languages */\n \n-void perl_quote_print(FILE *stream, const char *src)\n+void perl_quote_buf(struct strbuf *sb, const char *src)\n {\n \tconst char sq = '\\'';\n \tconst char bq = '\\\\';\n \tchar c;\n \n-\tfputc(sq, stream);\n+\tstrbuf_addch(sb, sq);\n \twhile ((c = *src++)) {\n \t\tif (c == sq || c == bq)\n-\t\t\tfputc(bq, stream);\n-\t\tfputc(c, stream);\n+\t\t\tstrbuf_addch(sb, bq);\n+\t\tstrbuf_addch(sb, c);\n \t}\n-\tfputc(sq, stream);\n+\tstrbuf_addch(sb, sq);\n }\n \n-void python_quote_print(FILE *stream, const char *src)\n+void python_quote_buf(struct strbuf *sb, const char *src)\n {\n \tconst char sq = '\\'';\n \tconst char bq = '\\\\';\n \tconst char nl = '\\n';\n \tchar c;\n \n-\tfputc(sq, stream);\n+\tstrbuf_addch(sb, sq);\n \twhile ((c = *src++)) {\n \t\tif (c == nl) {\n-\t\t\tfputc(bq, stream);\n-\t\t\tfputc('n', stream);\n+\t\t\tstrbuf_addch(sb, bq);\n+\t\t\tstrbuf_addch(sb, 'n');\n \t\t\tcontinue;\n \t\t}\n \t\tif (c == sq || c == bq)\n-\t\t\tfputc(bq, stream);\n-\t\tfputc(c, stream);\n+\t\t\tstrbuf_addch(sb, bq);\n+\t\tstrbuf_addch(sb, c);\n \t}\n-\tfputc(sq, stream);\n+\tstrbuf_addch(sb, sq);\n }\n \n-void tcl_quote_print(FILE *stream, const char *src)\n+void tcl_quote_buf(struct strbuf *sb, const char *src)\n {\n \tchar c;\n \n-\tfputc('\"', stream);\n+\tstrbuf_addch(sb, '\"');\n \twhile ((c = *src++)) {\n \t\tswitch (c) {\n \t\tcase '[': case ']':\n \t\tcase '{': case '}':\n \t\tcase '$': case '\\\\': case '\"':\n-\t\t\tfputc('\\\\', stream);\n+\t\t\tstrbuf_addch(sb, '\\\\');\n \t\tdefault:\n-\t\t\tfputc(c, stream);\n+\t\t\tstrbuf_addch(sb, c);\n \t\t\tbreak;\n \t\tcase '\\f':\n-\t\t\tfputs(\"\\\\f\", stream);\n+\t\t\tstrbuf_addstr(sb, \"\\\\f\");\n \t\t\tbreak;\n \t\tcase '\\r':\n-\t\t\tfputs(\"\\\\r\", stream);\n+\t\t\tstrbuf_addstr(sb, \"\\\\r\");\n \t\t\tbreak;\n \t\tcase '\\n':\n-\t\t\tfputs(\"\\\\n\", stream);\n+\t\t\tstrbuf_addstr(sb, \"\\\\n\");\n \t\t\tbreak;\n \t\tcase '\\t':\n-\t\t\tfputs(\"\\\\t\", stream);\n+\t\t\tstrbuf_addstr(sb, \"\\\\t\");\n \t\t\tbreak;\n \t\tcase '\\v':\n-\t\t\tfputs(\"\\\\v\", stream);\n+\t\t\tstrbuf_addstr(sb, \"\\\\v\");\n \t\t\tbreak;\n \t\t}\n \t}\n-\tfputc('\"', stream);\n+\tstrbuf_addch(sb, '\"');\n }\ndiff --git a/quote.h b/quote.h\nindex 133155a..ed06df5 100644\n--- a/quote.h\n+++ b/quote.h\n@@ -69,8 +69,8 @@ extern char *quote_path_relative(const char *in, int len,\n \t\t\t  struct strbuf *out, const char *prefix);\n \n /* quoting as a string literal for other languages */\n-extern void perl_quote_print(FILE *stream, const char *src);\n-extern void python_quote_print(FILE *stream, const char *src);\n-extern void tcl_quote_print(FILE *stream, const char *src);\n+extern void perl_quote_buf(struct strbuf *sb, const char *src);\n+extern void python_quote_buf(struct strbuf *sb, const char *src);\n+extern void tcl_quote_buf(struct strbuf *sb, const char *src);\n \n #endif\n-- \n1.8.3.GIT\n"},{"id":"219349","messageId":"1370349337-20938-3-git-send-email-artagnon@gmail.com","threadId":"34024","inReplyTo":"1370349337-20938-1-git-send-email-artagnon@gmail.com","subject":"[PATCH 02/15] for-each-ref: don't print out elements directly","fromName":"Ramkumar Ramachandra","fromEmail":"artagnon@gmail.com","sentAt":"2013-06-04T12:35:24Z","receivedAt":"2013-06-04T12:35:24Z","isPatch":true,"sender":{"key":"r@artagnon.com","avatar":"https://avatars.githubusercontent.com/u/37226?v=4"},"body":"From: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n\nCurrently, the entire callchain starting from show_ref() parses and\nprints immediately.  This inflexibility limits our ability to extend the\nparser.  So, convert the entire callchain to accept a strbuf argument to\nwrite to.  Also introduce a show_refs() helper that calls show_ref() in\na loop to avoid cluttering up cmd_for_each_ref() with the task of\ninitializing/freeing the strbuf.\n\n[rr: commit message]\n\nSigned-off-by: Ramkumar Ramachandra <artagnon@gmail.com>\n---\n builtin/for-each-ref.c | 55 ++++++++++++++++++++++++++++++++------------------\n 1 file changed, 35 insertions(+), 20 deletions(-)\n\ndiff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c\nindex 1d4083c..e2d6c5a 100644\n--- a/builtin/for-each-ref.c\n+++ b/builtin/for-each-ref.c\n@@ -864,31 +864,31 @@ static void sort_refs(struct ref_sort *sort, struct refinfo **refs, int num_refs\n \tqsort(refs, num_refs, sizeof(struct refinfo *), compare_refs);\n }\n \n-static void print_value(struct refinfo *ref, int atom, int quote_style)\n+static void print_value(struct strbuf *sb, struct refinfo *ref,\n+\t\t\tint atom, int quote_style)\n {\n \tstruct atom_value *v;\n-\tstruct strbuf sb = STRBUF_INIT;\n \tget_value(ref, atom, &v);\n \tswitch (quote_style) {\n \tcase QUOTE_NONE:\n-\t\tfputs(v->s, stdout);\n+\t\tstrbuf_addstr(sb, v->s);\n \t\tbreak;\n \tcase QUOTE_SHELL:\n-\t\tsq_quote_buf(&sb, v->s);\n+\t\tsq_quote_buf(sb, v->s);\n \t\tbreak;\n \tcase QUOTE_PERL:\n-\t\tperl_quote_buf(&sb, v->s);\n+\t\tperl_quote_buf(sb, v->s);\n \t\tbreak;\n \tcase QUOTE_PYTHON:\n-\t\tpython_quote_buf(&sb, v->s);\n+\t\tpython_quote_buf(sb, v->s);\n \t\tbreak;\n \tcase QUOTE_TCL:\n-\t\ttcl_quote_buf(&sb, v->s);\n+\t\ttcl_quote_buf(sb, v->s);\n \t\tbreak;\n \t}\n \tif (quote_style != QUOTE_NONE) {\n-\t\tfputs(sb.buf, stdout);\n-\t\tstrbuf_release(&sb);\n+\t\tfputs(sb->buf, stdout);\n+\t\tstrbuf_release(sb);\n \t}\n }\n \n@@ -910,7 +910,7 @@ static int hex2(const char *cp)\n \t\treturn -1;\n }\n \n-static void emit(const char *cp, const char *ep)\n+static void emit(struct strbuf *sb, const char *cp, const char *ep)\n {\n \twhile (*cp && (!ep || cp < ep)) {\n \t\tif (*cp == '%') {\n@@ -919,32 +919,47 @@ static void emit(const char *cp, const char *ep)\n \t\t\telse {\n \t\t\t\tint ch = hex2(cp + 1);\n \t\t\t\tif (0 <= ch) {\n-\t\t\t\t\tputchar(ch);\n+\t\t\t\t\tstrbuf_addch(sb, ch);\n \t\t\t\t\tcp += 3;\n \t\t\t\t\tcontinue;\n \t\t\t\t}\n \t\t\t}\n \t\t}\n-\t\tputchar(*cp);\n+\t\tstrbuf_addch(sb, *cp);\n \t\tcp++;\n \t}\n }\n \n-static void show_ref(struct refinfo *info, const char *format, int quote_style)\n+static void show_ref(struct strbuf *sb, struct refinfo *info,\n+\t\t     const char *format, int quote_style)\n {\n \tconst char *cp, *sp, *ep;\n \n \tfor (cp = format; *cp && (sp = find_next(cp)); cp = ep + 1) {\n \t\tep = strchr(sp, ')');\n \t\tif (cp < sp)\n-\t\t\temit(cp, sp);\n-\t\tprint_value(info, parse_atom(sp + 2, ep), quote_style);\n+\t\t\temit(sb, cp, sp);\n+\t\tprint_value(sb, info, parse_atom(sp + 2, ep), quote_style);\n \t}\n \tif (*cp) {\n \t\tsp = cp + strlen(cp);\n-\t\temit(cp, sp);\n+\t\temit(sb, cp, sp);\n \t}\n-\tputchar('\\n');\n+\tstrbuf_addch(sb, '\\n');\n+}\n+\n+static void show_refs(struct refinfo **refs, int maxcount,\n+\t\t      const char *format, int quote_style)\n+{\n+\tstruct strbuf sb = STRBUF_INIT;\n+\tint i;\n+\n+\tfor (i = 0; i < maxcount; i++) {\n+\t\tstrbuf_reset(&sb);\n+\t\tshow_ref(&sb, refs[i], format, quote_style);\n+\t\tfputs(sb.buf, stdout);\n+\t}\n+\tstrbuf_release(&sb);\n }\n \n static struct ref_sort *default_sort(void)\n@@ -987,7 +1002,7 @@ static char const * const for_each_ref_usage[] = {\n \n int cmd_for_each_ref(int argc, const char **argv, const char *prefix)\n {\n-\tint i, num_refs;\n+\tint num_refs;\n \tconst char *format = \"%(objectname) %(objecttype)\\t%(refname)\";\n \tstruct ref_sort *sort = NULL, **sort_tail = &sort;\n \tint maxcount = 0, quote_style = 0;\n@@ -1041,7 +1056,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)\n \n \tif (!maxcount || num_refs < maxcount)\n \t\tmaxcount = num_refs;\n-\tfor (i = 0; i < maxcount; i++)\n-\t\tshow_ref(refs[i], format, quote_style);\n+\n+\tshow_refs(refs, maxcount, format, quote_style);\n \treturn 0;\n }\n-- \n1.8.3.GIT\n"},{"id":"219351","messageId":"1370349337-20938-4-git-send-email-artagnon@gmail.com","threadId":"34024","inReplyTo":"1370349337-20938-1-git-send-email-artagnon@gmail.com","subject":"[PATCH 03/15] tar-tree: remove dependency on sq_quote_print()","fromName":"Ramkumar Ramachandra","fromEmail":"artagnon@gmail.com","sentAt":"2013-06-04T12:35:25Z","receivedAt":"2013-06-04T12:35:25Z","isPatch":true,"sender":{"key":"r@artagnon.com","avatar":"https://avatars.githubusercontent.com/u/37226?v=4"},"body":"Currently, there is exactly one caller of sq_quote_print(), namely\ncmd_tar_tree().  In the interest of removing sq_quote_print() and\nsimplification, replace it with an equivalent call to sq_quote_argv().\nNo functional changes intended.\n\nSigned-off-by: Ramkumar Ramachandra <artagnon@gmail.com>\n---\n builtin/tar-tree.c | 11 +++++------\n 1 file changed, 5 insertions(+), 6 deletions(-)\n\ndiff --git a/builtin/tar-tree.c b/builtin/tar-tree.c\nindex 3f1e701..ba3ffe6 100644\n--- a/builtin/tar-tree.c\n+++ b/builtin/tar-tree.c\n@@ -26,8 +26,8 @@ int cmd_tar_tree(int argc, const char **argv, const char *prefix)\n \t * $0 tree-ish basedir ==>\n \t * \tgit archive --format-tar --prefix=basedir tree-ish\n \t */\n-\tint i;\n \tconst char **nargv = xcalloc(sizeof(*nargv), argc + 3);\n+\tstruct strbuf sb = STRBUF_INIT;\n \tchar *basedir_arg;\n \tint nargc = 0;\n \n@@ -65,11 +65,10 @@ int cmd_tar_tree(int argc, const char **argv, const char *prefix)\n \tfprintf(stderr,\n \t\t\"*** \\\"git tar-tree\\\" is now deprecated.\\n\"\n \t\t\"*** Running \\\"git archive\\\" instead.\\n***\");\n-\tfor (i = 0; i < nargc; i++) {\n-\t\tfputc(' ', stderr);\n-\t\tsq_quote_print(stderr, nargv[i]);\n-\t}\n-\tfputc('\\n', stderr);\n+\tsq_quote_argv(&sb, nargv, 0);\n+\tstrbuf_addch(&sb, '\\n');\n+\tfputs(sb.buf, stderr);\n+\tstrbuf_release(&sb);\n \treturn cmd_archive(nargc, nargv, prefix);\n }\n \n-- \n1.8.3.GIT\n"},{"id":"219352","messageId":"1370349337-20938-5-git-send-email-artagnon@gmail.com","threadId":"34024","inReplyTo":"1370349337-20938-1-git-send-email-artagnon@gmail.com","subject":"[PATCH 04/15] quote: remove sq_quote_print()","fromName":"Ramkumar Ramachandra","fromEmail":"artagnon@gmail.com","sentAt":"2013-06-04T12:35:26Z","receivedAt":"2013-06-04T12:35:26Z","isPatch":true,"sender":{"key":"r@artagnon.com","avatar":"https://avatars.githubusercontent.com/u/37226?v=4"},"body":"Remove sq_quote_print() since it has no callers.  A nicer alternative\nsq_quote_buf() exists: its callers aren't forced to print immediately.\n\nFor historical context, sq_quote_print() was first introduced in\n575ba9d6 (GIT_TRACE: show which built-in/external commands are executed,\n2006-06-25) for the purpose of printing argv for $GIT_TRACE.  Today, we\nachieve this using trace_argv_printf() -> sq_quote_argv() ->\nsq_quote_buf(), which ultimately fills in a strbuf.\n\nSigned-off-by: Ramkumar Ramachandra <artagnon@gmail.com>\n---\n quote.c | 17 -----------------\n quote.h |  2 --\n 2 files changed, 19 deletions(-)\n\ndiff --git a/quote.c b/quote.c\nindex 8c294df..778b39a 100644\n--- a/quote.c\n+++ b/quote.c\n@@ -42,23 +42,6 @@ void sq_quote_buf(struct strbuf *dst, const char *src)\n \tfree(to_free);\n }\n \n-void sq_quote_print(FILE *stream, const char *src)\n-{\n-\tchar c;\n-\n-\tfputc('\\'', stream);\n-\twhile ((c = *src++)) {\n-\t\tif (need_bs_quote(c)) {\n-\t\t\tfputs(\"'\\\\\", stream);\n-\t\t\tfputc(c, stream);\n-\t\t\tfputc('\\'', stream);\n-\t\t} else {\n-\t\t\tfputc(c, stream);\n-\t\t}\n-\t}\n-\tfputc('\\'', stream);\n-}\n-\n void sq_quote_argv(struct strbuf *dst, const char** argv, size_t maxlen)\n {\n \tint i;\ndiff --git a/quote.h b/quote.h\nindex ed06df5..251f3cc 100644\n--- a/quote.h\n+++ b/quote.h\n@@ -27,8 +27,6 @@ struct strbuf;\n  * excluding the final null regardless of the buffer size.\n  */\n \n-extern void sq_quote_print(FILE *stream, const char *src);\n-\n extern void sq_quote_buf(struct strbuf *, const char *src);\n extern void sq_quote_argv(struct strbuf *, const char **argv, size_t maxlen);\n \n-- \n1.8.3.GIT\n"},{"id":"219353","messageId":"1370349337-20938-6-git-send-email-artagnon@gmail.com","threadId":"34024","inReplyTo":"1370349337-20938-1-git-send-email-artagnon@gmail.com","subject":"[PATCH 05/15] pretty: extend pretty_print_context with callback","fromName":"Ramkumar Ramachandra","fromEmail":"artagnon@gmail.com","sentAt":"2013-06-04T12:35:27Z","receivedAt":"2013-06-04T12:35:27Z","isPatch":true,"sender":{"key":"r@artagnon.com","avatar":"https://avatars.githubusercontent.com/u/37226?v=4"},"body":"From: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n\nThe struct pretty_print_context contains the context in which the\nplaceholders in format_commit_one() should be parsed.  Although\nformat_commit_one() primarily acts as a parser, there is no way for a\ncaller to plug in custom callbacks.  Now, callers can:\n\n1. Parse a custom placeholder that is not supported by\n   format_commit_one(), and act on it independently of the pretty\n   machinery.\n\n2. Parse a custom placeholder to substitute the custom placeholder with\n   a placeholder that format_commit_one() understands.  This is\n   especially useful for supporting %>(*), where * is substituted with a\n   length computed by the caller.\n\nTo support these two usecases, the interface for the function looks\nlike:\n\ntypedef size_t (*format_message_fn)(struct strbuf *sb,\n\t\t\t\tconst char *placeholder,\n\t\t\t\tvoid *format_context,\n\t\t\t\tvoid *user_data,\n\t\t\t\tstruct strbuf *placeholder_subst)\n\nIt is exactly like format_commit_one(), except that there are two\nadditional fields: user_data (to pass custom data to the callback), and\nplaceholder_subst (to set the substitution).  The callback should return\nthe length of the original string parsed, and optionally set\nplaceholder_subst.\n\n[rr: commit message, minor modifications]\n\nSigned-off-by: Ramkumar Ramachandra <artagnon@gmail.com>\n---\n commit.h |  8 ++++++++\n pretty.c | 25 +++++++++++++++++++++++++\n 2 files changed, 33 insertions(+)\n\ndiff --git a/commit.h b/commit.h\nindex 67bd509..04bd935 100644\n--- a/commit.h\n+++ b/commit.h\n@@ -78,6 +78,12 @@ enum cmit_fmt {\n \tCMIT_FMT_UNSPECIFIED\n };\n \n+typedef size_t (*format_message_fn)(struct strbuf *sb,\n+\t\t\t\tconst char *placeholder,\n+\t\t\t\tvoid *format_context,\n+\t\t\t\tvoid *user_data,\n+\t\t\t\tstruct strbuf *placeholder_subst);\n+\n struct pretty_print_context {\n \tenum cmit_fmt fmt;\n \tint abbrev;\n@@ -92,6 +98,8 @@ struct pretty_print_context {\n \tconst char *output_encoding;\n \tstruct string_list *mailmap;\n \tint color;\n+\tformat_message_fn format;\n+\tvoid *user_data;\n };\n \n struct userformat_want {\ndiff --git a/pretty.c b/pretty.c\nindex 9e43154..095e5ba 100644\n--- a/pretty.c\n+++ b/pretty.c\n@@ -1069,6 +1069,31 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */\n \tstruct commit_list *p;\n \tint h1, h2;\n \n+\tif (c->pretty_ctx->format) {\n+\t\tstruct strbuf subst = STRBUF_INIT;\n+\t\tint ret = c->pretty_ctx->format(sb, placeholder, context,\n+\t\t\t\t\t\tc->pretty_ctx->user_data,\n+\t\t\t\t\t\t&subst);\n+\t\tif (ret && subst.len) {\n+\t\t\t/*\n+\t\t\t * Something was parsed by format(), and a\n+\t\t\t * placeholder-substitution was set.\n+\t\t\t * Recursion is required to override the\n+\t\t\t * return value of format_commit_one() with\n+\t\t\t * ret: the length of the original string\n+\t\t\t * before substitution.\n+\t\t\t */\n+\t\t\tret = format_commit_one(sb, subst.buf, context) ? ret : 0;\n+\t\t\tstrbuf_release(&subst);\n+\t\t\treturn ret;\n+\t\t} else if (ret)\n+\t\t\t/*\n+\t\t\t * Something was parsed by format(), but\n+\t\t\t * no placeholder-substitution was set.\n+\t\t\t */\n+\t\t\treturn ret;\n+\t}\n+\n \t/* these are independent of the commit */\n \tswitch (placeholder[0]) {\n \tcase 'C':\n-- \n1.8.3.GIT\n"},{"id":"219354","messageId":"1370349337-20938-7-git-send-email-artagnon@gmail.com","threadId":"34024","inReplyTo":"1370349337-20938-1-git-send-email-artagnon@gmail.com","subject":"[PATCH 06/15] pretty: limit recursion in format_commit_one()","fromName":"Ramkumar Ramachandra","fromEmail":"artagnon@gmail.com","sentAt":"2013-06-04T12:35:28Z","receivedAt":"2013-06-04T12:35:28Z","isPatch":true,"sender":{"key":"r@artagnon.com","avatar":"https://avatars.githubusercontent.com/u/37226?v=4"},"body":"To make sure that a pretty_ctx->format substitution doesn't result in an\ninfinite recursion, change the prototype of format_commit_one() to\naccept one last argument: no_recurse.  So, a single substitution by\nformat() must yield a result that can be parsed by format_commit_one()\nwithout the help of format().\n\nSigned-off-by: Ramkumar Ramachandra <artagnon@gmail.com>\n---\n pretty.c | 11 ++++++-----\n 1 file changed, 6 insertions(+), 5 deletions(-)\n\ndiff --git a/pretty.c b/pretty.c\nindex 095e5ba..0063f2d 100644\n--- a/pretty.c\n+++ b/pretty.c\n@@ -1061,7 +1061,8 @@ static size_t parse_padding_placeholder(struct strbuf *sb,\n \n static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */\n \t\t\t\tconst char *placeholder,\n-\t\t\t\tvoid *context)\n+\t\t\t\tvoid *context,\n+\t\t\t\tint no_recurse)\n {\n \tstruct format_commit_context *c = context;\n \tconst struct commit *commit = c->commit;\n@@ -1069,7 +1070,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */\n \tstruct commit_list *p;\n \tint h1, h2;\n \n-\tif (c->pretty_ctx->format) {\n+\tif (!no_recurse && c->pretty_ctx->format) {\n \t\tstruct strbuf subst = STRBUF_INIT;\n \t\tint ret = c->pretty_ctx->format(sb, placeholder, context,\n \t\t\t\t\t\tc->pretty_ctx->user_data,\n@@ -1083,7 +1084,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */\n \t\t\t * ret: the length of the original string\n \t\t\t * before substitution.\n \t\t\t */\n-\t\t\tret = format_commit_one(sb, subst.buf, context) ? ret : 0;\n+\t\t\tret = format_commit_one(sb, subst.buf, context, 1) ? ret : 0;\n \t\t\tstrbuf_release(&subst);\n \t\t\treturn ret;\n \t\t} else if (ret)\n@@ -1332,7 +1333,7 @@ static size_t format_and_pad_commit(struct strbuf *sb, /* in UTF-8 */\n \t}\n \twhile (1) {\n \t\tint modifier = *placeholder == 'C';\n-\t\tint consumed = format_commit_one(&local_sb, placeholder, c);\n+\t\tint consumed = format_commit_one(&local_sb, placeholder, c, 0);\n \t\ttotal_consumed += consumed;\n \n \t\tif (!modifier)\n@@ -1452,7 +1453,7 @@ static size_t format_commit_item(struct strbuf *sb, /* in UTF-8 */\n \tif (((struct format_commit_context *)context)->flush_type != no_flush)\n \t\tconsumed = format_and_pad_commit(sb, placeholder, context);\n \telse\n-\t\tconsumed = format_commit_one(sb, placeholder, context);\n+\t\tconsumed = format_commit_one(sb, placeholder, context, 0);\n \tif (magic == NO_MAGIC)\n \t\treturn consumed;\n \n-- \n1.8.3.GIT\n"},{"id":"219355","messageId":"1370349337-20938-8-git-send-email-artagnon@gmail.com","threadId":"34024","inReplyTo":"1370349337-20938-1-git-send-email-artagnon@gmail.com","subject":"[PATCH 07/15] pretty: allow passing NULL commit to format_commit_message()","fromName":"Ramkumar Ramachandra","fromEmail":"artagnon@gmail.com","sentAt":"2013-06-04T12:35:29Z","receivedAt":"2013-06-04T12:35:29Z","isPatch":true,"sender":{"key":"r@artagnon.com","avatar":"https://avatars.githubusercontent.com/u/37226?v=4"},"body":"From: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n\nThe new formatter, for-each-ref, may use non-commit placeholders only.\nWhile it could audit the format line and warn/exclude commit\nplaceholders, that's a lot more work than simply ignore them.\nUnrecognized placeholders are displayed as-is, pretty obvious that they\nare not handled.\n\nSigned-off-by: Ramkumar Ramachandra <artagnon@gmail.com>\n---\n pretty.c | 16 ++++++++++++----\n 1 file changed, 12 insertions(+), 4 deletions(-)\n\ndiff --git a/pretty.c b/pretty.c\nindex 0063f2d..816aa32 100644\n--- a/pretty.c\n+++ b/pretty.c\n@@ -1156,6 +1156,9 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */\n \t}\n \n \t/* these depend on the commit */\n+\tif (!commit)\n+\t\treturn 0;\n+\n \tif (!commit->object.parsed)\n \t\tparse_object(commit->object.sha1);\n \n@@ -1276,6 +1279,9 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */\n \t}\n \n \n+\tif (!c->message)\n+\t\treturn 0;\n+\n \t/* For the rest we have to parse the commit header. */\n \tif (!c->commit_header_parsed)\n \t\tparse_commit_header(c);\n@@ -1510,9 +1516,10 @@ void format_commit_message(const struct commit *commit,\n \tcontext.commit = commit;\n \tcontext.pretty_ctx = pretty_ctx;\n \tcontext.wrap_start = sb->len;\n-\tcontext.message = logmsg_reencode(commit,\n-\t\t\t\t\t  &context.commit_encoding,\n-\t\t\t\t\t  output_enc);\n+\tif (commit)\n+\t\tcontext.message = logmsg_reencode(commit,\n+\t\t\t\t\t\t  &context.commit_encoding,\n+\t\t\t\t\t\t  output_enc);\n \n \tstrbuf_expand(sb, format, format_commit_item, &context);\n \trewrap_message_tail(sb, &context, 0, 0, 0);\n@@ -1535,7 +1542,8 @@ void format_commit_message(const struct commit *commit,\n \t}\n \n \tfree(context.commit_encoding);\n-\tlogmsg_free(context.message, commit);\n+\tif (commit)\n+\t\tlogmsg_free(context.message, commit);\n \tfree(context.signature_check.gpg_output);\n \tfree(context.signature_check.signer);\n }\n-- \n1.8.3.GIT\n"},{"id":"219356","messageId":"1370349337-20938-9-git-send-email-artagnon@gmail.com","threadId":"34024","inReplyTo":"1370349337-20938-1-git-send-email-artagnon@gmail.com","subject":"[PATCH 08/15] for-each-ref: get --pretty using format_commit_message","fromName":"Ramkumar Ramachandra","fromEmail":"artagnon@gmail.com","sentAt":"2013-06-04T12:35:30Z","receivedAt":"2013-06-04T12:35:30Z","isPatch":true,"sender":{"key":"r@artagnon.com","avatar":"https://avatars.githubusercontent.com/u/37226?v=4"},"body":"From: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n\n--format is very limited in its capabilities.  Introduce --pretty, which\nextends the existing --format with pretty-formats.  In --pretty:\n\n - Existing --format %(atom) is available. They also accept some pretty\n   magic.  For example, you can use \"% (atom)\" to only display a leading\n   space if the atom produces something.\n\n - %ab to display a hex character 0xab is not available as it may\n   conflict with other pretty's placeholders.  Use %xab instead.\n\n - Many pretty placeholders are designed to work on commits.  While some\n   of them should work on tags too, they don't (yet).\n\n - Unsupported atoms cause for-each-ref to exit early and report.\n   Unsupported pretty placeholders are displayed as-is.\n\n - Pretty placeholders can not be used as a sorting criteria.\n\n--format is considered deprecated. If the user hits a bug specific in\n--format code, they are advised to migrate to --pretty.\n\n[rr: documentation]\n\nSigned-off-by: Ramkumar Ramachandra <artagnon@gmail.com>\n---\n Documentation/git-for-each-ref.txt | 22 ++++++++++++-\n builtin/for-each-ref.c             | 67 ++++++++++++++++++++++++++++++++++++--\n 2 files changed, 85 insertions(+), 4 deletions(-)\n\ndiff --git a/Documentation/git-for-each-ref.txt b/Documentation/git-for-each-ref.txt\nindex f2e08d1..6135812 100644\n--- a/Documentation/git-for-each-ref.txt\n+++ b/Documentation/git-for-each-ref.txt\n@@ -9,7 +9,8 @@ SYNOPSIS\n --------\n [verse]\n 'git for-each-ref' [--count=<count>] [--shell|--perl|--python|--tcl]\n-\t\t   [(--sort=<key>)...] [--format=<format>] [<pattern>...]\n+\t\t   [(--sort=<key>)...] [--format=<format>|--pretty=<pretty>]\n+\t\t   [<pattern>...]\n \n DESCRIPTION\n -----------\n@@ -47,6 +48,25 @@ OPTIONS\n \t`xx`; for example `%00` interpolates to `\\0` (NUL),\n \t`%09` to `\\t` (TAB) and `%0a` to `\\n` (LF).\n \n+<pretty>::\n+\tA format string with supporting placeholders described in the\n+\t\"PRETTY FORMATS\" section in linkgit:git-log[1].  Additionally\n+\tsupports placeholders from `<format>`\n+\t(i.e. `%[*](fieldname)`).\n++\n+Caveats:\n+\n+1. Many of the placeholders in \"PRETTY FORMATS\" are designed to work\n+   specifically on commit objects: when non-commit objects are\n+   supplied, those placeholders won't work.\n+\n+2. Does not interpolate `%ab` (where `ab` are hex digits) with the\n+   corresponding hex code.  To print a byte from a hex code, use\n+   `%xab` (from pretty-formats) instead.\n+\n+3. Only the placeholders inherited from `<format>` will respect\n+   quoting settings.\n+\n <pattern>...::\n \tIf one or more patterns are given, only refs are shown that\n \tmatch against at least one pattern, either using fnmatch(3) or\ndiff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c\nindex e2d6c5a..576a882 100644\n--- a/builtin/for-each-ref.c\n+++ b/builtin/for-each-ref.c\n@@ -962,6 +962,58 @@ static void show_refs(struct refinfo **refs, int maxcount,\n \tstrbuf_release(&sb);\n }\n \n+struct format_one_atom_context {\n+\tstruct refinfo *info;\n+\tint quote_style;\n+};\n+\n+static size_t format_one_atom(struct strbuf *sb, const char *placeholder,\n+\t\t\t      void *format_context, void *user_data,\n+\t\t\t      struct strbuf *subst)\n+{\n+\tstruct format_one_atom_context *ctx = user_data;\n+\tconst char *ep;\n+\n+\tif (*placeholder == '%') {\n+\t\tstrbuf_addch(sb, '%');\n+\t\treturn 1;\n+\t}\n+\n+\tif (*placeholder != '(')\n+\t\treturn 0;\n+\n+\tep = strchr(placeholder + 1, ')');\n+\tif (!ep)\n+\t\treturn 0;\n+\tprint_value(sb, ctx->info, parse_atom(placeholder + 1, ep),\n+\t\t    ctx->quote_style);\n+\treturn ep + 1 - placeholder;\n+}\n+\n+static void show_pretty_refs(struct refinfo **refs, int maxcount,\n+\t\t\t     const char *format, int quote_style)\n+{\n+\tstruct pretty_print_context ctx = {0};\n+\tstruct format_one_atom_context fctx;\n+\tstruct strbuf sb = STRBUF_INIT;\n+\tint i;\n+\n+\tctx.format = format_one_atom;\n+\tctx.user_data = &fctx;\n+\tfctx.quote_style = quote_style;\n+\tfor (i = 0; i < maxcount; i++) {\n+\t\tstruct commit *commit = NULL;\n+\t\tfctx.info = refs[i];\n+\t\tif (sha1_object_info(refs[i]->objectname, NULL) == OBJ_COMMIT)\n+\t\t\tcommit = lookup_commit(refs[i]->objectname);\n+\t\tstrbuf_reset(&sb);\n+\t\tformat_commit_message(commit, format, &sb, &ctx);\n+\t\tstrbuf_addch(&sb, '\\n');\n+\t\tfputs(sb.buf, stdout);\n+\t}\n+\tstrbuf_release(&sb);\n+}\n+\n static struct ref_sort *default_sort(void)\n {\n \tstatic const char cstr_name[] = \"refname\";\n@@ -1003,7 +1055,9 @@ static char const * const for_each_ref_usage[] = {\n int cmd_for_each_ref(int argc, const char **argv, const char *prefix)\n {\n \tint num_refs;\n-\tconst char *format = \"%(objectname) %(objecttype)\\t%(refname)\";\n+\tconst char *default_format = \"%(objectname) %(objecttype)\\t%(refname)\";\n+\tconst char *format = default_format;\n+\tconst char *pretty = NULL;\n \tstruct ref_sort *sort = NULL, **sort_tail = &sort;\n \tint maxcount = 0, quote_style = 0;\n \tstruct refinfo **refs;\n@@ -1022,6 +1076,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)\n \t\tOPT_GROUP(\"\"),\n \t\tOPT_INTEGER( 0 , \"count\", &maxcount, N_(\"show only <n> matched refs\")),\n \t\tOPT_STRING(  0 , \"format\", &format, N_(\"format\"), N_(\"format to use for the output\")),\n+\t\tOPT_STRING(  0 , \"pretty\", &pretty, N_(\"format\"), N_(\"alternative format to use for the output\")),\n \t\tOPT_CALLBACK(0 , \"sort\", sort_tail, N_(\"key\"),\n \t\t\t    N_(\"field name to sort on\"), &opt_parse_sort),\n \t\tOPT_END(),\n@@ -1036,7 +1091,10 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)\n \t\terror(\"more than one quoting style?\");\n \t\tusage_with_options(for_each_ref_usage, opts);\n \t}\n-\tif (verify_format(format))\n+\tif (format != default_format && pretty)\n+\t\tdie(\"--format and --pretty cannot be used together\");\n+\tif ((pretty && verify_format(pretty)) ||\n+\t    (!pretty && verify_format(format)))\n \t\tusage_with_options(for_each_ref_usage, opts);\n \n \tif (!sort)\n@@ -1057,6 +1115,9 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)\n \tif (!maxcount || num_refs < maxcount)\n \t\tmaxcount = num_refs;\n \n-\tshow_refs(refs, maxcount, format, quote_style);\n+\tif (pretty)\n+\t\tshow_pretty_refs(refs, maxcount, pretty, quote_style);\n+\telse\n+\t\tshow_refs(refs, maxcount, format, quote_style);\n \treturn 0;\n }\n-- \n1.8.3.GIT\n"},{"id":"219362","messageId":"1370349337-20938-10-git-send-email-artagnon@gmail.com","threadId":"34024","inReplyTo":"1370349337-20938-1-git-send-email-artagnon@gmail.com","subject":"[PATCH 09/15] for-each-ref: teach verify_format() about pretty's syntax","fromName":"Ramkumar Ramachandra","fromEmail":"artagnon@gmail.com","sentAt":"2013-06-04T12:35:31Z","receivedAt":"2013-06-04T12:35:31Z","isPatch":true,"sender":{"key":"r@artagnon.com","avatar":"https://avatars.githubusercontent.com/u/37226?v=4"},"body":"From: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n\nPretty format accepts either ' ', '+' or '-' after '%' and before the\nplaceholder name to modify certain behaviors. Teach verify_format()\nabout this so that it finds atom \"upstream\" in, for example,\n'% (upstream)'. This is important because verify_format populates\nused_atom, which get_value() and populate_value() later rely on.\n\nSigned-off-by: Ramkumar Ramachandra <artagnon@gmail.com>\n---\n builtin/for-each-ref.c | 15 +++++++++------\n pretty.c               |  4 ++++\n 2 files changed, 13 insertions(+), 6 deletions(-)\n\ndiff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c\nindex 576a882..f8a3880 100644\n--- a/builtin/for-each-ref.c\n+++ b/builtin/for-each-ref.c\n@@ -150,7 +150,7 @@ static int parse_atom(const char *atom, const char *ep)\n /*\n  * In a format string, find the next occurrence of %(atom).\n  */\n-static const char *find_next(const char *cp)\n+static const char *find_next(const char *cp, int pretty)\n {\n \twhile (*cp) {\n \t\tif (*cp == '%') {\n@@ -160,6 +160,9 @@ static const char *find_next(const char *cp)\n \t\t\t */\n \t\t\tif (cp[1] == '(')\n \t\t\t\treturn cp;\n+\t\t\telse if (pretty && cp[1] && cp[2] == '(' &&\n+\t\t\t\t strchr(\" +-\", cp[1])) /* see format_commit_item() */\n+\t\t\t\treturn cp + 1;\n \t\t\telse if (cp[1] == '%')\n \t\t\t\tcp++; /* skip over two % */\n \t\t\t/* otherwise this is a singleton, literal % */\n@@ -173,10 +176,10 @@ static const char *find_next(const char *cp)\n  * Make sure the format string is well formed, and parse out\n  * the used atoms.\n  */\n-static int verify_format(const char *format)\n+static int verify_format(const char *format, int pretty)\n {\n \tconst char *cp, *sp;\n-\tfor (cp = format; *cp && (sp = find_next(cp)); ) {\n+\tfor (cp = format; *cp && (sp = find_next(cp, pretty)); ) {\n \t\tconst char *ep = strchr(sp, ')');\n \t\tif (!ep)\n \t\t\treturn error(\"malformed format string %s\", sp);\n@@ -935,7 +938,7 @@ static void show_ref(struct strbuf *sb, struct refinfo *info,\n {\n \tconst char *cp, *sp, *ep;\n \n-\tfor (cp = format; *cp && (sp = find_next(cp)); cp = ep + 1) {\n+\tfor (cp = format; *cp && (sp = find_next(cp, 0)); cp = ep + 1) {\n \t\tep = strchr(sp, ')');\n \t\tif (cp < sp)\n \t\t\temit(sb, cp, sp);\n@@ -1093,8 +1096,8 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)\n \t}\n \tif (format != default_format && pretty)\n \t\tdie(\"--format and --pretty cannot be used together\");\n-\tif ((pretty && verify_format(pretty)) ||\n-\t    (!pretty && verify_format(format)))\n+\tif ((pretty && verify_format(pretty, 1)) ||\n+\t    (!pretty && verify_format(format, 0)))\n \t\tusage_with_options(for_each_ref_usage, opts);\n \n \tif (!sort)\ndiff --git a/pretty.c b/pretty.c\nindex 816aa32..28c0a72 100644\n--- a/pretty.c\n+++ b/pretty.c\n@@ -1439,6 +1439,10 @@ static size_t format_commit_item(struct strbuf *sb, /* in UTF-8 */\n \t\tADD_SP_BEFORE_NON_EMPTY\n \t} magic = NO_MAGIC;\n \n+\t/*\n+\t * Note: any modification in what placeholder[0] contains\n+\t * should be redone in builtin/for-each-ref.c:find_next().\n+\t */\n \tswitch (placeholder[0]) {\n \tcase '-':\n \t\tmagic = DEL_LF_BEFORE_EMPTY;\n-- \n1.8.3.GIT\n"},{"id":"219357","messageId":"1370349337-20938-11-git-send-email-artagnon@gmail.com","threadId":"34024","inReplyTo":"1370349337-20938-1-git-send-email-artagnon@gmail.com","subject":"[PATCH 10/15] for-each-ref: introduce format specifier %>(*) and %<(*)","fromName":"Ramkumar Ramachandra","fromEmail":"artagnon@gmail.com","sentAt":"2013-06-04T12:35:32Z","receivedAt":"2013-06-04T12:35:32Z","isPatch":true,"sender":{"key":"r@artagnon.com","avatar":"https://avatars.githubusercontent.com/u/37226?v=4"},"body":"From: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n\nPretty placeholders %>(N) and %<(N) require a user provided width N,\nwhich makes sense because the commit chain could be really long and the\nuser only needs to look at a few at the top, going to the end just to\ncalculate the best width wastes CPU cycles.\n\nfor-each-ref is different; the display set is small, and we display them\nall at once. We even support sorting, which goes through all display\nitems anyway.  This patch introduces new %>(*) and %<(*), which are\nsupposed to be followed immediately by %(fieldname) (i.e. original\nfor-each-ref specifiers, not ones coming from pretty.c). They calculate\nthe best width for the %(fieldname), ignoring ansi escape sequences if\nany.\n\n[rr: documentation]\n\nSigned-off-by: Ramkumar Ramachandra <artagnon@gmail.com>\n---\n Documentation/git-for-each-ref.txt |  7 +++++++\n builtin/for-each-ref.c             | 38 ++++++++++++++++++++++++++++++++++++++\n 2 files changed, 45 insertions(+)\n\ndiff --git a/Documentation/git-for-each-ref.txt b/Documentation/git-for-each-ref.txt\nindex 6135812..7d6db7f 100644\n--- a/Documentation/git-for-each-ref.txt\n+++ b/Documentation/git-for-each-ref.txt\n@@ -47,6 +47,10 @@ OPTIONS\n \tare hex digits interpolates to character with hex code\n \t`xx`; for example `%00` interpolates to `\\0` (NUL),\n \t`%09` to `\\t` (TAB) and `%0a` to `\\n` (LF).\n++\n+Placeholders `%<(*)` and `%>(*)` work like `%<(<N>)` and `%>(<N>)`\n+respectively, except that the width of the next placeholder is\n+calculated.\n \n <pretty>::\n \tA format string with supporting placeholders described in the\n@@ -67,6 +71,9 @@ Caveats:\n 3. Only the placeholders inherited from `<format>` will respect\n    quoting settings.\n \n+3. Only the placeholders inherited from `<format>` will work with the\n+   alignment placeholders `%<(*)` and '%>(*)`.\n+\n <pattern>...::\n \tIf one or more patterns are given, only refs are shown that\n \tmatch against at least one pattern, either using fnmatch(3) or\ndiff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c\nindex f8a3880..053a622 100644\n--- a/builtin/for-each-ref.c\n+++ b/builtin/for-each-ref.c\n@@ -9,6 +9,7 @@\n #include \"quote.h\"\n #include \"parse-options.h\"\n #include \"remote.h\"\n+#include \"utf8.h\"\n \n /* Quoting styles */\n #define QUOTE_NONE 0\n@@ -966,10 +967,30 @@ static void show_refs(struct refinfo **refs, int maxcount,\n }\n \n struct format_one_atom_context {\n+\tstruct refinfo **refs;\n+\tint maxcount;\n+\n \tstruct refinfo *info;\n \tint quote_style;\n };\n \n+static unsigned int get_atom_width(struct format_one_atom_context *ctx,\n+\t\t\t\t   const char *start, const char *end)\n+{\n+\tstruct strbuf sb = STRBUF_INIT;\n+\tint i, atom = parse_atom(start, end);\n+\tunsigned int len = 0, sb_len;\n+\tfor (i = 0; i < ctx->maxcount; i++) {\n+\t\tprint_value(&sb, ctx->refs[i], atom, ctx->quote_style);\n+\t\tsb_len = utf8_strnwidth(sb.buf, sb.len, 1);\n+\t\tif (sb_len > len)\n+\t\t\tlen = sb_len;\n+\t\tstrbuf_reset(&sb);\n+\t}\n+\tstrbuf_release(&sb);\n+\treturn len;\n+}\n+\n static size_t format_one_atom(struct strbuf *sb, const char *placeholder,\n \t\t\t      void *format_context, void *user_data,\n \t\t\t      struct strbuf *subst)\n@@ -982,6 +1003,21 @@ static size_t format_one_atom(struct strbuf *sb, const char *placeholder,\n \t\treturn 1;\n \t}\n \n+\t/*\n+\t * Substitute %>(*)%(atom) and friends with real width.\n+\t */\n+\tif (*placeholder == '>' || *placeholder == '<') {\n+\t\tconst char *star = placeholder + 1;\n+\t\tif (!prefixcmp(star, \"(*)%(\") &&\n+\t\t    ((ep = strchr(star + strlen(\"(*)%(\"), ')')) != NULL)) {\n+\t\t\tstar++;\n+\t\t\tstrbuf_addf(subst, \"%c(%u)\",\n+\t\t\t\t    *placeholder,\n+\t\t\t\t    get_atom_width(ctx, star + strlen(\"*)%(\"), ep));\n+\t\t\treturn 1 + strlen(\"(*)\");\n+\t\t}\n+\t}\n+\n \tif (*placeholder != '(')\n \t\treturn 0;\n \n@@ -1003,6 +1039,8 @@ static void show_pretty_refs(struct refinfo **refs, int maxcount,\n \n \tctx.format = format_one_atom;\n \tctx.user_data = &fctx;\n+\tfctx.refs = refs;\n+\tfctx.maxcount = maxcount;\n \tfctx.quote_style = quote_style;\n \tfor (i = 0; i < maxcount; i++) {\n \t\tstruct commit *commit = NULL;\n-- \n1.8.3.GIT\n"},{"id":"219358","messageId":"1370349337-20938-12-git-send-email-artagnon@gmail.com","threadId":"34024","inReplyTo":"1370349337-20938-1-git-send-email-artagnon@gmail.com","subject":"[PATCH 11/15] for-each-ref: introduce %(HEAD) marker","fromName":"Ramkumar Ramachandra","fromEmail":"artagnon@gmail.com","sentAt":"2013-06-04T12:35:33Z","receivedAt":"2013-06-04T12:35:33Z","isPatch":true,"sender":{"key":"r@artagnon.com","avatar":"https://avatars.githubusercontent.com/u/37226?v=4"},"body":"'git branch' shows which branch you are currently on with an '*', but\n'git for-each-ref' misses this feature.  So, extend the format with\n%(HEAD) to do exactly the same thing.\n\nNow you can use the following format in for-each-ref:\n\n  %C(red)%(HEAD)%C(reset) %C(green)%(refname:short)%C(reset)\n\nto display a red asterisk next to the current ref.\n\nSigned-off-by: Ramkumar Ramachandra <artagnon@gmail.com>\n---\n Documentation/git-for-each-ref.txt |  4 ++++\n builtin/for-each-ref.c             | 13 +++++++++++--\n 2 files changed, 15 insertions(+), 2 deletions(-)\n\ndiff --git a/Documentation/git-for-each-ref.txt b/Documentation/git-for-each-ref.txt\nindex 7d6db7f..d529296 100644\n--- a/Documentation/git-for-each-ref.txt\n+++ b/Documentation/git-for-each-ref.txt\n@@ -120,6 +120,10 @@ upstream::\n \tfrom the displayed ref. Respects `:short` in the same way as\n \t`refname` above.\n \n+HEAD::\n+\tUseful to indicate the currently checked out branch.  Is '*'\n+\tif HEAD points to the current ref, and ' ' otherwise.\n+\n In addition to the above, for commit and tag objects, the header\n field names (`tree`, `parent`, `object`, `type`, and `tag`) can\n be used to specify the value in the header field.\ndiff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c\nindex 053a622..b0b8236 100644\n--- a/builtin/for-each-ref.c\n+++ b/builtin/for-each-ref.c\n@@ -76,6 +76,7 @@ static struct {\n \t{ \"upstream\" },\n \t{ \"symref\" },\n \t{ \"flag\" },\n+\t{ \"HEAD\" },\n };\n \n /*\n@@ -679,8 +680,16 @@ static void populate_value(struct refinfo *ref)\n \t\t\t\tv->s = xstrdup(buf + 1);\n \t\t\t}\n \t\t\tcontinue;\n-\t\t}\n-\t\telse\n+\t\t} else if (!strcmp(name, \"HEAD\")) {\n+\t\t\tconst char *head;\n+\t\t\tunsigned char sha1[20];\n+\t\t\thead = resolve_ref_unsafe(\"HEAD\", sha1, 1, NULL);\n+\t\t\tif (!strcmp(ref->refname, head))\n+\t\t\t\tv->s = \"*\";\n+\t\t\telse\n+\t\t\t\tv->s = \" \";\n+\t\t\tcontinue;\n+\t\t} else\n \t\t\tcontinue;\n \n \t\tformatp = strchr(name, ':');\n-- \n1.8.3.GIT\n"},{"id":"219360","messageId":"1370349337-20938-13-git-send-email-artagnon@gmail.com","threadId":"34024","inReplyTo":"1370349337-20938-1-git-send-email-artagnon@gmail.com","subject":"[PATCH 12/15] for-each-ref: introduce %(upstream:track[short])","fromName":"Ramkumar Ramachandra","fromEmail":"artagnon@gmail.com","sentAt":"2013-06-04T12:35:34Z","receivedAt":"2013-06-04T12:35:34Z","isPatch":true,"sender":{"key":"r@artagnon.com","avatar":"https://avatars.githubusercontent.com/u/37226?v=4"},"body":"Introduce %(upstream:track) to display \"[ahead M, behind N]\" and\n%(upstream:trackshort) to display \"=\", \">\", \"<\", or \"<>\"\nappropriately (inspired by the contrib/completion/git-prompt.sh).\n\nNow you can use the following format in for-each-ref:\n\n  %C(green)%(refname:short)%C(reset)%(upstream:trackshort)\n\nto display refs with terse tracking information.\n\nNote that :track and :trackshort only works with upstream, and errors\nout when used with anything else.\n\nSigned-off-by: Ramkumar Ramachandra <artagnon@gmail.com>\n---\n Documentation/git-for-each-ref.txt |  6 +++++-\n builtin/for-each-ref.c             | 42 ++++++++++++++++++++++++++++++++++++--\n 2 files changed, 45 insertions(+), 3 deletions(-)\n\ndiff --git a/Documentation/git-for-each-ref.txt b/Documentation/git-for-each-ref.txt\nindex d529296..05ff7ba 100644\n--- a/Documentation/git-for-each-ref.txt\n+++ b/Documentation/git-for-each-ref.txt\n@@ -118,7 +118,11 @@ objectname::\n upstream::\n \tThe name of a local ref which can be considered ``upstream''\n \tfrom the displayed ref. Respects `:short` in the same way as\n-\t`refname` above.\n+\t`refname` above.  Additionally respects `:track` to show\n+\t\"[ahead N, behind M]\" and `:trackshort` to show the terse\n+\tversion (like the prompt) \">\", \"<\", \"<>\", or \"=\".  Has no\n+\teffect if the ref does not have tracking information\n+\tassociated with it.\n \n HEAD::\n \tUseful to indicate the currently checked out branch.  Is '*'\ndiff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c\nindex b0b8236..adc965e 100644\n--- a/builtin/for-each-ref.c\n+++ b/builtin/for-each-ref.c\n@@ -628,6 +628,7 @@ static void populate_value(struct refinfo *ref)\n \tint eaten, i;\n \tunsigned long size;\n \tconst unsigned char *tagged;\n+\tint upstream_present = 0;\n \n \tref->value = xcalloc(sizeof(struct atom_value), used_atom_cnt);\n \n@@ -645,6 +646,7 @@ static void populate_value(struct refinfo *ref)\n \t\tint deref = 0;\n \t\tconst char *refname;\n \t\tconst char *formatp;\n+\t\tstruct branch *branch;\n \n \t\tif (*name == '*') {\n \t\t\tderef = 1;\n@@ -656,7 +658,6 @@ static void populate_value(struct refinfo *ref)\n \t\telse if (!prefixcmp(name, \"symref\"))\n \t\t\trefname = ref->symref ? ref->symref : \"\";\n \t\telse if (!prefixcmp(name, \"upstream\")) {\n-\t\t\tstruct branch *branch;\n \t\t\t/* only local branches may have an upstream */\n \t\t\tif (prefixcmp(ref->refname, \"refs/heads/\"))\n \t\t\t\tcontinue;\n@@ -666,6 +667,7 @@ static void populate_value(struct refinfo *ref)\n \t\t\t    !branch->merge[0]->dst)\n \t\t\t\tcontinue;\n \t\t\trefname = branch->merge[0]->dst;\n+\t\t\tupstream_present = 1;\n \t\t}\n \t\telse if (!strcmp(name, \"flag\")) {\n \t\t\tchar buf[256], *cp = buf;\n@@ -683,6 +685,7 @@ static void populate_value(struct refinfo *ref)\n \t\t} else if (!strcmp(name, \"HEAD\")) {\n \t\t\tconst char *head;\n \t\t\tunsigned char sha1[20];\n+\n \t\t\thead = resolve_ref_unsafe(\"HEAD\", sha1, 1, NULL);\n \t\t\tif (!strcmp(ref->refname, head))\n \t\t\t\tv->s = \"*\";\n@@ -695,11 +698,46 @@ static void populate_value(struct refinfo *ref)\n \t\tformatp = strchr(name, ':');\n \t\t/* look for \"short\" refname format */\n \t\tif (formatp) {\n+\t\t\tint num_ours, num_theirs;\n+\n \t\t\tformatp++;\n \t\t\tif (!strcmp(formatp, \"short\"))\n \t\t\t\trefname = shorten_unambiguous_ref(refname,\n \t\t\t\t\t\t      warn_ambiguous_refs);\n-\t\t\telse\n+\t\t\telse if (!strcmp(formatp, \"track\") &&\n+\t\t\t\t!prefixcmp(name, \"upstream\")) {\n+\t\t\t\tchar buf[40];\n+\n+\t\t\t\tif (!upstream_present)\n+\t\t\t\t\tcontinue;\n+\t\t\t\tif (!stat_tracking_info(branch, &num_ours, &num_theirs))\n+\t\t\t\t\tv->s = \"\";\n+\t\t\t\telse if (!num_ours) {\n+\t\t\t\t\tsprintf(buf, \"[behind %d]\", num_theirs);\n+\t\t\t\t\tv->s = xstrdup(buf);\n+\t\t\t\t} else if (!num_theirs) {\n+\t\t\t\t\tsprintf(buf, \"[ahead %d]\", num_ours);\n+\t\t\t\t\tv->s = xstrdup(buf);\n+\t\t\t\t} else {\n+\t\t\t\t\tsprintf(buf, \"[ahead %d, behind %d]\",\n+\t\t\t\t\t\tnum_ours, num_theirs);\n+\t\t\t\t\tv->s = xstrdup(buf);\n+\t\t\t\t}\n+\t\t\t\tcontinue;\n+\t\t\t} else if (!strcmp(formatp, \"trackshort\") &&\n+\t\t\t\t!prefixcmp(name, \"upstream\")) {\n+\t\t\t\tif (!upstream_present)\n+\t\t\t\t\tcontinue;\n+\t\t\t\tif (!stat_tracking_info(branch, &num_ours, &num_theirs))\n+\t\t\t\t\tv->s = \"=\";\n+\t\t\t\telse if (!num_ours)\n+\t\t\t\t\tv->s = \"<\";\n+\t\t\t\telse if (!num_theirs)\n+\t\t\t\t\tv->s = \">\";\n+\t\t\t\telse\n+\t\t\t\t\tv->s = \"<>\";\n+\t\t\t\tcontinue;\n+\t\t\t} else\n \t\t\t\tdie(\"unknown %.*s format %s\",\n \t\t\t\t    (int)(formatp - name), name, formatp);\n \t\t}\n-- \n1.8.3.GIT\n"},{"id":"219361","messageId":"1370349337-20938-14-git-send-email-artagnon@gmail.com","threadId":"34024","inReplyTo":"1370349337-20938-1-git-send-email-artagnon@gmail.com","subject":"[PATCH 13/15] for-each-ref: improve responsiveness of %(upstream:track)","fromName":"Ramkumar Ramachandra","fromEmail":"artagnon@gmail.com","sentAt":"2013-06-04T12:35:35Z","receivedAt":"2013-06-04T12:35:35Z","isPatch":true,"sender":{"key":"r@artagnon.com","avatar":"https://avatars.githubusercontent.com/u/37226?v=4"},"body":"From: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n\nBefore anything is printed, for-each-ref sorts all refs first.  As\npart of the sorting, populate_value() is called to fill the values in\nfor all atoms/placeholders per entry. By the time sort_refs() is done,\npretty much all data is already retrieved.\n\nThis works fine when data can be cheaply retrieved before\n%(upstream:track) comes into the picture. It may take a noticeable\namount of time to process %(upstream:track) for each entry. All\nentries add up and make --format='%(refname)%(upstream:track)' seem\nhung for a few seconds, then display everything at once.\n\nImprove the responsiveness by only processing the one atom (*) at a\ntime so that processing one atom for all entries (e.g. sorting) won't\ncause much delay (unless you choose a \"heavy\" atom to process).\n\n(*) This is not entirely correct. If you sort by an atom that needs\nobject database access, then it will fill all atoms that need odb.\nWhich is not a bad thing. We don't want to access odb once at sorting\nphase and again at display phase.\n\nSigned-off-by: Ramkumar Ramachandra <artagnon@gmail.com>\n---\n builtin/for-each-ref.c | 49 +++++++++++++++++++++++++------------------------\n 1 file changed, 25 insertions(+), 24 deletions(-)\n\ndiff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c\nindex adc965e..edaa6b2 100644\n--- a/builtin/for-each-ref.c\n+++ b/builtin/for-each-ref.c\n@@ -565,20 +565,6 @@ static void grab_sub_body_contents(struct atom_value *val, int deref, struct obj\n }\n \n /*\n- * We want to have empty print-string for field requests\n- * that do not apply (e.g. \"authordate\" for a tag object)\n- */\n-static void fill_missing_values(struct atom_value *val)\n-{\n-\tint i;\n-\tfor (i = 0; i < used_atom_cnt; i++) {\n-\t\tstruct atom_value *v = &val[i];\n-\t\tif (v->s == NULL)\n-\t\t\tv->s = \"\";\n-\t}\n-}\n-\n-/*\n  * val is a list of atom_value to hold returned values.  Extract\n  * the values for atoms in used_atom array out of (obj, buf, sz).\n  * when deref is false, (obj, buf, sz) is the object that is\n@@ -621,7 +607,7 @@ static inline char *copy_advance(char *dst, const char *src)\n /*\n  * Parse the object referred by ref, and grab needed value.\n  */\n-static void populate_value(struct refinfo *ref)\n+static void populate_value(struct refinfo *ref, int only_this_atom)\n {\n \tvoid *buf;\n \tstruct object *obj;\n@@ -630,13 +616,15 @@ static void populate_value(struct refinfo *ref)\n \tconst unsigned char *tagged;\n \tint upstream_present = 0;\n \n-\tref->value = xcalloc(sizeof(struct atom_value), used_atom_cnt);\n+\tif (!ref->value) {\n+\t\tref->value = xcalloc(sizeof(struct atom_value), used_atom_cnt);\n \n-\tif (need_symref && (ref->flag & REF_ISSYMREF) && !ref->symref) {\n-\t\tunsigned char unused1[20];\n-\t\tref->symref = resolve_refdup(ref->refname, unused1, 1, NULL);\n-\t\tif (!ref->symref)\n-\t\t\tref->symref = \"\";\n+\t\tif (need_symref && (ref->flag & REF_ISSYMREF) && !ref->symref) {\n+\t\t\tunsigned char unused1[20];\n+\t\t\tref->symref = resolve_refdup(ref->refname, unused1, 1, NULL);\n+\t\t\tif (!ref->symref)\n+\t\t\t\tref->symref = \"\";\n+\t\t}\n \t}\n \n \t/* Fill in specials first */\n@@ -648,6 +636,9 @@ static void populate_value(struct refinfo *ref)\n \t\tconst char *formatp;\n \t\tstruct branch *branch;\n \n+\t\tif (only_this_atom != -1 && only_this_atom != i)\n+\t\t\tcontinue;\n+\n \t\tif (*name == '*') {\n \t\t\tderef = 1;\n \t\t\tname++;\n@@ -754,6 +745,10 @@ static void populate_value(struct refinfo *ref)\n \n \tfor (i = 0; i < used_atom_cnt; i++) {\n \t\tstruct atom_value *v = &ref->value[i];\n+\n+\t\tif (only_this_atom != -1 && only_this_atom != i)\n+\t\t\tcontinue;\n+\n \t\tif (v->s == NULL)\n \t\t\tgoto need_obj;\n \t}\n@@ -809,9 +804,15 @@ static void populate_value(struct refinfo *ref)\n  */\n static void get_value(struct refinfo *ref, int atom, struct atom_value **v)\n {\n-\tif (!ref->value) {\n-\t\tpopulate_value(ref);\n-\t\tfill_missing_values(ref->value);\n+\tif (!ref->value || !ref->value[atom].s) {\n+\t\tpopulate_value(ref, atom);\n+\t\t/*\n+\t\t * We want to have empty print-string for field\n+\t\t * requests that do not apply (e.g. \"authordate\" for a\n+\t\t * tag object)\n+\t\t */\n+\t\tif (!ref->value[atom].s)\n+\t\t\tref->value[atom].s = \"\";\n \t}\n \t*v = &ref->value[atom];\n }\n-- \n1.8.3.GIT\n"},{"id":"219359","messageId":"1370349337-20938-15-git-send-email-artagnon@gmail.com","threadId":"34024","inReplyTo":"1370349337-20938-1-git-send-email-artagnon@gmail.com","subject":"[PATCH 14/15] pretty: introduce get_pretty_userformat","fromName":"Ramkumar Ramachandra","fromEmail":"artagnon@gmail.com","sentAt":"2013-06-04T12:35:36Z","receivedAt":"2013-06-04T12:35:36Z","isPatch":true,"sender":{"key":"r@artagnon.com","avatar":"https://avatars.githubusercontent.com/u/37226?v=4"},"body":"This helper function is intended to be used by callers implementing\n--pretty themselves; it parses pretty.* configuration variables\nrecursively and hands the user-defined format back to the caller.  No\nbuiltins are supported, as CMT_FMT_* are really only useful when\ndisplaying commits.  Callers might like to define their own builtins in\nthe future.\n\nSigned-off-by: Ramkumar Ramachandra <artagnon@gmail.com>\n---\n commit.h |  1 +\n pretty.c | 25 +++++++++++++++++++++++++\n 2 files changed, 26 insertions(+)\n\ndiff --git a/commit.h b/commit.h\nindex 04bd935..48424c9 100644\n--- a/commit.h\n+++ b/commit.h\n@@ -113,6 +113,7 @@ extern char *logmsg_reencode(const struct commit *commit,\n \t\t\t     const char *output_encoding);\n extern void logmsg_free(char *msg, const struct commit *commit);\n extern void get_commit_format(const char *arg, struct rev_info *);\n+extern const char *get_pretty_userformat(const char *arg);\n extern const char *format_subject(struct strbuf *sb, const char *msg,\n \t\t\t\t  const char *line_separator);\n extern void userformat_find_requirements(const char *fmt, struct userformat_want *w);\ndiff --git a/pretty.c b/pretty.c\nindex 28c0a72..70e4e44 100644\n--- a/pretty.c\n+++ b/pretty.c\n@@ -174,6 +174,31 @@ void get_commit_format(const char *arg, struct rev_info *rev)\n }\n \n /*\n+ * Function to parse --pretty string, lookup pretty.* configuration\n+ * variables and return the format string, assuming no builtin\n+ * formats.  Not limited to commits, unlike get_commit_format().\n+ */\n+const char *get_pretty_userformat(const char *arg)\n+{\n+\tstruct cmt_fmt_map *commit_format;\n+\n+\tif (!arg || !*arg)\n+\t\treturn NULL;\n+\n+\tif (!prefixcmp(arg, \"format:\") || !prefixcmp(arg, \"tformat:\"))\n+\t\treturn xstrdup(strchr(arg, ':' + 1));\n+\n+\tif (strchr(arg, '%'))\n+\t\treturn xstrdup(arg);\n+\n+\tcommit_format = find_commit_format(arg);\n+\tif (!commit_format || commit_format->format != CMIT_FMT_USERFORMAT)\n+\t\tdie(\"invalid --pretty format: %s\", arg);\n+\n+\treturn xstrdup(commit_format->user_format);\n+}\n+\n+/*\n  * Generic support for pretty-printing the header\n  */\n static int get_one_line(const char *msg)\n-- \n1.8.3.GIT\n"},{"id":"219363","messageId":"1370349337-20938-16-git-send-email-artagnon@gmail.com","threadId":"34024","inReplyTo":"1370349337-20938-1-git-send-email-artagnon@gmail.com","subject":"[PATCH 15/15] for-each-ref: use get_pretty_userformat in --pretty","fromName":"Ramkumar Ramachandra","fromEmail":"artagnon@gmail.com","sentAt":"2013-06-04T12:35:37Z","receivedAt":"2013-06-04T12:35:37Z","isPatch":true,"sender":{"key":"r@artagnon.com","avatar":"https://avatars.githubusercontent.com/u/37226?v=4"},"body":"Use get_pretty_userformat() to interpret the --pretty string.  This\nmeans that leading you can now reference a format specified in a pretty.*\nconfiguration variable as an argument to 'git for-each-ref --pretty='.\nThere are two caveats:\n\n1. A leading \"format:\" or \"tformat:\" is automatically stripped and\n   ignored.  Separator semantics are not configurable (yet).\n\n2. No built-in formats are available.  The ones specified in\n   pretty-formats (oneline, short etc) don't make sense when displaying\n   refs anyway.\n\nSigned-off-by: Ramkumar Ramachandra <artagnon@gmail.com>\n---\n Documentation/git-for-each-ref.txt |  3 +++\n builtin/for-each-ref.c             | 16 +++++++++-------\n 2 files changed, 12 insertions(+), 7 deletions(-)\n\ndiff --git a/Documentation/git-for-each-ref.txt b/Documentation/git-for-each-ref.txt\nindex 05ff7ba..7f3cba5 100644\n--- a/Documentation/git-for-each-ref.txt\n+++ b/Documentation/git-for-each-ref.txt\n@@ -60,6 +60,9 @@ calculated.\n +\n Caveats:\n \n+0. No built-in formats from PRETTY FORMATS (like oneline, short) are\n+   available.\n+\n 1. Many of the placeholders in \"PRETTY FORMATS\" are designed to work\n    specifically on commit objects: when non-commit objects are\n    supplied, those placeholders won't work.\ndiff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c\nindex edaa6b2..c00ab05 100644\n--- a/builtin/for-each-ref.c\n+++ b/builtin/for-each-ref.c\n@@ -1146,7 +1146,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)\n \tint num_refs;\n \tconst char *default_format = \"%(objectname) %(objecttype)\\t%(refname)\";\n \tconst char *format = default_format;\n-\tconst char *pretty = NULL;\n+\tconst char *pretty_raw = NULL, *pretty_userformat = NULL;\n \tstruct ref_sort *sort = NULL, **sort_tail = &sort;\n \tint maxcount = 0, quote_style = 0;\n \tstruct refinfo **refs;\n@@ -1165,13 +1165,15 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)\n \t\tOPT_GROUP(\"\"),\n \t\tOPT_INTEGER( 0 , \"count\", &maxcount, N_(\"show only <n> matched refs\")),\n \t\tOPT_STRING(  0 , \"format\", &format, N_(\"format\"), N_(\"format to use for the output\")),\n-\t\tOPT_STRING(  0 , \"pretty\", &pretty, N_(\"format\"), N_(\"alternative format to use for the output\")),\n+\t\tOPT_STRING(  0 , \"pretty\", &pretty_raw, N_(\"format\"), N_(\"alternative format to use for the output\")),\n \t\tOPT_CALLBACK(0 , \"sort\", sort_tail, N_(\"key\"),\n \t\t\t    N_(\"field name to sort on\"), &opt_parse_sort),\n \t\tOPT_END(),\n \t};\n \n \tparse_options(argc, argv, prefix, opts, for_each_ref_usage, 0);\n+\tif (pretty_raw)\n+\t\tpretty_userformat = get_pretty_userformat(pretty_raw);\n \tif (maxcount < 0) {\n \t\terror(\"invalid --count argument: `%d'\", maxcount);\n \t\tusage_with_options(for_each_ref_usage, opts);\n@@ -1180,10 +1182,10 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)\n \t\terror(\"more than one quoting style?\");\n \t\tusage_with_options(for_each_ref_usage, opts);\n \t}\n-\tif (format != default_format && pretty)\n+\tif (format != default_format && pretty_userformat)\n \t\tdie(\"--format and --pretty cannot be used together\");\n-\tif ((pretty && verify_format(pretty, 1)) ||\n-\t    (!pretty && verify_format(format, 0)))\n+\tif ((pretty_userformat && verify_format(pretty_userformat, 1)) ||\n+\t    (!pretty_userformat && verify_format(format, 0)))\n \t\tusage_with_options(for_each_ref_usage, opts);\n \n \tif (!sort)\n@@ -1204,8 +1206,8 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)\n \tif (!maxcount || num_refs < maxcount)\n \t\tmaxcount = num_refs;\n \n-\tif (pretty)\n-\t\tshow_pretty_refs(refs, maxcount, pretty, quote_style);\n+\tif (pretty_userformat)\n+\t\tshow_pretty_refs(refs, maxcount, pretty_userformat, quote_style);\n \telse\n \t\tshow_refs(refs, maxcount, format, quote_style);\n \treturn 0;\n-- \n1.8.3.GIT\n"},{"id":"219365","messageId":"CACsJy8BDwVL3NC-vweCzn1Lr7-rdOvFGzopBf3qZcO6wyx4fhA@mail.gmail.com","threadId":"34024","inReplyTo":"1370349337-20938-1-git-send-email-artagnon@gmail.com","subject":"Re: [PATCH 00/15] Towards a more awesome git-branch","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2013-06-04T12:49:36Z","receivedAt":"2013-06-04T12:49:36Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Tue, Jun 4, 2013 at 7:35 PM, Ramkumar Ramachandra <artagnon@gmail.com> wrote:\n> Hi,\n>\n> Duy and I have been working on this topic for some time now.  Here's a\n> review candidate.\n\nI'm still hung up one the detached HEAD thing. It's a bit quirky to\nput in for-each-ref, but for-each-ref can't truly replace branch\n--list until it can display detached HEAD. But I think we can finish\nthis part and get it in first. Should be useful for some people\nalready.\n\n> Duy is currently writing code to factor out -v[v] and adding --sort,\n> --count to git-branch, but I'm having a lot of difficulty working with\n> a series of this size.  I'm leaning towards getting this merged before\n> tackling branch (who wants to review a 40-part series?).  Especially\n> after [14/15] and [15/15], git for-each-ref should be usable in its\n> own right.\n\nAgreed (a bit of concern about 14 and 15/15, though). Signed-off-by:\nme for the whole series.\n--\nDuy\n"},{"id":"219367","messageId":"CACsJy8A2h0pjZ759NpByrEJYzg3ETyDkUGTptC-FFkeZCpGSbQ@mail.gmail.com","threadId":"34024","inReplyTo":"1370349337-20938-11-git-send-email-artagnon@gmail.com","subject":"Re: [PATCH 10/15] for-each-ref: introduce format specifier %>(*) and %<(*)","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2013-06-04T12:54:49Z","receivedAt":"2013-06-04T12:54:49Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Tue, Jun 4, 2013 at 7:35 PM, Ramkumar Ramachandra <artagnon@gmail.com> wrote:\n> +static unsigned int get_atom_width(struct format_one_atom_context *ctx,\n> +                                  const char *start, const char *end)\n> +{\n> +       struct strbuf sb = STRBUF_INIT;\n> +       int i, atom = parse_atom(start, end);\n> +       unsigned int len = 0, sb_len;\n> +       for (i = 0; i < ctx->maxcount; i++) {\n> +               print_value(&sb, ctx->refs[i], atom, ctx->quote_style);\n> +               sb_len = utf8_strnwidth(sb.buf, sb.len, 1);\n> +               if (sb_len > len)\n> +                       len = sb_len;\n> +               strbuf_reset(&sb);\n> +       }\n> +       strbuf_release(&sb);\n> +       return len;\n> +}\n> +\n\nI mentioned it before and I do it again. This is not optimal. We\nshould cache the result of get_atom_width() after the first\ncalculation. I haven't done it yet because I'm still not sure if it's\nworth supporting %<(*)%non-atom at this stage. Caching for atoms only\nis much easier because atom is indexed. But I guess it's ok in this\nshape unless you run this over hundreds of refs.\n--\nDuy\n"},{"id":"219368","messageId":"CACsJy8ABNVW=KT9mR4oqiXisEwb5Q3ri4zM467dB-N5ELn=FYQ@mail.gmail.com","threadId":"34024","inReplyTo":"1370349337-20938-16-git-send-email-artagnon@gmail.com","subject":"Re: [PATCH 15/15] for-each-ref: use get_pretty_userformat in --pretty","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2013-06-04T12:57:54Z","receivedAt":"2013-06-04T12:57:54Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Tue, Jun 4, 2013 at 7:35 PM, Ramkumar Ramachandra <artagnon@gmail.com> wrote:\n> Use get_pretty_userformat() to interpret the --pretty string.  This\n> means that leading you can now reference a format specified in a pretty.*\n> configuration variable as an argument to 'git for-each-ref --pretty='.\n> There are two caveats:\n>\n> 1. A leading \"format:\" or \"tformat:\" is automatically stripped and\n>    ignored.  Separator semantics are not configurable (yet).\n>\n> 2. No built-in formats are available.  The ones specified in\n>    pretty-formats (oneline, short etc) don't make sense when displaying\n>    refs anyway.\n>\n\nI spoke too early about the concern with this patch. At first it\nlooked like you replace default formats with config keys. But nope,\nthis is brilliant.\n\n> Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>\n> ---\n>  Documentation/git-for-each-ref.txt |  3 +++\n>  builtin/for-each-ref.c             | 16 +++++++++-------\n>  2 files changed, 12 insertions(+), 7 deletions(-)\n>\n> diff --git a/Documentation/git-for-each-ref.txt b/Documentation/git-for-each-ref.txt\n> index 05ff7ba..7f3cba5 100644\n> --- a/Documentation/git-for-each-ref.txt\n> +++ b/Documentation/git-for-each-ref.txt\n> @@ -60,6 +60,9 @@ calculated.\n>  +\n>  Caveats:\n>\n> +0. No built-in formats from PRETTY FORMATS (like oneline, short) are\n> +   available.\n> +\n>  1. Many of the placeholders in \"PRETTY FORMATS\" are designed to work\n>     specifically on commit objects: when non-commit objects are\n>     supplied, those placeholders won't work.\n> diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c\n> index edaa6b2..c00ab05 100644\n> --- a/builtin/for-each-ref.c\n> +++ b/builtin/for-each-ref.c\n> @@ -1146,7 +1146,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)\n>         int num_refs;\n>         const char *default_format = \"%(objectname) %(objecttype)\\t%(refname)\";\n>         const char *format = default_format;\n> -       const char *pretty = NULL;\n> +       const char *pretty_raw = NULL, *pretty_userformat = NULL;\n>         struct ref_sort *sort = NULL, **sort_tail = &sort;\n>         int maxcount = 0, quote_style = 0;\n>         struct refinfo **refs;\n> @@ -1165,13 +1165,15 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)\n>                 OPT_GROUP(\"\"),\n>                 OPT_INTEGER( 0 , \"count\", &maxcount, N_(\"show only <n> matched refs\")),\n>                 OPT_STRING(  0 , \"format\", &format, N_(\"format\"), N_(\"format to use for the output\")),\n> -               OPT_STRING(  0 , \"pretty\", &pretty, N_(\"format\"), N_(\"alternative format to use for the output\")),\n> +               OPT_STRING(  0 , \"pretty\", &pretty_raw, N_(\"format\"), N_(\"alternative format to use for the output\")),\n>                 OPT_CALLBACK(0 , \"sort\", sort_tail, N_(\"key\"),\n>                             N_(\"field name to sort on\"), &opt_parse_sort),\n>                 OPT_END(),\n>         };\n>\n>         parse_options(argc, argv, prefix, opts, for_each_ref_usage, 0);\n> +       if (pretty_raw)\n> +               pretty_userformat = get_pretty_userformat(pretty_raw);\n>         if (maxcount < 0) {\n>                 error(\"invalid --count argument: `%d'\", maxcount);\n>                 usage_with_options(for_each_ref_usage, opts);\n> @@ -1180,10 +1182,10 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)\n>                 error(\"more than one quoting style?\");\n>                 usage_with_options(for_each_ref_usage, opts);\n>         }\n> -       if (format != default_format && pretty)\n> +       if (format != default_format && pretty_userformat)\n>                 die(\"--format and --pretty cannot be used together\");\n> -       if ((pretty && verify_format(pretty, 1)) ||\n> -           (!pretty && verify_format(format, 0)))\n> +       if ((pretty_userformat && verify_format(pretty_userformat, 1)) ||\n> +           (!pretty_userformat && verify_format(format, 0)))\n>                 usage_with_options(for_each_ref_usage, opts);\n>\n>         if (!sort)\n> @@ -1204,8 +1206,8 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)\n>         if (!maxcount || num_refs < maxcount)\n>                 maxcount = num_refs;\n>\n> -       if (pretty)\n> -               show_pretty_refs(refs, maxcount, pretty, quote_style);\n> +       if (pretty_userformat)\n> +               show_pretty_refs(refs, maxcount, pretty_userformat, quote_style);\n>         else\n>                 show_refs(refs, maxcount, format, quote_style);\n>         return 0;\n> --\n> 1.8.3.GIT\n>\n\n\n\n--\nDuy\n"},{"id":"219407","messageId":"CAPig+cRmnc=poOZUxZms_OWdLpBM_q=h+MDYisu8WtuF11PYXA@mail.gmail.com","threadId":"34024","inReplyTo":"1370349337-20938-9-git-send-email-artagnon@gmail.com","subject":"Re: [PATCH 08/15] for-each-ref: get --pretty using format_commit_message","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2013-06-04T21:12:10Z","receivedAt":"2013-06-04T21:12:10Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Tue, Jun 4, 2013 at 8:35 AM, Ramkumar Ramachandra <artagnon@gmail.com> wrote:\n> From: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n>\n> [rr: documentation]\n>\n> Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>\n> ---\n>  Documentation/git-for-each-ref.txt | 22 ++++++++++++-\n>  builtin/for-each-ref.c             | 67 ++++++++++++++++++++++++++++++++++++--\n>  2 files changed, 85 insertions(+), 4 deletions(-)\n>\n> diff --git a/Documentation/git-for-each-ref.txt b/Documentation/git-for-each-ref.txt\n> index f2e08d1..6135812 100644\n> --- a/Documentation/git-for-each-ref.txt\n> +++ b/Documentation/git-for-each-ref.txt\n> @@ -47,6 +48,25 @@ OPTIONS\n>         `xx`; for example `%00` interpolates to `\\0` (NUL),\n>         `%09` to `\\t` (TAB) and `%0a` to `\\n` (LF).\n>\n> +<pretty>::\n> +       A format string with supporting placeholders described in the\n> +       \"PRETTY FORMATS\" section in linkgit:git-log[1].  Additionally\n> +       supports placeholders from `<format>`\n> +       (i.e. `%[*](fieldname)`).\n> ++\n> +Caveats:\n> +\n> +1. Many of the placeholders in \"PRETTY FORMATS\" are designed to work\n> +   specifically on commit objects: when non-commit objects are\n> +   supplied, those placeholders won't work.\n\nShould \"won't work\" be expanded upon? It's not clear if this means\nthat git will outright crash, or if it will abort with an appropriate\nerror message, or if the directive will be displayed as-is or removed\nfrom the output.\n\n> +2. Does not interpolate `%ab` (where `ab` are hex digits) with the\n> +   corresponding hex code.  To print a byte from a hex code, use\n> +   `%xab` (from pretty-formats) instead.\n> +\n> +3. Only the placeholders inherited from `<format>` will respect\n> +   quoting settings.\n> +\n>  <pattern>...::\n>         If one or more patterns are given, only refs are shown that\n>         match against at least one pattern, either using fnmatch(3) or\n"},{"id":"219409","messageId":"CAPig+cRsDWSsC+K0SN9x06LmBOGi02SLPMLC5jgONuSU_8-Dfg@mail.gmail.com","threadId":"34024","inReplyTo":"1370349337-20938-13-git-send-email-artagnon@gmail.com","subject":"Re: [PATCH 12/15] for-each-ref: introduce %(upstream:track[short])","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2013-06-04T21:14:19Z","receivedAt":"2013-06-04T21:14:19Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Tue, Jun 4, 2013 at 8:35 AM, Ramkumar Ramachandra <artagnon@gmail.com> wrote:\n> Introduce %(upstream:track) to display \"[ahead M, behind N]\" and\n> %(upstream:trackshort) to display \"=\", \">\", \"<\", or \"<>\"\n> appropriately (inspired by the contrib/completion/git-prompt.sh).\n\nBikeshedding: s/trackshort/trackbrief/ perhaps?\n\n> Now you can use the following format in for-each-ref:\n>\n>   %C(green)%(refname:short)%C(reset)%(upstream:trackshort)\n>\n> to display refs with terse tracking information.\n>\n> Note that :track and :trackshort only works with upstream, and errors\n\ns/works/work/\ns/errors/error/\n\n> out when used with anything else.\n>\n> Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>\n"},{"id":"219410","messageId":"CAPig+cTjYjhn5_r3AdoCJvt8fGOeVr=-5yu_tmzpDszjsDX8ag@mail.gmail.com","threadId":"34024","inReplyTo":"1370349337-20938-16-git-send-email-artagnon@gmail.com","subject":"Re: [PATCH 15/15] for-each-ref: use get_pretty_userformat in --pretty","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2013-06-04T21:15:24Z","receivedAt":"2013-06-04T21:15:24Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Tue, Jun 4, 2013 at 8:35 AM, Ramkumar Ramachandra <artagnon@gmail.com> wrote:\n> Use get_pretty_userformat() to interpret the --pretty string.  This\n> means that leading you can now reference a format specified in a pretty.*\n\ns/leading// perhaps?\n\n> configuration variable as an argument to 'git for-each-ref --pretty='.\n> There are two caveats:\n>\n> 1. A leading \"format:\" or \"tformat:\" is automatically stripped and\n>    ignored.  Separator semantics are not configurable (yet).\n>\n> 2. No built-in formats are available.  The ones specified in\n>    pretty-formats (oneline, short etc) don't make sense when displaying\n>    refs anyway.\n>\n> Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>\n"},{"id":"219425","messageId":"7vsj0x4ghm.fsf@alter.siamese.dyndns.org","threadId":"34024","inReplyTo":"1370349337-20938-3-git-send-email-artagnon@gmail.com","subject":"Re: [PATCH 02/15] for-each-ref: don't print out elements directly","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2013-06-04T23:13:09Z","receivedAt":"2013-06-04T23:13:09Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Ramkumar Ramachandra <artagnon@gmail.com> writes:\n\n> From: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n>\n> Currently, the entire callchain starting from show_ref() parses and\n> prints immediately.  This inflexibility limits our ability to extend the\n> parser.  So, convert the entire callchain to accept a strbuf argument to\n> write to.  Also introduce a show_refs() helper that calls show_ref() in\n> a loop to avoid cluttering up cmd_for_each_ref() with the task of\n> initializing/freeing the strbuf.\n>\n> [rr: commit message]\n>\n> Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>\n> ---\n>  builtin/for-each-ref.c | 55 ++++++++++++++++++++++++++++++++------------------\n>  1 file changed, 35 insertions(+), 20 deletions(-)\n>\n> diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c\n> index 1d4083c..e2d6c5a 100644\n> --- a/builtin/for-each-ref.c\n> +++ b/builtin/for-each-ref.c\n> @@ -864,31 +864,31 @@ static void sort_refs(struct ref_sort *sort, struct refinfo **refs, int num_refs\n>  \tqsort(refs, num_refs, sizeof(struct refinfo *), compare_refs);\n>  }\n>  \n> -static void print_value(struct refinfo *ref, int atom, int quote_style)\n> +static void print_value(struct strbuf *sb, struct refinfo *ref,\n> +\t\t\tint atom, int quote_style)\n>  {\n>  \tstruct atom_value *v;\n> -\tstruct strbuf sb = STRBUF_INIT;\n>  \tget_value(ref, atom, &v);\n>  \tswitch (quote_style) {\n>  \tcase QUOTE_NONE:\n> -\t\tfputs(v->s, stdout);\n> +\t\tstrbuf_addstr(sb, v->s);\n>  \t\tbreak;\n>  \tcase QUOTE_SHELL:\n> -\t\tsq_quote_buf(&sb, v->s);\n> +\t\tsq_quote_buf(sb, v->s);\n>  \t\tbreak;\n>  \tcase QUOTE_PERL:\n> -\t\tperl_quote_buf(&sb, v->s);\n> +\t\tperl_quote_buf(sb, v->s);\n>  \t\tbreak;\n>  \tcase QUOTE_PYTHON:\n> -\t\tpython_quote_buf(&sb, v->s);\n> +\t\tpython_quote_buf(sb, v->s);\n>  \t\tbreak;\n>  \tcase QUOTE_TCL:\n> -\t\ttcl_quote_buf(&sb, v->s);\n> +\t\ttcl_quote_buf(sb, v->s);\n>  \t\tbreak;\n>  \t}\n>  \tif (quote_style != QUOTE_NONE) {\n> -\t\tfputs(sb.buf, stdout);\n> -\t\tstrbuf_release(&sb);\n> +\t\tfputs(sb->buf, stdout);\n> +\t\tstrbuf_release(sb);\n>  \t}\n>  }\n\nThe change in the first step made some sense; if asked not to quote,\nit just emits the atom with fputs() in the switch (quote_style), so\nthere is nothing more to do after the switch.\n\nBut this change cannot be correct.  It prints literally to sb, and\nleaves the function without emitting anything nor freeing the\nresource in sb.\n"},{"id":"219426","messageId":"CACsJy8CES9qebDSuO2_rzqNgPPZrhMfvRiaO+ReQNaLVxMAsuA@mail.gmail.com","threadId":"34024","inReplyTo":"7vsj0x4ghm.fsf@alter.siamese.dyndns.org","subject":"Re: [PATCH 02/15] for-each-ref: don't print out elements directly","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2013-06-04T23:44:26Z","receivedAt":"2013-06-04T23:44:26Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Wed, Jun 5, 2013 at 6:13 AM, Junio C Hamano <gitster@pobox.com> wrote:\n> Ramkumar Ramachandra <artagnon@gmail.com> writes:\n>\n>> From: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n>>\n>> Currently, the entire callchain starting from show_ref() parses and\n>> prints immediately.  This inflexibility limits our ability to extend the\n>> parser.  So, convert the entire callchain to accept a strbuf argument to\n>> write to.  Also introduce a show_refs() helper that calls show_ref() in\n>> a loop to avoid cluttering up cmd_for_each_ref() with the task of\n>> initializing/freeing the strbuf.\n>>\n>> [rr: commit message]\n>>\n>> Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>\n>> ---\n>>  builtin/for-each-ref.c | 55 ++++++++++++++++++++++++++++++++------------------\n>>  1 file changed, 35 insertions(+), 20 deletions(-)\n>>\n>> diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c\n>> index 1d4083c..e2d6c5a 100644\n>> --- a/builtin/for-each-ref.c\n>> +++ b/builtin/for-each-ref.c\n>> @@ -864,31 +864,31 @@ static void sort_refs(struct ref_sort *sort, struct refinfo **refs, int num_refs\n>>       qsort(refs, num_refs, sizeof(struct refinfo *), compare_refs);\n>>  }\n>>\n>> -static void print_value(struct refinfo *ref, int atom, int quote_style)\n>> +static void print_value(struct strbuf *sb, struct refinfo *ref,\n>> +                     int atom, int quote_style)\n>>  {\n>>       struct atom_value *v;\n>> -     struct strbuf sb = STRBUF_INIT;\n>>       get_value(ref, atom, &v);\n>>       switch (quote_style) {\n>>       case QUOTE_NONE:\n>> -             fputs(v->s, stdout);\n>> +             strbuf_addstr(sb, v->s);\n>>               break;\n>>       case QUOTE_SHELL:\n>> -             sq_quote_buf(&sb, v->s);\n>> +             sq_quote_buf(sb, v->s);\n>>               break;\n>>       case QUOTE_PERL:\n>> -             perl_quote_buf(&sb, v->s);\n>> +             perl_quote_buf(sb, v->s);\n>>               break;\n>>       case QUOTE_PYTHON:\n>> -             python_quote_buf(&sb, v->s);\n>> +             python_quote_buf(sb, v->s);\n>>               break;\n>>       case QUOTE_TCL:\n>> -             tcl_quote_buf(&sb, v->s);\n>> +             tcl_quote_buf(sb, v->s);\n>>               break;\n>>       }\n>>       if (quote_style != QUOTE_NONE) {\n>> -             fputs(sb.buf, stdout);\n>> -             strbuf_release(&sb);\n>> +             fputs(sb->buf, stdout);\n>> +             strbuf_release(sb);\n>>       }\n>>  }\n>\n> The change in the first step made some sense; if asked not to quote,\n> it just emits the atom with fputs() in the switch (quote_style), so\n> there is nothing more to do after the switch.\n>\n> But this change cannot be correct.  It prints literally to sb, and\n> leaves the function without emitting anything nor freeing the\n> resource in sb.\n\nEverything will/should be emitted in show_refs(), so the \"if\n(quote_style !=..)\" block should be removed. The end result still\nlooks correct due to the order of fputs (in print_value and\nshow_refs). It only breaks down when %<(*) is used together because\nthen \"sb\" content matters. Will fix.\n--\nDuy\n"},{"id":"219444","messageId":"CALkWK0mbyoEMBkJa8VEBnPCxSBOFz1i3_EH13Zn4rAvCr_Cfcg@mail.gmail.com","threadId":"34024","inReplyTo":"CACsJy8BDwVL3NC-vweCzn1Lr7-rdOvFGzopBf3qZcO6wyx4fhA@mail.gmail.com","subject":"Re: [PATCH 00/15] Towards a more awesome git-branch","fromName":"Ramkumar Ramachandra","fromEmail":"artagnon@gmail.com","sentAt":"2013-06-05T08:11:40Z","receivedAt":"2013-06-05T08:11:40Z","isPatch":true,"sender":{"key":"r@artagnon.com","avatar":"https://avatars.githubusercontent.com/u/37226?v=4"},"body":"Duy Nguyen wrote:\n> I'm still hung up one the detached HEAD thing. It's a bit quirky to\n> put in for-each-ref, but for-each-ref can't truly replace branch\n> --list until it can display detached HEAD. But I think we can finish\n> this part and get it in first. Should be useful for some people\n> already.\n\nRight.  The branch is called hot-branch; let's polish it for\ninclusion, and rebase for-each-ref-pretty onto it when it's done (no\nneed to stall work there).\n\n> Signed-off-by:\n> me for the whole series.\n\nThanks.  Will add in the re-roll.\n"},{"id":"219445","messageId":"CALkWK0=9X6QhCOPfV6Gq31VVccyDoO3ciPxG+2kqpgL2foiQSw@mail.gmail.com","threadId":"34024","inReplyTo":"CACsJy8A2h0pjZ759NpByrEJYzg3ETyDkUGTptC-FFkeZCpGSbQ@mail.gmail.com","subject":"Re: [PATCH 10/15] for-each-ref: introduce format specifier %>(*) and %<(*)","fromName":"Ramkumar Ramachandra","fromEmail":"artagnon@gmail.com","sentAt":"2013-06-05T08:14:05Z","receivedAt":"2013-06-05T08:14:05Z","isPatch":true,"sender":{"key":"r@artagnon.com","avatar":"https://avatars.githubusercontent.com/u/37226?v=4"},"body":"Duy Nguyen wrote:\n> I mentioned it before and I do it again. This is not optimal.\n\nYeah, I'll attempt to fix this, but it's not urgent.\n\n> But I guess it's ok in this\n> shape unless you run this over hundreds of refs.\n\nOh, you can run over a hundred refs just fine, for scripting purposes;\nbut why would you want to run over a hundred refs with a pretty that\nincludes %>(*) or %<(*)?\n"},{"id":"219447","messageId":"CACsJy8Bm5tmyUO_e2Uus3RuyOUphyrJOT955T646BdFT060UBA@mail.gmail.com","threadId":"34024","inReplyTo":"CALkWK0=9X6QhCOPfV6Gq31VVccyDoO3ciPxG+2kqpgL2foiQSw@mail.gmail.com","subject":"Re: [PATCH 10/15] for-each-ref: introduce format specifier %>(*) and %<(*)","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2013-06-05T10:15:30Z","receivedAt":"2013-06-05T10:15:30Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Wed, Jun 5, 2013 at 3:14 PM, Ramkumar Ramachandra <artagnon@gmail.com> wrote:\n> Duy Nguyen wrote:\n>> I mentioned it before and I do it again. This is not optimal.\n>\n> Yeah, I'll attempt to fix this, but it's not urgent.\n\nAgreed it's not urgent.\n\n>> But I guess it's ok in this\n>> shape unless you run this over hundreds of refs.\n>\n> Oh, you can run over a hundred refs just fine, for scripting purposes;\n> but why would you want to run over a hundred refs with a pretty that\n> includes %>(*) or %<(*)?\n\nGood point. \"git for-each-ref|wc -l\" on my git repo says I have 673\nrefs. A naive use for-each-ref --pretty without any ref patterns could\nhappen. But I guess people will quickly learn to limit the ref\nselection soon after doing that :-)\n--\nDuy\n"},{"id":"219451","messageId":"CACsJy8ABwroExpXQRX8OYAnDum9+sX1+JB9z+WNn+YcZoXi5KA@mail.gmail.com","threadId":"34024","inReplyTo":"CAPig+cRmnc=poOZUxZms_OWdLpBM_q=h+MDYisu8WtuF11PYXA@mail.gmail.com","subject":"Re: [PATCH 08/15] for-each-ref: get --pretty using format_commit_message","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2013-06-05T13:21:31Z","receivedAt":"2013-06-05T13:21:31Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Wed, Jun 5, 2013 at 4:12 AM, Eric Sunshine <sunshine@sunshineco.com> wrote:\n>> +Caveats:\n>> +\n>> +1. Many of the placeholders in \"PRETTY FORMATS\" are designed to work\n>> +   specifically on commit objects: when non-commit objects are\n>> +   supplied, those placeholders won't work.\n>\n> Should \"won't work\" be expanded upon? It's not clear if this means\n> that git will outright crash, or if it will abort with an appropriate\n> error message, or if the directive will be displayed as-is or removed\n> from the output.\n\nIt will be displayed as-is but that's a bit inconsistent: %(unknown)\nprints error and aborts while %unknown simply produces %unknown. The\nlatter is how \"git log --format\" does it. But I think we could make\nfor-each-ref --pretty to do the former for %unknown. It'll be\nconsistent with %(unknown) and we do not need to elaborate much (it's\npretty obvious when it does not work).\n--\nDuy\n"},{"id":"219463","messageId":"7v61xs32n0.fsf@alter.siamese.dyndns.org","threadId":"34024","inReplyTo":"CACsJy8ABwroExpXQRX8OYAnDum9+sX1+JB9z+WNn+YcZoXi5KA@mail.gmail.com","subject":"Re: [PATCH 08/15] for-each-ref: get --pretty using format_commit_message","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2013-06-05T17:09:55Z","receivedAt":"2013-06-05T17:09:55Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Duy Nguyen <pclouds@gmail.com> writes:\n\n> On Wed, Jun 5, 2013 at 4:12 AM, Eric Sunshine <sunshine@sunshineco.com> wrote:\n>>> +Caveats:\n>>> +\n>>> +1. Many of the placeholders in \"PRETTY FORMATS\" are designed to work\n>>> +   specifically on commit objects: when non-commit objects are\n>>> +   supplied, those placeholders won't work.\n>>\n>> Should \"won't work\" be expanded upon? It's not clear if this means\n>> that git will outright crash, or if it will abort with an appropriate\n>> error message, or if the directive will be displayed as-is or removed\n>> from the output.\n>\n> It will be displayed as-is but that's a bit inconsistent: %(unknown)\n> prints error and aborts while %unknown simply produces %unknown. The\n> latter is how \"git log --format\" does it. But I think we could make\n> for-each-ref --pretty to do the former for %unknown. It'll be\n> consistent with %(unknown) and we do not need to elaborate much (it's\n> pretty obvious when it does not work).\n\nThe Caveat Eric is asking about talks about \"what happens to a\n%(field) that only makes sense for a commit when showing a ref\npointing at a non-commit?\", but you are answering \"what happend to a\n%(invalidfield) that is not defined\", aren't you?\n\nIIRC, the reason we show literal from \"log --format\" is to make it\neasier for the person who misspelt %placeholder to spot it in the\noutput, and also make it easier for the person who use %placeholder\nmeant for newer versions of Git with an older one.  It would be a\nbit unnice to die for the latter, especially if the format string is\nin a script or something.\n\nTo \"log --format\", all input objects are expected to be commits, so\nit does not have the \"what does %(authordate) give when given a blob\"\nissue.\n\nBut for \"for-each-ref --format\", it is perfectly normal that you may\nfeed a non-commit; it makes the mechanism unusable if you errored\nout %(authordate) when showing a ref that points at a tag, doesn't\nit?  Substituting an inapplicable placeholder with an empty string\nwould be an easies way out, unless it learns a flexible/elaborate\nconditional formatting mechanism, I would think.\n"},{"id":"219478","messageId":"CACsJy8D9bLijPX7JnUEkJ4e9OU9tDN_vGa=gkkHSm3Sr34fWQQ@mail.gmail.com","threadId":"34024","inReplyTo":"7v61xs32n0.fsf@alter.siamese.dyndns.org","subject":"Re: [PATCH 08/15] for-each-ref: get --pretty using format_commit_message","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2013-06-06T00:07:44Z","receivedAt":"2013-06-06T00:07:44Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Thu, Jun 6, 2013 at 12:09 AM, Junio C Hamano <gitster@pobox.com> wrote:\n> Duy Nguyen <pclouds@gmail.com> writes:\n>\n>> On Wed, Jun 5, 2013 at 4:12 AM, Eric Sunshine <sunshine@sunshineco.com> wrote:\n>>>> +Caveats:\n>>>> +\n>>>> +1. Many of the placeholders in \"PRETTY FORMATS\" are designed to work\n>>>> +   specifically on commit objects: when non-commit objects are\n>>>> +   supplied, those placeholders won't work.\n>>>\n>>> Should \"won't work\" be expanded upon? It's not clear if this means\n>>> that git will outright crash, or if it will abort with an appropriate\n>>> error message, or if the directive will be displayed as-is or removed\n>>> from the output.\n>>\n>> It will be displayed as-is but that's a bit inconsistent: %(unknown)\n>> prints error and aborts while %unknown simply produces %unknown. The\n>> latter is how \"git log --format\" does it. But I think we could make\n>> for-each-ref --pretty to do the former for %unknown. It'll be\n>> consistent with %(unknown) and we do not need to elaborate much (it's\n>> pretty obvious when it does not work).\n>\n> The Caveat Eric is asking about talks about \"what happens to a\n> %(field) that only makes sense for a commit when showing a ref\n> pointing at a non-commit?\", but you are answering \"what happend to a\n> %(invalidfield) that is not defined\", aren't you?\n\nBecause %(field) is treated like %(invalidfield) in this case. I'm not\nsaying this is the best thing to do though.\n\n> IIRC, the reason we show literal from \"log --format\" is to make it\n> easier for the person who misspelt %placeholder to spot it in the\n> output, and also make it easier for the person who use %placeholder\n> meant for newer versions of Git with an older one.  It would be a\n> bit unnice to die for the latter, especially if the format string is\n> in a script or something.\n\nThat makes more sense for for-each-ref than log because for-each-ref\nis a plumbing and should support scripting. But old for-each-ref will\nhappily reject %(newplacholder) right away. Should we take this\nopportunity to change this behavior in for-each-ref too?\n\n> To \"log --format\", all input objects are expected to be commits, so\n> it does not have the \"what does %(authordate) give when given a blob\"\n> issue.\n>\n> But for \"for-each-ref --format\", it is perfectly normal that you may\n> feed a non-commit; it makes the mechanism unusable if you errored\n> out %(authordate) when showing a ref that points at a tag, doesn't\n> it?  Substituting an inapplicable placeholder with an empty string\n> would be an easies way out, unless it learns a flexible/elaborate\n> conditional formatting mechanism, I would think.\n\nThere is a blurred line here. %H (or %h) should produce an object name\neven for tags or blobs, but right now it produces \"%H\" instead. The\nsame applies for %ad and friends, but these are clearly for commits\nand should probably behave like %(authordate), i.e. producing empty\nstring.\n--\nDuy\n"}]}