The seventh batch
[git.git] / builtin / shortlog.c
blobc86b75d9813ad9207a773e95ce897b65929e86b9
1 #define USE_THE_REPOSITORY_VARIABLE
2 #include "builtin.h"
3 #include "config.h"
4 #include "commit.h"
5 #include "diff.h"
6 #include "environment.h"
7 #include "gettext.h"
8 #include "string-list.h"
9 #include "revision.h"
10 #include "utf8.h"
11 #include "mailmap.h"
12 #include "setup.h"
13 #include "shortlog.h"
14 #include "parse-options.h"
15 #include "trailer.h"
16 #include "strmap.h"
18 static char const * const shortlog_usage[] = {
19 N_("git shortlog [<options>] [<revision-range>] [[--] <path>...]"),
20 N_("git log --pretty=short | git shortlog [<options>]"),
21 NULL
25 * The util field of our string_list_items will contain one of two things:
27 * - if --summary is not in use, it will point to a string list of the
28 * oneline subjects assigned to this author
30 * - if --summary is in use, we don't need that list; we only need to know
31 * its size. So we abuse the pointer slot to store our integer counter.
33 * This macro accesses the latter.
35 #define UTIL_TO_INT(x) ((intptr_t)(x)->util)
37 static int compare_by_counter(const void *a1, const void *a2)
39 const struct string_list_item *i1 = a1, *i2 = a2;
40 return UTIL_TO_INT(i2) - UTIL_TO_INT(i1);
43 static int compare_by_list(const void *a1, const void *a2)
45 const struct string_list_item *i1 = a1, *i2 = a2;
46 const struct string_list *l1 = i1->util, *l2 = i2->util;
48 if (l1->nr < l2->nr)
49 return 1;
50 else if (l1->nr == l2->nr)
51 return 0;
52 else
53 return -1;
56 static void insert_one_record(struct shortlog *log,
57 const char *ident,
58 const char *oneline)
60 struct string_list_item *item;
62 item = string_list_insert(&log->list, ident);
64 if (log->summary)
65 item->util = (void *)(UTIL_TO_INT(item) + 1);
66 else {
67 char *buffer;
68 struct strbuf subject = STRBUF_INIT;
69 const char *eol;
71 /* Skip any leading whitespace, including any blank lines. */
72 while (*oneline && isspace(*oneline))
73 oneline++;
74 eol = strchr(oneline, '\n');
75 if (!eol)
76 eol = oneline + strlen(oneline);
77 if (starts_with(oneline, "[PATCH")) {
78 char *eob = strchr(oneline, ']');
79 if (eob && (!eol || eob < eol))
80 oneline = eob + 1;
82 while (*oneline && isspace(*oneline) && *oneline != '\n')
83 oneline++;
84 format_subject(&subject, oneline, " ");
85 buffer = strbuf_detach(&subject, NULL);
87 if (!item->util) {
88 item->util = xmalloc(sizeof(struct string_list));
89 string_list_init_nodup(item->util);
91 string_list_append(item->util, buffer);
95 static int parse_ident(struct shortlog *log,
96 struct strbuf *out, const char *in)
98 const char *mailbuf, *namebuf;
99 size_t namelen, maillen;
100 struct ident_split ident;
102 if (split_ident_line(&ident, in, strlen(in)))
103 return -1;
105 namebuf = ident.name_begin;
106 mailbuf = ident.mail_begin;
107 namelen = ident.name_end - ident.name_begin;
108 maillen = ident.mail_end - ident.mail_begin;
110 map_user(&log->mailmap, &mailbuf, &maillen, &namebuf, &namelen);
111 strbuf_add(out, namebuf, namelen);
112 if (log->email)
113 strbuf_addf(out, " <%.*s>", (int)maillen, mailbuf);
115 return 0;
118 static void read_from_stdin(struct shortlog *log)
120 struct strbuf ident = STRBUF_INIT;
121 struct strbuf mapped_ident = STRBUF_INIT;
122 struct strbuf oneline = STRBUF_INIT;
123 static const char *author_match[2] = { "Author: ", "author " };
124 static const char *committer_match[2] = { "Commit: ", "committer " };
125 const char **match;
127 if (HAS_MULTI_BITS(log->groups))
128 die(_("using multiple --group options with stdin is not supported"));
130 switch (log->groups) {
131 case SHORTLOG_GROUP_AUTHOR:
132 match = author_match;
133 break;
134 case SHORTLOG_GROUP_COMMITTER:
135 match = committer_match;
136 break;
137 case SHORTLOG_GROUP_TRAILER:
138 die(_("using %s with stdin is not supported"), "--group=trailer");
139 case SHORTLOG_GROUP_FORMAT:
140 die(_("using %s with stdin is not supported"), "--group=format");
141 default:
142 BUG("unhandled shortlog group");
145 while (strbuf_getline_lf(&ident, stdin) != EOF) {
146 const char *v;
147 if (!skip_prefix(ident.buf, match[0], &v) &&
148 !skip_prefix(ident.buf, match[1], &v))
149 continue;
150 while (strbuf_getline_lf(&oneline, stdin) != EOF &&
151 oneline.len)
152 ; /* discard headers */
153 while (strbuf_getline_lf(&oneline, stdin) != EOF &&
154 !oneline.len)
155 ; /* discard blanks */
157 strbuf_reset(&mapped_ident);
158 if (parse_ident(log, &mapped_ident, v) < 0)
159 continue;
161 insert_one_record(log, mapped_ident.buf, oneline.buf);
163 strbuf_release(&ident);
164 strbuf_release(&mapped_ident);
165 strbuf_release(&oneline);
168 static void insert_records_from_trailers(struct shortlog *log,
169 struct strset *dups,
170 struct commit *commit,
171 struct pretty_print_context *ctx,
172 const char *oneline)
174 struct trailer_iterator iter;
175 const char *commit_buffer, *body;
176 struct strbuf ident = STRBUF_INIT;
178 if (!log->trailers.nr)
179 return;
182 * Using repo_format_commit_message("%B") would be simpler here, but
183 * this saves us copying the message.
185 commit_buffer = repo_logmsg_reencode(the_repository, commit, NULL,
186 ctx->output_encoding);
187 body = strstr(commit_buffer, "\n\n");
188 if (!body)
189 return;
191 trailer_iterator_init(&iter, body);
192 while (trailer_iterator_advance(&iter)) {
193 const char *value = iter.val.buf;
195 if (!string_list_has_string(&log->trailers, iter.key.buf))
196 continue;
198 strbuf_reset(&ident);
199 if (!parse_ident(log, &ident, value))
200 value = ident.buf;
202 if (!strset_add(dups, value))
203 continue;
204 insert_one_record(log, value, oneline);
206 trailer_iterator_release(&iter);
208 strbuf_release(&ident);
209 repo_unuse_commit_buffer(the_repository, commit, commit_buffer);
212 static int shortlog_needs_dedup(const struct shortlog *log)
214 return HAS_MULTI_BITS(log->groups) || log->format.nr > 1 || log->trailers.nr;
217 static void insert_records_from_format(struct shortlog *log,
218 struct strset *dups,
219 struct commit *commit,
220 struct pretty_print_context *ctx,
221 const char *oneline)
223 struct strbuf buf = STRBUF_INIT;
224 struct string_list_item *item;
226 for_each_string_list_item(item, &log->format) {
227 strbuf_reset(&buf);
229 repo_format_commit_message(the_repository, commit,
230 item->string, &buf, ctx);
232 if (!shortlog_needs_dedup(log) || strset_add(dups, buf.buf))
233 insert_one_record(log, buf.buf, oneline);
236 strbuf_release(&buf);
239 void shortlog_add_commit(struct shortlog *log, struct commit *commit)
241 struct strbuf oneline = STRBUF_INIT;
242 struct strset dups = STRSET_INIT;
243 struct pretty_print_context ctx = {0};
244 const char *oneline_str;
246 ctx.fmt = CMIT_FMT_USERFORMAT;
247 ctx.abbrev = log->abbrev;
248 ctx.date_mode = log->date_mode;
249 ctx.output_encoding = get_log_output_encoding();
251 if (!log->summary) {
252 if (log->user_format)
253 pretty_print_commit(&ctx, commit, &oneline);
254 else
255 repo_format_commit_message(the_repository, commit,
256 "%s", &oneline, &ctx);
258 oneline_str = oneline.len ? oneline.buf : "<none>";
260 insert_records_from_trailers(log, &dups, commit, &ctx, oneline_str);
261 insert_records_from_format(log, &dups, commit, &ctx, oneline_str);
263 strset_clear(&dups);
264 strbuf_release(&oneline);
267 static void get_from_rev(struct rev_info *rev, struct shortlog *log)
269 struct commit *commit;
271 if (prepare_revision_walk(rev))
272 die(_("revision walk setup failed"));
273 while ((commit = get_revision(rev)) != NULL)
274 shortlog_add_commit(log, commit);
277 static int parse_uint(char const **arg, int comma, int defval)
279 unsigned long ul;
280 int ret;
281 char *endp;
283 ul = strtoul(*arg, &endp, 10);
284 if (*endp && *endp != comma)
285 return -1;
286 if (ul > INT_MAX)
287 return -1;
288 ret = *arg == endp ? defval : (int)ul;
289 *arg = *endp ? endp + 1 : endp;
290 return ret;
293 static const char wrap_arg_usage[] = "-w[<width>[,<indent1>[,<indent2>]]]";
294 #define DEFAULT_WRAPLEN 76
295 #define DEFAULT_INDENT1 6
296 #define DEFAULT_INDENT2 9
298 static int parse_wrap_args(const struct option *opt, const char *arg, int unset)
300 struct shortlog *log = opt->value;
302 log->wrap_lines = !unset;
303 if (unset)
304 return 0;
305 if (!arg) {
306 log->wrap = DEFAULT_WRAPLEN;
307 log->in1 = DEFAULT_INDENT1;
308 log->in2 = DEFAULT_INDENT2;
309 return 0;
312 log->wrap = parse_uint(&arg, ',', DEFAULT_WRAPLEN);
313 log->in1 = parse_uint(&arg, ',', DEFAULT_INDENT1);
314 log->in2 = parse_uint(&arg, '\0', DEFAULT_INDENT2);
315 if (log->wrap < 0 || log->in1 < 0 || log->in2 < 0)
316 return error(wrap_arg_usage);
317 if (log->wrap &&
318 ((log->in1 && log->wrap <= log->in1) ||
319 (log->in2 && log->wrap <= log->in2)))
320 return error(wrap_arg_usage);
321 return 0;
324 static int parse_group_option(const struct option *opt, const char *arg, int unset)
326 struct shortlog *log = opt->value;
327 const char *field;
329 if (unset) {
330 log->groups = 0;
331 string_list_clear(&log->trailers, 0);
332 string_list_clear(&log->format, 0);
333 } else if (!strcasecmp(arg, "author"))
334 log->groups |= SHORTLOG_GROUP_AUTHOR;
335 else if (!strcasecmp(arg, "committer"))
336 log->groups |= SHORTLOG_GROUP_COMMITTER;
337 else if (skip_prefix(arg, "trailer:", &field)) {
338 log->groups |= SHORTLOG_GROUP_TRAILER;
339 string_list_append(&log->trailers, field);
340 } else if (skip_prefix(arg, "format:", &field)) {
341 log->groups |= SHORTLOG_GROUP_FORMAT;
342 string_list_append(&log->format, field);
343 } else if (strchr(arg, '%')) {
344 log->groups |= SHORTLOG_GROUP_FORMAT;
345 string_list_append(&log->format, arg);
346 } else {
347 return error(_("unknown group type: %s"), arg);
350 return 0;
354 void shortlog_init(struct shortlog *log)
356 memset(log, 0, sizeof(*log));
358 read_mailmap(&log->mailmap);
360 log->list.strdup_strings = 1;
361 log->wrap = DEFAULT_WRAPLEN;
362 log->in1 = DEFAULT_INDENT1;
363 log->in2 = DEFAULT_INDENT2;
364 log->trailers.strdup_strings = 1;
365 log->trailers.cmp = strcasecmp;
366 log->format.strdup_strings = 1;
369 void shortlog_finish_setup(struct shortlog *log)
371 if (log->groups & SHORTLOG_GROUP_AUTHOR)
372 string_list_append(&log->format,
373 log->email ? "%aN <%aE>" : "%aN");
374 if (log->groups & SHORTLOG_GROUP_COMMITTER)
375 string_list_append(&log->format,
376 log->email ? "%cN <%cE>" : "%cN");
378 string_list_sort(&log->trailers);
381 int cmd_shortlog(int argc,
382 const char **argv,
383 const char *prefix,
384 struct repository *repo UNUSED)
386 struct shortlog log = { STRING_LIST_INIT_NODUP };
387 struct rev_info rev;
388 int nongit = !startup_info->have_repository;
390 const struct option options[] = {
391 OPT_BIT('c', "committer", &log.groups,
392 N_("group by committer rather than author"),
393 SHORTLOG_GROUP_COMMITTER),
394 OPT_BOOL('n', "numbered", &log.sort_by_number,
395 N_("sort output according to the number of commits per author")),
396 OPT_BOOL('s', "summary", &log.summary,
397 N_("suppress commit descriptions, only provides commit count")),
398 OPT_BOOL('e', "email", &log.email,
399 N_("show the email address of each author")),
400 OPT_CALLBACK_F('w', NULL, &log, N_("<w>[,<i1>[,<i2>]]"),
401 N_("linewrap output"), PARSE_OPT_OPTARG,
402 &parse_wrap_args),
403 OPT_CALLBACK(0, "group", &log, N_("field"),
404 N_("group by field"), parse_group_option),
405 OPT_END(),
408 struct parse_opt_ctx_t ctx;
411 * NEEDSWORK: Later on we'll call parse_revision_opt which relies on
412 * the hash algorithm being set but since we are operating outside of a
413 * Git repository we cannot determine one. This is only needed because
414 * parse_revision_opt expects hexsz for --abbrev which is irrelevant
415 * for shortlog outside of a git repository. For now explicitly set
416 * SHA1, but ideally the parsing machinery would be split between
417 * git/nongit so that we do not have to do this.
419 if (nongit && !the_hash_algo)
420 repo_set_hash_algo(the_repository, GIT_HASH_SHA1);
422 git_config(git_default_config, NULL);
423 shortlog_init(&log);
424 repo_init_revisions(the_repository, &rev, prefix);
425 parse_options_start(&ctx, argc, argv, prefix, options,
426 PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0);
428 for (;;) {
429 switch (parse_options_step(&ctx, options, shortlog_usage)) {
430 case PARSE_OPT_NON_OPTION:
431 case PARSE_OPT_UNKNOWN:
432 break;
433 case PARSE_OPT_HELP:
434 case PARSE_OPT_ERROR:
435 case PARSE_OPT_SUBCOMMAND:
436 exit(129);
437 case PARSE_OPT_COMPLETE:
438 exit(0);
439 case PARSE_OPT_DONE:
440 goto parse_done;
442 parse_revision_opt(&rev, &ctx, options, shortlog_usage);
444 parse_done:
445 revision_opts_finish(&rev);
446 argc = parse_options_end(&ctx);
448 if (nongit && argc > 1) {
449 error(_("too many arguments given outside repository"));
450 usage_with_options(shortlog_usage, options);
453 if (!nongit && setup_revisions(argc, argv, &rev, NULL) != 1) {
454 error(_("unrecognized argument: %s"), argv[1]);
455 usage_with_options(shortlog_usage, options);
458 log.user_format = rev.commit_format == CMIT_FMT_USERFORMAT;
459 log.abbrev = rev.abbrev;
460 log.file = rev.diffopt.file;
461 log.date_mode = rev.date_mode;
463 if (!log.groups)
464 log.groups = SHORTLOG_GROUP_AUTHOR;
465 shortlog_finish_setup(&log);
467 /* assume HEAD if from a tty */
468 if (!nongit && !rev.pending.nr && isatty(0))
469 add_head_to_pending(&rev);
470 if (rev.pending.nr == 0) {
471 if (isatty(0))
472 fprintf(stderr, _("(reading log message from standard input)\n"));
473 read_from_stdin(&log);
475 else
476 get_from_rev(&rev, &log);
478 shortlog_output(&log);
479 release_revisions(&rev);
480 return 0;
483 static void add_wrapped_shortlog_msg(struct strbuf *sb, const char *s,
484 const struct shortlog *log)
486 strbuf_add_wrapped_text(sb, s, log->in1, log->in2, log->wrap);
487 strbuf_addch(sb, '\n');
490 void shortlog_output(struct shortlog *log)
492 size_t i, j;
493 struct strbuf sb = STRBUF_INIT;
495 if (log->sort_by_number)
496 STABLE_QSORT(log->list.items, log->list.nr,
497 log->summary ? compare_by_counter : compare_by_list);
498 for (i = 0; i < log->list.nr; i++) {
499 const struct string_list_item *item = &log->list.items[i];
500 if (log->summary) {
501 fprintf(log->file, "%6d\t%s\n",
502 (int)UTIL_TO_INT(item), item->string);
503 } else {
504 struct string_list *onelines = item->util;
505 fprintf(log->file, "%s (%"PRIuMAX"):\n",
506 item->string, (uintmax_t)onelines->nr);
507 for (j = onelines->nr; j >= 1; j--) {
508 const char *msg = onelines->items[j - 1].string;
510 if (log->wrap_lines) {
511 strbuf_reset(&sb);
512 add_wrapped_shortlog_msg(&sb, msg, log);
513 fwrite(sb.buf, sb.len, 1, log->file);
515 else
516 fprintf(log->file, " %s\n", msg);
518 putc('\n', log->file);
519 onelines->strdup_strings = 1;
520 string_list_clear(onelines, 0);
521 free(onelines);
524 log->list.items[i].util = NULL;
527 strbuf_release(&sb);
528 log->list.strdup_strings = 1;
529 string_list_clear(&log->list, 1);
530 clear_mailmap(&log->mailmap);
531 string_list_clear(&log->format, 0);
532 string_list_clear(&log->trailers, 0);