Merge branch 'ad/t4129-setfacl-target-fix'
[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                 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);
84
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                                 }
93                         }
94                 }
95
96                 if (item->util == NULL)
97                         item->util = xcalloc(1, sizeof(struct string_list));
98                 string_list_append(item->util, buffer);
99         }
100 }
101
102 static int parse_ident(struct shortlog *log,
103                        struct strbuf *out, const char *in)
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
125 static void read_from_stdin(struct shortlog *log)
126 {
127         struct strbuf ident = STRBUF_INIT;
128         struct strbuf mapped_ident = STRBUF_INIT;
129         struct strbuf oneline = STRBUF_INIT;
130         static const char *author_match[2] = { "Author: ", "author " };
131         static const char *committer_match[2] = { "Commit: ", "committer " };
132         const char **match;
133
134         if (HAS_MULTI_BITS(log->groups))
135                 die(_("using multiple --group options with stdin is not supported"));
136
137         switch (log->groups) {
138         case SHORTLOG_GROUP_AUTHOR:
139                 match = author_match;
140                 break;
141         case SHORTLOG_GROUP_COMMITTER:
142                 match = committer_match;
143                 break;
144         case SHORTLOG_GROUP_TRAILER:
145                 die(_("using --group=trailer with stdin is not supported"));
146         default:
147                 BUG("unhandled shortlog group");
148         }
149
150         while (strbuf_getline_lf(&ident, stdin) != EOF) {
151                 const char *v;
152                 if (!skip_prefix(ident.buf, match[0], &v) &&
153                     !skip_prefix(ident.buf, match[1], &v))
154                         continue;
155                 while (strbuf_getline_lf(&oneline, stdin) != EOF &&
156                        oneline.len)
157                         ; /* discard headers */
158                 while (strbuf_getline_lf(&oneline, stdin) != EOF &&
159                        !oneline.len)
160                         ; /* discard blanks */
161
162                 strbuf_reset(&mapped_ident);
163                 if (parse_ident(log, &mapped_ident, v) < 0)
164                         continue;
165
166                 insert_one_record(log, mapped_ident.buf, oneline.buf);
167         }
168         strbuf_release(&ident);
169         strbuf_release(&mapped_ident);
170         strbuf_release(&oneline);
171 }
172
173 static void insert_records_from_trailers(struct shortlog *log,
174                                          struct strset *dups,
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;
181         struct strbuf ident = STRBUF_INIT;
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
196                 if (!string_list_has_string(&log->trailers, iter.key.buf))
197                         continue;
198
199                 strbuf_reset(&ident);
200                 if (!parse_ident(log, &ident, value))
201                         value = ident.buf;
202
203                 if (!strset_add(dups, value))
204                         continue;
205                 insert_one_record(log, value, oneline);
206         }
207         trailer_iterator_release(&iter);
208
209         strbuf_release(&ident);
210         unuse_commit_buffer(commit, commit_buffer);
211 }
212
213 void shortlog_add_commit(struct shortlog *log, struct commit *commit)
214 {
215         struct strbuf ident = STRBUF_INIT;
216         struct strbuf oneline = STRBUF_INIT;
217         struct strset dups = STRSET_INIT;
218         struct pretty_print_context ctx = {0};
219         const char *oneline_str;
220
221         ctx.fmt = CMIT_FMT_USERFORMAT;
222         ctx.abbrev = log->abbrev;
223         ctx.print_email_subject = 1;
224         ctx.date_mode.type = DATE_NORMAL;
225         ctx.output_encoding = get_log_output_encoding();
226
227         if (!log->summary) {
228                 if (log->user_format)
229                         pretty_print_commit(&ctx, commit, &oneline);
230                 else
231                         format_commit_message(commit, "%s", &oneline, &ctx);
232         }
233         oneline_str = oneline.len ? oneline.buf : "<none>";
234
235         if (log->groups & SHORTLOG_GROUP_AUTHOR) {
236                 strbuf_reset(&ident);
237                 format_commit_message(commit,
238                                       log->email ? "%aN <%aE>" : "%aN",
239                                       &ident, &ctx);
240                 if (!HAS_MULTI_BITS(log->groups) ||
241                     strset_add(&dups, ident.buf))
242                         insert_one_record(log, ident.buf, oneline_str);
243         }
244         if (log->groups & SHORTLOG_GROUP_COMMITTER) {
245                 strbuf_reset(&ident);
246                 format_commit_message(commit,
247                                       log->email ? "%cN <%cE>" : "%cN",
248                                       &ident, &ctx);
249                 if (!HAS_MULTI_BITS(log->groups) ||
250                     strset_add(&dups, ident.buf))
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);
255         }
256
257         strset_clear(&dups);
258         strbuf_release(&ident);
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         } else if (!strcasecmp(arg, "author"))
328                 log->groups |= SHORTLOG_GROUP_AUTHOR;
329         else if (!strcasecmp(arg, "committer"))
330                 log->groups |= SHORTLOG_GROUP_COMMITTER;
331         else if (skip_prefix(arg, "trailer:", &field)) {
332                 log->groups |= SHORTLOG_GROUP_TRAILER;
333                 string_list_append(&log->trailers, field);
334         } else
335                 return error(_("unknown group type: %s"), arg);
336
337         return 0;
338 }
339
340
341 void shortlog_init(struct shortlog *log)
342 {
343         memset(log, 0, sizeof(*log));
344
345         read_mailmap(&log->mailmap, &log->common_repo_prefix);
346
347         log->list.strdup_strings = 1;
348         log->wrap = DEFAULT_WRAPLEN;
349         log->in1 = DEFAULT_INDENT1;
350         log->in2 = DEFAULT_INDENT2;
351         log->trailers.strdup_strings = 1;
352         log->trailers.cmp = strcasecmp;
353 }
354
355 int cmd_shortlog(int argc, const char **argv, const char *prefix)
356 {
357         struct shortlog log = { STRING_LIST_INIT_NODUP };
358         struct rev_info rev;
359         int nongit = !startup_info->have_repository;
360
361         const struct option options[] = {
362                 OPT_BIT('c', "committer", &log.groups,
363                         N_("group by committer rather than author"),
364                         SHORTLOG_GROUP_COMMITTER),
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")),
371                 OPT_CALLBACK_F('w', NULL, &log, N_("<w>[,<i1>[,<i2>]]"),
372                         N_("linewrap output"), PARSE_OPT_OPTARG,
373                         &parse_wrap_args),
374                 OPT_CALLBACK(0, "group", &log, N_("field"),
375                         N_("group by field"), parse_group_option),
376                 OPT_END(),
377         };
378
379         struct parse_opt_ctx_t ctx;
380
381         git_config(git_default_config, NULL);
382         shortlog_init(&log);
383         repo_init_revisions(the_repository, &rev, prefix);
384         parse_options_start(&ctx, argc, argv, prefix, options,
385                             PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0);
386
387         for (;;) {
388                 switch (parse_options_step(&ctx, options, shortlog_usage)) {
389                 case PARSE_OPT_HELP:
390                 case PARSE_OPT_ERROR:
391                         exit(129);
392                 case PARSE_OPT_COMPLETE:
393                         exit(0);
394                 case PARSE_OPT_DONE:
395                         goto parse_done;
396                 }
397                 parse_revision_opt(&rev, &ctx, options, shortlog_usage);
398         }
399 parse_done:
400         argc = parse_options_end(&ctx);
401
402         if (nongit && argc > 1) {
403                 error(_("too many arguments given outside repository"));
404                 usage_with_options(shortlog_usage, options);
405         }
406
407         if (setup_revisions(argc, argv, &rev, NULL) != 1) {
408                 error(_("unrecognized argument: %s"), argv[1]);
409                 usage_with_options(shortlog_usage, options);
410         }
411
412         log.user_format = rev.commit_format == CMIT_FMT_USERFORMAT;
413         log.abbrev = rev.abbrev;
414         log.file = rev.diffopt.file;
415
416         if (!log.groups)
417                 log.groups = SHORTLOG_GROUP_AUTHOR;
418         string_list_sort(&log.trailers);
419
420         /* assume HEAD if from a tty */
421         if (!nongit && !rev.pending.nr && isatty(0))
422                 add_head_to_pending(&rev);
423         if (rev.pending.nr == 0) {
424                 if (isatty(0))
425                         fprintf(stderr, _("(reading log message from standard input)\n"));
426                 read_from_stdin(&log);
427         }
428         else
429                 get_from_rev(&rev, &log);
430
431         shortlog_output(&log);
432         if (log.file != stdout)
433                 fclose(log.file);
434         return 0;
435 }
436
437 static void add_wrapped_shortlog_msg(struct strbuf *sb, const char *s,
438                                      const struct shortlog *log)
439 {
440         strbuf_add_wrapped_text(sb, s, log->in1, log->in2, log->wrap);
441         strbuf_addch(sb, '\n');
442 }
443
444 void shortlog_output(struct shortlog *log)
445 {
446         int i, j;
447         struct strbuf sb = STRBUF_INIT;
448
449         if (log->sort_by_number)
450                 QSORT(log->list.items, log->list.nr,
451                       log->summary ? compare_by_counter : compare_by_list);
452         for (i = 0; i < log->list.nr; i++) {
453                 const struct string_list_item *item = &log->list.items[i];
454                 if (log->summary) {
455                         fprintf(log->file, "%6d\t%s\n",
456                                 (int)UTIL_TO_INT(item), item->string);
457                 } else {
458                         struct string_list *onelines = item->util;
459                         fprintf(log->file, "%s (%d):\n",
460                                 item->string, onelines->nr);
461                         for (j = onelines->nr - 1; j >= 0; j--) {
462                                 const char *msg = onelines->items[j].string;
463
464                                 if (log->wrap_lines) {
465                                         strbuf_reset(&sb);
466                                         add_wrapped_shortlog_msg(&sb, msg, log);
467                                         fwrite(sb.buf, sb.len, 1, log->file);
468                                 }
469                                 else
470                                         fprintf(log->file, "      %s\n", msg);
471                         }
472                         putc('\n', log->file);
473                         onelines->strdup_strings = 1;
474                         string_list_clear(onelines, 0);
475                         free(onelines);
476                 }
477
478                 log->list.items[i].util = NULL;
479         }
480
481         strbuf_release(&sb);
482         log->list.strdup_strings = 1;
483         string_list_clear(&log->list, 1);
484         clear_mailmap(&log->mailmap);
485 }