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