{"thread":{"id":"53048","subject":"[PATCH 0/2] Lib-ify functions in builtin.h","startedAt":"2020-03-21T10:29:35Z","lastAt":"2020-03-24T01:08:21Z","messageCount":7,"participants":["Denton Liu","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"393609","messageId":"cover.1584786523.git.liu.denton@gmail.com","threadId":"53048","inReplyTo":null,"subject":"[PATCH 0/2] Lib-ify functions in builtin.h","fromName":"Denton Liu","fromEmail":"liu.denton@gmail.com","sentAt":"2020-03-21T10:29:21Z","receivedAt":"2020-03-21T10:29:35Z","isPatch":true,"sender":{"key":"liu.denton@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9620836?v=4"},"body":"In builtin.h, there exist a couple of functions that are very lib-ish.\nExtract these functions into their own files which should make more\norganizational sense.\n\nDenton Liu (2):\n  Lib-ify fmt-merge-msg\n  Lib-ify prune-packed\n\n Makefile                |   2 +\n builtin.h               |  16 -\n builtin/fmt-merge-msg.c | 655 +---------------------------------------\n builtin/prune-packed.c  |  44 +--\n builtin/prune.c         |   1 +\n builtin/repack.c        |   1 +\n fmt-merge-msg.c         | 653 +++++++++++++++++++++++++++++++++++++++\n fmt-merge-msg.h         |  13 +\n prune-packed.c          |  43 +++\n prune-packed.h          |   9 +\n 10 files changed, 724 insertions(+), 713 deletions(-)\n create mode 100644 fmt-merge-msg.c\n create mode 100644 prune-packed.c\n create mode 100644 prune-packed.h\n\n-- \n2.25.0.114.g5b0ca878e0\n\n"},{"id":"393610","messageId":"249dbb9420598fcc08fb041f0bb4dd089cdd5c31.1584786523.git.liu.denton@gmail.com","threadId":"53048","inReplyTo":"cover.1584786523.git.liu.denton@gmail.com","subject":"[PATCH 2/2] Lib-ify prune-packed","fromName":"Denton Liu","fromEmail":"liu.denton@gmail.com","sentAt":"2020-03-21T10:29:23Z","receivedAt":"2020-03-21T10:29:38Z","isPatch":true,"sender":{"key":"liu.denton@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9620836?v=4"},"body":"In builtin.h, there exists the distinctly lib-ish function\nprune_packed_objects(). Extract this function into prune-packed.c so that\nrelated definitions can exist clearly in their own header file.\n\nWhile we're at it, clean up #includes that are unused.\n\nThis patch is best viewed with --color-moved.\n\nSigned-off-by: Denton Liu <liu.denton@gmail.com>\n---\n Makefile               |  1 +\n builtin.h              |  5 -----\n builtin/prune-packed.c | 44 +-----------------------------------------\n builtin/prune.c        |  1 +\n builtin/repack.c       |  1 +\n prune-packed.c         | 43 +++++++++++++++++++++++++++++++++++++++++\n prune-packed.h         |  9 +++++++++\n 7 files changed, 56 insertions(+), 48 deletions(-)\n create mode 100644 prune-packed.c\n create mode 100644 prune-packed.h\n\ndiff --git a/Makefile b/Makefile\nindex 04e3cc179e..608afd58b1 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -952,6 +952,7 @@ LIB_OBJS += progress.o\n LIB_OBJS += promisor-remote.o\n LIB_OBJS += prompt.o\n LIB_OBJS += protocol.o\n+LIB_OBJS += prune-packed.o\n LIB_OBJS += quote.o\n LIB_OBJS += range-diff.o\n LIB_OBJS += reachable.o\ndiff --git a/builtin.h b/builtin.h\nindex 6101d98661..a5ae15bfe5 100644\n--- a/builtin.h\n+++ b/builtin.h\n@@ -97,11 +97,6 @@\n extern const char git_usage_string[];\n extern const char git_more_info_string[];\n \n-#define PRUNE_PACKED_DRY_RUN 01\n-#define PRUNE_PACKED_VERBOSE 02\n-\n-void prune_packed_objects(int);\n-\n /**\n  * If a built-in has DELAY_PAGER_CONFIG set, the built-in should call this early\n  * when it wishes to respect the `pager.foo`-config. The `cmd` is the name of\ndiff --git a/builtin/prune-packed.c b/builtin/prune-packed.c\nindex 48c5e78e33..b7b9281a8c 100644\n--- a/builtin/prune-packed.c\n+++ b/builtin/prune-packed.c\n@@ -1,54 +1,12 @@\n #include \"builtin.h\"\n-#include \"cache.h\"\n-#include \"progress.h\"\n #include \"parse-options.h\"\n-#include \"packfile.h\"\n-#include \"object-store.h\"\n+#include \"prune-packed.h\"\n \n static const char * const prune_packed_usage[] = {\n \tN_(\"git prune-packed [-n | --dry-run] [-q | --quiet]\"),\n \tNULL\n };\n \n-static struct progress *progress;\n-\n-static int prune_subdir(unsigned int nr, const char *path, void *data)\n-{\n-\tint *opts = data;\n-\tdisplay_progress(progress, nr + 1);\n-\tif (!(*opts & PRUNE_PACKED_DRY_RUN))\n-\t\trmdir(path);\n-\treturn 0;\n-}\n-\n-static int prune_object(const struct object_id *oid, const char *path,\n-\t\t\t void *data)\n-{\n-\tint *opts = data;\n-\n-\tif (!has_object_pack(oid))\n-\t\treturn 0;\n-\n-\tif (*opts & PRUNE_PACKED_DRY_RUN)\n-\t\tprintf(\"rm -f %s\\n\", path);\n-\telse\n-\t\tunlink_or_warn(path);\n-\treturn 0;\n-}\n-\n-void prune_packed_objects(int opts)\n-{\n-\tif (opts & PRUNE_PACKED_VERBOSE)\n-\t\tprogress = start_delayed_progress(_(\"Removing duplicate objects\"), 256);\n-\n-\tfor_each_loose_file_in_objdir(get_object_directory(),\n-\t\t\t\t      prune_object, NULL, prune_subdir, &opts);\n-\n-\t/* Ensure we show 100% before finishing progress */\n-\tdisplay_progress(progress, 256);\n-\tstop_progress(&progress);\n-}\n-\n int cmd_prune_packed(int argc, const char **argv, const char *prefix)\n {\n \tint opts = isatty(2) ? PRUNE_PACKED_VERBOSE : 0;\ndiff --git a/builtin/prune.c b/builtin/prune.c\nindex 2b76872ad2..fd9acc7222 100644\n--- a/builtin/prune.c\n+++ b/builtin/prune.c\n@@ -6,6 +6,7 @@\n #include \"reachable.h\"\n #include \"parse-options.h\"\n #include \"progress.h\"\n+#include \"prune-packed.h\"\n #include \"object-store.h\"\n \n static const char * const prune_usage[] = {\ndiff --git a/builtin/repack.c b/builtin/repack.c\nindex 0781763b06..1b686ee9ce 100644\n--- a/builtin/repack.c\n+++ b/builtin/repack.c\n@@ -10,6 +10,7 @@\n #include \"argv-array.h\"\n #include \"midx.h\"\n #include \"packfile.h\"\n+#include \"prune-packed.h\"\n #include \"object-store.h\"\n #include \"promisor-remote.h\"\n \ndiff --git a/prune-packed.c b/prune-packed.c\nnew file mode 100644\nindex 0000000000..261520b472\n--- /dev/null\n+++ b/prune-packed.c\n@@ -0,0 +1,43 @@\n+#include \"object-store.h\"\n+#include \"packfile.h\"\n+#include \"progress.h\"\n+#include \"prune-packed.h\"\n+\n+static struct progress *progress;\n+\n+static int prune_subdir(unsigned int nr, const char *path, void *data)\n+{\n+\tint *opts = data;\n+\tdisplay_progress(progress, nr + 1);\n+\tif (!(*opts & PRUNE_PACKED_DRY_RUN))\n+\t\trmdir(path);\n+\treturn 0;\n+}\n+\n+static int prune_object(const struct object_id *oid, const char *path,\n+\t\t\t void *data)\n+{\n+\tint *opts = data;\n+\n+\tif (!has_object_pack(oid))\n+\t\treturn 0;\n+\n+\tif (*opts & PRUNE_PACKED_DRY_RUN)\n+\t\tprintf(\"rm -f %s\\n\", path);\n+\telse\n+\t\tunlink_or_warn(path);\n+\treturn 0;\n+}\n+\n+void prune_packed_objects(int opts)\n+{\n+\tif (opts & PRUNE_PACKED_VERBOSE)\n+\t\tprogress = start_delayed_progress(_(\"Removing duplicate objects\"), 256);\n+\n+\tfor_each_loose_file_in_objdir(get_object_directory(),\n+\t\t\t\t      prune_object, NULL, prune_subdir, &opts);\n+\n+\t/* Ensure we show 100% before finishing progress */\n+\tdisplay_progress(progress, 256);\n+\tstop_progress(&progress);\n+}\ndiff --git a/prune-packed.h b/prune-packed.h\nnew file mode 100644\nindex 0000000000..936fa9df23\n--- /dev/null\n+++ b/prune-packed.h\n@@ -0,0 +1,9 @@\n+#ifndef PRUNE_PACKED_H\n+#define PRUNE_PACKED_H\n+\n+#define PRUNE_PACKED_DRY_RUN 01\n+#define PRUNE_PACKED_VERBOSE 02\n+\n+void prune_packed_objects(int);\n+\n+#endif\n-- \n2.25.0.114.g5b0ca878e0\n\n"},{"id":"393611","messageId":"d989ab8b088742378cb9a7deb8412b8fe10e38b7.1584786523.git.liu.denton@gmail.com","threadId":"53048","inReplyTo":"cover.1584786523.git.liu.denton@gmail.com","subject":"[PATCH 1/2] Lib-ify fmt-merge-msg","fromName":"Denton Liu","fromEmail":"liu.denton@gmail.com","sentAt":"2020-03-21T10:29:22Z","receivedAt":"2020-03-21T10:29:41Z","isPatch":true,"sender":{"key":"liu.denton@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9620836?v=4"},"body":"In builtin.h, there exists the distinctly lib-ish function\nfmt_merge_msg(). Extract this function into fmt-merge-msg.c so that\nrelated definitions can exist clearly in their own header file.\n\nWhile we're at it, clean up #includes that are unused.\n\nThis patch is best viewed with --color-moved.\n\nSigned-off-by: Denton Liu <liu.denton@gmail.com>\n---\n Makefile                |   1 +\n builtin.h               |  11 -\n builtin/fmt-merge-msg.c | 655 +---------------------------------------\n fmt-merge-msg.c         | 653 +++++++++++++++++++++++++++++++++++++++\n fmt-merge-msg.h         |  13 +\n 5 files changed, 668 insertions(+), 665 deletions(-)\n create mode 100644 fmt-merge-msg.c\n\ndiff --git a/Makefile b/Makefile\nindex 9804a0758b..04e3cc179e 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -886,6 +886,7 @@ LIB_OBJS += ewah/ewah_rlw.o\n LIB_OBJS += exec-cmd.o\n LIB_OBJS += fetch-negotiator.o\n LIB_OBJS += fetch-pack.o\n+LIB_OBJS += fmt-merge-msg.o\n LIB_OBJS += fsck.o\n LIB_OBJS += fsmonitor.o\n LIB_OBJS += gettext.o\ndiff --git a/builtin.h b/builtin.h\nindex 2b25a80cde..6101d98661 100644\n--- a/builtin.h\n+++ b/builtin.h\n@@ -94,8 +94,6 @@\n  * command.\n  */\n \n-#define DEFAULT_MERGE_LOG_LEN 20\n-\n extern const char git_usage_string[];\n extern const char git_more_info_string[];\n \n@@ -104,15 +102,6 @@ extern const char git_more_info_string[];\n \n void prune_packed_objects(int);\n \n-struct fmt_merge_msg_opts {\n-\tunsigned add_title:1,\n-\t\tcredit_people:1;\n-\tint shortlog_len;\n-};\n-\n-int fmt_merge_msg(struct strbuf *in, struct strbuf *out,\n-\t\t  struct fmt_merge_msg_opts *);\n-\n /**\n  * If a built-in has DELAY_PAGER_CONFIG set, the built-in should call this early\n  * when it wishes to respect the `pager.foo`-config. The `cmd` is the name of\ndiff --git a/builtin/fmt-merge-msg.c b/builtin/fmt-merge-msg.c\nindex 736f666f64..48a8699de7 100644\n--- a/builtin/fmt-merge-msg.c\n+++ b/builtin/fmt-merge-msg.c\n@@ -1,666 +1,13 @@\n #include \"builtin.h\"\n-#include \"cache.h\"\n #include \"config.h\"\n-#include \"refs.h\"\n-#include \"object-store.h\"\n-#include \"commit.h\"\n-#include \"diff.h\"\n-#include \"revision.h\"\n-#include \"tag.h\"\n-#include \"string-list.h\"\n-#include \"branch.h\"\n #include \"fmt-merge-msg.h\"\n-#include \"gpg-interface.h\"\n-#include \"repository.h\"\n-#include \"commit-reach.h\"\n+#include \"parse-options.h\"\n \n static const char * const fmt_merge_msg_usage[] = {\n \tN_(\"git fmt-merge-msg [-m <message>] [--log[=<n>] | --no-log] [--file <file>]\"),\n \tNULL\n };\n \n-static int use_branch_desc;\n-\n-int fmt_merge_msg_config(const char *key, const char *value, void *cb)\n-{\n-\tif (!strcmp(key, \"merge.log\") || !strcmp(key, \"merge.summary\")) {\n-\t\tint is_bool;\n-\t\tmerge_log_config = git_config_bool_or_int(key, value, &is_bool);\n-\t\tif (!is_bool && merge_log_config < 0)\n-\t\t\treturn error(\"%s: negative length %s\", key, value);\n-\t\tif (is_bool && merge_log_config)\n-\t\t\tmerge_log_config = DEFAULT_MERGE_LOG_LEN;\n-\t} else if (!strcmp(key, \"merge.branchdesc\")) {\n-\t\tuse_branch_desc = git_config_bool(key, value);\n-\t} else {\n-\t\treturn git_default_config(key, value, cb);\n-\t}\n-\treturn 0;\n-}\n-\n-/* merge data per repository where the merged tips came from */\n-struct src_data {\n-\tstruct string_list branch, tag, r_branch, generic;\n-\tint head_status;\n-};\n-\n-struct origin_data {\n-\tstruct object_id oid;\n-\tunsigned is_local_branch:1;\n-};\n-\n-static void init_src_data(struct src_data *data)\n-{\n-\tdata->branch.strdup_strings = 1;\n-\tdata->tag.strdup_strings = 1;\n-\tdata->r_branch.strdup_strings = 1;\n-\tdata->generic.strdup_strings = 1;\n-}\n-\n-static struct string_list srcs = STRING_LIST_INIT_DUP;\n-static struct string_list origins = STRING_LIST_INIT_DUP;\n-\n-struct merge_parents {\n-\tint alloc, nr;\n-\tstruct merge_parent {\n-\t\tstruct object_id given;\n-\t\tstruct object_id commit;\n-\t\tunsigned char used;\n-\t} *item;\n-};\n-\n-/*\n- * I know, I know, this is inefficient, but you won't be pulling and merging\n- * hundreds of heads at a time anyway.\n- */\n-static struct merge_parent *find_merge_parent(struct merge_parents *table,\n-\t\t\t\t\t      struct object_id *given,\n-\t\t\t\t\t      struct object_id *commit)\n-{\n-\tint i;\n-\tfor (i = 0; i < table->nr; i++) {\n-\t\tif (given && !oideq(&table->item[i].given, given))\n-\t\t\tcontinue;\n-\t\tif (commit && !oideq(&table->item[i].commit, commit))\n-\t\t\tcontinue;\n-\t\treturn &table->item[i];\n-\t}\n-\treturn NULL;\n-}\n-\n-static void add_merge_parent(struct merge_parents *table,\n-\t\t\t     struct object_id *given,\n-\t\t\t     struct object_id *commit)\n-{\n-\tif (table->nr && find_merge_parent(table, given, commit))\n-\t\treturn;\n-\tALLOC_GROW(table->item, table->nr + 1, table->alloc);\n-\toidcpy(&table->item[table->nr].given, given);\n-\toidcpy(&table->item[table->nr].commit, commit);\n-\ttable->item[table->nr].used = 0;\n-\ttable->nr++;\n-}\n-\n-static int handle_line(char *line, struct merge_parents *merge_parents)\n-{\n-\tint i, len = strlen(line);\n-\tstruct origin_data *origin_data;\n-\tchar *src;\n-\tconst char *origin, *tag_name;\n-\tstruct src_data *src_data;\n-\tstruct string_list_item *item;\n-\tint pulling_head = 0;\n-\tstruct object_id oid;\n-\tconst unsigned hexsz = the_hash_algo->hexsz;\n-\n-\tif (len < hexsz + 3 || line[hexsz] != '\\t')\n-\t\treturn 1;\n-\n-\tif (starts_with(line + hexsz + 1, \"not-for-merge\"))\n-\t\treturn 0;\n-\n-\tif (line[hexsz + 1] != '\\t')\n-\t\treturn 2;\n-\n-\ti = get_oid_hex(line, &oid);\n-\tif (i)\n-\t\treturn 3;\n-\n-\tif (!find_merge_parent(merge_parents, &oid, NULL))\n-\t\treturn 0; /* subsumed by other parents */\n-\n-\torigin_data = xcalloc(1, sizeof(struct origin_data));\n-\toidcpy(&origin_data->oid, &oid);\n-\n-\tif (line[len - 1] == '\\n')\n-\t\tline[len - 1] = 0;\n-\tline += hexsz + 2;\n-\n-\t/*\n-\t * At this point, line points at the beginning of comment e.g.\n-\t * \"branch 'frotz' of git://that/repository.git\".\n-\t * Find the repository name and point it with src.\n-\t */\n-\tsrc = strstr(line, \" of \");\n-\tif (src) {\n-\t\t*src = 0;\n-\t\tsrc += 4;\n-\t\tpulling_head = 0;\n-\t} else {\n-\t\tsrc = line;\n-\t\tpulling_head = 1;\n-\t}\n-\n-\titem = unsorted_string_list_lookup(&srcs, src);\n-\tif (!item) {\n-\t\titem = string_list_append(&srcs, src);\n-\t\titem->util = xcalloc(1, sizeof(struct src_data));\n-\t\tinit_src_data(item->util);\n-\t}\n-\tsrc_data = item->util;\n-\n-\tif (pulling_head) {\n-\t\torigin = src;\n-\t\tsrc_data->head_status |= 1;\n-\t} else if (skip_prefix(line, \"branch \", &origin)) {\n-\t\torigin_data->is_local_branch = 1;\n-\t\tstring_list_append(&src_data->branch, origin);\n-\t\tsrc_data->head_status |= 2;\n-\t} else if (skip_prefix(line, \"tag \", &tag_name)) {\n-\t\torigin = line;\n-\t\tstring_list_append(&src_data->tag, tag_name);\n-\t\tsrc_data->head_status |= 2;\n-\t} else if (skip_prefix(line, \"remote-tracking branch \", &origin)) {\n-\t\tstring_list_append(&src_data->r_branch, origin);\n-\t\tsrc_data->head_status |= 2;\n-\t} else {\n-\t\torigin = src;\n-\t\tstring_list_append(&src_data->generic, line);\n-\t\tsrc_data->head_status |= 2;\n-\t}\n-\n-\tif (!strcmp(\".\", src) || !strcmp(src, origin)) {\n-\t\tint len = strlen(origin);\n-\t\tif (origin[0] == '\\'' && origin[len - 1] == '\\'')\n-\t\t\torigin = xmemdupz(origin + 1, len - 2);\n-\t} else\n-\t\torigin = xstrfmt(\"%s of %s\", origin, src);\n-\tif (strcmp(\".\", src))\n-\t\torigin_data->is_local_branch = 0;\n-\tstring_list_append(&origins, origin)->util = origin_data;\n-\treturn 0;\n-}\n-\n-static void print_joined(const char *singular, const char *plural,\n-\t\tstruct string_list *list, struct strbuf *out)\n-{\n-\tif (list->nr == 0)\n-\t\treturn;\n-\tif (list->nr == 1) {\n-\t\tstrbuf_addf(out, \"%s%s\", singular, list->items[0].string);\n-\t} else {\n-\t\tint i;\n-\t\tstrbuf_addstr(out, plural);\n-\t\tfor (i = 0; i < list->nr - 1; i++)\n-\t\t\tstrbuf_addf(out, \"%s%s\", i > 0 ? \", \" : \"\",\n-\t\t\t\t    list->items[i].string);\n-\t\tstrbuf_addf(out, \" and %s\", list->items[list->nr - 1].string);\n-\t}\n-}\n-\n-static void add_branch_desc(struct strbuf *out, const char *name)\n-{\n-\tstruct strbuf desc = STRBUF_INIT;\n-\n-\tif (!read_branch_desc(&desc, name)) {\n-\t\tconst char *bp = desc.buf;\n-\t\twhile (*bp) {\n-\t\t\tconst char *ep = strchrnul(bp, '\\n');\n-\t\t\tif (*ep)\n-\t\t\t\tep++;\n-\t\t\tstrbuf_addf(out, \"  : %.*s\", (int)(ep - bp), bp);\n-\t\t\tbp = ep;\n-\t\t}\n-\t\tstrbuf_complete_line(out);\n-\t}\n-\tstrbuf_release(&desc);\n-}\n-\n-#define util_as_integral(elem) ((intptr_t)((elem)->util))\n-\n-static void record_person_from_buf(int which, struct string_list *people,\n-\t\t\t\t   const char *buffer)\n-{\n-\tchar *name_buf, *name, *name_end;\n-\tstruct string_list_item *elem;\n-\tconst char *field;\n-\n-\tfield = (which == 'a') ? \"\\nauthor \" : \"\\ncommitter \";\n-\tname = strstr(buffer, field);\n-\tif (!name)\n-\t\treturn;\n-\tname += strlen(field);\n-\tname_end = strchrnul(name, '<');\n-\tif (*name_end)\n-\t\tname_end--;\n-\twhile (isspace(*name_end) && name <= name_end)\n-\t\tname_end--;\n-\tif (name_end < name)\n-\t\treturn;\n-\tname_buf = xmemdupz(name, name_end - name + 1);\n-\n-\telem = string_list_lookup(people, name_buf);\n-\tif (!elem) {\n-\t\telem = string_list_insert(people, name_buf);\n-\t\telem->util = (void *)0;\n-\t}\n-\telem->util = (void*)(util_as_integral(elem) + 1);\n-\tfree(name_buf);\n-}\n-\n-\n-static void record_person(int which, struct string_list *people,\n-\t\t\t  struct commit *commit)\n-{\n-\tconst char *buffer = get_commit_buffer(commit, NULL);\n-\trecord_person_from_buf(which, people, buffer);\n-\tunuse_commit_buffer(commit, buffer);\n-}\n-\n-static int cmp_string_list_util_as_integral(const void *a_, const void *b_)\n-{\n-\tconst struct string_list_item *a = a_, *b = b_;\n-\treturn util_as_integral(b) - util_as_integral(a);\n-}\n-\n-static void add_people_count(struct strbuf *out, struct string_list *people)\n-{\n-\tif (people->nr == 1)\n-\t\tstrbuf_addstr(out, people->items[0].string);\n-\telse if (people->nr == 2)\n-\t\tstrbuf_addf(out, \"%s (%d) and %s (%d)\",\n-\t\t\t    people->items[0].string,\n-\t\t\t    (int)util_as_integral(&people->items[0]),\n-\t\t\t    people->items[1].string,\n-\t\t\t    (int)util_as_integral(&people->items[1]));\n-\telse if (people->nr)\n-\t\tstrbuf_addf(out, \"%s (%d) and others\",\n-\t\t\t    people->items[0].string,\n-\t\t\t    (int)util_as_integral(&people->items[0]));\n-}\n-\n-static void credit_people(struct strbuf *out,\n-\t\t\t  struct string_list *them,\n-\t\t\t  int kind)\n-{\n-\tconst char *label;\n-\tconst char *me;\n-\n-\tif (kind == 'a') {\n-\t\tlabel = \"By\";\n-\t\tme = git_author_info(IDENT_NO_DATE);\n-\t} else {\n-\t\tlabel = \"Via\";\n-\t\tme = git_committer_info(IDENT_NO_DATE);\n-\t}\n-\n-\tif (!them->nr ||\n-\t    (them->nr == 1 &&\n-\t     me &&\n-\t     skip_prefix(me, them->items->string, &me) &&\n-\t     starts_with(me, \" <\")))\n-\t\treturn;\n-\tstrbuf_addf(out, \"\\n%c %s \", comment_line_char, label);\n-\tadd_people_count(out, them);\n-}\n-\n-static void add_people_info(struct strbuf *out,\n-\t\t\t    struct string_list *authors,\n-\t\t\t    struct string_list *committers)\n-{\n-\tQSORT(authors->items, authors->nr,\n-\t      cmp_string_list_util_as_integral);\n-\tQSORT(committers->items, committers->nr,\n-\t      cmp_string_list_util_as_integral);\n-\n-\tcredit_people(out, authors, 'a');\n-\tcredit_people(out, committers, 'c');\n-}\n-\n-static void shortlog(const char *name,\n-\t\t     struct origin_data *origin_data,\n-\t\t     struct commit *head,\n-\t\t     struct rev_info *rev,\n-\t\t     struct fmt_merge_msg_opts *opts,\n-\t\t     struct strbuf *out)\n-{\n-\tint i, count = 0;\n-\tstruct commit *commit;\n-\tstruct object *branch;\n-\tstruct string_list subjects = STRING_LIST_INIT_DUP;\n-\tstruct string_list authors = STRING_LIST_INIT_DUP;\n-\tstruct string_list committers = STRING_LIST_INIT_DUP;\n-\tint flags = UNINTERESTING | TREESAME | SEEN | SHOWN | ADDED;\n-\tstruct strbuf sb = STRBUF_INIT;\n-\tconst struct object_id *oid = &origin_data->oid;\n-\tint limit = opts->shortlog_len;\n-\n-\tbranch = deref_tag(the_repository, parse_object(the_repository, oid),\n-\t\t\t   oid_to_hex(oid),\n-\t\t\t   the_hash_algo->hexsz);\n-\tif (!branch || branch->type != OBJ_COMMIT)\n-\t\treturn;\n-\n-\tsetup_revisions(0, NULL, rev, NULL);\n-\tadd_pending_object(rev, branch, name);\n-\tadd_pending_object(rev, &head->object, \"^HEAD\");\n-\thead->object.flags |= UNINTERESTING;\n-\tif (prepare_revision_walk(rev))\n-\t\tdie(\"revision walk setup failed\");\n-\twhile ((commit = get_revision(rev)) != NULL) {\n-\t\tstruct pretty_print_context ctx = {0};\n-\n-\t\tif (commit->parents && commit->parents->next) {\n-\t\t\t/* do not list a merge but count committer */\n-\t\t\tif (opts->credit_people)\n-\t\t\t\trecord_person('c', &committers, commit);\n-\t\t\tcontinue;\n-\t\t}\n-\t\tif (!count && opts->credit_people)\n-\t\t\t/* the 'tip' committer */\n-\t\t\trecord_person('c', &committers, commit);\n-\t\tif (opts->credit_people)\n-\t\t\trecord_person('a', &authors, commit);\n-\t\tcount++;\n-\t\tif (subjects.nr > limit)\n-\t\t\tcontinue;\n-\n-\t\tformat_commit_message(commit, \"%s\", &sb, &ctx);\n-\t\tstrbuf_ltrim(&sb);\n-\n-\t\tif (!sb.len)\n-\t\t\tstring_list_append(&subjects,\n-\t\t\t\t\t   oid_to_hex(&commit->object.oid));\n-\t\telse\n-\t\t\tstring_list_append_nodup(&subjects,\n-\t\t\t\t\t\t strbuf_detach(&sb, NULL));\n-\t}\n-\n-\tif (opts->credit_people)\n-\t\tadd_people_info(out, &authors, &committers);\n-\tif (count > limit)\n-\t\tstrbuf_addf(out, \"\\n* %s: (%d commits)\\n\", name, count);\n-\telse\n-\t\tstrbuf_addf(out, \"\\n* %s:\\n\", name);\n-\n-\tif (origin_data->is_local_branch && use_branch_desc)\n-\t\tadd_branch_desc(out, name);\n-\n-\tfor (i = 0; i < subjects.nr; i++)\n-\t\tif (i >= limit)\n-\t\t\tstrbuf_addstr(out, \"  ...\\n\");\n-\t\telse\n-\t\t\tstrbuf_addf(out, \"  %s\\n\", subjects.items[i].string);\n-\n-\tclear_commit_marks((struct commit *)branch, flags);\n-\tclear_commit_marks(head, flags);\n-\tfree_commit_list(rev->commits);\n-\trev->commits = NULL;\n-\trev->pending.nr = 0;\n-\n-\tstring_list_clear(&authors, 0);\n-\tstring_list_clear(&committers, 0);\n-\tstring_list_clear(&subjects, 0);\n-}\n-\n-static void fmt_merge_msg_title(struct strbuf *out,\n-\t\t\t\tconst char *current_branch)\n-{\n-\tint i = 0;\n-\tchar *sep = \"\";\n-\n-\tstrbuf_addstr(out, \"Merge \");\n-\tfor (i = 0; i < srcs.nr; i++) {\n-\t\tstruct src_data *src_data = srcs.items[i].util;\n-\t\tconst char *subsep = \"\";\n-\n-\t\tstrbuf_addstr(out, sep);\n-\t\tsep = \"; \";\n-\n-\t\tif (src_data->head_status == 1) {\n-\t\t\tstrbuf_addstr(out, srcs.items[i].string);\n-\t\t\tcontinue;\n-\t\t}\n-\t\tif (src_data->head_status == 3) {\n-\t\t\tsubsep = \", \";\n-\t\t\tstrbuf_addstr(out, \"HEAD\");\n-\t\t}\n-\t\tif (src_data->branch.nr) {\n-\t\t\tstrbuf_addstr(out, subsep);\n-\t\t\tsubsep = \", \";\n-\t\t\tprint_joined(\"branch \", \"branches \", &src_data->branch,\n-\t\t\t\t\tout);\n-\t\t}\n-\t\tif (src_data->r_branch.nr) {\n-\t\t\tstrbuf_addstr(out, subsep);\n-\t\t\tsubsep = \", \";\n-\t\t\tprint_joined(\"remote-tracking branch \", \"remote-tracking branches \",\n-\t\t\t\t\t&src_data->r_branch, out);\n-\t\t}\n-\t\tif (src_data->tag.nr) {\n-\t\t\tstrbuf_addstr(out, subsep);\n-\t\t\tsubsep = \", \";\n-\t\t\tprint_joined(\"tag \", \"tags \", &src_data->tag, out);\n-\t\t}\n-\t\tif (src_data->generic.nr) {\n-\t\t\tstrbuf_addstr(out, subsep);\n-\t\t\tprint_joined(\"commit \", \"commits \", &src_data->generic,\n-\t\t\t\t\tout);\n-\t\t}\n-\t\tif (strcmp(\".\", srcs.items[i].string))\n-\t\t\tstrbuf_addf(out, \" of %s\", srcs.items[i].string);\n-\t}\n-\n-\tif (!strcmp(\"master\", current_branch))\n-\t\tstrbuf_addch(out, '\\n');\n-\telse\n-\t\tstrbuf_addf(out, \" into %s\\n\", current_branch);\n-}\n-\n-static void fmt_tag_signature(struct strbuf *tagbuf,\n-\t\t\t      struct strbuf *sig,\n-\t\t\t      const char *buf,\n-\t\t\t      unsigned long len)\n-{\n-\tconst char *tag_body = strstr(buf, \"\\n\\n\");\n-\tif (tag_body) {\n-\t\ttag_body += 2;\n-\t\tstrbuf_add(tagbuf, tag_body, buf + len - tag_body);\n-\t}\n-\tstrbuf_complete_line(tagbuf);\n-\tif (sig->len) {\n-\t\tstrbuf_addch(tagbuf, '\\n');\n-\t\tstrbuf_add_commented_lines(tagbuf, sig->buf, sig->len);\n-\t}\n-}\n-\n-static void fmt_merge_msg_sigs(struct strbuf *out)\n-{\n-\tint i, tag_number = 0, first_tag = 0;\n-\tstruct strbuf tagbuf = STRBUF_INIT;\n-\n-\tfor (i = 0; i < origins.nr; i++) {\n-\t\tstruct object_id *oid = origins.items[i].util;\n-\t\tenum object_type type;\n-\t\tunsigned long size, len;\n-\t\tchar *buf = read_object_file(oid, &type, &size);\n-\t\tstruct strbuf sig = STRBUF_INIT;\n-\n-\t\tif (!buf || type != OBJ_TAG)\n-\t\t\tgoto next;\n-\t\tlen = parse_signature(buf, size);\n-\n-\t\tif (size == len)\n-\t\t\t; /* merely annotated */\n-\t\telse if (verify_signed_buffer(buf, len, buf + len, size - len, &sig, NULL)) {\n-\t\t\tif (!sig.len)\n-\t\t\t\tstrbuf_addstr(&sig, \"gpg verification failed.\\n\");\n-\t\t}\n-\n-\t\tif (!tag_number++) {\n-\t\t\tfmt_tag_signature(&tagbuf, &sig, buf, len);\n-\t\t\tfirst_tag = i;\n-\t\t} else {\n-\t\t\tif (tag_number == 2) {\n-\t\t\t\tstruct strbuf tagline = STRBUF_INIT;\n-\t\t\t\tstrbuf_addch(&tagline, '\\n');\n-\t\t\t\tstrbuf_add_commented_lines(&tagline,\n-\t\t\t\t\t\torigins.items[first_tag].string,\n-\t\t\t\t\t\tstrlen(origins.items[first_tag].string));\n-\t\t\t\tstrbuf_insert(&tagbuf, 0, tagline.buf,\n-\t\t\t\t\t      tagline.len);\n-\t\t\t\tstrbuf_release(&tagline);\n-\t\t\t}\n-\t\t\tstrbuf_addch(&tagbuf, '\\n');\n-\t\t\tstrbuf_add_commented_lines(&tagbuf,\n-\t\t\t\t\torigins.items[i].string,\n-\t\t\t\t\tstrlen(origins.items[i].string));\n-\t\t\tfmt_tag_signature(&tagbuf, &sig, buf, len);\n-\t\t}\n-\t\tstrbuf_release(&sig);\n-\tnext:\n-\t\tfree(buf);\n-\t}\n-\tif (tagbuf.len) {\n-\t\tstrbuf_addch(out, '\\n');\n-\t\tstrbuf_addbuf(out, &tagbuf);\n-\t}\n-\tstrbuf_release(&tagbuf);\n-}\n-\n-static void find_merge_parents(struct merge_parents *result,\n-\t\t\t       struct strbuf *in, struct object_id *head)\n-{\n-\tstruct commit_list *parents;\n-\tstruct commit *head_commit;\n-\tint pos = 0, i, j;\n-\n-\tparents = NULL;\n-\twhile (pos < in->len) {\n-\t\tint len;\n-\t\tchar *p = in->buf + pos;\n-\t\tchar *newline = strchr(p, '\\n');\n-\t\tconst char *q;\n-\t\tstruct object_id oid;\n-\t\tstruct commit *parent;\n-\t\tstruct object *obj;\n-\n-\t\tlen = newline ? newline - p : strlen(p);\n-\t\tpos += len + !!newline;\n-\n-\t\tif (parse_oid_hex(p, &oid, &q) ||\n-\t\t    q[0] != '\\t' ||\n-\t\t    q[1] != '\\t')\n-\t\t\tcontinue; /* skip not-for-merge */\n-\t\t/*\n-\t\t * Do not use get_merge_parent() here; we do not have\n-\t\t * \"name\" here and we do not want to contaminate its\n-\t\t * util field yet.\n-\t\t */\n-\t\tobj = parse_object(the_repository, &oid);\n-\t\tparent = (struct commit *)peel_to_type(NULL, 0, obj, OBJ_COMMIT);\n-\t\tif (!parent)\n-\t\t\tcontinue;\n-\t\tcommit_list_insert(parent, &parents);\n-\t\tadd_merge_parent(result, &obj->oid, &parent->object.oid);\n-\t}\n-\thead_commit = lookup_commit(the_repository, head);\n-\tif (head_commit)\n-\t\tcommit_list_insert(head_commit, &parents);\n-\treduce_heads_replace(&parents);\n-\n-\twhile (parents) {\n-\t\tstruct commit *cmit = pop_commit(&parents);\n-\t\tfor (i = 0; i < result->nr; i++)\n-\t\t\tif (oideq(&result->item[i].commit, &cmit->object.oid))\n-\t\t\t\tresult->item[i].used = 1;\n-\t}\n-\n-\tfor (i = j = 0; i < result->nr; i++) {\n-\t\tif (result->item[i].used) {\n-\t\t\tif (i != j)\n-\t\t\t\tresult->item[j] = result->item[i];\n-\t\t\tj++;\n-\t\t}\n-\t}\n-\tresult->nr = j;\n-}\n-\n-int fmt_merge_msg(struct strbuf *in, struct strbuf *out,\n-\t\t  struct fmt_merge_msg_opts *opts)\n-{\n-\tint i = 0, pos = 0;\n-\tstruct object_id head_oid;\n-\tconst char *current_branch;\n-\tvoid *current_branch_to_free;\n-\tstruct merge_parents merge_parents;\n-\n-\tmemset(&merge_parents, 0, sizeof(merge_parents));\n-\n-\t/* get current branch */\n-\tcurrent_branch = current_branch_to_free =\n-\t\tresolve_refdup(\"HEAD\", RESOLVE_REF_READING, &head_oid, NULL);\n-\tif (!current_branch)\n-\t\tdie(\"No current branch\");\n-\tif (starts_with(current_branch, \"refs/heads/\"))\n-\t\tcurrent_branch += 11;\n-\n-\tfind_merge_parents(&merge_parents, in, &head_oid);\n-\n-\t/* get a line */\n-\twhile (pos < in->len) {\n-\t\tint len;\n-\t\tchar *newline, *p = in->buf + pos;\n-\n-\t\tnewline = strchr(p, '\\n');\n-\t\tlen = newline ? newline - p : strlen(p);\n-\t\tpos += len + !!newline;\n-\t\ti++;\n-\t\tp[len] = 0;\n-\t\tif (handle_line(p, &merge_parents))\n-\t\t\tdie(\"error in line %d: %.*s\", i, len, p);\n-\t}\n-\n-\tif (opts->add_title && srcs.nr)\n-\t\tfmt_merge_msg_title(out, current_branch);\n-\n-\tif (origins.nr)\n-\t\tfmt_merge_msg_sigs(out);\n-\n-\tif (opts->shortlog_len) {\n-\t\tstruct commit *head;\n-\t\tstruct rev_info rev;\n-\n-\t\thead = lookup_commit_or_die(&head_oid, \"HEAD\");\n-\t\trepo_init_revisions(the_repository, &rev, NULL);\n-\t\trev.commit_format = CMIT_FMT_ONELINE;\n-\t\trev.ignore_merges = 1;\n-\t\trev.limited = 1;\n-\n-\t\tstrbuf_complete_line(out);\n-\n-\t\tfor (i = 0; i < origins.nr; i++)\n-\t\t\tshortlog(origins.items[i].string,\n-\t\t\t\t origins.items[i].util,\n-\t\t\t\t head, &rev, opts, out);\n-\t}\n-\n-\tstrbuf_complete_line(out);\n-\tfree(current_branch_to_free);\n-\tfree(merge_parents.item);\n-\treturn 0;\n-}\n-\n int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)\n {\n \tconst char *inpath = NULL;\ndiff --git a/fmt-merge-msg.c b/fmt-merge-msg.c\nnew file mode 100644\nindex 0000000000..5bcd4fd1c6\n--- /dev/null\n+++ b/fmt-merge-msg.c\n@@ -0,0 +1,653 @@\n+#include \"config.h\"\n+#include \"refs.h\"\n+#include \"object-store.h\"\n+#include \"diff.h\"\n+#include \"revision.h\"\n+#include \"tag.h\"\n+#include \"string-list.h\"\n+#include \"branch.h\"\n+#include \"fmt-merge-msg.h\"\n+#include \"commit-reach.h\"\n+\n+static int use_branch_desc;\n+\n+int fmt_merge_msg_config(const char *key, const char *value, void *cb)\n+{\n+\tif (!strcmp(key, \"merge.log\") || !strcmp(key, \"merge.summary\")) {\n+\t\tint is_bool;\n+\t\tmerge_log_config = git_config_bool_or_int(key, value, &is_bool);\n+\t\tif (!is_bool && merge_log_config < 0)\n+\t\t\treturn error(\"%s: negative length %s\", key, value);\n+\t\tif (is_bool && merge_log_config)\n+\t\t\tmerge_log_config = DEFAULT_MERGE_LOG_LEN;\n+\t} else if (!strcmp(key, \"merge.branchdesc\")) {\n+\t\tuse_branch_desc = git_config_bool(key, value);\n+\t} else {\n+\t\treturn git_default_config(key, value, cb);\n+\t}\n+\treturn 0;\n+}\n+\n+/* merge data per repository where the merged tips came from */\n+struct src_data {\n+\tstruct string_list branch, tag, r_branch, generic;\n+\tint head_status;\n+};\n+\n+struct origin_data {\n+\tstruct object_id oid;\n+\tunsigned is_local_branch:1;\n+};\n+\n+static void init_src_data(struct src_data *data)\n+{\n+\tdata->branch.strdup_strings = 1;\n+\tdata->tag.strdup_strings = 1;\n+\tdata->r_branch.strdup_strings = 1;\n+\tdata->generic.strdup_strings = 1;\n+}\n+\n+static struct string_list srcs = STRING_LIST_INIT_DUP;\n+static struct string_list origins = STRING_LIST_INIT_DUP;\n+\n+struct merge_parents {\n+\tint alloc, nr;\n+\tstruct merge_parent {\n+\t\tstruct object_id given;\n+\t\tstruct object_id commit;\n+\t\tunsigned char used;\n+\t} *item;\n+};\n+\n+/*\n+ * I know, I know, this is inefficient, but you won't be pulling and merging\n+ * hundreds of heads at a time anyway.\n+ */\n+static struct merge_parent *find_merge_parent(struct merge_parents *table,\n+\t\t\t\t\t      struct object_id *given,\n+\t\t\t\t\t      struct object_id *commit)\n+{\n+\tint i;\n+\tfor (i = 0; i < table->nr; i++) {\n+\t\tif (given && !oideq(&table->item[i].given, given))\n+\t\t\tcontinue;\n+\t\tif (commit && !oideq(&table->item[i].commit, commit))\n+\t\t\tcontinue;\n+\t\treturn &table->item[i];\n+\t}\n+\treturn NULL;\n+}\n+\n+static void add_merge_parent(struct merge_parents *table,\n+\t\t\t     struct object_id *given,\n+\t\t\t     struct object_id *commit)\n+{\n+\tif (table->nr && find_merge_parent(table, given, commit))\n+\t\treturn;\n+\tALLOC_GROW(table->item, table->nr + 1, table->alloc);\n+\toidcpy(&table->item[table->nr].given, given);\n+\toidcpy(&table->item[table->nr].commit, commit);\n+\ttable->item[table->nr].used = 0;\n+\ttable->nr++;\n+}\n+\n+static int handle_line(char *line, struct merge_parents *merge_parents)\n+{\n+\tint i, len = strlen(line);\n+\tstruct origin_data *origin_data;\n+\tchar *src;\n+\tconst char *origin, *tag_name;\n+\tstruct src_data *src_data;\n+\tstruct string_list_item *item;\n+\tint pulling_head = 0;\n+\tstruct object_id oid;\n+\tconst unsigned hexsz = the_hash_algo->hexsz;\n+\n+\tif (len < hexsz + 3 || line[hexsz] != '\\t')\n+\t\treturn 1;\n+\n+\tif (starts_with(line + hexsz + 1, \"not-for-merge\"))\n+\t\treturn 0;\n+\n+\tif (line[hexsz + 1] != '\\t')\n+\t\treturn 2;\n+\n+\ti = get_oid_hex(line, &oid);\n+\tif (i)\n+\t\treturn 3;\n+\n+\tif (!find_merge_parent(merge_parents, &oid, NULL))\n+\t\treturn 0; /* subsumed by other parents */\n+\n+\torigin_data = xcalloc(1, sizeof(struct origin_data));\n+\toidcpy(&origin_data->oid, &oid);\n+\n+\tif (line[len - 1] == '\\n')\n+\t\tline[len - 1] = 0;\n+\tline += hexsz + 2;\n+\n+\t/*\n+\t * At this point, line points at the beginning of comment e.g.\n+\t * \"branch 'frotz' of git://that/repository.git\".\n+\t * Find the repository name and point it with src.\n+\t */\n+\tsrc = strstr(line, \" of \");\n+\tif (src) {\n+\t\t*src = 0;\n+\t\tsrc += 4;\n+\t\tpulling_head = 0;\n+\t} else {\n+\t\tsrc = line;\n+\t\tpulling_head = 1;\n+\t}\n+\n+\titem = unsorted_string_list_lookup(&srcs, src);\n+\tif (!item) {\n+\t\titem = string_list_append(&srcs, src);\n+\t\titem->util = xcalloc(1, sizeof(struct src_data));\n+\t\tinit_src_data(item->util);\n+\t}\n+\tsrc_data = item->util;\n+\n+\tif (pulling_head) {\n+\t\torigin = src;\n+\t\tsrc_data->head_status |= 1;\n+\t} else if (skip_prefix(line, \"branch \", &origin)) {\n+\t\torigin_data->is_local_branch = 1;\n+\t\tstring_list_append(&src_data->branch, origin);\n+\t\tsrc_data->head_status |= 2;\n+\t} else if (skip_prefix(line, \"tag \", &tag_name)) {\n+\t\torigin = line;\n+\t\tstring_list_append(&src_data->tag, tag_name);\n+\t\tsrc_data->head_status |= 2;\n+\t} else if (skip_prefix(line, \"remote-tracking branch \", &origin)) {\n+\t\tstring_list_append(&src_data->r_branch, origin);\n+\t\tsrc_data->head_status |= 2;\n+\t} else {\n+\t\torigin = src;\n+\t\tstring_list_append(&src_data->generic, line);\n+\t\tsrc_data->head_status |= 2;\n+\t}\n+\n+\tif (!strcmp(\".\", src) || !strcmp(src, origin)) {\n+\t\tint len = strlen(origin);\n+\t\tif (origin[0] == '\\'' && origin[len - 1] == '\\'')\n+\t\t\torigin = xmemdupz(origin + 1, len - 2);\n+\t} else\n+\t\torigin = xstrfmt(\"%s of %s\", origin, src);\n+\tif (strcmp(\".\", src))\n+\t\torigin_data->is_local_branch = 0;\n+\tstring_list_append(&origins, origin)->util = origin_data;\n+\treturn 0;\n+}\n+\n+static void print_joined(const char *singular, const char *plural,\n+\t\tstruct string_list *list, struct strbuf *out)\n+{\n+\tif (list->nr == 0)\n+\t\treturn;\n+\tif (list->nr == 1) {\n+\t\tstrbuf_addf(out, \"%s%s\", singular, list->items[0].string);\n+\t} else {\n+\t\tint i;\n+\t\tstrbuf_addstr(out, plural);\n+\t\tfor (i = 0; i < list->nr - 1; i++)\n+\t\t\tstrbuf_addf(out, \"%s%s\", i > 0 ? \", \" : \"\",\n+\t\t\t\t    list->items[i].string);\n+\t\tstrbuf_addf(out, \" and %s\", list->items[list->nr - 1].string);\n+\t}\n+}\n+\n+static void add_branch_desc(struct strbuf *out, const char *name)\n+{\n+\tstruct strbuf desc = STRBUF_INIT;\n+\n+\tif (!read_branch_desc(&desc, name)) {\n+\t\tconst char *bp = desc.buf;\n+\t\twhile (*bp) {\n+\t\t\tconst char *ep = strchrnul(bp, '\\n');\n+\t\t\tif (*ep)\n+\t\t\t\tep++;\n+\t\t\tstrbuf_addf(out, \"  : %.*s\", (int)(ep - bp), bp);\n+\t\t\tbp = ep;\n+\t\t}\n+\t\tstrbuf_complete_line(out);\n+\t}\n+\tstrbuf_release(&desc);\n+}\n+\n+#define util_as_integral(elem) ((intptr_t)((elem)->util))\n+\n+static void record_person_from_buf(int which, struct string_list *people,\n+\t\t\t\t   const char *buffer)\n+{\n+\tchar *name_buf, *name, *name_end;\n+\tstruct string_list_item *elem;\n+\tconst char *field;\n+\n+\tfield = (which == 'a') ? \"\\nauthor \" : \"\\ncommitter \";\n+\tname = strstr(buffer, field);\n+\tif (!name)\n+\t\treturn;\n+\tname += strlen(field);\n+\tname_end = strchrnul(name, '<');\n+\tif (*name_end)\n+\t\tname_end--;\n+\twhile (isspace(*name_end) && name <= name_end)\n+\t\tname_end--;\n+\tif (name_end < name)\n+\t\treturn;\n+\tname_buf = xmemdupz(name, name_end - name + 1);\n+\n+\telem = string_list_lookup(people, name_buf);\n+\tif (!elem) {\n+\t\telem = string_list_insert(people, name_buf);\n+\t\telem->util = (void *)0;\n+\t}\n+\telem->util = (void*)(util_as_integral(elem) + 1);\n+\tfree(name_buf);\n+}\n+\n+\n+static void record_person(int which, struct string_list *people,\n+\t\t\t  struct commit *commit)\n+{\n+\tconst char *buffer = get_commit_buffer(commit, NULL);\n+\trecord_person_from_buf(which, people, buffer);\n+\tunuse_commit_buffer(commit, buffer);\n+}\n+\n+static int cmp_string_list_util_as_integral(const void *a_, const void *b_)\n+{\n+\tconst struct string_list_item *a = a_, *b = b_;\n+\treturn util_as_integral(b) - util_as_integral(a);\n+}\n+\n+static void add_people_count(struct strbuf *out, struct string_list *people)\n+{\n+\tif (people->nr == 1)\n+\t\tstrbuf_addstr(out, people->items[0].string);\n+\telse if (people->nr == 2)\n+\t\tstrbuf_addf(out, \"%s (%d) and %s (%d)\",\n+\t\t\t    people->items[0].string,\n+\t\t\t    (int)util_as_integral(&people->items[0]),\n+\t\t\t    people->items[1].string,\n+\t\t\t    (int)util_as_integral(&people->items[1]));\n+\telse if (people->nr)\n+\t\tstrbuf_addf(out, \"%s (%d) and others\",\n+\t\t\t    people->items[0].string,\n+\t\t\t    (int)util_as_integral(&people->items[0]));\n+}\n+\n+static void credit_people(struct strbuf *out,\n+\t\t\t  struct string_list *them,\n+\t\t\t  int kind)\n+{\n+\tconst char *label;\n+\tconst char *me;\n+\n+\tif (kind == 'a') {\n+\t\tlabel = \"By\";\n+\t\tme = git_author_info(IDENT_NO_DATE);\n+\t} else {\n+\t\tlabel = \"Via\";\n+\t\tme = git_committer_info(IDENT_NO_DATE);\n+\t}\n+\n+\tif (!them->nr ||\n+\t    (them->nr == 1 &&\n+\t     me &&\n+\t     skip_prefix(me, them->items->string, &me) &&\n+\t     starts_with(me, \" <\")))\n+\t\treturn;\n+\tstrbuf_addf(out, \"\\n%c %s \", comment_line_char, label);\n+\tadd_people_count(out, them);\n+}\n+\n+static void add_people_info(struct strbuf *out,\n+\t\t\t    struct string_list *authors,\n+\t\t\t    struct string_list *committers)\n+{\n+\tQSORT(authors->items, authors->nr,\n+\t      cmp_string_list_util_as_integral);\n+\tQSORT(committers->items, committers->nr,\n+\t      cmp_string_list_util_as_integral);\n+\n+\tcredit_people(out, authors, 'a');\n+\tcredit_people(out, committers, 'c');\n+}\n+\n+static void shortlog(const char *name,\n+\t\t     struct origin_data *origin_data,\n+\t\t     struct commit *head,\n+\t\t     struct rev_info *rev,\n+\t\t     struct fmt_merge_msg_opts *opts,\n+\t\t     struct strbuf *out)\n+{\n+\tint i, count = 0;\n+\tstruct commit *commit;\n+\tstruct object *branch;\n+\tstruct string_list subjects = STRING_LIST_INIT_DUP;\n+\tstruct string_list authors = STRING_LIST_INIT_DUP;\n+\tstruct string_list committers = STRING_LIST_INIT_DUP;\n+\tint flags = UNINTERESTING | TREESAME | SEEN | SHOWN | ADDED;\n+\tstruct strbuf sb = STRBUF_INIT;\n+\tconst struct object_id *oid = &origin_data->oid;\n+\tint limit = opts->shortlog_len;\n+\n+\tbranch = deref_tag(the_repository, parse_object(the_repository, oid),\n+\t\t\t   oid_to_hex(oid),\n+\t\t\t   the_hash_algo->hexsz);\n+\tif (!branch || branch->type != OBJ_COMMIT)\n+\t\treturn;\n+\n+\tsetup_revisions(0, NULL, rev, NULL);\n+\tadd_pending_object(rev, branch, name);\n+\tadd_pending_object(rev, &head->object, \"^HEAD\");\n+\thead->object.flags |= UNINTERESTING;\n+\tif (prepare_revision_walk(rev))\n+\t\tdie(\"revision walk setup failed\");\n+\twhile ((commit = get_revision(rev)) != NULL) {\n+\t\tstruct pretty_print_context ctx = {0};\n+\n+\t\tif (commit->parents && commit->parents->next) {\n+\t\t\t/* do not list a merge but count committer */\n+\t\t\tif (opts->credit_people)\n+\t\t\t\trecord_person('c', &committers, commit);\n+\t\t\tcontinue;\n+\t\t}\n+\t\tif (!count && opts->credit_people)\n+\t\t\t/* the 'tip' committer */\n+\t\t\trecord_person('c', &committers, commit);\n+\t\tif (opts->credit_people)\n+\t\t\trecord_person('a', &authors, commit);\n+\t\tcount++;\n+\t\tif (subjects.nr > limit)\n+\t\t\tcontinue;\n+\n+\t\tformat_commit_message(commit, \"%s\", &sb, &ctx);\n+\t\tstrbuf_ltrim(&sb);\n+\n+\t\tif (!sb.len)\n+\t\t\tstring_list_append(&subjects,\n+\t\t\t\t\t   oid_to_hex(&commit->object.oid));\n+\t\telse\n+\t\t\tstring_list_append_nodup(&subjects,\n+\t\t\t\t\t\t strbuf_detach(&sb, NULL));\n+\t}\n+\n+\tif (opts->credit_people)\n+\t\tadd_people_info(out, &authors, &committers);\n+\tif (count > limit)\n+\t\tstrbuf_addf(out, \"\\n* %s: (%d commits)\\n\", name, count);\n+\telse\n+\t\tstrbuf_addf(out, \"\\n* %s:\\n\", name);\n+\n+\tif (origin_data->is_local_branch && use_branch_desc)\n+\t\tadd_branch_desc(out, name);\n+\n+\tfor (i = 0; i < subjects.nr; i++)\n+\t\tif (i >= limit)\n+\t\t\tstrbuf_addstr(out, \"  ...\\n\");\n+\t\telse\n+\t\t\tstrbuf_addf(out, \"  %s\\n\", subjects.items[i].string);\n+\n+\tclear_commit_marks((struct commit *)branch, flags);\n+\tclear_commit_marks(head, flags);\n+\tfree_commit_list(rev->commits);\n+\trev->commits = NULL;\n+\trev->pending.nr = 0;\n+\n+\tstring_list_clear(&authors, 0);\n+\tstring_list_clear(&committers, 0);\n+\tstring_list_clear(&subjects, 0);\n+}\n+\n+static void fmt_merge_msg_title(struct strbuf *out,\n+\t\t\t\tconst char *current_branch)\n+{\n+\tint i = 0;\n+\tchar *sep = \"\";\n+\n+\tstrbuf_addstr(out, \"Merge \");\n+\tfor (i = 0; i < srcs.nr; i++) {\n+\t\tstruct src_data *src_data = srcs.items[i].util;\n+\t\tconst char *subsep = \"\";\n+\n+\t\tstrbuf_addstr(out, sep);\n+\t\tsep = \"; \";\n+\n+\t\tif (src_data->head_status == 1) {\n+\t\t\tstrbuf_addstr(out, srcs.items[i].string);\n+\t\t\tcontinue;\n+\t\t}\n+\t\tif (src_data->head_status == 3) {\n+\t\t\tsubsep = \", \";\n+\t\t\tstrbuf_addstr(out, \"HEAD\");\n+\t\t}\n+\t\tif (src_data->branch.nr) {\n+\t\t\tstrbuf_addstr(out, subsep);\n+\t\t\tsubsep = \", \";\n+\t\t\tprint_joined(\"branch \", \"branches \", &src_data->branch,\n+\t\t\t\t\tout);\n+\t\t}\n+\t\tif (src_data->r_branch.nr) {\n+\t\t\tstrbuf_addstr(out, subsep);\n+\t\t\tsubsep = \", \";\n+\t\t\tprint_joined(\"remote-tracking branch \", \"remote-tracking branches \",\n+\t\t\t\t\t&src_data->r_branch, out);\n+\t\t}\n+\t\tif (src_data->tag.nr) {\n+\t\t\tstrbuf_addstr(out, subsep);\n+\t\t\tsubsep = \", \";\n+\t\t\tprint_joined(\"tag \", \"tags \", &src_data->tag, out);\n+\t\t}\n+\t\tif (src_data->generic.nr) {\n+\t\t\tstrbuf_addstr(out, subsep);\n+\t\t\tprint_joined(\"commit \", \"commits \", &src_data->generic,\n+\t\t\t\t\tout);\n+\t\t}\n+\t\tif (strcmp(\".\", srcs.items[i].string))\n+\t\t\tstrbuf_addf(out, \" of %s\", srcs.items[i].string);\n+\t}\n+\n+\tif (!strcmp(\"master\", current_branch))\n+\t\tstrbuf_addch(out, '\\n');\n+\telse\n+\t\tstrbuf_addf(out, \" into %s\\n\", current_branch);\n+}\n+\n+static void fmt_tag_signature(struct strbuf *tagbuf,\n+\t\t\t      struct strbuf *sig,\n+\t\t\t      const char *buf,\n+\t\t\t      unsigned long len)\n+{\n+\tconst char *tag_body = strstr(buf, \"\\n\\n\");\n+\tif (tag_body) {\n+\t\ttag_body += 2;\n+\t\tstrbuf_add(tagbuf, tag_body, buf + len - tag_body);\n+\t}\n+\tstrbuf_complete_line(tagbuf);\n+\tif (sig->len) {\n+\t\tstrbuf_addch(tagbuf, '\\n');\n+\t\tstrbuf_add_commented_lines(tagbuf, sig->buf, sig->len);\n+\t}\n+}\n+\n+static void fmt_merge_msg_sigs(struct strbuf *out)\n+{\n+\tint i, tag_number = 0, first_tag = 0;\n+\tstruct strbuf tagbuf = STRBUF_INIT;\n+\n+\tfor (i = 0; i < origins.nr; i++) {\n+\t\tstruct object_id *oid = origins.items[i].util;\n+\t\tenum object_type type;\n+\t\tunsigned long size, len;\n+\t\tchar *buf = read_object_file(oid, &type, &size);\n+\t\tstruct strbuf sig = STRBUF_INIT;\n+\n+\t\tif (!buf || type != OBJ_TAG)\n+\t\t\tgoto next;\n+\t\tlen = parse_signature(buf, size);\n+\n+\t\tif (size == len)\n+\t\t\t; /* merely annotated */\n+\t\telse if (verify_signed_buffer(buf, len, buf + len, size - len, &sig, NULL)) {\n+\t\t\tif (!sig.len)\n+\t\t\t\tstrbuf_addstr(&sig, \"gpg verification failed.\\n\");\n+\t\t}\n+\n+\t\tif (!tag_number++) {\n+\t\t\tfmt_tag_signature(&tagbuf, &sig, buf, len);\n+\t\t\tfirst_tag = i;\n+\t\t} else {\n+\t\t\tif (tag_number == 2) {\n+\t\t\t\tstruct strbuf tagline = STRBUF_INIT;\n+\t\t\t\tstrbuf_addch(&tagline, '\\n');\n+\t\t\t\tstrbuf_add_commented_lines(&tagline,\n+\t\t\t\t\t\torigins.items[first_tag].string,\n+\t\t\t\t\t\tstrlen(origins.items[first_tag].string));\n+\t\t\t\tstrbuf_insert(&tagbuf, 0, tagline.buf,\n+\t\t\t\t\t      tagline.len);\n+\t\t\t\tstrbuf_release(&tagline);\n+\t\t\t}\n+\t\t\tstrbuf_addch(&tagbuf, '\\n');\n+\t\t\tstrbuf_add_commented_lines(&tagbuf,\n+\t\t\t\t\torigins.items[i].string,\n+\t\t\t\t\tstrlen(origins.items[i].string));\n+\t\t\tfmt_tag_signature(&tagbuf, &sig, buf, len);\n+\t\t}\n+\t\tstrbuf_release(&sig);\n+\tnext:\n+\t\tfree(buf);\n+\t}\n+\tif (tagbuf.len) {\n+\t\tstrbuf_addch(out, '\\n');\n+\t\tstrbuf_addbuf(out, &tagbuf);\n+\t}\n+\tstrbuf_release(&tagbuf);\n+}\n+\n+static void find_merge_parents(struct merge_parents *result,\n+\t\t\t       struct strbuf *in, struct object_id *head)\n+{\n+\tstruct commit_list *parents;\n+\tstruct commit *head_commit;\n+\tint pos = 0, i, j;\n+\n+\tparents = NULL;\n+\twhile (pos < in->len) {\n+\t\tint len;\n+\t\tchar *p = in->buf + pos;\n+\t\tchar *newline = strchr(p, '\\n');\n+\t\tconst char *q;\n+\t\tstruct object_id oid;\n+\t\tstruct commit *parent;\n+\t\tstruct object *obj;\n+\n+\t\tlen = newline ? newline - p : strlen(p);\n+\t\tpos += len + !!newline;\n+\n+\t\tif (parse_oid_hex(p, &oid, &q) ||\n+\t\t    q[0] != '\\t' ||\n+\t\t    q[1] != '\\t')\n+\t\t\tcontinue; /* skip not-for-merge */\n+\t\t/*\n+\t\t * Do not use get_merge_parent() here; we do not have\n+\t\t * \"name\" here and we do not want to contaminate its\n+\t\t * util field yet.\n+\t\t */\n+\t\tobj = parse_object(the_repository, &oid);\n+\t\tparent = (struct commit *)peel_to_type(NULL, 0, obj, OBJ_COMMIT);\n+\t\tif (!parent)\n+\t\t\tcontinue;\n+\t\tcommit_list_insert(parent, &parents);\n+\t\tadd_merge_parent(result, &obj->oid, &parent->object.oid);\n+\t}\n+\thead_commit = lookup_commit(the_repository, head);\n+\tif (head_commit)\n+\t\tcommit_list_insert(head_commit, &parents);\n+\treduce_heads_replace(&parents);\n+\n+\twhile (parents) {\n+\t\tstruct commit *cmit = pop_commit(&parents);\n+\t\tfor (i = 0; i < result->nr; i++)\n+\t\t\tif (oideq(&result->item[i].commit, &cmit->object.oid))\n+\t\t\t\tresult->item[i].used = 1;\n+\t}\n+\n+\tfor (i = j = 0; i < result->nr; i++) {\n+\t\tif (result->item[i].used) {\n+\t\t\tif (i != j)\n+\t\t\t\tresult->item[j] = result->item[i];\n+\t\t\tj++;\n+\t\t}\n+\t}\n+\tresult->nr = j;\n+}\n+\n+\n+int fmt_merge_msg(struct strbuf *in, struct strbuf *out,\n+\t\t  struct fmt_merge_msg_opts *opts)\n+{\n+\tint i = 0, pos = 0;\n+\tstruct object_id head_oid;\n+\tconst char *current_branch;\n+\tvoid *current_branch_to_free;\n+\tstruct merge_parents merge_parents;\n+\n+\tmemset(&merge_parents, 0, sizeof(merge_parents));\n+\n+\t/* get current branch */\n+\tcurrent_branch = current_branch_to_free =\n+\t\tresolve_refdup(\"HEAD\", RESOLVE_REF_READING, &head_oid, NULL);\n+\tif (!current_branch)\n+\t\tdie(\"No current branch\");\n+\tif (starts_with(current_branch, \"refs/heads/\"))\n+\t\tcurrent_branch += 11;\n+\n+\tfind_merge_parents(&merge_parents, in, &head_oid);\n+\n+\t/* get a line */\n+\twhile (pos < in->len) {\n+\t\tint len;\n+\t\tchar *newline, *p = in->buf + pos;\n+\n+\t\tnewline = strchr(p, '\\n');\n+\t\tlen = newline ? newline - p : strlen(p);\n+\t\tpos += len + !!newline;\n+\t\ti++;\n+\t\tp[len] = 0;\n+\t\tif (handle_line(p, &merge_parents))\n+\t\t\tdie(\"error in line %d: %.*s\", i, len, p);\n+\t}\n+\n+\tif (opts->add_title && srcs.nr)\n+\t\tfmt_merge_msg_title(out, current_branch);\n+\n+\tif (origins.nr)\n+\t\tfmt_merge_msg_sigs(out);\n+\n+\tif (opts->shortlog_len) {\n+\t\tstruct commit *head;\n+\t\tstruct rev_info rev;\n+\n+\t\thead = lookup_commit_or_die(&head_oid, \"HEAD\");\n+\t\trepo_init_revisions(the_repository, &rev, NULL);\n+\t\trev.commit_format = CMIT_FMT_ONELINE;\n+\t\trev.ignore_merges = 1;\n+\t\trev.limited = 1;\n+\n+\t\tstrbuf_complete_line(out);\n+\n+\t\tfor (i = 0; i < origins.nr; i++)\n+\t\t\tshortlog(origins.items[i].string,\n+\t\t\t\t origins.items[i].util,\n+\t\t\t\t head, &rev, opts, out);\n+\t}\n+\n+\tstrbuf_complete_line(out);\n+\tfree(current_branch_to_free);\n+\tfree(merge_parents.item);\n+\treturn 0;\n+}\ndiff --git a/fmt-merge-msg.h b/fmt-merge-msg.h\nindex 01e3aa88c5..f2ab0e0085 100644\n--- a/fmt-merge-msg.h\n+++ b/fmt-merge-msg.h\n@@ -1,7 +1,20 @@\n #ifndef FMT_MERGE_MSG_H\n #define FMT_MERGE_MSG_H\n \n+#include \"strbuf.h\"\n+\n+#define DEFAULT_MERGE_LOG_LEN 20\n+\n+struct fmt_merge_msg_opts {\n+\tunsigned add_title:1,\n+\t\tcredit_people:1;\n+\tint shortlog_len;\n+};\n+\n extern int merge_log_config;\n int fmt_merge_msg_config(const char *key, const char *value, void *cb);\n+int fmt_merge_msg(struct strbuf *in, struct strbuf *out,\n+\t\t  struct fmt_merge_msg_opts *);\n+\n \n #endif /* FMT_MERGE_MSG_H */\n-- \n2.25.0.114.g5b0ca878e0\n\n"},{"id":"393656","messageId":"xmqq36a1a4uv.fsf@gitster.c.googlers.com","threadId":"53048","inReplyTo":"cover.1584786523.git.liu.denton@gmail.com","subject":"Re: [PATCH 0/2] Lib-ify functions in builtin.h","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2020-03-21T19:24:24Z","receivedAt":"2020-03-21T19:24:29Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Denton Liu <liu.denton@gmail.com> writes:\n\n> In builtin.h, there exist a couple of functions that are very lib-ish.\n> Extract these functions into their own files which should make more\n> organizational sense.\n\n\"very lib-ish\" feels a bit subjective, but I agree that among those\nfunctions that are currently only allowed to be called from a\nbuilt-in command (hence, cannot be called from anything in libgit.a\nor non-builtin commands like transports), there may be some funtions\nthat may make sense to be callable by non-builtin users of libgit.a\n(e.g. cgit perhaps?).\n\nI do like the direction these patches are taking us to.  I just do\nnot know how best to phrase it, and having trouble coming up with a\nnon subjective description.\n\nThanks.\n\n> Denton Liu (2):\n>   Lib-ify fmt-merge-msg\n>   Lib-ify prune-packed\n>\n>  Makefile                |   2 +\n>  builtin.h               |  16 -\n>  builtin/fmt-merge-msg.c | 655 +---------------------------------------\n>  builtin/prune-packed.c  |  44 +--\n>  builtin/prune.c         |   1 +\n>  builtin/repack.c        |   1 +\n>  fmt-merge-msg.c         | 653 +++++++++++++++++++++++++++++++++++++++\n>  fmt-merge-msg.h         |  13 +\n>  prune-packed.c          |  43 +++\n>  prune-packed.h          |   9 +\n>  10 files changed, 724 insertions(+), 713 deletions(-)\n>  create mode 100644 fmt-merge-msg.c\n>  create mode 100644 prune-packed.c\n>  create mode 100644 prune-packed.h\n"},{"id":"393844","messageId":"cover.1585011949.git.liu.denton@gmail.com","threadId":"53048","inReplyTo":"cover.1584786523.git.liu.denton@gmail.com","subject":"[PATCH v2 0/2] Lib-ify functions in builtin.h","fromName":"Denton Liu","fromEmail":"liu.denton@gmail.com","sentAt":"2020-03-24T01:07:50Z","receivedAt":"2020-03-24T01:08:17Z","isPatch":true,"sender":{"key":"liu.denton@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9620836?v=4"},"body":"Since the last revision, I tried to rewrite the commit messages to be\nmore subjective but I was also having some trouble with it.\n\nIn builtin.h, there exist a couple of functions that are very \"lib-ish\".\nThese functions can currently only be called by built-in commands but,\nunlike all of the other functions in the header, it does not make sense\nto impose this restriction as their functionality can be logically reused\nin libgit.\n\nExtract these functions into their own files which should make more\norganizational sense.\n\nChanges since v1:\n\n* Rewrite commit messages to be less subjective\n\nDenton Liu (2):\n  Lib-ify fmt-merge-msg\n  Lib-ify prune-packed\n\n Makefile                |   2 +\n builtin.h               |  16 -\n builtin/fmt-merge-msg.c | 655 +---------------------------------------\n builtin/prune-packed.c  |  44 +--\n builtin/prune.c         |   1 +\n builtin/repack.c        |   1 +\n fmt-merge-msg.c         | 653 +++++++++++++++++++++++++++++++++++++++\n fmt-merge-msg.h         |  13 +\n prune-packed.c          |  43 +++\n prune-packed.h          |   9 +\n 10 files changed, 724 insertions(+), 713 deletions(-)\n create mode 100644 fmt-merge-msg.c\n create mode 100644 prune-packed.c\n create mode 100644 prune-packed.h\n\nRange-diff against v1:\n1:  d989ab8b08 ! 1:  fe76d79aa7 Lib-ify fmt-merge-msg\n    @@ Metadata\n      ## Commit message ##\n         Lib-ify fmt-merge-msg\n     \n    -    In builtin.h, there exists the distinctly lib-ish function\n    -    fmt_merge_msg(). Extract this function into fmt-merge-msg.c so that\n    -    related definitions can exist clearly in their own header file.\n    +    In builtin.h, there exists the distinctly \"lib-ish\" function\n    +    fmt_merge_msg(). This function can currently only be called by built-in\n    +    commands but, unlike most of the other functions in the header, it does\n    +    not make sense to impose this restriction as the functionality can be\n    +    logically reused in libgit.\n    +\n    +    Extract this function into fmt-merge-msg.c so that related definitions\n    +    can exist clearly in their own header file.\n     \n         While we're at it, clean up #includes that are unused.\n     \n2:  249dbb9420 ! 2:  c6a80ce882 Lib-ify prune-packed\n    @@ Commit message\n         Lib-ify prune-packed\n     \n         In builtin.h, there exists the distinctly lib-ish function\n    -    prune_packed_objects(). Extract this function into prune-packed.c so that\n    -    related definitions can exist clearly in their own header file.\n    +    prune_packed_objects(). This function can currently only be called by\n    +    built-in commands but, unlike all of the other functions in the header,\n    +    it does not make sense to impose this restriction as the functionality\n    +    can be logically reused in libgit.\n    +\n    +    Extract this function into prune-packed.c so that related definitions\n    +    can exist clearly in their own header file.\n     \n         While we're at it, clean up #includes that are unused.\n     \n-- \n2.25.0.114.g5b0ca878e0\n\n"},{"id":"393845","messageId":"c6a80ce8829c0b9bac22b4e264f238cf9b9359f7.1585011949.git.liu.denton@gmail.com","threadId":"53048","inReplyTo":"cover.1585011949.git.liu.denton@gmail.com","subject":"[PATCH v2 2/2] Lib-ify prune-packed","fromName":"Denton Liu","fromEmail":"liu.denton@gmail.com","sentAt":"2020-03-24T01:07:52Z","receivedAt":"2020-03-24T01:08:19Z","isPatch":true,"sender":{"key":"liu.denton@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9620836?v=4"},"body":"In builtin.h, there exists the distinctly lib-ish function\nprune_packed_objects(). This function can currently only be called by\nbuilt-in commands but, unlike all of the other functions in the header,\nit does not make sense to impose this restriction as the functionality\ncan be logically reused in libgit.\n\nExtract this function into prune-packed.c so that related definitions\ncan exist clearly in their own header file.\n\nWhile we're at it, clean up #includes that are unused.\n\nThis patch is best viewed with --color-moved.\n\nSigned-off-by: Denton Liu <liu.denton@gmail.com>\n---\n Makefile               |  1 +\n builtin.h              |  5 -----\n builtin/prune-packed.c | 44 +-----------------------------------------\n builtin/prune.c        |  1 +\n builtin/repack.c       |  1 +\n prune-packed.c         | 43 +++++++++++++++++++++++++++++++++++++++++\n prune-packed.h         |  9 +++++++++\n 7 files changed, 56 insertions(+), 48 deletions(-)\n create mode 100644 prune-packed.c\n create mode 100644 prune-packed.h\n\ndiff --git a/Makefile b/Makefile\nindex 04e3cc179e..608afd58b1 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -952,6 +952,7 @@ LIB_OBJS += progress.o\n LIB_OBJS += promisor-remote.o\n LIB_OBJS += prompt.o\n LIB_OBJS += protocol.o\n+LIB_OBJS += prune-packed.o\n LIB_OBJS += quote.o\n LIB_OBJS += range-diff.o\n LIB_OBJS += reachable.o\ndiff --git a/builtin.h b/builtin.h\nindex 6101d98661..a5ae15bfe5 100644\n--- a/builtin.h\n+++ b/builtin.h\n@@ -97,11 +97,6 @@\n extern const char git_usage_string[];\n extern const char git_more_info_string[];\n \n-#define PRUNE_PACKED_DRY_RUN 01\n-#define PRUNE_PACKED_VERBOSE 02\n-\n-void prune_packed_objects(int);\n-\n /**\n  * If a built-in has DELAY_PAGER_CONFIG set, the built-in should call this early\n  * when it wishes to respect the `pager.foo`-config. The `cmd` is the name of\ndiff --git a/builtin/prune-packed.c b/builtin/prune-packed.c\nindex 48c5e78e33..b7b9281a8c 100644\n--- a/builtin/prune-packed.c\n+++ b/builtin/prune-packed.c\n@@ -1,54 +1,12 @@\n #include \"builtin.h\"\n-#include \"cache.h\"\n-#include \"progress.h\"\n #include \"parse-options.h\"\n-#include \"packfile.h\"\n-#include \"object-store.h\"\n+#include \"prune-packed.h\"\n \n static const char * const prune_packed_usage[] = {\n \tN_(\"git prune-packed [-n | --dry-run] [-q | --quiet]\"),\n \tNULL\n };\n \n-static struct progress *progress;\n-\n-static int prune_subdir(unsigned int nr, const char *path, void *data)\n-{\n-\tint *opts = data;\n-\tdisplay_progress(progress, nr + 1);\n-\tif (!(*opts & PRUNE_PACKED_DRY_RUN))\n-\t\trmdir(path);\n-\treturn 0;\n-}\n-\n-static int prune_object(const struct object_id *oid, const char *path,\n-\t\t\t void *data)\n-{\n-\tint *opts = data;\n-\n-\tif (!has_object_pack(oid))\n-\t\treturn 0;\n-\n-\tif (*opts & PRUNE_PACKED_DRY_RUN)\n-\t\tprintf(\"rm -f %s\\n\", path);\n-\telse\n-\t\tunlink_or_warn(path);\n-\treturn 0;\n-}\n-\n-void prune_packed_objects(int opts)\n-{\n-\tif (opts & PRUNE_PACKED_VERBOSE)\n-\t\tprogress = start_delayed_progress(_(\"Removing duplicate objects\"), 256);\n-\n-\tfor_each_loose_file_in_objdir(get_object_directory(),\n-\t\t\t\t      prune_object, NULL, prune_subdir, &opts);\n-\n-\t/* Ensure we show 100% before finishing progress */\n-\tdisplay_progress(progress, 256);\n-\tstop_progress(&progress);\n-}\n-\n int cmd_prune_packed(int argc, const char **argv, const char *prefix)\n {\n \tint opts = isatty(2) ? PRUNE_PACKED_VERBOSE : 0;\ndiff --git a/builtin/prune.c b/builtin/prune.c\nindex 2b76872ad2..fd9acc7222 100644\n--- a/builtin/prune.c\n+++ b/builtin/prune.c\n@@ -6,6 +6,7 @@\n #include \"reachable.h\"\n #include \"parse-options.h\"\n #include \"progress.h\"\n+#include \"prune-packed.h\"\n #include \"object-store.h\"\n \n static const char * const prune_usage[] = {\ndiff --git a/builtin/repack.c b/builtin/repack.c\nindex 0781763b06..1b686ee9ce 100644\n--- a/builtin/repack.c\n+++ b/builtin/repack.c\n@@ -10,6 +10,7 @@\n #include \"argv-array.h\"\n #include \"midx.h\"\n #include \"packfile.h\"\n+#include \"prune-packed.h\"\n #include \"object-store.h\"\n #include \"promisor-remote.h\"\n \ndiff --git a/prune-packed.c b/prune-packed.c\nnew file mode 100644\nindex 0000000000..261520b472\n--- /dev/null\n+++ b/prune-packed.c\n@@ -0,0 +1,43 @@\n+#include \"object-store.h\"\n+#include \"packfile.h\"\n+#include \"progress.h\"\n+#include \"prune-packed.h\"\n+\n+static struct progress *progress;\n+\n+static int prune_subdir(unsigned int nr, const char *path, void *data)\n+{\n+\tint *opts = data;\n+\tdisplay_progress(progress, nr + 1);\n+\tif (!(*opts & PRUNE_PACKED_DRY_RUN))\n+\t\trmdir(path);\n+\treturn 0;\n+}\n+\n+static int prune_object(const struct object_id *oid, const char *path,\n+\t\t\t void *data)\n+{\n+\tint *opts = data;\n+\n+\tif (!has_object_pack(oid))\n+\t\treturn 0;\n+\n+\tif (*opts & PRUNE_PACKED_DRY_RUN)\n+\t\tprintf(\"rm -f %s\\n\", path);\n+\telse\n+\t\tunlink_or_warn(path);\n+\treturn 0;\n+}\n+\n+void prune_packed_objects(int opts)\n+{\n+\tif (opts & PRUNE_PACKED_VERBOSE)\n+\t\tprogress = start_delayed_progress(_(\"Removing duplicate objects\"), 256);\n+\n+\tfor_each_loose_file_in_objdir(get_object_directory(),\n+\t\t\t\t      prune_object, NULL, prune_subdir, &opts);\n+\n+\t/* Ensure we show 100% before finishing progress */\n+\tdisplay_progress(progress, 256);\n+\tstop_progress(&progress);\n+}\ndiff --git a/prune-packed.h b/prune-packed.h\nnew file mode 100644\nindex 0000000000..936fa9df23\n--- /dev/null\n+++ b/prune-packed.h\n@@ -0,0 +1,9 @@\n+#ifndef PRUNE_PACKED_H\n+#define PRUNE_PACKED_H\n+\n+#define PRUNE_PACKED_DRY_RUN 01\n+#define PRUNE_PACKED_VERBOSE 02\n+\n+void prune_packed_objects(int);\n+\n+#endif\n-- \n2.25.0.114.g5b0ca878e0\n\n"},{"id":"393846","messageId":"fe76d79aa793a0a08f91087f433dc3a50a2d895d.1585011949.git.liu.denton@gmail.com","threadId":"53048","inReplyTo":"cover.1585011949.git.liu.denton@gmail.com","subject":"[PATCH v2 1/2] Lib-ify fmt-merge-msg","fromName":"Denton Liu","fromEmail":"liu.denton@gmail.com","sentAt":"2020-03-24T01:07:51Z","receivedAt":"2020-03-24T01:08:21Z","isPatch":true,"sender":{"key":"liu.denton@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9620836?v=4"},"body":"In builtin.h, there exists the distinctly \"lib-ish\" function\nfmt_merge_msg(). This function can currently only be called by built-in\ncommands but, unlike most of the other functions in the header, it does\nnot make sense to impose this restriction as the functionality can be\nlogically reused in libgit.\n\nExtract this function into fmt-merge-msg.c so that related definitions\ncan exist clearly in their own header file.\n\nWhile we're at it, clean up #includes that are unused.\n\nThis patch is best viewed with --color-moved.\n\nSigned-off-by: Denton Liu <liu.denton@gmail.com>\n---\n Makefile                |   1 +\n builtin.h               |  11 -\n builtin/fmt-merge-msg.c | 655 +---------------------------------------\n fmt-merge-msg.c         | 653 +++++++++++++++++++++++++++++++++++++++\n fmt-merge-msg.h         |  13 +\n 5 files changed, 668 insertions(+), 665 deletions(-)\n create mode 100644 fmt-merge-msg.c\n\ndiff --git a/Makefile b/Makefile\nindex 9804a0758b..04e3cc179e 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -886,6 +886,7 @@ LIB_OBJS += ewah/ewah_rlw.o\n LIB_OBJS += exec-cmd.o\n LIB_OBJS += fetch-negotiator.o\n LIB_OBJS += fetch-pack.o\n+LIB_OBJS += fmt-merge-msg.o\n LIB_OBJS += fsck.o\n LIB_OBJS += fsmonitor.o\n LIB_OBJS += gettext.o\ndiff --git a/builtin.h b/builtin.h\nindex 2b25a80cde..6101d98661 100644\n--- a/builtin.h\n+++ b/builtin.h\n@@ -94,8 +94,6 @@\n  * command.\n  */\n \n-#define DEFAULT_MERGE_LOG_LEN 20\n-\n extern const char git_usage_string[];\n extern const char git_more_info_string[];\n \n@@ -104,15 +102,6 @@ extern const char git_more_info_string[];\n \n void prune_packed_objects(int);\n \n-struct fmt_merge_msg_opts {\n-\tunsigned add_title:1,\n-\t\tcredit_people:1;\n-\tint shortlog_len;\n-};\n-\n-int fmt_merge_msg(struct strbuf *in, struct strbuf *out,\n-\t\t  struct fmt_merge_msg_opts *);\n-\n /**\n  * If a built-in has DELAY_PAGER_CONFIG set, the built-in should call this early\n  * when it wishes to respect the `pager.foo`-config. The `cmd` is the name of\ndiff --git a/builtin/fmt-merge-msg.c b/builtin/fmt-merge-msg.c\nindex 736f666f64..48a8699de7 100644\n--- a/builtin/fmt-merge-msg.c\n+++ b/builtin/fmt-merge-msg.c\n@@ -1,666 +1,13 @@\n #include \"builtin.h\"\n-#include \"cache.h\"\n #include \"config.h\"\n-#include \"refs.h\"\n-#include \"object-store.h\"\n-#include \"commit.h\"\n-#include \"diff.h\"\n-#include \"revision.h\"\n-#include \"tag.h\"\n-#include \"string-list.h\"\n-#include \"branch.h\"\n #include \"fmt-merge-msg.h\"\n-#include \"gpg-interface.h\"\n-#include \"repository.h\"\n-#include \"commit-reach.h\"\n+#include \"parse-options.h\"\n \n static const char * const fmt_merge_msg_usage[] = {\n \tN_(\"git fmt-merge-msg [-m <message>] [--log[=<n>] | --no-log] [--file <file>]\"),\n \tNULL\n };\n \n-static int use_branch_desc;\n-\n-int fmt_merge_msg_config(const char *key, const char *value, void *cb)\n-{\n-\tif (!strcmp(key, \"merge.log\") || !strcmp(key, \"merge.summary\")) {\n-\t\tint is_bool;\n-\t\tmerge_log_config = git_config_bool_or_int(key, value, &is_bool);\n-\t\tif (!is_bool && merge_log_config < 0)\n-\t\t\treturn error(\"%s: negative length %s\", key, value);\n-\t\tif (is_bool && merge_log_config)\n-\t\t\tmerge_log_config = DEFAULT_MERGE_LOG_LEN;\n-\t} else if (!strcmp(key, \"merge.branchdesc\")) {\n-\t\tuse_branch_desc = git_config_bool(key, value);\n-\t} else {\n-\t\treturn git_default_config(key, value, cb);\n-\t}\n-\treturn 0;\n-}\n-\n-/* merge data per repository where the merged tips came from */\n-struct src_data {\n-\tstruct string_list branch, tag, r_branch, generic;\n-\tint head_status;\n-};\n-\n-struct origin_data {\n-\tstruct object_id oid;\n-\tunsigned is_local_branch:1;\n-};\n-\n-static void init_src_data(struct src_data *data)\n-{\n-\tdata->branch.strdup_strings = 1;\n-\tdata->tag.strdup_strings = 1;\n-\tdata->r_branch.strdup_strings = 1;\n-\tdata->generic.strdup_strings = 1;\n-}\n-\n-static struct string_list srcs = STRING_LIST_INIT_DUP;\n-static struct string_list origins = STRING_LIST_INIT_DUP;\n-\n-struct merge_parents {\n-\tint alloc, nr;\n-\tstruct merge_parent {\n-\t\tstruct object_id given;\n-\t\tstruct object_id commit;\n-\t\tunsigned char used;\n-\t} *item;\n-};\n-\n-/*\n- * I know, I know, this is inefficient, but you won't be pulling and merging\n- * hundreds of heads at a time anyway.\n- */\n-static struct merge_parent *find_merge_parent(struct merge_parents *table,\n-\t\t\t\t\t      struct object_id *given,\n-\t\t\t\t\t      struct object_id *commit)\n-{\n-\tint i;\n-\tfor (i = 0; i < table->nr; i++) {\n-\t\tif (given && !oideq(&table->item[i].given, given))\n-\t\t\tcontinue;\n-\t\tif (commit && !oideq(&table->item[i].commit, commit))\n-\t\t\tcontinue;\n-\t\treturn &table->item[i];\n-\t}\n-\treturn NULL;\n-}\n-\n-static void add_merge_parent(struct merge_parents *table,\n-\t\t\t     struct object_id *given,\n-\t\t\t     struct object_id *commit)\n-{\n-\tif (table->nr && find_merge_parent(table, given, commit))\n-\t\treturn;\n-\tALLOC_GROW(table->item, table->nr + 1, table->alloc);\n-\toidcpy(&table->item[table->nr].given, given);\n-\toidcpy(&table->item[table->nr].commit, commit);\n-\ttable->item[table->nr].used = 0;\n-\ttable->nr++;\n-}\n-\n-static int handle_line(char *line, struct merge_parents *merge_parents)\n-{\n-\tint i, len = strlen(line);\n-\tstruct origin_data *origin_data;\n-\tchar *src;\n-\tconst char *origin, *tag_name;\n-\tstruct src_data *src_data;\n-\tstruct string_list_item *item;\n-\tint pulling_head = 0;\n-\tstruct object_id oid;\n-\tconst unsigned hexsz = the_hash_algo->hexsz;\n-\n-\tif (len < hexsz + 3 || line[hexsz] != '\\t')\n-\t\treturn 1;\n-\n-\tif (starts_with(line + hexsz + 1, \"not-for-merge\"))\n-\t\treturn 0;\n-\n-\tif (line[hexsz + 1] != '\\t')\n-\t\treturn 2;\n-\n-\ti = get_oid_hex(line, &oid);\n-\tif (i)\n-\t\treturn 3;\n-\n-\tif (!find_merge_parent(merge_parents, &oid, NULL))\n-\t\treturn 0; /* subsumed by other parents */\n-\n-\torigin_data = xcalloc(1, sizeof(struct origin_data));\n-\toidcpy(&origin_data->oid, &oid);\n-\n-\tif (line[len - 1] == '\\n')\n-\t\tline[len - 1] = 0;\n-\tline += hexsz + 2;\n-\n-\t/*\n-\t * At this point, line points at the beginning of comment e.g.\n-\t * \"branch 'frotz' of git://that/repository.git\".\n-\t * Find the repository name and point it with src.\n-\t */\n-\tsrc = strstr(line, \" of \");\n-\tif (src) {\n-\t\t*src = 0;\n-\t\tsrc += 4;\n-\t\tpulling_head = 0;\n-\t} else {\n-\t\tsrc = line;\n-\t\tpulling_head = 1;\n-\t}\n-\n-\titem = unsorted_string_list_lookup(&srcs, src);\n-\tif (!item) {\n-\t\titem = string_list_append(&srcs, src);\n-\t\titem->util = xcalloc(1, sizeof(struct src_data));\n-\t\tinit_src_data(item->util);\n-\t}\n-\tsrc_data = item->util;\n-\n-\tif (pulling_head) {\n-\t\torigin = src;\n-\t\tsrc_data->head_status |= 1;\n-\t} else if (skip_prefix(line, \"branch \", &origin)) {\n-\t\torigin_data->is_local_branch = 1;\n-\t\tstring_list_append(&src_data->branch, origin);\n-\t\tsrc_data->head_status |= 2;\n-\t} else if (skip_prefix(line, \"tag \", &tag_name)) {\n-\t\torigin = line;\n-\t\tstring_list_append(&src_data->tag, tag_name);\n-\t\tsrc_data->head_status |= 2;\n-\t} else if (skip_prefix(line, \"remote-tracking branch \", &origin)) {\n-\t\tstring_list_append(&src_data->r_branch, origin);\n-\t\tsrc_data->head_status |= 2;\n-\t} else {\n-\t\torigin = src;\n-\t\tstring_list_append(&src_data->generic, line);\n-\t\tsrc_data->head_status |= 2;\n-\t}\n-\n-\tif (!strcmp(\".\", src) || !strcmp(src, origin)) {\n-\t\tint len = strlen(origin);\n-\t\tif (origin[0] == '\\'' && origin[len - 1] == '\\'')\n-\t\t\torigin = xmemdupz(origin + 1, len - 2);\n-\t} else\n-\t\torigin = xstrfmt(\"%s of %s\", origin, src);\n-\tif (strcmp(\".\", src))\n-\t\torigin_data->is_local_branch = 0;\n-\tstring_list_append(&origins, origin)->util = origin_data;\n-\treturn 0;\n-}\n-\n-static void print_joined(const char *singular, const char *plural,\n-\t\tstruct string_list *list, struct strbuf *out)\n-{\n-\tif (list->nr == 0)\n-\t\treturn;\n-\tif (list->nr == 1) {\n-\t\tstrbuf_addf(out, \"%s%s\", singular, list->items[0].string);\n-\t} else {\n-\t\tint i;\n-\t\tstrbuf_addstr(out, plural);\n-\t\tfor (i = 0; i < list->nr - 1; i++)\n-\t\t\tstrbuf_addf(out, \"%s%s\", i > 0 ? \", \" : \"\",\n-\t\t\t\t    list->items[i].string);\n-\t\tstrbuf_addf(out, \" and %s\", list->items[list->nr - 1].string);\n-\t}\n-}\n-\n-static void add_branch_desc(struct strbuf *out, const char *name)\n-{\n-\tstruct strbuf desc = STRBUF_INIT;\n-\n-\tif (!read_branch_desc(&desc, name)) {\n-\t\tconst char *bp = desc.buf;\n-\t\twhile (*bp) {\n-\t\t\tconst char *ep = strchrnul(bp, '\\n');\n-\t\t\tif (*ep)\n-\t\t\t\tep++;\n-\t\t\tstrbuf_addf(out, \"  : %.*s\", (int)(ep - bp), bp);\n-\t\t\tbp = ep;\n-\t\t}\n-\t\tstrbuf_complete_line(out);\n-\t}\n-\tstrbuf_release(&desc);\n-}\n-\n-#define util_as_integral(elem) ((intptr_t)((elem)->util))\n-\n-static void record_person_from_buf(int which, struct string_list *people,\n-\t\t\t\t   const char *buffer)\n-{\n-\tchar *name_buf, *name, *name_end;\n-\tstruct string_list_item *elem;\n-\tconst char *field;\n-\n-\tfield = (which == 'a') ? \"\\nauthor \" : \"\\ncommitter \";\n-\tname = strstr(buffer, field);\n-\tif (!name)\n-\t\treturn;\n-\tname += strlen(field);\n-\tname_end = strchrnul(name, '<');\n-\tif (*name_end)\n-\t\tname_end--;\n-\twhile (isspace(*name_end) && name <= name_end)\n-\t\tname_end--;\n-\tif (name_end < name)\n-\t\treturn;\n-\tname_buf = xmemdupz(name, name_end - name + 1);\n-\n-\telem = string_list_lookup(people, name_buf);\n-\tif (!elem) {\n-\t\telem = string_list_insert(people, name_buf);\n-\t\telem->util = (void *)0;\n-\t}\n-\telem->util = (void*)(util_as_integral(elem) + 1);\n-\tfree(name_buf);\n-}\n-\n-\n-static void record_person(int which, struct string_list *people,\n-\t\t\t  struct commit *commit)\n-{\n-\tconst char *buffer = get_commit_buffer(commit, NULL);\n-\trecord_person_from_buf(which, people, buffer);\n-\tunuse_commit_buffer(commit, buffer);\n-}\n-\n-static int cmp_string_list_util_as_integral(const void *a_, const void *b_)\n-{\n-\tconst struct string_list_item *a = a_, *b = b_;\n-\treturn util_as_integral(b) - util_as_integral(a);\n-}\n-\n-static void add_people_count(struct strbuf *out, struct string_list *people)\n-{\n-\tif (people->nr == 1)\n-\t\tstrbuf_addstr(out, people->items[0].string);\n-\telse if (people->nr == 2)\n-\t\tstrbuf_addf(out, \"%s (%d) and %s (%d)\",\n-\t\t\t    people->items[0].string,\n-\t\t\t    (int)util_as_integral(&people->items[0]),\n-\t\t\t    people->items[1].string,\n-\t\t\t    (int)util_as_integral(&people->items[1]));\n-\telse if (people->nr)\n-\t\tstrbuf_addf(out, \"%s (%d) and others\",\n-\t\t\t    people->items[0].string,\n-\t\t\t    (int)util_as_integral(&people->items[0]));\n-}\n-\n-static void credit_people(struct strbuf *out,\n-\t\t\t  struct string_list *them,\n-\t\t\t  int kind)\n-{\n-\tconst char *label;\n-\tconst char *me;\n-\n-\tif (kind == 'a') {\n-\t\tlabel = \"By\";\n-\t\tme = git_author_info(IDENT_NO_DATE);\n-\t} else {\n-\t\tlabel = \"Via\";\n-\t\tme = git_committer_info(IDENT_NO_DATE);\n-\t}\n-\n-\tif (!them->nr ||\n-\t    (them->nr == 1 &&\n-\t     me &&\n-\t     skip_prefix(me, them->items->string, &me) &&\n-\t     starts_with(me, \" <\")))\n-\t\treturn;\n-\tstrbuf_addf(out, \"\\n%c %s \", comment_line_char, label);\n-\tadd_people_count(out, them);\n-}\n-\n-static void add_people_info(struct strbuf *out,\n-\t\t\t    struct string_list *authors,\n-\t\t\t    struct string_list *committers)\n-{\n-\tQSORT(authors->items, authors->nr,\n-\t      cmp_string_list_util_as_integral);\n-\tQSORT(committers->items, committers->nr,\n-\t      cmp_string_list_util_as_integral);\n-\n-\tcredit_people(out, authors, 'a');\n-\tcredit_people(out, committers, 'c');\n-}\n-\n-static void shortlog(const char *name,\n-\t\t     struct origin_data *origin_data,\n-\t\t     struct commit *head,\n-\t\t     struct rev_info *rev,\n-\t\t     struct fmt_merge_msg_opts *opts,\n-\t\t     struct strbuf *out)\n-{\n-\tint i, count = 0;\n-\tstruct commit *commit;\n-\tstruct object *branch;\n-\tstruct string_list subjects = STRING_LIST_INIT_DUP;\n-\tstruct string_list authors = STRING_LIST_INIT_DUP;\n-\tstruct string_list committers = STRING_LIST_INIT_DUP;\n-\tint flags = UNINTERESTING | TREESAME | SEEN | SHOWN | ADDED;\n-\tstruct strbuf sb = STRBUF_INIT;\n-\tconst struct object_id *oid = &origin_data->oid;\n-\tint limit = opts->shortlog_len;\n-\n-\tbranch = deref_tag(the_repository, parse_object(the_repository, oid),\n-\t\t\t   oid_to_hex(oid),\n-\t\t\t   the_hash_algo->hexsz);\n-\tif (!branch || branch->type != OBJ_COMMIT)\n-\t\treturn;\n-\n-\tsetup_revisions(0, NULL, rev, NULL);\n-\tadd_pending_object(rev, branch, name);\n-\tadd_pending_object(rev, &head->object, \"^HEAD\");\n-\thead->object.flags |= UNINTERESTING;\n-\tif (prepare_revision_walk(rev))\n-\t\tdie(\"revision walk setup failed\");\n-\twhile ((commit = get_revision(rev)) != NULL) {\n-\t\tstruct pretty_print_context ctx = {0};\n-\n-\t\tif (commit->parents && commit->parents->next) {\n-\t\t\t/* do not list a merge but count committer */\n-\t\t\tif (opts->credit_people)\n-\t\t\t\trecord_person('c', &committers, commit);\n-\t\t\tcontinue;\n-\t\t}\n-\t\tif (!count && opts->credit_people)\n-\t\t\t/* the 'tip' committer */\n-\t\t\trecord_person('c', &committers, commit);\n-\t\tif (opts->credit_people)\n-\t\t\trecord_person('a', &authors, commit);\n-\t\tcount++;\n-\t\tif (subjects.nr > limit)\n-\t\t\tcontinue;\n-\n-\t\tformat_commit_message(commit, \"%s\", &sb, &ctx);\n-\t\tstrbuf_ltrim(&sb);\n-\n-\t\tif (!sb.len)\n-\t\t\tstring_list_append(&subjects,\n-\t\t\t\t\t   oid_to_hex(&commit->object.oid));\n-\t\telse\n-\t\t\tstring_list_append_nodup(&subjects,\n-\t\t\t\t\t\t strbuf_detach(&sb, NULL));\n-\t}\n-\n-\tif (opts->credit_people)\n-\t\tadd_people_info(out, &authors, &committers);\n-\tif (count > limit)\n-\t\tstrbuf_addf(out, \"\\n* %s: (%d commits)\\n\", name, count);\n-\telse\n-\t\tstrbuf_addf(out, \"\\n* %s:\\n\", name);\n-\n-\tif (origin_data->is_local_branch && use_branch_desc)\n-\t\tadd_branch_desc(out, name);\n-\n-\tfor (i = 0; i < subjects.nr; i++)\n-\t\tif (i >= limit)\n-\t\t\tstrbuf_addstr(out, \"  ...\\n\");\n-\t\telse\n-\t\t\tstrbuf_addf(out, \"  %s\\n\", subjects.items[i].string);\n-\n-\tclear_commit_marks((struct commit *)branch, flags);\n-\tclear_commit_marks(head, flags);\n-\tfree_commit_list(rev->commits);\n-\trev->commits = NULL;\n-\trev->pending.nr = 0;\n-\n-\tstring_list_clear(&authors, 0);\n-\tstring_list_clear(&committers, 0);\n-\tstring_list_clear(&subjects, 0);\n-}\n-\n-static void fmt_merge_msg_title(struct strbuf *out,\n-\t\t\t\tconst char *current_branch)\n-{\n-\tint i = 0;\n-\tchar *sep = \"\";\n-\n-\tstrbuf_addstr(out, \"Merge \");\n-\tfor (i = 0; i < srcs.nr; i++) {\n-\t\tstruct src_data *src_data = srcs.items[i].util;\n-\t\tconst char *subsep = \"\";\n-\n-\t\tstrbuf_addstr(out, sep);\n-\t\tsep = \"; \";\n-\n-\t\tif (src_data->head_status == 1) {\n-\t\t\tstrbuf_addstr(out, srcs.items[i].string);\n-\t\t\tcontinue;\n-\t\t}\n-\t\tif (src_data->head_status == 3) {\n-\t\t\tsubsep = \", \";\n-\t\t\tstrbuf_addstr(out, \"HEAD\");\n-\t\t}\n-\t\tif (src_data->branch.nr) {\n-\t\t\tstrbuf_addstr(out, subsep);\n-\t\t\tsubsep = \", \";\n-\t\t\tprint_joined(\"branch \", \"branches \", &src_data->branch,\n-\t\t\t\t\tout);\n-\t\t}\n-\t\tif (src_data->r_branch.nr) {\n-\t\t\tstrbuf_addstr(out, subsep);\n-\t\t\tsubsep = \", \";\n-\t\t\tprint_joined(\"remote-tracking branch \", \"remote-tracking branches \",\n-\t\t\t\t\t&src_data->r_branch, out);\n-\t\t}\n-\t\tif (src_data->tag.nr) {\n-\t\t\tstrbuf_addstr(out, subsep);\n-\t\t\tsubsep = \", \";\n-\t\t\tprint_joined(\"tag \", \"tags \", &src_data->tag, out);\n-\t\t}\n-\t\tif (src_data->generic.nr) {\n-\t\t\tstrbuf_addstr(out, subsep);\n-\t\t\tprint_joined(\"commit \", \"commits \", &src_data->generic,\n-\t\t\t\t\tout);\n-\t\t}\n-\t\tif (strcmp(\".\", srcs.items[i].string))\n-\t\t\tstrbuf_addf(out, \" of %s\", srcs.items[i].string);\n-\t}\n-\n-\tif (!strcmp(\"master\", current_branch))\n-\t\tstrbuf_addch(out, '\\n');\n-\telse\n-\t\tstrbuf_addf(out, \" into %s\\n\", current_branch);\n-}\n-\n-static void fmt_tag_signature(struct strbuf *tagbuf,\n-\t\t\t      struct strbuf *sig,\n-\t\t\t      const char *buf,\n-\t\t\t      unsigned long len)\n-{\n-\tconst char *tag_body = strstr(buf, \"\\n\\n\");\n-\tif (tag_body) {\n-\t\ttag_body += 2;\n-\t\tstrbuf_add(tagbuf, tag_body, buf + len - tag_body);\n-\t}\n-\tstrbuf_complete_line(tagbuf);\n-\tif (sig->len) {\n-\t\tstrbuf_addch(tagbuf, '\\n');\n-\t\tstrbuf_add_commented_lines(tagbuf, sig->buf, sig->len);\n-\t}\n-}\n-\n-static void fmt_merge_msg_sigs(struct strbuf *out)\n-{\n-\tint i, tag_number = 0, first_tag = 0;\n-\tstruct strbuf tagbuf = STRBUF_INIT;\n-\n-\tfor (i = 0; i < origins.nr; i++) {\n-\t\tstruct object_id *oid = origins.items[i].util;\n-\t\tenum object_type type;\n-\t\tunsigned long size, len;\n-\t\tchar *buf = read_object_file(oid, &type, &size);\n-\t\tstruct strbuf sig = STRBUF_INIT;\n-\n-\t\tif (!buf || type != OBJ_TAG)\n-\t\t\tgoto next;\n-\t\tlen = parse_signature(buf, size);\n-\n-\t\tif (size == len)\n-\t\t\t; /* merely annotated */\n-\t\telse if (verify_signed_buffer(buf, len, buf + len, size - len, &sig, NULL)) {\n-\t\t\tif (!sig.len)\n-\t\t\t\tstrbuf_addstr(&sig, \"gpg verification failed.\\n\");\n-\t\t}\n-\n-\t\tif (!tag_number++) {\n-\t\t\tfmt_tag_signature(&tagbuf, &sig, buf, len);\n-\t\t\tfirst_tag = i;\n-\t\t} else {\n-\t\t\tif (tag_number == 2) {\n-\t\t\t\tstruct strbuf tagline = STRBUF_INIT;\n-\t\t\t\tstrbuf_addch(&tagline, '\\n');\n-\t\t\t\tstrbuf_add_commented_lines(&tagline,\n-\t\t\t\t\t\torigins.items[first_tag].string,\n-\t\t\t\t\t\tstrlen(origins.items[first_tag].string));\n-\t\t\t\tstrbuf_insert(&tagbuf, 0, tagline.buf,\n-\t\t\t\t\t      tagline.len);\n-\t\t\t\tstrbuf_release(&tagline);\n-\t\t\t}\n-\t\t\tstrbuf_addch(&tagbuf, '\\n');\n-\t\t\tstrbuf_add_commented_lines(&tagbuf,\n-\t\t\t\t\torigins.items[i].string,\n-\t\t\t\t\tstrlen(origins.items[i].string));\n-\t\t\tfmt_tag_signature(&tagbuf, &sig, buf, len);\n-\t\t}\n-\t\tstrbuf_release(&sig);\n-\tnext:\n-\t\tfree(buf);\n-\t}\n-\tif (tagbuf.len) {\n-\t\tstrbuf_addch(out, '\\n');\n-\t\tstrbuf_addbuf(out, &tagbuf);\n-\t}\n-\tstrbuf_release(&tagbuf);\n-}\n-\n-static void find_merge_parents(struct merge_parents *result,\n-\t\t\t       struct strbuf *in, struct object_id *head)\n-{\n-\tstruct commit_list *parents;\n-\tstruct commit *head_commit;\n-\tint pos = 0, i, j;\n-\n-\tparents = NULL;\n-\twhile (pos < in->len) {\n-\t\tint len;\n-\t\tchar *p = in->buf + pos;\n-\t\tchar *newline = strchr(p, '\\n');\n-\t\tconst char *q;\n-\t\tstruct object_id oid;\n-\t\tstruct commit *parent;\n-\t\tstruct object *obj;\n-\n-\t\tlen = newline ? newline - p : strlen(p);\n-\t\tpos += len + !!newline;\n-\n-\t\tif (parse_oid_hex(p, &oid, &q) ||\n-\t\t    q[0] != '\\t' ||\n-\t\t    q[1] != '\\t')\n-\t\t\tcontinue; /* skip not-for-merge */\n-\t\t/*\n-\t\t * Do not use get_merge_parent() here; we do not have\n-\t\t * \"name\" here and we do not want to contaminate its\n-\t\t * util field yet.\n-\t\t */\n-\t\tobj = parse_object(the_repository, &oid);\n-\t\tparent = (struct commit *)peel_to_type(NULL, 0, obj, OBJ_COMMIT);\n-\t\tif (!parent)\n-\t\t\tcontinue;\n-\t\tcommit_list_insert(parent, &parents);\n-\t\tadd_merge_parent(result, &obj->oid, &parent->object.oid);\n-\t}\n-\thead_commit = lookup_commit(the_repository, head);\n-\tif (head_commit)\n-\t\tcommit_list_insert(head_commit, &parents);\n-\treduce_heads_replace(&parents);\n-\n-\twhile (parents) {\n-\t\tstruct commit *cmit = pop_commit(&parents);\n-\t\tfor (i = 0; i < result->nr; i++)\n-\t\t\tif (oideq(&result->item[i].commit, &cmit->object.oid))\n-\t\t\t\tresult->item[i].used = 1;\n-\t}\n-\n-\tfor (i = j = 0; i < result->nr; i++) {\n-\t\tif (result->item[i].used) {\n-\t\t\tif (i != j)\n-\t\t\t\tresult->item[j] = result->item[i];\n-\t\t\tj++;\n-\t\t}\n-\t}\n-\tresult->nr = j;\n-}\n-\n-int fmt_merge_msg(struct strbuf *in, struct strbuf *out,\n-\t\t  struct fmt_merge_msg_opts *opts)\n-{\n-\tint i = 0, pos = 0;\n-\tstruct object_id head_oid;\n-\tconst char *current_branch;\n-\tvoid *current_branch_to_free;\n-\tstruct merge_parents merge_parents;\n-\n-\tmemset(&merge_parents, 0, sizeof(merge_parents));\n-\n-\t/* get current branch */\n-\tcurrent_branch = current_branch_to_free =\n-\t\tresolve_refdup(\"HEAD\", RESOLVE_REF_READING, &head_oid, NULL);\n-\tif (!current_branch)\n-\t\tdie(\"No current branch\");\n-\tif (starts_with(current_branch, \"refs/heads/\"))\n-\t\tcurrent_branch += 11;\n-\n-\tfind_merge_parents(&merge_parents, in, &head_oid);\n-\n-\t/* get a line */\n-\twhile (pos < in->len) {\n-\t\tint len;\n-\t\tchar *newline, *p = in->buf + pos;\n-\n-\t\tnewline = strchr(p, '\\n');\n-\t\tlen = newline ? newline - p : strlen(p);\n-\t\tpos += len + !!newline;\n-\t\ti++;\n-\t\tp[len] = 0;\n-\t\tif (handle_line(p, &merge_parents))\n-\t\t\tdie(\"error in line %d: %.*s\", i, len, p);\n-\t}\n-\n-\tif (opts->add_title && srcs.nr)\n-\t\tfmt_merge_msg_title(out, current_branch);\n-\n-\tif (origins.nr)\n-\t\tfmt_merge_msg_sigs(out);\n-\n-\tif (opts->shortlog_len) {\n-\t\tstruct commit *head;\n-\t\tstruct rev_info rev;\n-\n-\t\thead = lookup_commit_or_die(&head_oid, \"HEAD\");\n-\t\trepo_init_revisions(the_repository, &rev, NULL);\n-\t\trev.commit_format = CMIT_FMT_ONELINE;\n-\t\trev.ignore_merges = 1;\n-\t\trev.limited = 1;\n-\n-\t\tstrbuf_complete_line(out);\n-\n-\t\tfor (i = 0; i < origins.nr; i++)\n-\t\t\tshortlog(origins.items[i].string,\n-\t\t\t\t origins.items[i].util,\n-\t\t\t\t head, &rev, opts, out);\n-\t}\n-\n-\tstrbuf_complete_line(out);\n-\tfree(current_branch_to_free);\n-\tfree(merge_parents.item);\n-\treturn 0;\n-}\n-\n int cmd_fmt_merge_msg(int argc, const char **argv, const char *prefix)\n {\n \tconst char *inpath = NULL;\ndiff --git a/fmt-merge-msg.c b/fmt-merge-msg.c\nnew file mode 100644\nindex 0000000000..5bcd4fd1c6\n--- /dev/null\n+++ b/fmt-merge-msg.c\n@@ -0,0 +1,653 @@\n+#include \"config.h\"\n+#include \"refs.h\"\n+#include \"object-store.h\"\n+#include \"diff.h\"\n+#include \"revision.h\"\n+#include \"tag.h\"\n+#include \"string-list.h\"\n+#include \"branch.h\"\n+#include \"fmt-merge-msg.h\"\n+#include \"commit-reach.h\"\n+\n+static int use_branch_desc;\n+\n+int fmt_merge_msg_config(const char *key, const char *value, void *cb)\n+{\n+\tif (!strcmp(key, \"merge.log\") || !strcmp(key, \"merge.summary\")) {\n+\t\tint is_bool;\n+\t\tmerge_log_config = git_config_bool_or_int(key, value, &is_bool);\n+\t\tif (!is_bool && merge_log_config < 0)\n+\t\t\treturn error(\"%s: negative length %s\", key, value);\n+\t\tif (is_bool && merge_log_config)\n+\t\t\tmerge_log_config = DEFAULT_MERGE_LOG_LEN;\n+\t} else if (!strcmp(key, \"merge.branchdesc\")) {\n+\t\tuse_branch_desc = git_config_bool(key, value);\n+\t} else {\n+\t\treturn git_default_config(key, value, cb);\n+\t}\n+\treturn 0;\n+}\n+\n+/* merge data per repository where the merged tips came from */\n+struct src_data {\n+\tstruct string_list branch, tag, r_branch, generic;\n+\tint head_status;\n+};\n+\n+struct origin_data {\n+\tstruct object_id oid;\n+\tunsigned is_local_branch:1;\n+};\n+\n+static void init_src_data(struct src_data *data)\n+{\n+\tdata->branch.strdup_strings = 1;\n+\tdata->tag.strdup_strings = 1;\n+\tdata->r_branch.strdup_strings = 1;\n+\tdata->generic.strdup_strings = 1;\n+}\n+\n+static struct string_list srcs = STRING_LIST_INIT_DUP;\n+static struct string_list origins = STRING_LIST_INIT_DUP;\n+\n+struct merge_parents {\n+\tint alloc, nr;\n+\tstruct merge_parent {\n+\t\tstruct object_id given;\n+\t\tstruct object_id commit;\n+\t\tunsigned char used;\n+\t} *item;\n+};\n+\n+/*\n+ * I know, I know, this is inefficient, but you won't be pulling and merging\n+ * hundreds of heads at a time anyway.\n+ */\n+static struct merge_parent *find_merge_parent(struct merge_parents *table,\n+\t\t\t\t\t      struct object_id *given,\n+\t\t\t\t\t      struct object_id *commit)\n+{\n+\tint i;\n+\tfor (i = 0; i < table->nr; i++) {\n+\t\tif (given && !oideq(&table->item[i].given, given))\n+\t\t\tcontinue;\n+\t\tif (commit && !oideq(&table->item[i].commit, commit))\n+\t\t\tcontinue;\n+\t\treturn &table->item[i];\n+\t}\n+\treturn NULL;\n+}\n+\n+static void add_merge_parent(struct merge_parents *table,\n+\t\t\t     struct object_id *given,\n+\t\t\t     struct object_id *commit)\n+{\n+\tif (table->nr && find_merge_parent(table, given, commit))\n+\t\treturn;\n+\tALLOC_GROW(table->item, table->nr + 1, table->alloc);\n+\toidcpy(&table->item[table->nr].given, given);\n+\toidcpy(&table->item[table->nr].commit, commit);\n+\ttable->item[table->nr].used = 0;\n+\ttable->nr++;\n+}\n+\n+static int handle_line(char *line, struct merge_parents *merge_parents)\n+{\n+\tint i, len = strlen(line);\n+\tstruct origin_data *origin_data;\n+\tchar *src;\n+\tconst char *origin, *tag_name;\n+\tstruct src_data *src_data;\n+\tstruct string_list_item *item;\n+\tint pulling_head = 0;\n+\tstruct object_id oid;\n+\tconst unsigned hexsz = the_hash_algo->hexsz;\n+\n+\tif (len < hexsz + 3 || line[hexsz] != '\\t')\n+\t\treturn 1;\n+\n+\tif (starts_with(line + hexsz + 1, \"not-for-merge\"))\n+\t\treturn 0;\n+\n+\tif (line[hexsz + 1] != '\\t')\n+\t\treturn 2;\n+\n+\ti = get_oid_hex(line, &oid);\n+\tif (i)\n+\t\treturn 3;\n+\n+\tif (!find_merge_parent(merge_parents, &oid, NULL))\n+\t\treturn 0; /* subsumed by other parents */\n+\n+\torigin_data = xcalloc(1, sizeof(struct origin_data));\n+\toidcpy(&origin_data->oid, &oid);\n+\n+\tif (line[len - 1] == '\\n')\n+\t\tline[len - 1] = 0;\n+\tline += hexsz + 2;\n+\n+\t/*\n+\t * At this point, line points at the beginning of comment e.g.\n+\t * \"branch 'frotz' of git://that/repository.git\".\n+\t * Find the repository name and point it with src.\n+\t */\n+\tsrc = strstr(line, \" of \");\n+\tif (src) {\n+\t\t*src = 0;\n+\t\tsrc += 4;\n+\t\tpulling_head = 0;\n+\t} else {\n+\t\tsrc = line;\n+\t\tpulling_head = 1;\n+\t}\n+\n+\titem = unsorted_string_list_lookup(&srcs, src);\n+\tif (!item) {\n+\t\titem = string_list_append(&srcs, src);\n+\t\titem->util = xcalloc(1, sizeof(struct src_data));\n+\t\tinit_src_data(item->util);\n+\t}\n+\tsrc_data = item->util;\n+\n+\tif (pulling_head) {\n+\t\torigin = src;\n+\t\tsrc_data->head_status |= 1;\n+\t} else if (skip_prefix(line, \"branch \", &origin)) {\n+\t\torigin_data->is_local_branch = 1;\n+\t\tstring_list_append(&src_data->branch, origin);\n+\t\tsrc_data->head_status |= 2;\n+\t} else if (skip_prefix(line, \"tag \", &tag_name)) {\n+\t\torigin = line;\n+\t\tstring_list_append(&src_data->tag, tag_name);\n+\t\tsrc_data->head_status |= 2;\n+\t} else if (skip_prefix(line, \"remote-tracking branch \", &origin)) {\n+\t\tstring_list_append(&src_data->r_branch, origin);\n+\t\tsrc_data->head_status |= 2;\n+\t} else {\n+\t\torigin = src;\n+\t\tstring_list_append(&src_data->generic, line);\n+\t\tsrc_data->head_status |= 2;\n+\t}\n+\n+\tif (!strcmp(\".\", src) || !strcmp(src, origin)) {\n+\t\tint len = strlen(origin);\n+\t\tif (origin[0] == '\\'' && origin[len - 1] == '\\'')\n+\t\t\torigin = xmemdupz(origin + 1, len - 2);\n+\t} else\n+\t\torigin = xstrfmt(\"%s of %s\", origin, src);\n+\tif (strcmp(\".\", src))\n+\t\torigin_data->is_local_branch = 0;\n+\tstring_list_append(&origins, origin)->util = origin_data;\n+\treturn 0;\n+}\n+\n+static void print_joined(const char *singular, const char *plural,\n+\t\tstruct string_list *list, struct strbuf *out)\n+{\n+\tif (list->nr == 0)\n+\t\treturn;\n+\tif (list->nr == 1) {\n+\t\tstrbuf_addf(out, \"%s%s\", singular, list->items[0].string);\n+\t} else {\n+\t\tint i;\n+\t\tstrbuf_addstr(out, plural);\n+\t\tfor (i = 0; i < list->nr - 1; i++)\n+\t\t\tstrbuf_addf(out, \"%s%s\", i > 0 ? \", \" : \"\",\n+\t\t\t\t    list->items[i].string);\n+\t\tstrbuf_addf(out, \" and %s\", list->items[list->nr - 1].string);\n+\t}\n+}\n+\n+static void add_branch_desc(struct strbuf *out, const char *name)\n+{\n+\tstruct strbuf desc = STRBUF_INIT;\n+\n+\tif (!read_branch_desc(&desc, name)) {\n+\t\tconst char *bp = desc.buf;\n+\t\twhile (*bp) {\n+\t\t\tconst char *ep = strchrnul(bp, '\\n');\n+\t\t\tif (*ep)\n+\t\t\t\tep++;\n+\t\t\tstrbuf_addf(out, \"  : %.*s\", (int)(ep - bp), bp);\n+\t\t\tbp = ep;\n+\t\t}\n+\t\tstrbuf_complete_line(out);\n+\t}\n+\tstrbuf_release(&desc);\n+}\n+\n+#define util_as_integral(elem) ((intptr_t)((elem)->util))\n+\n+static void record_person_from_buf(int which, struct string_list *people,\n+\t\t\t\t   const char *buffer)\n+{\n+\tchar *name_buf, *name, *name_end;\n+\tstruct string_list_item *elem;\n+\tconst char *field;\n+\n+\tfield = (which == 'a') ? \"\\nauthor \" : \"\\ncommitter \";\n+\tname = strstr(buffer, field);\n+\tif (!name)\n+\t\treturn;\n+\tname += strlen(field);\n+\tname_end = strchrnul(name, '<');\n+\tif (*name_end)\n+\t\tname_end--;\n+\twhile (isspace(*name_end) && name <= name_end)\n+\t\tname_end--;\n+\tif (name_end < name)\n+\t\treturn;\n+\tname_buf = xmemdupz(name, name_end - name + 1);\n+\n+\telem = string_list_lookup(people, name_buf);\n+\tif (!elem) {\n+\t\telem = string_list_insert(people, name_buf);\n+\t\telem->util = (void *)0;\n+\t}\n+\telem->util = (void*)(util_as_integral(elem) + 1);\n+\tfree(name_buf);\n+}\n+\n+\n+static void record_person(int which, struct string_list *people,\n+\t\t\t  struct commit *commit)\n+{\n+\tconst char *buffer = get_commit_buffer(commit, NULL);\n+\trecord_person_from_buf(which, people, buffer);\n+\tunuse_commit_buffer(commit, buffer);\n+}\n+\n+static int cmp_string_list_util_as_integral(const void *a_, const void *b_)\n+{\n+\tconst struct string_list_item *a = a_, *b = b_;\n+\treturn util_as_integral(b) - util_as_integral(a);\n+}\n+\n+static void add_people_count(struct strbuf *out, struct string_list *people)\n+{\n+\tif (people->nr == 1)\n+\t\tstrbuf_addstr(out, people->items[0].string);\n+\telse if (people->nr == 2)\n+\t\tstrbuf_addf(out, \"%s (%d) and %s (%d)\",\n+\t\t\t    people->items[0].string,\n+\t\t\t    (int)util_as_integral(&people->items[0]),\n+\t\t\t    people->items[1].string,\n+\t\t\t    (int)util_as_integral(&people->items[1]));\n+\telse if (people->nr)\n+\t\tstrbuf_addf(out, \"%s (%d) and others\",\n+\t\t\t    people->items[0].string,\n+\t\t\t    (int)util_as_integral(&people->items[0]));\n+}\n+\n+static void credit_people(struct strbuf *out,\n+\t\t\t  struct string_list *them,\n+\t\t\t  int kind)\n+{\n+\tconst char *label;\n+\tconst char *me;\n+\n+\tif (kind == 'a') {\n+\t\tlabel = \"By\";\n+\t\tme = git_author_info(IDENT_NO_DATE);\n+\t} else {\n+\t\tlabel = \"Via\";\n+\t\tme = git_committer_info(IDENT_NO_DATE);\n+\t}\n+\n+\tif (!them->nr ||\n+\t    (them->nr == 1 &&\n+\t     me &&\n+\t     skip_prefix(me, them->items->string, &me) &&\n+\t     starts_with(me, \" <\")))\n+\t\treturn;\n+\tstrbuf_addf(out, \"\\n%c %s \", comment_line_char, label);\n+\tadd_people_count(out, them);\n+}\n+\n+static void add_people_info(struct strbuf *out,\n+\t\t\t    struct string_list *authors,\n+\t\t\t    struct string_list *committers)\n+{\n+\tQSORT(authors->items, authors->nr,\n+\t      cmp_string_list_util_as_integral);\n+\tQSORT(committers->items, committers->nr,\n+\t      cmp_string_list_util_as_integral);\n+\n+\tcredit_people(out, authors, 'a');\n+\tcredit_people(out, committers, 'c');\n+}\n+\n+static void shortlog(const char *name,\n+\t\t     struct origin_data *origin_data,\n+\t\t     struct commit *head,\n+\t\t     struct rev_info *rev,\n+\t\t     struct fmt_merge_msg_opts *opts,\n+\t\t     struct strbuf *out)\n+{\n+\tint i, count = 0;\n+\tstruct commit *commit;\n+\tstruct object *branch;\n+\tstruct string_list subjects = STRING_LIST_INIT_DUP;\n+\tstruct string_list authors = STRING_LIST_INIT_DUP;\n+\tstruct string_list committers = STRING_LIST_INIT_DUP;\n+\tint flags = UNINTERESTING | TREESAME | SEEN | SHOWN | ADDED;\n+\tstruct strbuf sb = STRBUF_INIT;\n+\tconst struct object_id *oid = &origin_data->oid;\n+\tint limit = opts->shortlog_len;\n+\n+\tbranch = deref_tag(the_repository, parse_object(the_repository, oid),\n+\t\t\t   oid_to_hex(oid),\n+\t\t\t   the_hash_algo->hexsz);\n+\tif (!branch || branch->type != OBJ_COMMIT)\n+\t\treturn;\n+\n+\tsetup_revisions(0, NULL, rev, NULL);\n+\tadd_pending_object(rev, branch, name);\n+\tadd_pending_object(rev, &head->object, \"^HEAD\");\n+\thead->object.flags |= UNINTERESTING;\n+\tif (prepare_revision_walk(rev))\n+\t\tdie(\"revision walk setup failed\");\n+\twhile ((commit = get_revision(rev)) != NULL) {\n+\t\tstruct pretty_print_context ctx = {0};\n+\n+\t\tif (commit->parents && commit->parents->next) {\n+\t\t\t/* do not list a merge but count committer */\n+\t\t\tif (opts->credit_people)\n+\t\t\t\trecord_person('c', &committers, commit);\n+\t\t\tcontinue;\n+\t\t}\n+\t\tif (!count && opts->credit_people)\n+\t\t\t/* the 'tip' committer */\n+\t\t\trecord_person('c', &committers, commit);\n+\t\tif (opts->credit_people)\n+\t\t\trecord_person('a', &authors, commit);\n+\t\tcount++;\n+\t\tif (subjects.nr > limit)\n+\t\t\tcontinue;\n+\n+\t\tformat_commit_message(commit, \"%s\", &sb, &ctx);\n+\t\tstrbuf_ltrim(&sb);\n+\n+\t\tif (!sb.len)\n+\t\t\tstring_list_append(&subjects,\n+\t\t\t\t\t   oid_to_hex(&commit->object.oid));\n+\t\telse\n+\t\t\tstring_list_append_nodup(&subjects,\n+\t\t\t\t\t\t strbuf_detach(&sb, NULL));\n+\t}\n+\n+\tif (opts->credit_people)\n+\t\tadd_people_info(out, &authors, &committers);\n+\tif (count > limit)\n+\t\tstrbuf_addf(out, \"\\n* %s: (%d commits)\\n\", name, count);\n+\telse\n+\t\tstrbuf_addf(out, \"\\n* %s:\\n\", name);\n+\n+\tif (origin_data->is_local_branch && use_branch_desc)\n+\t\tadd_branch_desc(out, name);\n+\n+\tfor (i = 0; i < subjects.nr; i++)\n+\t\tif (i >= limit)\n+\t\t\tstrbuf_addstr(out, \"  ...\\n\");\n+\t\telse\n+\t\t\tstrbuf_addf(out, \"  %s\\n\", subjects.items[i].string);\n+\n+\tclear_commit_marks((struct commit *)branch, flags);\n+\tclear_commit_marks(head, flags);\n+\tfree_commit_list(rev->commits);\n+\trev->commits = NULL;\n+\trev->pending.nr = 0;\n+\n+\tstring_list_clear(&authors, 0);\n+\tstring_list_clear(&committers, 0);\n+\tstring_list_clear(&subjects, 0);\n+}\n+\n+static void fmt_merge_msg_title(struct strbuf *out,\n+\t\t\t\tconst char *current_branch)\n+{\n+\tint i = 0;\n+\tchar *sep = \"\";\n+\n+\tstrbuf_addstr(out, \"Merge \");\n+\tfor (i = 0; i < srcs.nr; i++) {\n+\t\tstruct src_data *src_data = srcs.items[i].util;\n+\t\tconst char *subsep = \"\";\n+\n+\t\tstrbuf_addstr(out, sep);\n+\t\tsep = \"; \";\n+\n+\t\tif (src_data->head_status == 1) {\n+\t\t\tstrbuf_addstr(out, srcs.items[i].string);\n+\t\t\tcontinue;\n+\t\t}\n+\t\tif (src_data->head_status == 3) {\n+\t\t\tsubsep = \", \";\n+\t\t\tstrbuf_addstr(out, \"HEAD\");\n+\t\t}\n+\t\tif (src_data->branch.nr) {\n+\t\t\tstrbuf_addstr(out, subsep);\n+\t\t\tsubsep = \", \";\n+\t\t\tprint_joined(\"branch \", \"branches \", &src_data->branch,\n+\t\t\t\t\tout);\n+\t\t}\n+\t\tif (src_data->r_branch.nr) {\n+\t\t\tstrbuf_addstr(out, subsep);\n+\t\t\tsubsep = \", \";\n+\t\t\tprint_joined(\"remote-tracking branch \", \"remote-tracking branches \",\n+\t\t\t\t\t&src_data->r_branch, out);\n+\t\t}\n+\t\tif (src_data->tag.nr) {\n+\t\t\tstrbuf_addstr(out, subsep);\n+\t\t\tsubsep = \", \";\n+\t\t\tprint_joined(\"tag \", \"tags \", &src_data->tag, out);\n+\t\t}\n+\t\tif (src_data->generic.nr) {\n+\t\t\tstrbuf_addstr(out, subsep);\n+\t\t\tprint_joined(\"commit \", \"commits \", &src_data->generic,\n+\t\t\t\t\tout);\n+\t\t}\n+\t\tif (strcmp(\".\", srcs.items[i].string))\n+\t\t\tstrbuf_addf(out, \" of %s\", srcs.items[i].string);\n+\t}\n+\n+\tif (!strcmp(\"master\", current_branch))\n+\t\tstrbuf_addch(out, '\\n');\n+\telse\n+\t\tstrbuf_addf(out, \" into %s\\n\", current_branch);\n+}\n+\n+static void fmt_tag_signature(struct strbuf *tagbuf,\n+\t\t\t      struct strbuf *sig,\n+\t\t\t      const char *buf,\n+\t\t\t      unsigned long len)\n+{\n+\tconst char *tag_body = strstr(buf, \"\\n\\n\");\n+\tif (tag_body) {\n+\t\ttag_body += 2;\n+\t\tstrbuf_add(tagbuf, tag_body, buf + len - tag_body);\n+\t}\n+\tstrbuf_complete_line(tagbuf);\n+\tif (sig->len) {\n+\t\tstrbuf_addch(tagbuf, '\\n');\n+\t\tstrbuf_add_commented_lines(tagbuf, sig->buf, sig->len);\n+\t}\n+}\n+\n+static void fmt_merge_msg_sigs(struct strbuf *out)\n+{\n+\tint i, tag_number = 0, first_tag = 0;\n+\tstruct strbuf tagbuf = STRBUF_INIT;\n+\n+\tfor (i = 0; i < origins.nr; i++) {\n+\t\tstruct object_id *oid = origins.items[i].util;\n+\t\tenum object_type type;\n+\t\tunsigned long size, len;\n+\t\tchar *buf = read_object_file(oid, &type, &size);\n+\t\tstruct strbuf sig = STRBUF_INIT;\n+\n+\t\tif (!buf || type != OBJ_TAG)\n+\t\t\tgoto next;\n+\t\tlen = parse_signature(buf, size);\n+\n+\t\tif (size == len)\n+\t\t\t; /* merely annotated */\n+\t\telse if (verify_signed_buffer(buf, len, buf + len, size - len, &sig, NULL)) {\n+\t\t\tif (!sig.len)\n+\t\t\t\tstrbuf_addstr(&sig, \"gpg verification failed.\\n\");\n+\t\t}\n+\n+\t\tif (!tag_number++) {\n+\t\t\tfmt_tag_signature(&tagbuf, &sig, buf, len);\n+\t\t\tfirst_tag = i;\n+\t\t} else {\n+\t\t\tif (tag_number == 2) {\n+\t\t\t\tstruct strbuf tagline = STRBUF_INIT;\n+\t\t\t\tstrbuf_addch(&tagline, '\\n');\n+\t\t\t\tstrbuf_add_commented_lines(&tagline,\n+\t\t\t\t\t\torigins.items[first_tag].string,\n+\t\t\t\t\t\tstrlen(origins.items[first_tag].string));\n+\t\t\t\tstrbuf_insert(&tagbuf, 0, tagline.buf,\n+\t\t\t\t\t      tagline.len);\n+\t\t\t\tstrbuf_release(&tagline);\n+\t\t\t}\n+\t\t\tstrbuf_addch(&tagbuf, '\\n');\n+\t\t\tstrbuf_add_commented_lines(&tagbuf,\n+\t\t\t\t\torigins.items[i].string,\n+\t\t\t\t\tstrlen(origins.items[i].string));\n+\t\t\tfmt_tag_signature(&tagbuf, &sig, buf, len);\n+\t\t}\n+\t\tstrbuf_release(&sig);\n+\tnext:\n+\t\tfree(buf);\n+\t}\n+\tif (tagbuf.len) {\n+\t\tstrbuf_addch(out, '\\n');\n+\t\tstrbuf_addbuf(out, &tagbuf);\n+\t}\n+\tstrbuf_release(&tagbuf);\n+}\n+\n+static void find_merge_parents(struct merge_parents *result,\n+\t\t\t       struct strbuf *in, struct object_id *head)\n+{\n+\tstruct commit_list *parents;\n+\tstruct commit *head_commit;\n+\tint pos = 0, i, j;\n+\n+\tparents = NULL;\n+\twhile (pos < in->len) {\n+\t\tint len;\n+\t\tchar *p = in->buf + pos;\n+\t\tchar *newline = strchr(p, '\\n');\n+\t\tconst char *q;\n+\t\tstruct object_id oid;\n+\t\tstruct commit *parent;\n+\t\tstruct object *obj;\n+\n+\t\tlen = newline ? newline - p : strlen(p);\n+\t\tpos += len + !!newline;\n+\n+\t\tif (parse_oid_hex(p, &oid, &q) ||\n+\t\t    q[0] != '\\t' ||\n+\t\t    q[1] != '\\t')\n+\t\t\tcontinue; /* skip not-for-merge */\n+\t\t/*\n+\t\t * Do not use get_merge_parent() here; we do not have\n+\t\t * \"name\" here and we do not want to contaminate its\n+\t\t * util field yet.\n+\t\t */\n+\t\tobj = parse_object(the_repository, &oid);\n+\t\tparent = (struct commit *)peel_to_type(NULL, 0, obj, OBJ_COMMIT);\n+\t\tif (!parent)\n+\t\t\tcontinue;\n+\t\tcommit_list_insert(parent, &parents);\n+\t\tadd_merge_parent(result, &obj->oid, &parent->object.oid);\n+\t}\n+\thead_commit = lookup_commit(the_repository, head);\n+\tif (head_commit)\n+\t\tcommit_list_insert(head_commit, &parents);\n+\treduce_heads_replace(&parents);\n+\n+\twhile (parents) {\n+\t\tstruct commit *cmit = pop_commit(&parents);\n+\t\tfor (i = 0; i < result->nr; i++)\n+\t\t\tif (oideq(&result->item[i].commit, &cmit->object.oid))\n+\t\t\t\tresult->item[i].used = 1;\n+\t}\n+\n+\tfor (i = j = 0; i < result->nr; i++) {\n+\t\tif (result->item[i].used) {\n+\t\t\tif (i != j)\n+\t\t\t\tresult->item[j] = result->item[i];\n+\t\t\tj++;\n+\t\t}\n+\t}\n+\tresult->nr = j;\n+}\n+\n+\n+int fmt_merge_msg(struct strbuf *in, struct strbuf *out,\n+\t\t  struct fmt_merge_msg_opts *opts)\n+{\n+\tint i = 0, pos = 0;\n+\tstruct object_id head_oid;\n+\tconst char *current_branch;\n+\tvoid *current_branch_to_free;\n+\tstruct merge_parents merge_parents;\n+\n+\tmemset(&merge_parents, 0, sizeof(merge_parents));\n+\n+\t/* get current branch */\n+\tcurrent_branch = current_branch_to_free =\n+\t\tresolve_refdup(\"HEAD\", RESOLVE_REF_READING, &head_oid, NULL);\n+\tif (!current_branch)\n+\t\tdie(\"No current branch\");\n+\tif (starts_with(current_branch, \"refs/heads/\"))\n+\t\tcurrent_branch += 11;\n+\n+\tfind_merge_parents(&merge_parents, in, &head_oid);\n+\n+\t/* get a line */\n+\twhile (pos < in->len) {\n+\t\tint len;\n+\t\tchar *newline, *p = in->buf + pos;\n+\n+\t\tnewline = strchr(p, '\\n');\n+\t\tlen = newline ? newline - p : strlen(p);\n+\t\tpos += len + !!newline;\n+\t\ti++;\n+\t\tp[len] = 0;\n+\t\tif (handle_line(p, &merge_parents))\n+\t\t\tdie(\"error in line %d: %.*s\", i, len, p);\n+\t}\n+\n+\tif (opts->add_title && srcs.nr)\n+\t\tfmt_merge_msg_title(out, current_branch);\n+\n+\tif (origins.nr)\n+\t\tfmt_merge_msg_sigs(out);\n+\n+\tif (opts->shortlog_len) {\n+\t\tstruct commit *head;\n+\t\tstruct rev_info rev;\n+\n+\t\thead = lookup_commit_or_die(&head_oid, \"HEAD\");\n+\t\trepo_init_revisions(the_repository, &rev, NULL);\n+\t\trev.commit_format = CMIT_FMT_ONELINE;\n+\t\trev.ignore_merges = 1;\n+\t\trev.limited = 1;\n+\n+\t\tstrbuf_complete_line(out);\n+\n+\t\tfor (i = 0; i < origins.nr; i++)\n+\t\t\tshortlog(origins.items[i].string,\n+\t\t\t\t origins.items[i].util,\n+\t\t\t\t head, &rev, opts, out);\n+\t}\n+\n+\tstrbuf_complete_line(out);\n+\tfree(current_branch_to_free);\n+\tfree(merge_parents.item);\n+\treturn 0;\n+}\ndiff --git a/fmt-merge-msg.h b/fmt-merge-msg.h\nindex 01e3aa88c5..f2ab0e0085 100644\n--- a/fmt-merge-msg.h\n+++ b/fmt-merge-msg.h\n@@ -1,7 +1,20 @@\n #ifndef FMT_MERGE_MSG_H\n #define FMT_MERGE_MSG_H\n \n+#include \"strbuf.h\"\n+\n+#define DEFAULT_MERGE_LOG_LEN 20\n+\n+struct fmt_merge_msg_opts {\n+\tunsigned add_title:1,\n+\t\tcredit_people:1;\n+\tint shortlog_len;\n+};\n+\n extern int merge_log_config;\n int fmt_merge_msg_config(const char *key, const char *value, void *cb);\n+int fmt_merge_msg(struct strbuf *in, struct strbuf *out,\n+\t\t  struct fmt_merge_msg_opts *);\n+\n \n #endif /* FMT_MERGE_MSG_H */\n-- \n2.25.0.114.g5b0ca878e0\n\n"}]}