6 #include "string-list.h"
11 #include "parse-options.h"
15 static char const * const shortlog_usage[] = {
16 N_("git shortlog [<options>] [<revision-range>] [[--] <path>...]"),
17 N_("git log --pretty=short | git shortlog [<options>]"),
22 * The util field of our string_list_items will contain one of two things:
24 * - if --summary is not in use, it will point to a string list of the
25 * oneline subjects assigned to this author
27 * - if --summary is in use, we don't need that list; we only need to know
28 * its size. So we abuse the pointer slot to store our integer counter.
30 * This macro accesses the latter.
32 #define UTIL_TO_INT(x) ((intptr_t)(x)->util)
34 static int compare_by_counter(const void *a1, const void *a2)
36 const struct string_list_item *i1 = a1, *i2 = a2;
37 return UTIL_TO_INT(i2) - UTIL_TO_INT(i1);
40 static int compare_by_list(const void *a1, const void *a2)
42 const struct string_list_item *i1 = a1, *i2 = a2;
43 const struct string_list *l1 = i1->util, *l2 = i2->util;
47 else if (l1->nr == l2->nr)
53 static void insert_one_record(struct shortlog *log,
57 struct string_list_item *item;
59 item = string_list_insert(&log->list, ident);
62 item->util = (void *)(UTIL_TO_INT(item) + 1);
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);
84 if (item->util == NULL)
85 item->util = xcalloc(1, sizeof(struct string_list));
86 string_list_append(item->util, buffer);
90 static int parse_ident(struct shortlog *log,
91 struct strbuf *out, const char *in)
93 const char *mailbuf, *namebuf;
94 size_t namelen, maillen;
95 struct ident_split ident;
97 if (split_ident_line(&ident, in, strlen(in)))
100 namebuf = ident.name_begin;
101 mailbuf = ident.mail_begin;
102 namelen = ident.name_end - ident.name_begin;
103 maillen = ident.mail_end - ident.mail_begin;
105 map_user(&log->mailmap, &mailbuf, &maillen, &namebuf, &namelen);
106 strbuf_add(out, namebuf, namelen);
108 strbuf_addf(out, " <%.*s>", (int)maillen, mailbuf);
113 static void read_from_stdin(struct shortlog *log)
115 struct strbuf ident = STRBUF_INIT;
116 struct strbuf mapped_ident = STRBUF_INIT;
117 struct strbuf oneline = STRBUF_INIT;
118 static const char *author_match[2] = { "Author: ", "author " };
119 static const char *committer_match[2] = { "Commit: ", "committer " };
122 if (HAS_MULTI_BITS(log->groups))
123 die(_("using multiple --group options with stdin is not supported"));
125 switch (log->groups) {
126 case SHORTLOG_GROUP_AUTHOR:
127 match = author_match;
129 case SHORTLOG_GROUP_COMMITTER:
130 match = committer_match;
132 case SHORTLOG_GROUP_TRAILER:
133 die(_("using --group=trailer with stdin is not supported"));
135 BUG("unhandled shortlog group");
138 while (strbuf_getline_lf(&ident, stdin) != EOF) {
140 if (!skip_prefix(ident.buf, match[0], &v) &&
141 !skip_prefix(ident.buf, match[1], &v))
143 while (strbuf_getline_lf(&oneline, stdin) != EOF &&
145 ; /* discard headers */
146 while (strbuf_getline_lf(&oneline, stdin) != EOF &&
148 ; /* discard blanks */
150 strbuf_reset(&mapped_ident);
151 if (parse_ident(log, &mapped_ident, v) < 0)
154 insert_one_record(log, mapped_ident.buf, oneline.buf);
156 strbuf_release(&ident);
157 strbuf_release(&mapped_ident);
158 strbuf_release(&oneline);
161 static void insert_records_from_trailers(struct shortlog *log,
163 struct commit *commit,
164 struct pretty_print_context *ctx,
167 struct trailer_iterator iter;
168 const char *commit_buffer, *body;
169 struct strbuf ident = STRBUF_INIT;
172 * Using format_commit_message("%B") would be simpler here, but
173 * this saves us copying the message.
175 commit_buffer = logmsg_reencode(commit, NULL, ctx->output_encoding);
176 body = strstr(commit_buffer, "\n\n");
180 trailer_iterator_init(&iter, body);
181 while (trailer_iterator_advance(&iter)) {
182 const char *value = iter.val.buf;
184 if (!string_list_has_string(&log->trailers, iter.key.buf))
187 strbuf_reset(&ident);
188 if (!parse_ident(log, &ident, value))
191 if (!strset_add(dups, value))
193 insert_one_record(log, value, oneline);
195 trailer_iterator_release(&iter);
197 strbuf_release(&ident);
198 unuse_commit_buffer(commit, commit_buffer);
201 void shortlog_add_commit(struct shortlog *log, struct commit *commit)
203 struct strbuf ident = STRBUF_INIT;
204 struct strbuf oneline = STRBUF_INIT;
205 struct strset dups = STRSET_INIT;
206 struct pretty_print_context ctx = {0};
207 const char *oneline_str;
209 ctx.fmt = CMIT_FMT_USERFORMAT;
210 ctx.abbrev = log->abbrev;
211 ctx.print_email_subject = 1;
212 ctx.date_mode.type = DATE_NORMAL;
213 ctx.output_encoding = get_log_output_encoding();
216 if (log->user_format)
217 pretty_print_commit(&ctx, commit, &oneline);
219 format_commit_message(commit, "%s", &oneline, &ctx);
221 oneline_str = oneline.len ? oneline.buf : "<none>";
223 if (log->groups & SHORTLOG_GROUP_AUTHOR) {
224 strbuf_reset(&ident);
225 format_commit_message(commit,
226 log->email ? "%aN <%aE>" : "%aN",
228 if (!HAS_MULTI_BITS(log->groups) ||
229 strset_add(&dups, ident.buf))
230 insert_one_record(log, ident.buf, oneline_str);
232 if (log->groups & SHORTLOG_GROUP_COMMITTER) {
233 strbuf_reset(&ident);
234 format_commit_message(commit,
235 log->email ? "%cN <%cE>" : "%cN",
237 if (!HAS_MULTI_BITS(log->groups) ||
238 strset_add(&dups, ident.buf))
239 insert_one_record(log, ident.buf, oneline_str);
241 if (log->groups & SHORTLOG_GROUP_TRAILER) {
242 insert_records_from_trailers(log, &dups, commit, &ctx, oneline_str);
246 strbuf_release(&ident);
247 strbuf_release(&oneline);
250 static void get_from_rev(struct rev_info *rev, struct shortlog *log)
252 struct commit *commit;
254 if (prepare_revision_walk(rev))
255 die(_("revision walk setup failed"));
256 while ((commit = get_revision(rev)) != NULL)
257 shortlog_add_commit(log, commit);
260 static int parse_uint(char const **arg, int comma, int defval)
266 ul = strtoul(*arg, &endp, 10);
267 if (*endp && *endp != comma)
271 ret = *arg == endp ? defval : (int)ul;
272 *arg = *endp ? endp + 1 : endp;
276 static const char wrap_arg_usage[] = "-w[<width>[,<indent1>[,<indent2>]]]";
277 #define DEFAULT_WRAPLEN 76
278 #define DEFAULT_INDENT1 6
279 #define DEFAULT_INDENT2 9
281 static int parse_wrap_args(const struct option *opt, const char *arg, int unset)
283 struct shortlog *log = opt->value;
285 log->wrap_lines = !unset;
289 log->wrap = DEFAULT_WRAPLEN;
290 log->in1 = DEFAULT_INDENT1;
291 log->in2 = DEFAULT_INDENT2;
295 log->wrap = parse_uint(&arg, ',', DEFAULT_WRAPLEN);
296 log->in1 = parse_uint(&arg, ',', DEFAULT_INDENT1);
297 log->in2 = parse_uint(&arg, '\0', DEFAULT_INDENT2);
298 if (log->wrap < 0 || log->in1 < 0 || log->in2 < 0)
299 return error(wrap_arg_usage);
301 ((log->in1 && log->wrap <= log->in1) ||
302 (log->in2 && log->wrap <= log->in2)))
303 return error(wrap_arg_usage);
307 static int parse_group_option(const struct option *opt, const char *arg, int unset)
309 struct shortlog *log = opt->value;
314 string_list_clear(&log->trailers, 0);
315 } else if (!strcasecmp(arg, "author"))
316 log->groups |= SHORTLOG_GROUP_AUTHOR;
317 else if (!strcasecmp(arg, "committer"))
318 log->groups |= SHORTLOG_GROUP_COMMITTER;
319 else if (skip_prefix(arg, "trailer:", &field)) {
320 log->groups |= SHORTLOG_GROUP_TRAILER;
321 string_list_append(&log->trailers, field);
323 return error(_("unknown group type: %s"), arg);
329 void shortlog_init(struct shortlog *log)
331 memset(log, 0, sizeof(*log));
333 read_mailmap(&log->mailmap);
335 log->list.strdup_strings = 1;
336 log->wrap = DEFAULT_WRAPLEN;
337 log->in1 = DEFAULT_INDENT1;
338 log->in2 = DEFAULT_INDENT2;
339 log->trailers.strdup_strings = 1;
340 log->trailers.cmp = strcasecmp;
343 int cmd_shortlog(int argc, const char **argv, const char *prefix)
345 struct shortlog log = { STRING_LIST_INIT_NODUP };
347 int nongit = !startup_info->have_repository;
349 const struct option options[] = {
350 OPT_BIT('c', "committer", &log.groups,
351 N_("group by committer rather than author"),
352 SHORTLOG_GROUP_COMMITTER),
353 OPT_BOOL('n', "numbered", &log.sort_by_number,
354 N_("sort output according to the number of commits per author")),
355 OPT_BOOL('s', "summary", &log.summary,
356 N_("suppress commit descriptions, only provides commit count")),
357 OPT_BOOL('e', "email", &log.email,
358 N_("show the email address of each author")),
359 OPT_CALLBACK_F('w', NULL, &log, N_("<w>[,<i1>[,<i2>]]"),
360 N_("linewrap output"), PARSE_OPT_OPTARG,
362 OPT_CALLBACK(0, "group", &log, N_("field"),
363 N_("group by field"), parse_group_option),
367 struct parse_opt_ctx_t ctx;
369 git_config(git_default_config, NULL);
371 repo_init_revisions(the_repository, &rev, prefix);
372 parse_options_start(&ctx, argc, argv, prefix, options,
373 PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0);
376 switch (parse_options_step(&ctx, options, shortlog_usage)) {
378 case PARSE_OPT_ERROR:
380 case PARSE_OPT_COMPLETE:
385 parse_revision_opt(&rev, &ctx, options, shortlog_usage);
388 argc = parse_options_end(&ctx);
390 if (nongit && argc > 1) {
391 error(_("too many arguments given outside repository"));
392 usage_with_options(shortlog_usage, options);
395 if (setup_revisions(argc, argv, &rev, NULL) != 1) {
396 error(_("unrecognized argument: %s"), argv[1]);
397 usage_with_options(shortlog_usage, options);
400 log.user_format = rev.commit_format == CMIT_FMT_USERFORMAT;
401 log.abbrev = rev.abbrev;
402 log.file = rev.diffopt.file;
405 log.groups = SHORTLOG_GROUP_AUTHOR;
406 string_list_sort(&log.trailers);
408 /* assume HEAD if from a tty */
409 if (!nongit && !rev.pending.nr && isatty(0))
410 add_head_to_pending(&rev);
411 if (rev.pending.nr == 0) {
413 fprintf(stderr, _("(reading log message from standard input)\n"));
414 read_from_stdin(&log);
417 get_from_rev(&rev, &log);
419 shortlog_output(&log);
420 if (log.file != stdout)
425 static void add_wrapped_shortlog_msg(struct strbuf *sb, const char *s,
426 const struct shortlog *log)
428 strbuf_add_wrapped_text(sb, s, log->in1, log->in2, log->wrap);
429 strbuf_addch(sb, '\n');
432 void shortlog_output(struct shortlog *log)
435 struct strbuf sb = STRBUF_INIT;
437 if (log->sort_by_number)
438 QSORT(log->list.items, log->list.nr,
439 log->summary ? compare_by_counter : compare_by_list);
440 for (i = 0; i < log->list.nr; i++) {
441 const struct string_list_item *item = &log->list.items[i];
443 fprintf(log->file, "%6d\t%s\n",
444 (int)UTIL_TO_INT(item), item->string);
446 struct string_list *onelines = item->util;
447 fprintf(log->file, "%s (%d):\n",
448 item->string, onelines->nr);
449 for (j = onelines->nr - 1; j >= 0; j--) {
450 const char *msg = onelines->items[j].string;
452 if (log->wrap_lines) {
454 add_wrapped_shortlog_msg(&sb, msg, log);
455 fwrite(sb.buf, sb.len, 1, log->file);
458 fprintf(log->file, " %s\n", msg);
460 putc('\n', log->file);
461 onelines->strdup_strings = 1;
462 string_list_clear(onelines, 0);
466 log->list.items[i].util = NULL;
470 log->list.strdup_strings = 1;
471 string_list_clear(&log->list, 1);
472 clear_mailmap(&log->mailmap);