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