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