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