]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/shortlog.c
commit -a -m: allow the top-level tree to become empty again
[thirdparty/git.git] / builtin / shortlog.c
1 #include "builtin.h"
2 #include "cache.h"
3 #include "config.h"
4 #include "commit.h"
5 #include "diff.h"
6 #include "string-list.h"
7 #include "revision.h"
8 #include "utf8.h"
9 #include "mailmap.h"
10 #include "shortlog.h"
11 #include "parse-options.h"
12 #include "trailer.h"
13 #include "strmap.h"
14
15 static char const * const shortlog_usage[] = {
16 N_("git shortlog [<options>] [<revision-range>] [[--] <path>...]"),
17 N_("git log --pretty=short | git shortlog [<options>]"),
18 NULL
19 };
20
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
34 static 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
40 static int compare_by_list(const void *a1, const void *a2)
41 {
42 const struct string_list_item *i1 = a1, *i2 = a2;
43 const struct string_list *l1 = i1->util, *l2 = i2->util;
44
45 if (l1->nr < l2->nr)
46 return 1;
47 else if (l1->nr == l2->nr)
48 return 0;
49 else
50 return -1;
51 }
52
53 static void insert_one_record(struct shortlog *log,
54 const char *ident,
55 const char *oneline)
56 {
57 struct string_list_item *item;
58
59 item = string_list_insert(&log->list, ident);
60
61 if (log->summary)
62 item->util = (void *)(UTIL_TO_INT(item) + 1);
63 else {
64 char *buffer;
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);
83
84 if (!item->util) {
85 item->util = xmalloc(sizeof(struct string_list));
86 string_list_init_nodup(item->util);
87 }
88 string_list_append(item->util, buffer);
89 }
90 }
91
92 static int parse_ident(struct shortlog *log,
93 struct strbuf *out, const char *in)
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
115 static void read_from_stdin(struct shortlog *log)
116 {
117 struct strbuf ident = STRBUF_INIT;
118 struct strbuf mapped_ident = STRBUF_INIT;
119 struct strbuf oneline = STRBUF_INIT;
120 static const char *author_match[2] = { "Author: ", "author " };
121 static const char *committer_match[2] = { "Commit: ", "committer " };
122 const char **match;
123
124 if (HAS_MULTI_BITS(log->groups))
125 die(_("using multiple --group options with stdin is not supported"));
126
127 switch (log->groups) {
128 case SHORTLOG_GROUP_AUTHOR:
129 match = author_match;
130 break;
131 case SHORTLOG_GROUP_COMMITTER:
132 match = committer_match;
133 break;
134 case SHORTLOG_GROUP_TRAILER:
135 die(_("using %s with stdin is not supported"), "--group=trailer");
136 case SHORTLOG_GROUP_FORMAT:
137 die(_("using %s with stdin is not supported"), "--group=format");
138 default:
139 BUG("unhandled shortlog group");
140 }
141
142 while (strbuf_getline_lf(&ident, stdin) != EOF) {
143 const char *v;
144 if (!skip_prefix(ident.buf, match[0], &v) &&
145 !skip_prefix(ident.buf, match[1], &v))
146 continue;
147 while (strbuf_getline_lf(&oneline, stdin) != EOF &&
148 oneline.len)
149 ; /* discard headers */
150 while (strbuf_getline_lf(&oneline, stdin) != EOF &&
151 !oneline.len)
152 ; /* discard blanks */
153
154 strbuf_reset(&mapped_ident);
155 if (parse_ident(log, &mapped_ident, v) < 0)
156 continue;
157
158 insert_one_record(log, mapped_ident.buf, oneline.buf);
159 }
160 strbuf_release(&ident);
161 strbuf_release(&mapped_ident);
162 strbuf_release(&oneline);
163 }
164
165 static void insert_records_from_trailers(struct shortlog *log,
166 struct strset *dups,
167 struct commit *commit,
168 struct pretty_print_context *ctx,
169 const char *oneline)
170 {
171 struct trailer_iterator iter;
172 const char *commit_buffer, *body;
173 struct strbuf ident = STRBUF_INIT;
174
175 if (!log->trailers.nr)
176 return;
177
178 /*
179 * Using format_commit_message("%B") would be simpler here, but
180 * this saves us copying the message.
181 */
182 commit_buffer = logmsg_reencode(commit, NULL, ctx->output_encoding);
183 body = strstr(commit_buffer, "\n\n");
184 if (!body)
185 return;
186
187 trailer_iterator_init(&iter, body);
188 while (trailer_iterator_advance(&iter)) {
189 const char *value = iter.val.buf;
190
191 if (!string_list_has_string(&log->trailers, iter.key.buf))
192 continue;
193
194 strbuf_reset(&ident);
195 if (!parse_ident(log, &ident, value))
196 value = ident.buf;
197
198 if (!strset_add(dups, value))
199 continue;
200 insert_one_record(log, value, oneline);
201 }
202 trailer_iterator_release(&iter);
203
204 strbuf_release(&ident);
205 unuse_commit_buffer(commit, commit_buffer);
206 }
207
208 static int shortlog_needs_dedup(const struct shortlog *log)
209 {
210 return HAS_MULTI_BITS(log->groups) || log->format.nr > 1 || log->trailers.nr;
211 }
212
213 static void insert_records_from_format(struct shortlog *log,
214 struct strset *dups,
215 struct commit *commit,
216 struct pretty_print_context *ctx,
217 const char *oneline)
218 {
219 struct strbuf buf = STRBUF_INIT;
220 struct string_list_item *item;
221
222 for_each_string_list_item(item, &log->format) {
223 strbuf_reset(&buf);
224
225 format_commit_message(commit, item->string, &buf, ctx);
226
227 if (!shortlog_needs_dedup(log) || strset_add(dups, buf.buf))
228 insert_one_record(log, buf.buf, oneline);
229 }
230
231 strbuf_release(&buf);
232 }
233
234 void shortlog_add_commit(struct shortlog *log, struct commit *commit)
235 {
236 struct strbuf oneline = STRBUF_INIT;
237 struct strset dups = STRSET_INIT;
238 struct pretty_print_context ctx = {0};
239 const char *oneline_str;
240
241 ctx.fmt = CMIT_FMT_USERFORMAT;
242 ctx.abbrev = log->abbrev;
243 ctx.print_email_subject = 1;
244 ctx.date_mode = log->date_mode;
245 ctx.output_encoding = get_log_output_encoding();
246
247 if (!log->summary) {
248 if (log->user_format)
249 pretty_print_commit(&ctx, commit, &oneline);
250 else
251 format_commit_message(commit, "%s", &oneline, &ctx);
252 }
253 oneline_str = oneline.len ? oneline.buf : "<none>";
254
255 insert_records_from_trailers(log, &dups, commit, &ctx, oneline_str);
256 insert_records_from_format(log, &dups, commit, &ctx, oneline_str);
257
258 strset_clear(&dups);
259 strbuf_release(&oneline);
260 }
261
262 static void get_from_rev(struct rev_info *rev, struct shortlog *log)
263 {
264 struct commit *commit;
265
266 if (prepare_revision_walk(rev))
267 die(_("revision walk setup failed"));
268 while ((commit = get_revision(rev)) != NULL)
269 shortlog_add_commit(log, commit);
270 }
271
272 static int parse_uint(char const **arg, int comma, int defval)
273 {
274 unsigned long ul;
275 int ret;
276 char *endp;
277
278 ul = strtoul(*arg, &endp, 10);
279 if (*endp && *endp != comma)
280 return -1;
281 if (ul > INT_MAX)
282 return -1;
283 ret = *arg == endp ? defval : (int)ul;
284 *arg = *endp ? endp + 1 : endp;
285 return ret;
286 }
287
288 static 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
293 static int parse_wrap_args(const struct option *opt, const char *arg, int unset)
294 {
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;
317 }
318
319 static int parse_group_option(const struct option *opt, const char *arg, int unset)
320 {
321 struct shortlog *log = opt->value;
322 const char *field;
323
324 if (unset) {
325 log->groups = 0;
326 string_list_clear(&log->trailers, 0);
327 string_list_clear(&log->format, 0);
328 } else if (!strcasecmp(arg, "author"))
329 log->groups |= SHORTLOG_GROUP_AUTHOR;
330 else if (!strcasecmp(arg, "committer"))
331 log->groups |= SHORTLOG_GROUP_COMMITTER;
332 else if (skip_prefix(arg, "trailer:", &field)) {
333 log->groups |= SHORTLOG_GROUP_TRAILER;
334 string_list_append(&log->trailers, field);
335 } else if (skip_prefix(arg, "format:", &field)) {
336 log->groups |= SHORTLOG_GROUP_FORMAT;
337 string_list_append(&log->format, field);
338 } else if (strchr(arg, '%')) {
339 log->groups |= SHORTLOG_GROUP_FORMAT;
340 string_list_append(&log->format, arg);
341 } else {
342 return error(_("unknown group type: %s"), arg);
343 }
344
345 return 0;
346 }
347
348
349 void shortlog_init(struct shortlog *log)
350 {
351 memset(log, 0, sizeof(*log));
352
353 read_mailmap(&log->mailmap);
354
355 log->list.strdup_strings = 1;
356 log->wrap = DEFAULT_WRAPLEN;
357 log->in1 = DEFAULT_INDENT1;
358 log->in2 = DEFAULT_INDENT2;
359 log->trailers.strdup_strings = 1;
360 log->trailers.cmp = strcasecmp;
361 log->format.strdup_strings = 1;
362 }
363
364 void shortlog_finish_setup(struct shortlog *log)
365 {
366 if (log->groups & SHORTLOG_GROUP_AUTHOR)
367 string_list_append(&log->format,
368 log->email ? "%aN <%aE>" : "%aN");
369 if (log->groups & SHORTLOG_GROUP_COMMITTER)
370 string_list_append(&log->format,
371 log->email ? "%cN <%cE>" : "%cN");
372
373 string_list_sort(&log->trailers);
374 }
375
376 int cmd_shortlog(int argc, const char **argv, const char *prefix)
377 {
378 struct shortlog log = { STRING_LIST_INIT_NODUP };
379 struct rev_info rev;
380 int nongit = !startup_info->have_repository;
381
382 const struct option options[] = {
383 OPT_BIT('c', "committer", &log.groups,
384 N_("group by committer rather than author"),
385 SHORTLOG_GROUP_COMMITTER),
386 OPT_BOOL('n', "numbered", &log.sort_by_number,
387 N_("sort output according to the number of commits per author")),
388 OPT_BOOL('s', "summary", &log.summary,
389 N_("suppress commit descriptions, only provides commit count")),
390 OPT_BOOL('e', "email", &log.email,
391 N_("show the email address of each author")),
392 OPT_CALLBACK_F('w', NULL, &log, N_("<w>[,<i1>[,<i2>]]"),
393 N_("linewrap output"), PARSE_OPT_OPTARG,
394 &parse_wrap_args),
395 OPT_CALLBACK(0, "group", &log, N_("field"),
396 N_("group by field"), parse_group_option),
397 OPT_END(),
398 };
399
400 struct parse_opt_ctx_t ctx;
401
402 git_config(git_default_config, NULL);
403 shortlog_init(&log);
404 repo_init_revisions(the_repository, &rev, prefix);
405 parse_options_start(&ctx, argc, argv, prefix, options,
406 PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0);
407
408 for (;;) {
409 switch (parse_options_step(&ctx, options, shortlog_usage)) {
410 case PARSE_OPT_NON_OPTION:
411 case PARSE_OPT_UNKNOWN:
412 break;
413 case PARSE_OPT_HELP:
414 case PARSE_OPT_ERROR:
415 case PARSE_OPT_SUBCOMMAND:
416 exit(129);
417 case PARSE_OPT_COMPLETE:
418 exit(0);
419 case PARSE_OPT_DONE:
420 goto parse_done;
421 }
422 parse_revision_opt(&rev, &ctx, options, shortlog_usage);
423 }
424 parse_done:
425 revision_opts_finish(&rev);
426 argc = parse_options_end(&ctx);
427
428 if (nongit && argc > 1) {
429 error(_("too many arguments given outside repository"));
430 usage_with_options(shortlog_usage, options);
431 }
432
433 if (setup_revisions(argc, argv, &rev, NULL) != 1) {
434 error(_("unrecognized argument: %s"), argv[1]);
435 usage_with_options(shortlog_usage, options);
436 }
437
438 log.user_format = rev.commit_format == CMIT_FMT_USERFORMAT;
439 log.abbrev = rev.abbrev;
440 log.file = rev.diffopt.file;
441 log.date_mode = rev.date_mode;
442
443 if (!log.groups)
444 log.groups = SHORTLOG_GROUP_AUTHOR;
445 shortlog_finish_setup(&log);
446
447 /* assume HEAD if from a tty */
448 if (!nongit && !rev.pending.nr && isatty(0))
449 add_head_to_pending(&rev);
450 if (rev.pending.nr == 0) {
451 if (isatty(0))
452 fprintf(stderr, _("(reading log message from standard input)\n"));
453 read_from_stdin(&log);
454 }
455 else
456 get_from_rev(&rev, &log);
457
458 release_revisions(&rev);
459
460 shortlog_output(&log);
461 if (log.file != stdout)
462 fclose(log.file);
463 return 0;
464 }
465
466 static void add_wrapped_shortlog_msg(struct strbuf *sb, const char *s,
467 const struct shortlog *log)
468 {
469 strbuf_add_wrapped_text(sb, s, log->in1, log->in2, log->wrap);
470 strbuf_addch(sb, '\n');
471 }
472
473 void shortlog_output(struct shortlog *log)
474 {
475 size_t i, j;
476 struct strbuf sb = STRBUF_INIT;
477
478 if (log->sort_by_number)
479 STABLE_QSORT(log->list.items, log->list.nr,
480 log->summary ? compare_by_counter : compare_by_list);
481 for (i = 0; i < log->list.nr; i++) {
482 const struct string_list_item *item = &log->list.items[i];
483 if (log->summary) {
484 fprintf(log->file, "%6d\t%s\n",
485 (int)UTIL_TO_INT(item), item->string);
486 } else {
487 struct string_list *onelines = item->util;
488 fprintf(log->file, "%s (%"PRIuMAX"):\n",
489 item->string, (uintmax_t)onelines->nr);
490 for (j = onelines->nr; j >= 1; j--) {
491 const char *msg = onelines->items[j - 1].string;
492
493 if (log->wrap_lines) {
494 strbuf_reset(&sb);
495 add_wrapped_shortlog_msg(&sb, msg, log);
496 fwrite(sb.buf, sb.len, 1, log->file);
497 }
498 else
499 fprintf(log->file, " %s\n", msg);
500 }
501 putc('\n', log->file);
502 onelines->strdup_strings = 1;
503 string_list_clear(onelines, 0);
504 free(onelines);
505 }
506
507 log->list.items[i].util = NULL;
508 }
509
510 strbuf_release(&sb);
511 log->list.strdup_strings = 1;
512 string_list_clear(&log->list, 1);
513 clear_mailmap(&log->mailmap);
514 string_list_clear(&log->format, 0);
515 }