leak tests: mark passing SANITIZE=leak tests as leak-free
[git/debian.git] / builtin / shortlog.c
blob086dfee45aa8be8cc316132ce29bfb93c2ef295a
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"
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
22 * The util field of our string_list_items will contain one of two things:
24 * - if --summary is not in use, it will point to a string list of the
25 * oneline subjects assigned to this author
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.
30 * This macro accesses the latter.
32 #define UTIL_TO_INT(x) ((intptr_t)(x)->util)
34 static int compare_by_counter(const void *a1, const void *a2)
36 const struct string_list_item *i1 = a1, *i2 = a2;
37 return UTIL_TO_INT(i2) - UTIL_TO_INT(i1);
40 static int compare_by_list(const void *a1, const void *a2)
42 const struct string_list_item *i1 = a1, *i2 = a2;
43 const struct string_list *l1 = i1->util, *l2 = i2->util;
45 if (l1->nr < l2->nr)
46 return 1;
47 else if (l1->nr == l2->nr)
48 return 0;
49 else
50 return -1;
53 static void insert_one_record(struct shortlog *log,
54 const char *ident,
55 const char *oneline)
57 struct string_list_item *item;
59 item = string_list_insert(&log->list, ident);
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;
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;
79 while (*oneline && isspace(*oneline) && *oneline != '\n')
80 oneline++;
81 format_subject(&subject, oneline, " ");
82 buffer = strbuf_detach(&subject, NULL);
84 if (!item->util) {
85 item->util = xmalloc(sizeof(struct string_list));
86 string_list_init_nodup(item->util);
88 string_list_append(item->util, buffer);
92 static int parse_ident(struct shortlog *log,
93 struct strbuf *out, const char *in)
95 const char *mailbuf, *namebuf;
96 size_t namelen, maillen;
97 struct ident_split ident;
99 if (split_ident_line(&ident, in, strlen(in)))
100 return -1;
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;
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);
112 return 0;
115 static void read_from_stdin(struct shortlog *log)
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;
124 if (HAS_MULTI_BITS(log->groups))
125 die(_("using multiple --group options with stdin is not supported"));
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 --group=trailer with stdin is not supported"));
136 default:
137 BUG("unhandled shortlog group");
140 while (strbuf_getline_lf(&ident, stdin) != EOF) {
141 const char *v;
142 if (!skip_prefix(ident.buf, match[0], &v) &&
143 !skip_prefix(ident.buf, match[1], &v))
144 continue;
145 while (strbuf_getline_lf(&oneline, stdin) != EOF &&
146 oneline.len)
147 ; /* discard headers */
148 while (strbuf_getline_lf(&oneline, stdin) != EOF &&
149 !oneline.len)
150 ; /* discard blanks */
152 strbuf_reset(&mapped_ident);
153 if (parse_ident(log, &mapped_ident, v) < 0)
154 continue;
156 insert_one_record(log, mapped_ident.buf, oneline.buf);
158 strbuf_release(&ident);
159 strbuf_release(&mapped_ident);
160 strbuf_release(&oneline);
163 static void insert_records_from_trailers(struct shortlog *log,
164 struct strset *dups,
165 struct commit *commit,
166 struct pretty_print_context *ctx,
167 const char *oneline)
169 struct trailer_iterator iter;
170 const char *commit_buffer, *body;
171 struct strbuf ident = STRBUF_INIT;
174 * Using format_commit_message("%B") would be simpler here, but
175 * this saves us copying the message.
177 commit_buffer = logmsg_reencode(commit, NULL, ctx->output_encoding);
178 body = strstr(commit_buffer, "\n\n");
179 if (!body)
180 return;
182 trailer_iterator_init(&iter, body);
183 while (trailer_iterator_advance(&iter)) {
184 const char *value = iter.val.buf;
186 if (!string_list_has_string(&log->trailers, iter.key.buf))
187 continue;
189 strbuf_reset(&ident);
190 if (!parse_ident(log, &ident, value))
191 value = ident.buf;
193 if (!strset_add(dups, value))
194 continue;
195 insert_one_record(log, value, oneline);
197 trailer_iterator_release(&iter);
199 strbuf_release(&ident);
200 unuse_commit_buffer(commit, commit_buffer);
203 void shortlog_add_commit(struct shortlog *log, struct commit *commit)
205 struct strbuf ident = STRBUF_INIT;
206 struct strbuf oneline = STRBUF_INIT;
207 struct strset dups = STRSET_INIT;
208 struct pretty_print_context ctx = {0};
209 const char *oneline_str;
211 ctx.fmt = CMIT_FMT_USERFORMAT;
212 ctx.abbrev = log->abbrev;
213 ctx.print_email_subject = 1;
214 ctx.date_mode.type = DATE_NORMAL;
215 ctx.output_encoding = get_log_output_encoding();
217 if (!log->summary) {
218 if (log->user_format)
219 pretty_print_commit(&ctx, commit, &oneline);
220 else
221 format_commit_message(commit, "%s", &oneline, &ctx);
223 oneline_str = oneline.len ? oneline.buf : "<none>";
225 if (log->groups & SHORTLOG_GROUP_AUTHOR) {
226 strbuf_reset(&ident);
227 format_commit_message(commit,
228 log->email ? "%aN <%aE>" : "%aN",
229 &ident, &ctx);
230 if (!HAS_MULTI_BITS(log->groups) ||
231 strset_add(&dups, ident.buf))
232 insert_one_record(log, ident.buf, oneline_str);
234 if (log->groups & SHORTLOG_GROUP_COMMITTER) {
235 strbuf_reset(&ident);
236 format_commit_message(commit,
237 log->email ? "%cN <%cE>" : "%cN",
238 &ident, &ctx);
239 if (!HAS_MULTI_BITS(log->groups) ||
240 strset_add(&dups, ident.buf))
241 insert_one_record(log, ident.buf, oneline_str);
243 if (log->groups & SHORTLOG_GROUP_TRAILER) {
244 insert_records_from_trailers(log, &dups, commit, &ctx, oneline_str);
247 strset_clear(&dups);
248 strbuf_release(&ident);
249 strbuf_release(&oneline);
252 static void get_from_rev(struct rev_info *rev, struct shortlog *log)
254 struct commit *commit;
256 if (prepare_revision_walk(rev))
257 die(_("revision walk setup failed"));
258 while ((commit = get_revision(rev)) != NULL)
259 shortlog_add_commit(log, commit);
262 static int parse_uint(char const **arg, int comma, int defval)
264 unsigned long ul;
265 int ret;
266 char *endp;
268 ul = strtoul(*arg, &endp, 10);
269 if (*endp && *endp != comma)
270 return -1;
271 if (ul > INT_MAX)
272 return -1;
273 ret = *arg == endp ? defval : (int)ul;
274 *arg = *endp ? endp + 1 : endp;
275 return ret;
278 static const char wrap_arg_usage[] = "-w[<width>[,<indent1>[,<indent2>]]]";
279 #define DEFAULT_WRAPLEN 76
280 #define DEFAULT_INDENT1 6
281 #define DEFAULT_INDENT2 9
283 static int parse_wrap_args(const struct option *opt, const char *arg, int unset)
285 struct shortlog *log = opt->value;
287 log->wrap_lines = !unset;
288 if (unset)
289 return 0;
290 if (!arg) {
291 log->wrap = DEFAULT_WRAPLEN;
292 log->in1 = DEFAULT_INDENT1;
293 log->in2 = DEFAULT_INDENT2;
294 return 0;
297 log->wrap = parse_uint(&arg, ',', DEFAULT_WRAPLEN);
298 log->in1 = parse_uint(&arg, ',', DEFAULT_INDENT1);
299 log->in2 = parse_uint(&arg, '\0', DEFAULT_INDENT2);
300 if (log->wrap < 0 || log->in1 < 0 || log->in2 < 0)
301 return error(wrap_arg_usage);
302 if (log->wrap &&
303 ((log->in1 && log->wrap <= log->in1) ||
304 (log->in2 && log->wrap <= log->in2)))
305 return error(wrap_arg_usage);
306 return 0;
309 static int parse_group_option(const struct option *opt, const char *arg, int unset)
311 struct shortlog *log = opt->value;
312 const char *field;
314 if (unset) {
315 log->groups = 0;
316 string_list_clear(&log->trailers, 0);
317 } else if (!strcasecmp(arg, "author"))
318 log->groups |= SHORTLOG_GROUP_AUTHOR;
319 else if (!strcasecmp(arg, "committer"))
320 log->groups |= SHORTLOG_GROUP_COMMITTER;
321 else if (skip_prefix(arg, "trailer:", &field)) {
322 log->groups |= SHORTLOG_GROUP_TRAILER;
323 string_list_append(&log->trailers, field);
324 } else
325 return error(_("unknown group type: %s"), arg);
327 return 0;
331 void shortlog_init(struct shortlog *log)
333 memset(log, 0, sizeof(*log));
335 read_mailmap(&log->mailmap);
337 log->list.strdup_strings = 1;
338 log->wrap = DEFAULT_WRAPLEN;
339 log->in1 = DEFAULT_INDENT1;
340 log->in2 = DEFAULT_INDENT2;
341 log->trailers.strdup_strings = 1;
342 log->trailers.cmp = strcasecmp;
345 int cmd_shortlog(int argc, const char **argv, const char *prefix)
347 struct shortlog log = { STRING_LIST_INIT_NODUP };
348 struct rev_info rev;
349 int nongit = !startup_info->have_repository;
351 const struct option options[] = {
352 OPT_BIT('c', "committer", &log.groups,
353 N_("group by committer rather than author"),
354 SHORTLOG_GROUP_COMMITTER),
355 OPT_BOOL('n', "numbered", &log.sort_by_number,
356 N_("sort output according to the number of commits per author")),
357 OPT_BOOL('s', "summary", &log.summary,
358 N_("suppress commit descriptions, only provides commit count")),
359 OPT_BOOL('e', "email", &log.email,
360 N_("show the email address of each author")),
361 OPT_CALLBACK_F('w', NULL, &log, N_("<w>[,<i1>[,<i2>]]"),
362 N_("linewrap output"), PARSE_OPT_OPTARG,
363 &parse_wrap_args),
364 OPT_CALLBACK(0, "group", &log, N_("field"),
365 N_("group by field"), parse_group_option),
366 OPT_END(),
369 struct parse_opt_ctx_t ctx;
371 git_config(git_default_config, NULL);
372 shortlog_init(&log);
373 repo_init_revisions(the_repository, &rev, prefix);
374 parse_options_start(&ctx, argc, argv, prefix, options,
375 PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0);
377 for (;;) {
378 switch (parse_options_step(&ctx, options, shortlog_usage)) {
379 case PARSE_OPT_NON_OPTION:
380 case PARSE_OPT_UNKNOWN:
381 break;
382 case PARSE_OPT_HELP:
383 case PARSE_OPT_ERROR:
384 exit(129);
385 case PARSE_OPT_COMPLETE:
386 exit(0);
387 case PARSE_OPT_DONE:
388 goto parse_done;
390 parse_revision_opt(&rev, &ctx, options, shortlog_usage);
392 parse_done:
393 revision_opts_finish(&rev);
394 argc = parse_options_end(&ctx);
396 if (nongit && argc > 1) {
397 error(_("too many arguments given outside repository"));
398 usage_with_options(shortlog_usage, options);
401 if (setup_revisions(argc, argv, &rev, NULL) != 1) {
402 error(_("unrecognized argument: %s"), argv[1]);
403 usage_with_options(shortlog_usage, options);
406 log.user_format = rev.commit_format == CMIT_FMT_USERFORMAT;
407 log.abbrev = rev.abbrev;
408 log.file = rev.diffopt.file;
410 if (!log.groups)
411 log.groups = SHORTLOG_GROUP_AUTHOR;
412 string_list_sort(&log.trailers);
414 /* assume HEAD if from a tty */
415 if (!nongit && !rev.pending.nr && isatty(0))
416 add_head_to_pending(&rev);
417 if (rev.pending.nr == 0) {
418 if (isatty(0))
419 fprintf(stderr, _("(reading log message from standard input)\n"));
420 read_from_stdin(&log);
422 else
423 get_from_rev(&rev, &log);
425 release_revisions(&rev);
427 shortlog_output(&log);
428 if (log.file != stdout)
429 fclose(log.file);
430 return 0;
433 static void add_wrapped_shortlog_msg(struct strbuf *sb, const char *s,
434 const struct shortlog *log)
436 strbuf_add_wrapped_text(sb, s, log->in1, log->in2, log->wrap);
437 strbuf_addch(sb, '\n');
440 void shortlog_output(struct shortlog *log)
442 size_t i, j;
443 struct strbuf sb = STRBUF_INIT;
445 if (log->sort_by_number)
446 STABLE_QSORT(log->list.items, log->list.nr,
447 log->summary ? compare_by_counter : compare_by_list);
448 for (i = 0; i < log->list.nr; i++) {
449 const struct string_list_item *item = &log->list.items[i];
450 if (log->summary) {
451 fprintf(log->file, "%6d\t%s\n",
452 (int)UTIL_TO_INT(item), item->string);
453 } else {
454 struct string_list *onelines = item->util;
455 fprintf(log->file, "%s (%"PRIuMAX"):\n",
456 item->string, (uintmax_t)onelines->nr);
457 for (j = onelines->nr; j >= 1; j--) {
458 const char *msg = onelines->items[j - 1].string;
460 if (log->wrap_lines) {
461 strbuf_reset(&sb);
462 add_wrapped_shortlog_msg(&sb, msg, log);
463 fwrite(sb.buf, sb.len, 1, log->file);
465 else
466 fprintf(log->file, " %s\n", msg);
468 putc('\n', log->file);
469 onelines->strdup_strings = 1;
470 string_list_clear(onelines, 0);
471 free(onelines);
474 log->list.items[i].util = NULL;
477 strbuf_release(&sb);
478 log->list.strdup_strings = 1;
479 string_list_clear(&log->list, 1);
480 clear_mailmap(&log->mailmap);