]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/shortlog.c
treewide: be explicit about dependence on gettext.h
[thirdparty/git.git] / builtin / shortlog.c
CommitLineData
b8ec5923
JS
1#include "builtin.h"
2#include "cache.h"
b2141fc1 3#include "config.h"
b8ec5923
JS
4#include "commit.h"
5#include "diff.h"
f394e093 6#include "gettext.h"
c455c87c 7#include "string-list.h"
b8ec5923 8#include "revision.h"
3714e7c8 9#include "utf8.h"
7c1c6782 10#include "mailmap.h"
552bcac3 11#include "shortlog.h"
14ec9cbd 12#include "parse-options.h"
47beb37b 13#include "trailer.h"
449a9009 14#include "strmap.h"
b8ec5923 15
14ec9cbd 16static char const * const shortlog_usage[] = {
cd56d4e5
17 N_("git shortlog [<options>] [<revision-range>] [[--] <path>...]"),
18 N_("git log --pretty=short | git shortlog [<options>]"),
14ec9cbd
PH
19 NULL
20};
b8ec5923 21
9b21a34a
JK
22/*
23 * The util field of our string_list_items will contain one of two things:
24 *
25 * - if --summary is not in use, it will point to a string list of the
26 * oneline subjects assigned to this author
27 *
28 * - if --summary is in use, we don't need that list; we only need to know
29 * its size. So we abuse the pointer slot to store our integer counter.
30 *
31 * This macro accesses the latter.
32 */
33#define UTIL_TO_INT(x) ((intptr_t)(x)->util)
34
35static int compare_by_counter(const void *a1, const void *a2)
36{
37 const struct string_list_item *i1 = a1, *i2 = a2;
38 return UTIL_TO_INT(i2) - UTIL_TO_INT(i1);
39}
40
41static int compare_by_list(const void *a1, const void *a2)
b8ec5923 42{
c455c87c
JS
43 const struct string_list_item *i1 = a1, *i2 = a2;
44 const struct string_list *l1 = i1->util, *l2 = i2->util;
b8ec5923
JS
45
46 if (l1->nr < l2->nr)
6d6ab610 47 return 1;
b8ec5923
JS
48 else if (l1->nr == l2->nr)
49 return 0;
50 else
6d6ab610 51 return -1;
b8ec5923
JS
52}
53
552bcac3 54static void insert_one_record(struct shortlog *log,
45d93eb8 55 const char *ident,
1e931cb4 56 const char *oneline)
b8ec5923 57{
c455c87c 58 struct string_list_item *item;
1e931cb4 59
45d93eb8 60 item = string_list_insert(&log->list, ident);
b8ec5923 61
ed7eba90 62 if (log->summary)
9b21a34a 63 item->util = (void *)(UTIL_TO_INT(item) + 1);
ed7eba90 64 else {
4e168333 65 char *buffer;
ed7eba90
JK
66 struct strbuf subject = STRBUF_INIT;
67 const char *eol;
68
69 /* Skip any leading whitespace, including any blank lines. */
70 while (*oneline && isspace(*oneline))
71 oneline++;
72 eol = strchr(oneline, '\n');
73 if (!eol)
74 eol = oneline + strlen(oneline);
75 if (starts_with(oneline, "[PATCH")) {
76 char *eob = strchr(oneline, ']');
77 if (eob && (!eol || eob < eol))
78 oneline = eob + 1;
79 }
80 while (*oneline && isspace(*oneline) && *oneline != '\n')
81 oneline++;
82 format_subject(&subject, oneline, " ");
83 buffer = strbuf_detach(&subject, NULL);
b8ec5923 84
f2605051
ÆAB
85 if (!item->util) {
86 item->util = xmalloc(sizeof(struct string_list));
87 string_list_init_nodup(item->util);
88 }
ed7eba90
JK
89 string_list_append(item->util, buffer);
90 }
b8ec5923
JS
91}
92
87abb962
JK
93static int parse_ident(struct shortlog *log,
94 struct strbuf *out, const char *in)
1ab03a57
JK
95{
96 const char *mailbuf, *namebuf;
97 size_t namelen, maillen;
98 struct ident_split ident;
99
100 if (split_ident_line(&ident, in, strlen(in)))
101 return -1;
102
103 namebuf = ident.name_begin;
104 mailbuf = ident.mail_begin;
105 namelen = ident.name_end - ident.name_begin;
106 maillen = ident.mail_end - ident.mail_begin;
107
108 map_user(&log->mailmap, &mailbuf, &maillen, &namebuf, &namelen);
109 strbuf_add(out, namebuf, namelen);
110 if (log->email)
111 strbuf_addf(out, " <%.*s>", (int)maillen, mailbuf);
112
113 return 0;
114}
115
552bcac3 116static void read_from_stdin(struct shortlog *log)
b8ec5923 117{
45d93eb8
JK
118 struct strbuf ident = STRBUF_INIT;
119 struct strbuf mapped_ident = STRBUF_INIT;
50250491 120 struct strbuf oneline = STRBUF_INIT;
fbfda15f
LT
121 static const char *author_match[2] = { "Author: ", "author " };
122 static const char *committer_match[2] = { "Commit: ", "committer " };
123 const char **match;
1e931cb4 124
63d24fa0
JK
125 if (HAS_MULTI_BITS(log->groups))
126 die(_("using multiple --group options with stdin is not supported"));
127
128 switch (log->groups) {
92338c45
JK
129 case SHORTLOG_GROUP_AUTHOR:
130 match = author_match;
131 break;
132 case SHORTLOG_GROUP_COMMITTER:
133 match = committer_match;
134 break;
47beb37b 135 case SHORTLOG_GROUP_TRAILER:
b017d3da 136 die(_("using %s with stdin is not supported"), "--group=trailer");
3dc95e09
TB
137 case SHORTLOG_GROUP_FORMAT:
138 die(_("using %s with stdin is not supported"), "--group=format");
92338c45
JK
139 default:
140 BUG("unhandled shortlog group");
141 }
142
45d93eb8 143 while (strbuf_getline_lf(&ident, stdin) != EOF) {
5c3894c3 144 const char *v;
45d93eb8
JK
145 if (!skip_prefix(ident.buf, match[0], &v) &&
146 !skip_prefix(ident.buf, match[1], &v))
1e931cb4 147 continue;
a1c5405a 148 while (strbuf_getline_lf(&oneline, stdin) != EOF &&
50250491 149 oneline.len)
1e931cb4 150 ; /* discard headers */
a1c5405a 151 while (strbuf_getline_lf(&oneline, stdin) != EOF &&
50250491 152 !oneline.len)
1e931cb4 153 ; /* discard blanks */
1ab03a57 154
45d93eb8 155 strbuf_reset(&mapped_ident);
87abb962 156 if (parse_ident(log, &mapped_ident, v) < 0)
1ab03a57
JK
157 continue;
158
45d93eb8 159 insert_one_record(log, mapped_ident.buf, oneline.buf);
b8ec5923 160 }
45d93eb8
JK
161 strbuf_release(&ident);
162 strbuf_release(&mapped_ident);
50250491 163 strbuf_release(&oneline);
b8ec5923
JS
164}
165
47beb37b 166static void insert_records_from_trailers(struct shortlog *log,
63d24fa0 167 struct strset *dups,
47beb37b
JK
168 struct commit *commit,
169 struct pretty_print_context *ctx,
170 const char *oneline)
171{
172 struct trailer_iterator iter;
173 const char *commit_buffer, *body;
56d5dde7 174 struct strbuf ident = STRBUF_INIT;
47beb37b 175
0b293df9
TB
176 if (!log->trailers.nr)
177 return;
178
47beb37b
JK
179 /*
180 * Using format_commit_message("%B") would be simpler here, but
181 * this saves us copying the message.
182 */
183 commit_buffer = logmsg_reencode(commit, NULL, ctx->output_encoding);
184 body = strstr(commit_buffer, "\n\n");
185 if (!body)
186 return;
187
188 trailer_iterator_init(&iter, body);
189 while (trailer_iterator_advance(&iter)) {
190 const char *value = iter.val.buf;
191
63d24fa0 192 if (!string_list_has_string(&log->trailers, iter.key.buf))
47beb37b
JK
193 continue;
194
56d5dde7
JK
195 strbuf_reset(&ident);
196 if (!parse_ident(log, &ident, value))
197 value = ident.buf;
198
449a9009 199 if (!strset_add(dups, value))
f17b0b99 200 continue;
47beb37b
JK
201 insert_one_record(log, value, oneline);
202 }
203 trailer_iterator_release(&iter);
204
56d5dde7 205 strbuf_release(&ident);
47beb37b
JK
206 unuse_commit_buffer(commit, commit_buffer);
207}
208
3dc95e09
TB
209static int shortlog_needs_dedup(const struct shortlog *log)
210{
211 return HAS_MULTI_BITS(log->groups) || log->format.nr > 1 || log->trailers.nr;
212}
213
214static void insert_records_from_format(struct shortlog *log,
215 struct strset *dups,
216 struct commit *commit,
217 struct pretty_print_context *ctx,
218 const char *oneline)
219{
220 struct strbuf buf = STRBUF_INIT;
221 struct string_list_item *item;
222
223 for_each_string_list_item(item, &log->format) {
224 strbuf_reset(&buf);
225
226 format_commit_message(commit, item->string, &buf, ctx);
227
228 if (!shortlog_needs_dedup(log) || strset_add(dups, buf.buf))
229 insert_one_record(log, buf.buf, oneline);
230 }
231
232 strbuf_release(&buf);
233}
234
552bcac3 235void shortlog_add_commit(struct shortlog *log, struct commit *commit)
b8ec5923 236{
2db6b83d 237 struct strbuf oneline = STRBUF_INIT;
63d24fa0 238 struct strset dups = STRSET_INIT;
2db6b83d 239 struct pretty_print_context ctx = {0};
92338c45 240 const char *oneline_str;
2db6b83d
JK
241
242 ctx.fmt = CMIT_FMT_USERFORMAT;
243 ctx.abbrev = log->abbrev;
6d167fd7 244 ctx.print_email_subject = 1;
251554c2 245 ctx.date_mode = log->date_mode;
2db6b83d
JK
246 ctx.output_encoding = get_log_output_encoding();
247
4e1d1a2e
JK
248 if (!log->summary) {
249 if (log->user_format)
250 pretty_print_commit(&ctx, commit, &oneline);
552bcac3 251 else
4e1d1a2e 252 format_commit_message(commit, "%s", &oneline, &ctx);
cd4f09e3 253 }
92338c45
JK
254 oneline_str = oneline.len ? oneline.buf : "<none>";
255
0b293df9 256 insert_records_from_trailers(log, &dups, commit, &ctx, oneline_str);
3dc95e09 257 insert_records_from_format(log, &dups, commit, &ctx, oneline_str);
2db6b83d 258
63d24fa0 259 strset_clear(&dups);
2db6b83d 260 strbuf_release(&oneline);
552bcac3
DB
261}
262
263static void get_from_rev(struct rev_info *rev, struct shortlog *log)
264{
265 struct commit *commit;
266
267 if (prepare_revision_walk(rev))
ab8b53bb 268 die(_("revision walk setup failed"));
552bcac3
DB
269 while ((commit = get_revision(rev)) != NULL)
270 shortlog_add_commit(log, commit);
b8ec5923
JS
271}
272
14ec9cbd 273static int parse_uint(char const **arg, int comma, int defval)
3d711d97
JH
274{
275 unsigned long ul;
276 int ret;
277 char *endp;
278
279 ul = strtoul(*arg, &endp, 10);
14ec9cbd 280 if (*endp && *endp != comma)
3d711d97 281 return -1;
14ec9cbd 282 if (ul > INT_MAX)
3d711d97 283 return -1;
14ec9cbd
PH
284 ret = *arg == endp ? defval : (int)ul;
285 *arg = *endp ? endp + 1 : endp;
3d711d97
JH
286 return ret;
287}
288
289static const char wrap_arg_usage[] = "-w[<width>[,<indent1>[,<indent2>]]]";
290#define DEFAULT_WRAPLEN 76
291#define DEFAULT_INDENT1 6
292#define DEFAULT_INDENT2 9
293
14ec9cbd 294static int parse_wrap_args(const struct option *opt, const char *arg, int unset)
3d711d97 295{
14ec9cbd
PH
296 struct shortlog *log = opt->value;
297
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
308 log->wrap = parse_uint(&arg, ',', DEFAULT_WRAPLEN);
309 log->in1 = parse_uint(&arg, ',', DEFAULT_INDENT1);
310 log->in2 = parse_uint(&arg, '\0', DEFAULT_INDENT2);
311 if (log->wrap < 0 || log->in1 < 0 || log->in2 < 0)
312 return error(wrap_arg_usage);
313 if (log->wrap &&
314 ((log->in1 && log->wrap <= log->in1) ||
315 (log->in2 && log->wrap <= log->in2)))
316 return error(wrap_arg_usage);
317 return 0;
3d711d97
JH
318}
319
92338c45
JK
320static int parse_group_option(const struct option *opt, const char *arg, int unset)
321{
322 struct shortlog *log = opt->value;
47beb37b 323 const char *field;
92338c45 324
63d24fa0
JK
325 if (unset) {
326 log->groups = 0;
327 string_list_clear(&log->trailers, 0);
3dc95e09 328 string_list_clear(&log->format, 0);
63d24fa0
JK
329 } else if (!strcasecmp(arg, "author"))
330 log->groups |= SHORTLOG_GROUP_AUTHOR;
92338c45 331 else if (!strcasecmp(arg, "committer"))
63d24fa0 332 log->groups |= SHORTLOG_GROUP_COMMITTER;
47beb37b 333 else if (skip_prefix(arg, "trailer:", &field)) {
63d24fa0
JK
334 log->groups |= SHORTLOG_GROUP_TRAILER;
335 string_list_append(&log->trailers, field);
3dc95e09
TB
336 } else if (skip_prefix(arg, "format:", &field)) {
337 log->groups |= SHORTLOG_GROUP_FORMAT;
338 string_list_append(&log->format, field);
339 } else if (strchr(arg, '%')) {
340 log->groups |= SHORTLOG_GROUP_FORMAT;
341 string_list_append(&log->format, arg);
342 } else {
92338c45 343 return error(_("unknown group type: %s"), arg);
3dc95e09 344 }
92338c45
JK
345
346 return 0;
347}
348
349
552bcac3
DB
350void shortlog_init(struct shortlog *log)
351{
352 memset(log, 0, sizeof(*log));
353
4e168333 354 read_mailmap(&log->mailmap);
552bcac3 355
c455c87c 356 log->list.strdup_strings = 1;
552bcac3
DB
357 log->wrap = DEFAULT_WRAPLEN;
358 log->in1 = DEFAULT_INDENT1;
359 log->in2 = DEFAULT_INDENT2;
63d24fa0
JK
360 log->trailers.strdup_strings = 1;
361 log->trailers.cmp = strcasecmp;
3dc95e09 362 log->format.strdup_strings = 1;
552bcac3
DB
363}
364
10538e2a
TB
365void shortlog_finish_setup(struct shortlog *log)
366{
9c10d4ff
TB
367 if (log->groups & SHORTLOG_GROUP_AUTHOR)
368 string_list_append(&log->format,
369 log->email ? "%aN <%aE>" : "%aN");
7b11234e
TB
370 if (log->groups & SHORTLOG_GROUP_COMMITTER)
371 string_list_append(&log->format,
372 log->email ? "%cN <%cE>" : "%cN");
9c10d4ff 373
10538e2a
TB
374 string_list_sort(&log->trailers);
375}
376
b8ec5923
JS
377int cmd_shortlog(int argc, const char **argv, const char *prefix)
378{
64093fc0
JK
379 struct shortlog log = { STRING_LIST_INIT_NODUP };
380 struct rev_info rev;
773b69bf 381 int nongit = !startup_info->have_repository;
552bcac3 382
64093fc0 383 const struct option options[] = {
63d24fa0 384 OPT_BIT('c', "committer", &log.groups,
e73fe3dd 385 N_("group by committer rather than author"),
63d24fa0 386 SHORTLOG_GROUP_COMMITTER),
d5d09d47
SB
387 OPT_BOOL('n', "numbered", &log.sort_by_number,
388 N_("sort output according to the number of commits per author")),
389 OPT_BOOL('s', "summary", &log.summary,
e73fe3dd 390 N_("suppress commit descriptions, only provides commit count")),
d5d09d47 391 OPT_BOOL('e', "email", &log.email,
e73fe3dd 392 N_("show the email address of each author")),
203c8533 393 OPT_CALLBACK_F('w', NULL, &log, N_("<w>[,<i1>[,<i2>]]"),
e73fe3dd 394 N_("linewrap output"), PARSE_OPT_OPTARG,
203c8533 395 &parse_wrap_args),
92338c45 396 OPT_CALLBACK(0, "group", &log, N_("field"),
e73fe3dd 397 N_("group by field"), parse_group_option),
14ec9cbd
PH
398 OPT_END(),
399 };
400
401 struct parse_opt_ctx_t ctx;
402
d551a488 403 git_config(git_default_config, NULL);
552bcac3 404 shortlog_init(&log);
2abf3503 405 repo_init_revisions(the_repository, &rev, prefix);
9ca1169f
SB
406 parse_options_start(&ctx, argc, argv, prefix, options,
407 PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0);
14ec9cbd
PH
408
409 for (;;) {
14ec9cbd 410 switch (parse_options_step(&ctx, options, shortlog_usage)) {
352e7613
ÆAB
411 case PARSE_OPT_NON_OPTION:
412 case PARSE_OPT_UNKNOWN:
413 break;
14ec9cbd 414 case PARSE_OPT_HELP:
3bb0923f 415 case PARSE_OPT_ERROR:
fa83cc83 416 case PARSE_OPT_SUBCOMMAND:
14ec9cbd 417 exit(129);
a92ec7ef
NTND
418 case PARSE_OPT_COMPLETE:
419 exit(0);
14ec9cbd
PH
420 case PARSE_OPT_DONE:
421 goto parse_done;
3d711d97 422 }
6b61ec05 423 parse_revision_opt(&rev, &ctx, options, shortlog_usage);
14ec9cbd
PH
424 }
425parse_done:
087c7458 426 revision_opts_finish(&rev);
14ec9cbd
PH
427 argc = parse_options_end(&ctx);
428
4aa0161e
429 if (nongit && argc > 1) {
430 error(_("too many arguments given outside repository"));
431 usage_with_options(shortlog_usage, options);
432 }
433
14ec9cbd 434 if (setup_revisions(argc, argv, &rev, NULL) != 1) {
ab8b53bb 435 error(_("unrecognized argument: %s"), argv[1]);
14ec9cbd 436 usage_with_options(shortlog_usage, options);
b8ec5923
JS
437 }
438
b526f8ed 439 log.user_format = rev.commit_format == CMIT_FMT_USERFORMAT;
c1977021 440 log.abbrev = rev.abbrev;
7f7d712b 441 log.file = rev.diffopt.file;
251554c2 442 log.date_mode = rev.date_mode;
b526f8ed 443
63d24fa0
JK
444 if (!log.groups)
445 log.groups = SHORTLOG_GROUP_AUTHOR;
10538e2a 446 shortlog_finish_setup(&log);
63d24fa0 447
3384a2df 448 /* assume HEAD if from a tty */
abe549e1 449 if (!nongit && !rev.pending.nr && isatty(0))
3384a2df 450 add_head_to_pending(&rev);
0497c620 451 if (rev.pending.nr == 0) {
37314495 452 if (isatty(0))
ab8b53bb 453 fprintf(stderr, _("(reading log message from standard input)\n"));
552bcac3 454 read_from_stdin(&log);
0497c620 455 }
b8ec5923 456 else
552bcac3 457 get_from_rev(&rev, &log);
b8ec5923 458
2108fe4a
ÆAB
459 release_revisions(&rev);
460
552bcac3 461 shortlog_output(&log);
7f7d712b
JS
462 if (log.file != stdout)
463 fclose(log.file);
552bcac3
DB
464 return 0;
465}
b8ec5923 466
bb96a2c9
RS
467static void add_wrapped_shortlog_msg(struct strbuf *sb, const char *s,
468 const struct shortlog *log)
469{
5b597082
SP
470 strbuf_add_wrapped_text(sb, s, log->in1, log->in2, log->wrap);
471 strbuf_addch(sb, '\n');
bb96a2c9
RS
472}
473
552bcac3
DB
474void shortlog_output(struct shortlog *log)
475{
99d60545 476 size_t i, j;
bb96a2c9
RS
477 struct strbuf sb = STRBUF_INIT;
478
552bcac3 479 if (log->sort_by_number)
df534dcb 480 STABLE_QSORT(log->list.items, log->list.nr,
9b21a34a 481 log->summary ? compare_by_counter : compare_by_list);
552bcac3 482 for (i = 0; i < log->list.nr; i++) {
9b21a34a 483 const struct string_list_item *item = &log->list.items[i];
552bcac3 484 if (log->summary) {
0a7b3577
JS
485 fprintf(log->file, "%6d\t%s\n",
486 (int)UTIL_TO_INT(item), item->string);
ac60c94d 487 } else {
9b21a34a 488 struct string_list *onelines = item->util;
99d60545
ÆAB
489 fprintf(log->file, "%s (%"PRIuMAX"):\n",
490 item->string, (uintmax_t)onelines->nr);
491 for (j = onelines->nr; j >= 1; j--) {
492 const char *msg = onelines->items[j - 1].string;
3d711d97 493
552bcac3 494 if (log->wrap_lines) {
bb96a2c9
RS
495 strbuf_reset(&sb);
496 add_wrapped_shortlog_msg(&sb, msg, log);
0a7b3577 497 fwrite(sb.buf, sb.len, 1, log->file);
3d711d97
JH
498 }
499 else
0a7b3577 500 fprintf(log->file, " %s\n", msg);
3714e7c8 501 }
0a7b3577 502 putc('\n', log->file);
9b21a34a
JK
503 onelines->strdup_strings = 1;
504 string_list_clear(onelines, 0);
505 free(onelines);
b8ec5923
JS
506 }
507
552bcac3 508 log->list.items[i].util = NULL;
b8ec5923
JS
509 }
510
bb96a2c9 511 strbuf_release(&sb);
c455c87c
JS
512 log->list.strdup_strings = 1;
513 string_list_clear(&log->list, 1);
d20d654f 514 clear_mailmap(&log->mailmap);
3dc95e09 515 string_list_clear(&log->format, 0);
b8ec5923 516}