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