shortlog.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. #include "builtin.h"
  2. #include "cache.h"
  3. #include "config.h"
  4. #include "commit.h"
  5. #include "diff.h"
  6. #include "string-list.h"
  7. #include "revision.h"
  8. #include "utf8.h"
  9. #include "mailmap.h"
  10. #include "shortlog.h"
  11. #include "parse-options.h"
  12. #include "trailer.h"
  13. static char const * const shortlog_usage[] = {
  14. N_("git shortlog [<options>] [<revision-range>] [[--] <path>...]"),
  15. N_("git log --pretty=short | git shortlog [<options>]"),
  16. NULL
  17. };
  18. /*
  19. * The util field of our string_list_items will contain one of two things:
  20. *
  21. * - if --summary is not in use, it will point to a string list of the
  22. * oneline subjects assigned to this author
  23. *
  24. * - if --summary is in use, we don't need that list; we only need to know
  25. * its size. So we abuse the pointer slot to store our integer counter.
  26. *
  27. * This macro accesses the latter.
  28. */
  29. #define UTIL_TO_INT(x) ((intptr_t)(x)->util)
  30. static int compare_by_counter(const void *a1, const void *a2)
  31. {
  32. const struct string_list_item *i1 = a1, *i2 = a2;
  33. return UTIL_TO_INT(i2) - UTIL_TO_INT(i1);
  34. }
  35. static int compare_by_list(const void *a1, const void *a2)
  36. {
  37. const struct string_list_item *i1 = a1, *i2 = a2;
  38. const struct string_list *l1 = i1->util, *l2 = i2->util;
  39. if (l1->nr < l2->nr)
  40. return 1;
  41. else if (l1->nr == l2->nr)
  42. return 0;
  43. else
  44. return -1;
  45. }
  46. static void insert_one_record(struct shortlog *log,
  47. const char *ident,
  48. const char *oneline)
  49. {
  50. struct string_list_item *item;
  51. item = string_list_insert(&log->list, ident);
  52. if (log->summary)
  53. item->util = (void *)(UTIL_TO_INT(item) + 1);
  54. else {
  55. const char *dot3 = log->common_repo_prefix;
  56. char *buffer, *p;
  57. struct strbuf subject = STRBUF_INIT;
  58. const char *eol;
  59. /* Skip any leading whitespace, including any blank lines. */
  60. while (*oneline && isspace(*oneline))
  61. oneline++;
  62. eol = strchr(oneline, '\n');
  63. if (!eol)
  64. eol = oneline + strlen(oneline);
  65. if (starts_with(oneline, "[PATCH")) {
  66. char *eob = strchr(oneline, ']');
  67. if (eob && (!eol || eob < eol))
  68. oneline = eob + 1;
  69. }
  70. while (*oneline && isspace(*oneline) && *oneline != '\n')
  71. oneline++;
  72. format_subject(&subject, oneline, " ");
  73. buffer = strbuf_detach(&subject, NULL);
  74. if (dot3) {
  75. int dot3len = strlen(dot3);
  76. if (dot3len > 5) {
  77. while ((p = strstr(buffer, dot3)) != NULL) {
  78. int taillen = strlen(p) - dot3len;
  79. memcpy(p, "/.../", 5);
  80. memmove(p + 5, p + dot3len, taillen + 1);
  81. }
  82. }
  83. }
  84. if (item->util == NULL)
  85. item->util = xcalloc(1, sizeof(struct string_list));
  86. string_list_append(item->util, buffer);
  87. }
  88. }
  89. static int parse_ident(struct shortlog *log,
  90. struct strbuf *out, const char *in)
  91. {
  92. const char *mailbuf, *namebuf;
  93. size_t namelen, maillen;
  94. struct ident_split ident;
  95. if (split_ident_line(&ident, in, strlen(in)))
  96. return -1;
  97. namebuf = ident.name_begin;
  98. mailbuf = ident.mail_begin;
  99. namelen = ident.name_end - ident.name_begin;
  100. maillen = ident.mail_end - ident.mail_begin;
  101. map_user(&log->mailmap, &mailbuf, &maillen, &namebuf, &namelen);
  102. strbuf_add(out, namebuf, namelen);
  103. if (log->email)
  104. strbuf_addf(out, " <%.*s>", (int)maillen, mailbuf);
  105. return 0;
  106. }
  107. static void read_from_stdin(struct shortlog *log)
  108. {
  109. struct strbuf ident = STRBUF_INIT;
  110. struct strbuf mapped_ident = STRBUF_INIT;
  111. struct strbuf oneline = STRBUF_INIT;
  112. static const char *author_match[2] = { "Author: ", "author " };
  113. static const char *committer_match[2] = { "Commit: ", "committer " };
  114. const char **match;
  115. if (HAS_MULTI_BITS(log->groups))
  116. die(_("using multiple --group options with stdin is not supported"));
  117. switch (log->groups) {
  118. case SHORTLOG_GROUP_AUTHOR:
  119. match = author_match;
  120. break;
  121. case SHORTLOG_GROUP_COMMITTER:
  122. match = committer_match;
  123. break;
  124. case SHORTLOG_GROUP_TRAILER:
  125. die(_("using --group=trailer with stdin is not supported"));
  126. default:
  127. BUG("unhandled shortlog group");
  128. }
  129. while (strbuf_getline_lf(&ident, stdin) != EOF) {
  130. const char *v;
  131. if (!skip_prefix(ident.buf, match[0], &v) &&
  132. !skip_prefix(ident.buf, match[1], &v))
  133. continue;
  134. while (strbuf_getline_lf(&oneline, stdin) != EOF &&
  135. oneline.len)
  136. ; /* discard headers */
  137. while (strbuf_getline_lf(&oneline, stdin) != EOF &&
  138. !oneline.len)
  139. ; /* discard blanks */
  140. strbuf_reset(&mapped_ident);
  141. if (parse_ident(log, &mapped_ident, v) < 0)
  142. continue;
  143. insert_one_record(log, mapped_ident.buf, oneline.buf);
  144. }
  145. strbuf_release(&ident);
  146. strbuf_release(&mapped_ident);
  147. strbuf_release(&oneline);
  148. }
  149. struct strset_item {
  150. struct hashmap_entry ent;
  151. char value[FLEX_ARRAY];
  152. };
  153. struct strset {
  154. struct hashmap map;
  155. };
  156. #define STRSET_INIT { { NULL } }
  157. static int strset_item_hashcmp(const void *hash_data,
  158. const struct hashmap_entry *entry,
  159. const struct hashmap_entry *entry_or_key,
  160. const void *keydata)
  161. {
  162. const struct strset_item *a, *b;
  163. a = container_of(entry, const struct strset_item, ent);
  164. if (keydata)
  165. return strcmp(a->value, keydata);
  166. b = container_of(entry_or_key, const struct strset_item, ent);
  167. return strcmp(a->value, b->value);
  168. }
  169. /*
  170. * Adds "str" to the set if it was not already present; returns true if it was
  171. * already there.
  172. */
  173. static int strset_check_and_add(struct strset *ss, const char *str)
  174. {
  175. unsigned int hash = strhash(str);
  176. struct strset_item *item;
  177. if (!ss->map.table)
  178. hashmap_init(&ss->map, strset_item_hashcmp, NULL, 0);
  179. if (hashmap_get_from_hash(&ss->map, hash, str))
  180. return 1;
  181. FLEX_ALLOC_STR(item, value, str);
  182. hashmap_entry_init(&item->ent, hash);
  183. hashmap_add(&ss->map, &item->ent);
  184. return 0;
  185. }
  186. static void strset_clear(struct strset *ss)
  187. {
  188. if (!ss->map.table)
  189. return;
  190. hashmap_free_entries(&ss->map, struct strset_item, ent);
  191. }
  192. static void insert_records_from_trailers(struct shortlog *log,
  193. struct strset *dups,
  194. struct commit *commit,
  195. struct pretty_print_context *ctx,
  196. const char *oneline)
  197. {
  198. struct trailer_iterator iter;
  199. const char *commit_buffer, *body;
  200. struct strbuf ident = STRBUF_INIT;
  201. /*
  202. * Using format_commit_message("%B") would be simpler here, but
  203. * this saves us copying the message.
  204. */
  205. commit_buffer = logmsg_reencode(commit, NULL, ctx->output_encoding);
  206. body = strstr(commit_buffer, "\n\n");
  207. if (!body)
  208. return;
  209. trailer_iterator_init(&iter, body);
  210. while (trailer_iterator_advance(&iter)) {
  211. const char *value = iter.val.buf;
  212. if (!string_list_has_string(&log->trailers, iter.key.buf))
  213. continue;
  214. strbuf_reset(&ident);
  215. if (!parse_ident(log, &ident, value))
  216. value = ident.buf;
  217. if (strset_check_and_add(dups, value))
  218. continue;
  219. insert_one_record(log, value, oneline);
  220. }
  221. trailer_iterator_release(&iter);
  222. strbuf_release(&ident);
  223. unuse_commit_buffer(commit, commit_buffer);
  224. }
  225. void shortlog_add_commit(struct shortlog *log, struct commit *commit)
  226. {
  227. struct strbuf ident = STRBUF_INIT;
  228. struct strbuf oneline = STRBUF_INIT;
  229. struct strset dups = STRSET_INIT;
  230. struct pretty_print_context ctx = {0};
  231. const char *oneline_str;
  232. ctx.fmt = CMIT_FMT_USERFORMAT;
  233. ctx.abbrev = log->abbrev;
  234. ctx.print_email_subject = 1;
  235. ctx.date_mode.type = DATE_NORMAL;
  236. ctx.output_encoding = get_log_output_encoding();
  237. if (!log->summary) {
  238. if (log->user_format)
  239. pretty_print_commit(&ctx, commit, &oneline);
  240. else
  241. format_commit_message(commit, "%s", &oneline, &ctx);
  242. }
  243. oneline_str = oneline.len ? oneline.buf : "<none>";
  244. if (log->groups & SHORTLOG_GROUP_AUTHOR) {
  245. strbuf_reset(&ident);
  246. format_commit_message(commit,
  247. log->email ? "%aN <%aE>" : "%aN",
  248. &ident, &ctx);
  249. if (!HAS_MULTI_BITS(log->groups) ||
  250. !strset_check_and_add(&dups, ident.buf))
  251. insert_one_record(log, ident.buf, oneline_str);
  252. }
  253. if (log->groups & SHORTLOG_GROUP_COMMITTER) {
  254. strbuf_reset(&ident);
  255. format_commit_message(commit,
  256. log->email ? "%cN <%cE>" : "%cN",
  257. &ident, &ctx);
  258. if (!HAS_MULTI_BITS(log->groups) ||
  259. !strset_check_and_add(&dups, ident.buf))
  260. insert_one_record(log, ident.buf, oneline_str);
  261. }
  262. if (log->groups & SHORTLOG_GROUP_TRAILER) {
  263. insert_records_from_trailers(log, &dups, commit, &ctx, oneline_str);
  264. }
  265. strset_clear(&dups);
  266. strbuf_release(&ident);
  267. strbuf_release(&oneline);
  268. }
  269. static void get_from_rev(struct rev_info *rev, struct shortlog *log)
  270. {
  271. struct commit *commit;
  272. if (prepare_revision_walk(rev))
  273. die(_("revision walk setup failed"));
  274. while ((commit = get_revision(rev)) != NULL)
  275. shortlog_add_commit(log, commit);
  276. }
  277. static int parse_uint(char const **arg, int comma, int defval)
  278. {
  279. unsigned long ul;
  280. int ret;
  281. char *endp;
  282. ul = strtoul(*arg, &endp, 10);
  283. if (*endp && *endp != comma)
  284. return -1;
  285. if (ul > INT_MAX)
  286. return -1;
  287. ret = *arg == endp ? defval : (int)ul;
  288. *arg = *endp ? endp + 1 : endp;
  289. return ret;
  290. }
  291. static const char wrap_arg_usage[] = "-w[<width>[,<indent1>[,<indent2>]]]";
  292. #define DEFAULT_WRAPLEN 76
  293. #define DEFAULT_INDENT1 6
  294. #define DEFAULT_INDENT2 9
  295. static int parse_wrap_args(const struct option *opt, const char *arg, int unset)
  296. {
  297. struct shortlog *log = opt->value;
  298. log->wrap_lines = !unset;
  299. if (unset)
  300. return 0;
  301. if (!arg) {
  302. log->wrap = DEFAULT_WRAPLEN;
  303. log->in1 = DEFAULT_INDENT1;
  304. log->in2 = DEFAULT_INDENT2;
  305. return 0;
  306. }
  307. log->wrap = parse_uint(&arg, ',', DEFAULT_WRAPLEN);
  308. log->in1 = parse_uint(&arg, ',', DEFAULT_INDENT1);
  309. log->in2 = parse_uint(&arg, '\0', DEFAULT_INDENT2);
  310. if (log->wrap < 0 || log->in1 < 0 || log->in2 < 0)
  311. return error(wrap_arg_usage);
  312. if (log->wrap &&
  313. ((log->in1 && log->wrap <= log->in1) ||
  314. (log->in2 && log->wrap <= log->in2)))
  315. return error(wrap_arg_usage);
  316. return 0;
  317. }
  318. static int parse_group_option(const struct option *opt, const char *arg, int unset)
  319. {
  320. struct shortlog *log = opt->value;
  321. const char *field;
  322. if (unset) {
  323. log->groups = 0;
  324. string_list_clear(&log->trailers, 0);
  325. } else if (!strcasecmp(arg, "author"))
  326. log->groups |= SHORTLOG_GROUP_AUTHOR;
  327. else if (!strcasecmp(arg, "committer"))
  328. log->groups |= SHORTLOG_GROUP_COMMITTER;
  329. else if (skip_prefix(arg, "trailer:", &field)) {
  330. log->groups |= SHORTLOG_GROUP_TRAILER;
  331. string_list_append(&log->trailers, field);
  332. } else
  333. return error(_("unknown group type: %s"), arg);
  334. return 0;
  335. }
  336. void shortlog_init(struct shortlog *log)
  337. {
  338. memset(log, 0, sizeof(*log));
  339. read_mailmap(&log->mailmap, &log->common_repo_prefix);
  340. log->list.strdup_strings = 1;
  341. log->wrap = DEFAULT_WRAPLEN;
  342. log->in1 = DEFAULT_INDENT1;
  343. log->in2 = DEFAULT_INDENT2;
  344. log->trailers.strdup_strings = 1;
  345. log->trailers.cmp = strcasecmp;
  346. }
  347. int cmd_shortlog(int argc, const char **argv, const char *prefix)
  348. {
  349. struct shortlog log = { STRING_LIST_INIT_NODUP };
  350. struct rev_info rev;
  351. int nongit = !startup_info->have_repository;
  352. const struct option options[] = {
  353. OPT_BIT('c', "committer", &log.groups,
  354. N_("Group by committer rather than author"),
  355. SHORTLOG_GROUP_COMMITTER),
  356. OPT_BOOL('n', "numbered", &log.sort_by_number,
  357. N_("sort output according to the number of commits per author")),
  358. OPT_BOOL('s', "summary", &log.summary,
  359. N_("Suppress commit descriptions, only provides commit count")),
  360. OPT_BOOL('e', "email", &log.email,
  361. N_("Show the email address of each author")),
  362. OPT_CALLBACK_F('w', NULL, &log, N_("<w>[,<i1>[,<i2>]]"),
  363. N_("Linewrap output"), PARSE_OPT_OPTARG,
  364. &parse_wrap_args),
  365. OPT_CALLBACK(0, "group", &log, N_("field"),
  366. N_("Group by field"), parse_group_option),
  367. OPT_END(),
  368. };
  369. struct parse_opt_ctx_t ctx;
  370. git_config(git_default_config, NULL);
  371. shortlog_init(&log);
  372. repo_init_revisions(the_repository, &rev, prefix);
  373. parse_options_start(&ctx, argc, argv, prefix, options,
  374. PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0);
  375. for (;;) {
  376. switch (parse_options_step(&ctx, options, shortlog_usage)) {
  377. case PARSE_OPT_HELP:
  378. case PARSE_OPT_ERROR:
  379. exit(129);
  380. case PARSE_OPT_COMPLETE:
  381. exit(0);
  382. case PARSE_OPT_DONE:
  383. goto parse_done;
  384. }
  385. parse_revision_opt(&rev, &ctx, options, shortlog_usage);
  386. }
  387. parse_done:
  388. argc = parse_options_end(&ctx);
  389. if (nongit && argc > 1) {
  390. error(_("too many arguments given outside repository"));
  391. usage_with_options(shortlog_usage, options);
  392. }
  393. if (setup_revisions(argc, argv, &rev, NULL) != 1) {
  394. error(_("unrecognized argument: %s"), argv[1]);
  395. usage_with_options(shortlog_usage, options);
  396. }
  397. log.user_format = rev.commit_format == CMIT_FMT_USERFORMAT;
  398. log.abbrev = rev.abbrev;
  399. log.file = rev.diffopt.file;
  400. if (!log.groups)
  401. log.groups = SHORTLOG_GROUP_AUTHOR;
  402. string_list_sort(&log.trailers);
  403. /* assume HEAD if from a tty */
  404. if (!nongit && !rev.pending.nr && isatty(0))
  405. add_head_to_pending(&rev);
  406. if (rev.pending.nr == 0) {
  407. if (isatty(0))
  408. fprintf(stderr, _("(reading log message from standard input)\n"));
  409. read_from_stdin(&log);
  410. }
  411. else
  412. get_from_rev(&rev, &log);
  413. shortlog_output(&log);
  414. if (log.file != stdout)
  415. fclose(log.file);
  416. return 0;
  417. }
  418. static void add_wrapped_shortlog_msg(struct strbuf *sb, const char *s,
  419. const struct shortlog *log)
  420. {
  421. strbuf_add_wrapped_text(sb, s, log->in1, log->in2, log->wrap);
  422. strbuf_addch(sb, '\n');
  423. }
  424. void shortlog_output(struct shortlog *log)
  425. {
  426. int i, j;
  427. struct strbuf sb = STRBUF_INIT;
  428. if (log->sort_by_number)
  429. QSORT(log->list.items, log->list.nr,
  430. log->summary ? compare_by_counter : compare_by_list);
  431. for (i = 0; i < log->list.nr; i++) {
  432. const struct string_list_item *item = &log->list.items[i];
  433. if (log->summary) {
  434. fprintf(log->file, "%6d\t%s\n",
  435. (int)UTIL_TO_INT(item), item->string);
  436. } else {
  437. struct string_list *onelines = item->util;
  438. fprintf(log->file, "%s (%d):\n",
  439. item->string, onelines->nr);
  440. for (j = onelines->nr - 1; j >= 0; j--) {
  441. const char *msg = onelines->items[j].string;
  442. if (log->wrap_lines) {
  443. strbuf_reset(&sb);
  444. add_wrapped_shortlog_msg(&sb, msg, log);
  445. fwrite(sb.buf, sb.len, 1, log->file);
  446. }
  447. else
  448. fprintf(log->file, " %s\n", msg);
  449. }
  450. putc('\n', log->file);
  451. onelines->strdup_strings = 1;
  452. string_list_clear(onelines, 0);
  453. free(onelines);
  454. }
  455. log->list.items[i].util = NULL;
  456. }
  457. strbuf_release(&sb);
  458. log->list.strdup_strings = 1;
  459. string_list_clear(&log->list, 1);
  460. clear_mailmap(&log->mailmap);
  461. }