6 #include "string-list.h"
11 #include "parse-options.h"
14 static char const * const shortlog_usage[] = {
15 N_("git shortlog [<options>] [<revision-range>] [[--] <path>...]"),
16 N_("git log --pretty=short | git shortlog [<options>]"),
21 * The util field of our string_list_items will contain one of two things:
23 * - if --summary is not in use, it will point to a string list of the
24 * oneline subjects assigned to this author
26 * - if --summary is in use, we don't need that list; we only need to know
27 * its size. So we abuse the pointer slot to store our integer counter.
29 * This macro accesses the latter.
31 #define UTIL_TO_INT(x) ((intptr_t)(x)->util)
33 static int compare_by_counter(const void *a1, const void *a2)
35 const struct string_list_item *i1 = a1, *i2 = a2;
36 return UTIL_TO_INT(i2) - UTIL_TO_INT(i1);
39 static int compare_by_list(const void *a1, const void *a2)
41 const struct string_list_item *i1 = a1, *i2 = a2;
42 const struct string_list *l1 = i1->util, *l2 = i2->util;
46 else if (l1->nr == l2->nr)
52 static void insert_one_record(struct shortlog *log,
56 struct string_list_item *item;
58 item = string_list_insert(&log->list, ident);
61 item->util = (void *)(UTIL_TO_INT(item) + 1);
63 const char *dot3 = log->common_repo_prefix;
65 struct strbuf subject = STRBUF_INIT;
68 /* Skip any leading whitespace, including any blank lines. */
69 while (*oneline && isspace(*oneline))
71 eol = strchr(oneline, '\n');
73 eol = oneline + strlen(oneline);
74 if (starts_with(oneline, "[PATCH")) {
75 char *eob = strchr(oneline, ']');
76 if (eob && (!eol || eob < eol))
79 while (*oneline && isspace(*oneline) && *oneline != '\n')
81 format_subject(&subject, oneline, " ");
82 buffer = strbuf_detach(&subject, NULL);
85 int dot3len = strlen(dot3);
87 while ((p = strstr(buffer, dot3)) != NULL) {
88 int taillen = strlen(p) - dot3len;
89 memcpy(p, "/.../", 5);
90 memmove(p + 5, p + dot3len, taillen + 1);
95 if (item->util == NULL)
96 item->util = xcalloc(1, sizeof(struct string_list));
97 string_list_append(item->util, buffer);
101 static int parse_ident(struct shortlog *log,
102 struct strbuf *out, const char *in)
104 const char *mailbuf, *namebuf;
105 size_t namelen, maillen;
106 struct ident_split ident;
108 if (split_ident_line(&ident, in, strlen(in)))
111 namebuf = ident.name_begin;
112 mailbuf = ident.mail_begin;
113 namelen = ident.name_end - ident.name_begin;
114 maillen = ident.mail_end - ident.mail_begin;
116 map_user(&log->mailmap, &mailbuf, &maillen, &namebuf, &namelen);
117 strbuf_add(out, namebuf, namelen);
119 strbuf_addf(out, " <%.*s>", (int)maillen, mailbuf);
124 static void read_from_stdin(struct shortlog *log)
126 struct strbuf ident = STRBUF_INIT;
127 struct strbuf mapped_ident = STRBUF_INIT;
128 struct strbuf oneline = STRBUF_INIT;
129 static const char *author_match[2] = { "Author: ", "author " };
130 static const char *committer_match[2] = { "Commit: ", "committer " };
133 if (HAS_MULTI_BITS(log->groups))
134 die(_("using multiple --group options with stdin is not supported"));
136 switch (log->groups) {
137 case SHORTLOG_GROUP_AUTHOR:
138 match = author_match;
140 case SHORTLOG_GROUP_COMMITTER:
141 match = committer_match;
143 case SHORTLOG_GROUP_TRAILER:
144 die(_("using --group=trailer with stdin is not supported"));
146 BUG("unhandled shortlog group");
149 while (strbuf_getline_lf(&ident, stdin) != EOF) {
151 if (!skip_prefix(ident.buf, match[0], &v) &&
152 !skip_prefix(ident.buf, match[1], &v))
154 while (strbuf_getline_lf(&oneline, stdin) != EOF &&
156 ; /* discard headers */
157 while (strbuf_getline_lf(&oneline, stdin) != EOF &&
159 ; /* discard blanks */
161 strbuf_reset(&mapped_ident);
162 if (parse_ident(log, &mapped_ident, v) < 0)
165 insert_one_record(log, mapped_ident.buf, oneline.buf);
167 strbuf_release(&ident);
168 strbuf_release(&mapped_ident);
169 strbuf_release(&oneline);
173 struct hashmap_entry ent;
174 char value[FLEX_ARRAY];
181 #define STRSET_INIT { { NULL } }
183 static int strset_item_hashcmp(const void *hash_data,
184 const struct hashmap_entry *entry,
185 const struct hashmap_entry *entry_or_key,
188 const struct strset_item *a, *b;
190 a = container_of(entry, const struct strset_item, ent);
192 return strcmp(a->value, keydata);
194 b = container_of(entry_or_key, const struct strset_item, ent);
195 return strcmp(a->value, b->value);
199 * Adds "str" to the set if it was not already present; returns true if it was
202 static int strset_check_and_add(struct strset *ss, const char *str)
204 unsigned int hash = strhash(str);
205 struct strset_item *item;
208 hashmap_init(&ss->map, strset_item_hashcmp, NULL, 0);
210 if (hashmap_get_from_hash(&ss->map, hash, str))
213 FLEX_ALLOC_STR(item, value, str);
214 hashmap_entry_init(&item->ent, hash);
215 hashmap_add(&ss->map, &item->ent);
219 static void strset_clear(struct strset *ss)
223 hashmap_free_entries(&ss->map, struct strset_item, ent);
226 static void insert_records_from_trailers(struct shortlog *log,
228 struct commit *commit,
229 struct pretty_print_context *ctx,
232 struct trailer_iterator iter;
233 const char *commit_buffer, *body;
234 struct strbuf ident = STRBUF_INIT;
237 * Using format_commit_message("%B") would be simpler here, but
238 * this saves us copying the message.
240 commit_buffer = logmsg_reencode(commit, NULL, ctx->output_encoding);
241 body = strstr(commit_buffer, "\n\n");
245 trailer_iterator_init(&iter, body);
246 while (trailer_iterator_advance(&iter)) {
247 const char *value = iter.val.buf;
249 if (!string_list_has_string(&log->trailers, iter.key.buf))
252 strbuf_reset(&ident);
253 if (!parse_ident(log, &ident, value))
256 if (strset_check_and_add(dups, value))
258 insert_one_record(log, value, oneline);
260 trailer_iterator_release(&iter);
262 strbuf_release(&ident);
263 unuse_commit_buffer(commit, commit_buffer);
266 void shortlog_add_commit(struct shortlog *log, struct commit *commit)
268 struct strbuf ident = STRBUF_INIT;
269 struct strbuf oneline = STRBUF_INIT;
270 struct strset dups = STRSET_INIT;
271 struct pretty_print_context ctx = {0};
272 const char *oneline_str;
274 ctx.fmt = CMIT_FMT_USERFORMAT;
275 ctx.abbrev = log->abbrev;
276 ctx.print_email_subject = 1;
277 ctx.date_mode.type = DATE_NORMAL;
278 ctx.output_encoding = get_log_output_encoding();
281 if (log->user_format)
282 pretty_print_commit(&ctx, commit, &oneline);
284 format_commit_message(commit, "%s", &oneline, &ctx);
286 oneline_str = oneline.len ? oneline.buf : "<none>";
288 if (log->groups & SHORTLOG_GROUP_AUTHOR) {
289 strbuf_reset(&ident);
290 format_commit_message(commit,
291 log->email ? "%aN <%aE>" : "%aN",
293 if (!HAS_MULTI_BITS(log->groups) ||
294 !strset_check_and_add(&dups, ident.buf))
295 insert_one_record(log, ident.buf, oneline_str);
297 if (log->groups & SHORTLOG_GROUP_COMMITTER) {
298 strbuf_reset(&ident);
299 format_commit_message(commit,
300 log->email ? "%cN <%cE>" : "%cN",
302 if (!HAS_MULTI_BITS(log->groups) ||
303 !strset_check_and_add(&dups, ident.buf))
304 insert_one_record(log, ident.buf, oneline_str);
306 if (log->groups & SHORTLOG_GROUP_TRAILER) {
307 insert_records_from_trailers(log, &dups, commit, &ctx, oneline_str);
311 strbuf_release(&ident);
312 strbuf_release(&oneline);
315 static void get_from_rev(struct rev_info *rev, struct shortlog *log)
317 struct commit *commit;
319 if (prepare_revision_walk(rev))
320 die(_("revision walk setup failed"));
321 while ((commit = get_revision(rev)) != NULL)
322 shortlog_add_commit(log, commit);
325 static int parse_uint(char const **arg, int comma, int defval)
331 ul = strtoul(*arg, &endp, 10);
332 if (*endp && *endp != comma)
336 ret = *arg == endp ? defval : (int)ul;
337 *arg = *endp ? endp + 1 : endp;
341 static const char wrap_arg_usage[] = "-w[<width>[,<indent1>[,<indent2>]]]";
342 #define DEFAULT_WRAPLEN 76
343 #define DEFAULT_INDENT1 6
344 #define DEFAULT_INDENT2 9
346 static int parse_wrap_args(const struct option *opt, const char *arg, int unset)
348 struct shortlog *log = opt->value;
350 log->wrap_lines = !unset;
354 log->wrap = DEFAULT_WRAPLEN;
355 log->in1 = DEFAULT_INDENT1;
356 log->in2 = DEFAULT_INDENT2;
360 log->wrap = parse_uint(&arg, ',', DEFAULT_WRAPLEN);
361 log->in1 = parse_uint(&arg, ',', DEFAULT_INDENT1);
362 log->in2 = parse_uint(&arg, '\0', DEFAULT_INDENT2);
363 if (log->wrap < 0 || log->in1 < 0 || log->in2 < 0)
364 return error(wrap_arg_usage);
366 ((log->in1 && log->wrap <= log->in1) ||
367 (log->in2 && log->wrap <= log->in2)))
368 return error(wrap_arg_usage);
372 static int parse_group_option(const struct option *opt, const char *arg, int unset)
374 struct shortlog *log = opt->value;
379 string_list_clear(&log->trailers, 0);
380 } else if (!strcasecmp(arg, "author"))
381 log->groups |= SHORTLOG_GROUP_AUTHOR;
382 else if (!strcasecmp(arg, "committer"))
383 log->groups |= SHORTLOG_GROUP_COMMITTER;
384 else if (skip_prefix(arg, "trailer:", &field)) {
385 log->groups |= SHORTLOG_GROUP_TRAILER;
386 string_list_append(&log->trailers, field);
388 return error(_("unknown group type: %s"), arg);
394 void shortlog_init(struct shortlog *log)
396 memset(log, 0, sizeof(*log));
398 read_mailmap(&log->mailmap, &log->common_repo_prefix);
400 log->list.strdup_strings = 1;
401 log->wrap = DEFAULT_WRAPLEN;
402 log->in1 = DEFAULT_INDENT1;
403 log->in2 = DEFAULT_INDENT2;
404 log->trailers.strdup_strings = 1;
405 log->trailers.cmp = strcasecmp;
408 int cmd_shortlog(int argc, const char **argv, const char *prefix)
410 struct shortlog log = { STRING_LIST_INIT_NODUP };
412 int nongit = !startup_info->have_repository;
414 const struct option options[] = {
415 OPT_BIT('c', "committer", &log.groups,
416 N_("Group by committer rather than author"),
417 SHORTLOG_GROUP_COMMITTER),
418 OPT_BOOL('n', "numbered", &log.sort_by_number,
419 N_("sort output according to the number of commits per author")),
420 OPT_BOOL('s', "summary", &log.summary,
421 N_("Suppress commit descriptions, only provides commit count")),
422 OPT_BOOL('e', "email", &log.email,
423 N_("Show the email address of each author")),
424 OPT_CALLBACK_F('w', NULL, &log, N_("<w>[,<i1>[,<i2>]]"),
425 N_("Linewrap output"), PARSE_OPT_OPTARG,
427 OPT_CALLBACK(0, "group", &log, N_("field"),
428 N_("Group by field"), parse_group_option),
432 struct parse_opt_ctx_t ctx;
434 git_config(git_default_config, NULL);
436 repo_init_revisions(the_repository, &rev, prefix);
437 parse_options_start(&ctx, argc, argv, prefix, options,
438 PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0);
441 switch (parse_options_step(&ctx, options, shortlog_usage)) {
443 case PARSE_OPT_ERROR:
445 case PARSE_OPT_COMPLETE:
450 parse_revision_opt(&rev, &ctx, options, shortlog_usage);
453 argc = parse_options_end(&ctx);
455 if (nongit && argc > 1) {
456 error(_("too many arguments given outside repository"));
457 usage_with_options(shortlog_usage, options);
460 if (setup_revisions(argc, argv, &rev, NULL) != 1) {
461 error(_("unrecognized argument: %s"), argv[1]);
462 usage_with_options(shortlog_usage, options);
465 log.user_format = rev.commit_format == CMIT_FMT_USERFORMAT;
466 log.abbrev = rev.abbrev;
467 log.file = rev.diffopt.file;
470 log.groups = SHORTLOG_GROUP_AUTHOR;
471 string_list_sort(&log.trailers);
473 /* assume HEAD if from a tty */
474 if (!nongit && !rev.pending.nr && isatty(0))
475 add_head_to_pending(&rev);
476 if (rev.pending.nr == 0) {
478 fprintf(stderr, _("(reading log message from standard input)\n"));
479 read_from_stdin(&log);
482 get_from_rev(&rev, &log);
484 shortlog_output(&log);
485 if (log.file != stdout)
490 static void add_wrapped_shortlog_msg(struct strbuf *sb, const char *s,
491 const struct shortlog *log)
493 strbuf_add_wrapped_text(sb, s, log->in1, log->in2, log->wrap);
494 strbuf_addch(sb, '\n');
497 void shortlog_output(struct shortlog *log)
500 struct strbuf sb = STRBUF_INIT;
502 if (log->sort_by_number)
503 QSORT(log->list.items, log->list.nr,
504 log->summary ? compare_by_counter : compare_by_list);
505 for (i = 0; i < log->list.nr; i++) {
506 const struct string_list_item *item = &log->list.items[i];
508 fprintf(log->file, "%6d\t%s\n",
509 (int)UTIL_TO_INT(item), item->string);
511 struct string_list *onelines = item->util;
512 fprintf(log->file, "%s (%d):\n",
513 item->string, onelines->nr);
514 for (j = onelines->nr - 1; j >= 0; j--) {
515 const char *msg = onelines->items[j].string;
517 if (log->wrap_lines) {
519 add_wrapped_shortlog_msg(&sb, msg, log);
520 fwrite(sb.buf, sb.len, 1, log->file);
523 fprintf(log->file, " %s\n", msg);
525 putc('\n', log->file);
526 onelines->strdup_strings = 1;
527 string_list_clear(onelines, 0);
531 log->list.items[i].util = NULL;
535 log->list.strdup_strings = 1;
536 string_list_clear(&log->list, 1);
537 clear_mailmap(&log->mailmap);