Merge branch 'ab/ref-filter-no-contains' into maint
[git] / builtin / shortlog.c
1 #include "builtin.h"
2 #include "cache.h"
3 #include "commit.h"
4 #include "diff.h"
5 #include "string-list.h"
6 #include "revision.h"
7 #include "utf8.h"
8 #include "mailmap.h"
9 #include "shortlog.h"
10 #include "parse-options.h"
11
12 static char const * const shortlog_usage[] = {
13         N_("git shortlog [<options>] [<revision-range>] [[--] [<path>...]]"),
14         NULL
15 };
16
17 /*
18  * The util field of our string_list_items will contain one of two things:
19  *
20  *   - if --summary is not in use, it will point to a string list of the
21  *     oneline subjects assigned to this author
22  *
23  *   - if --summary is in use, we don't need that list; we only need to know
24  *     its size. So we abuse the pointer slot to store our integer counter.
25  *
26  *  This macro accesses the latter.
27  */
28 #define UTIL_TO_INT(x) ((intptr_t)(x)->util)
29
30 static int compare_by_counter(const void *a1, const void *a2)
31 {
32         const struct string_list_item *i1 = a1, *i2 = a2;
33         return UTIL_TO_INT(i2) - UTIL_TO_INT(i1);
34 }
35
36 static int compare_by_list(const void *a1, const void *a2)
37 {
38         const struct string_list_item *i1 = a1, *i2 = a2;
39         const struct string_list *l1 = i1->util, *l2 = i2->util;
40
41         if (l1->nr < l2->nr)
42                 return 1;
43         else if (l1->nr == l2->nr)
44                 return 0;
45         else
46                 return -1;
47 }
48
49 static void insert_one_record(struct shortlog *log,
50                               const char *author,
51                               const char *oneline)
52 {
53         struct string_list_item *item;
54         const char *mailbuf, *namebuf;
55         size_t namelen, maillen;
56         struct strbuf namemailbuf = STRBUF_INIT;
57         struct ident_split ident;
58
59         if (split_ident_line(&ident, author, strlen(author)))
60                 return;
61
62         namebuf = ident.name_begin;
63         mailbuf = ident.mail_begin;
64         namelen = ident.name_end - ident.name_begin;
65         maillen = ident.mail_end - ident.mail_begin;
66
67         map_user(&log->mailmap, &mailbuf, &maillen, &namebuf, &namelen);
68         strbuf_add(&namemailbuf, namebuf, namelen);
69
70         if (log->email)
71                 strbuf_addf(&namemailbuf, " <%.*s>", (int)maillen, mailbuf);
72
73         item = string_list_insert(&log->list, namemailbuf.buf);
74
75         if (log->summary)
76                 item->util = (void *)(UTIL_TO_INT(item) + 1);
77         else {
78                 const char *dot3 = log->common_repo_prefix;
79                 char *buffer, *p;
80                 struct strbuf subject = STRBUF_INIT;
81                 const char *eol;
82
83                 /* Skip any leading whitespace, including any blank lines. */
84                 while (*oneline && isspace(*oneline))
85                         oneline++;
86                 eol = strchr(oneline, '\n');
87                 if (!eol)
88                         eol = oneline + strlen(oneline);
89                 if (starts_with(oneline, "[PATCH")) {
90                         char *eob = strchr(oneline, ']');
91                         if (eob && (!eol || eob < eol))
92                                 oneline = eob + 1;
93                 }
94                 while (*oneline && isspace(*oneline) && *oneline != '\n')
95                         oneline++;
96                 format_subject(&subject, oneline, " ");
97                 buffer = strbuf_detach(&subject, NULL);
98
99                 if (dot3) {
100                         int dot3len = strlen(dot3);
101                         if (dot3len > 5) {
102                                 while ((p = strstr(buffer, dot3)) != NULL) {
103                                         int taillen = strlen(p) - dot3len;
104                                         memcpy(p, "/.../", 5);
105                                         memmove(p + 5, p + dot3len, taillen + 1);
106                                 }
107                         }
108                 }
109
110                 if (item->util == NULL)
111                         item->util = xcalloc(1, sizeof(struct string_list));
112                 string_list_append(item->util, buffer);
113         }
114 }
115
116 static void read_from_stdin(struct shortlog *log)
117 {
118         struct strbuf author = 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         match = log->committer ? committer_match : author_match;
125         while (strbuf_getline_lf(&author, stdin) != EOF) {
126                 const char *v;
127                 if (!skip_prefix(author.buf, match[0], &v) &&
128                     !skip_prefix(author.buf, match[1], &v))
129                         continue;
130                 while (strbuf_getline_lf(&oneline, stdin) != EOF &&
131                        oneline.len)
132                         ; /* discard headers */
133                 while (strbuf_getline_lf(&oneline, stdin) != EOF &&
134                        !oneline.len)
135                         ; /* discard blanks */
136                 insert_one_record(log, v, oneline.buf);
137         }
138         strbuf_release(&author);
139         strbuf_release(&oneline);
140 }
141
142 void shortlog_add_commit(struct shortlog *log, struct commit *commit)
143 {
144         struct strbuf author = STRBUF_INIT;
145         struct strbuf oneline = STRBUF_INIT;
146         struct pretty_print_context ctx = {0};
147         const char *fmt;
148
149         ctx.fmt = CMIT_FMT_USERFORMAT;
150         ctx.abbrev = log->abbrev;
151         ctx.print_email_subject = 1;
152         ctx.date_mode.type = DATE_NORMAL;
153         ctx.output_encoding = get_log_output_encoding();
154
155         fmt = log->committer ? "%cn <%ce>" : "%an <%ae>";
156
157         format_commit_message(commit, fmt, &author, &ctx);
158         if (!log->summary) {
159                 if (log->user_format)
160                         pretty_print_commit(&ctx, commit, &oneline);
161                 else
162                         format_commit_message(commit, "%s", &oneline, &ctx);
163         }
164
165         insert_one_record(log, author.buf, oneline.len ? oneline.buf : "<none>");
166
167         strbuf_release(&author);
168         strbuf_release(&oneline);
169 }
170
171 static void get_from_rev(struct rev_info *rev, struct shortlog *log)
172 {
173         struct commit *commit;
174
175         if (prepare_revision_walk(rev))
176                 die(_("revision walk setup failed"));
177         while ((commit = get_revision(rev)) != NULL)
178                 shortlog_add_commit(log, commit);
179 }
180
181 static int parse_uint(char const **arg, int comma, int defval)
182 {
183         unsigned long ul;
184         int ret;
185         char *endp;
186
187         ul = strtoul(*arg, &endp, 10);
188         if (*endp && *endp != comma)
189                 return -1;
190         if (ul > INT_MAX)
191                 return -1;
192         ret = *arg == endp ? defval : (int)ul;
193         *arg = *endp ? endp + 1 : endp;
194         return ret;
195 }
196
197 static const char wrap_arg_usage[] = "-w[<width>[,<indent1>[,<indent2>]]]";
198 #define DEFAULT_WRAPLEN 76
199 #define DEFAULT_INDENT1 6
200 #define DEFAULT_INDENT2 9
201
202 static int parse_wrap_args(const struct option *opt, const char *arg, int unset)
203 {
204         struct shortlog *log = opt->value;
205
206         log->wrap_lines = !unset;
207         if (unset)
208                 return 0;
209         if (!arg) {
210                 log->wrap = DEFAULT_WRAPLEN;
211                 log->in1 = DEFAULT_INDENT1;
212                 log->in2 = DEFAULT_INDENT2;
213                 return 0;
214         }
215
216         log->wrap = parse_uint(&arg, ',', DEFAULT_WRAPLEN);
217         log->in1 = parse_uint(&arg, ',', DEFAULT_INDENT1);
218         log->in2 = parse_uint(&arg, '\0', DEFAULT_INDENT2);
219         if (log->wrap < 0 || log->in1 < 0 || log->in2 < 0)
220                 return error(wrap_arg_usage);
221         if (log->wrap &&
222             ((log->in1 && log->wrap <= log->in1) ||
223              (log->in2 && log->wrap <= log->in2)))
224                 return error(wrap_arg_usage);
225         return 0;
226 }
227
228 void shortlog_init(struct shortlog *log)
229 {
230         memset(log, 0, sizeof(*log));
231
232         read_mailmap(&log->mailmap, &log->common_repo_prefix);
233
234         log->list.strdup_strings = 1;
235         log->wrap = DEFAULT_WRAPLEN;
236         log->in1 = DEFAULT_INDENT1;
237         log->in2 = DEFAULT_INDENT2;
238 }
239
240 int cmd_shortlog(int argc, const char **argv, const char *prefix)
241 {
242         struct shortlog log = { STRING_LIST_INIT_NODUP };
243         struct rev_info rev;
244         int nongit = !startup_info->have_repository;
245
246         const struct option options[] = {
247                 OPT_BOOL('c', "committer", &log.committer,
248                          N_("Group by committer rather than author")),
249                 OPT_BOOL('n', "numbered", &log.sort_by_number,
250                          N_("sort output according to the number of commits per author")),
251                 OPT_BOOL('s', "summary", &log.summary,
252                          N_("Suppress commit descriptions, only provides commit count")),
253                 OPT_BOOL('e', "email", &log.email,
254                          N_("Show the email address of each author")),
255                 { OPTION_CALLBACK, 'w', NULL, &log, N_("w[,i1[,i2]]"),
256                         N_("Linewrap output"), PARSE_OPT_OPTARG, &parse_wrap_args },
257                 OPT_END(),
258         };
259
260         struct parse_opt_ctx_t ctx;
261
262         git_config(git_default_config, NULL);
263         shortlog_init(&log);
264         init_revisions(&rev, prefix);
265         parse_options_start(&ctx, argc, argv, prefix, options,
266                             PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0);
267
268         for (;;) {
269                 switch (parse_options_step(&ctx, options, shortlog_usage)) {
270                 case PARSE_OPT_HELP:
271                         exit(129);
272                 case PARSE_OPT_DONE:
273                         goto parse_done;
274                 }
275                 parse_revision_opt(&rev, &ctx, options, shortlog_usage);
276         }
277 parse_done:
278         argc = parse_options_end(&ctx);
279
280         if (setup_revisions(argc, argv, &rev, NULL) != 1) {
281                 error(_("unrecognized argument: %s"), argv[1]);
282                 usage_with_options(shortlog_usage, options);
283         }
284
285         log.user_format = rev.commit_format == CMIT_FMT_USERFORMAT;
286         log.abbrev = rev.abbrev;
287         log.file = rev.diffopt.file;
288
289         /* assume HEAD if from a tty */
290         if (!nongit && !rev.pending.nr && isatty(0))
291                 add_head_to_pending(&rev);
292         if (rev.pending.nr == 0) {
293                 if (isatty(0))
294                         fprintf(stderr, _("(reading log message from standard input)\n"));
295                 read_from_stdin(&log);
296         }
297         else
298                 get_from_rev(&rev, &log);
299
300         shortlog_output(&log);
301         if (log.file != stdout)
302                 fclose(log.file);
303         return 0;
304 }
305
306 static void add_wrapped_shortlog_msg(struct strbuf *sb, const char *s,
307                                      const struct shortlog *log)
308 {
309         strbuf_add_wrapped_text(sb, s, log->in1, log->in2, log->wrap);
310         strbuf_addch(sb, '\n');
311 }
312
313 void shortlog_output(struct shortlog *log)
314 {
315         int i, j;
316         struct strbuf sb = STRBUF_INIT;
317
318         if (log->sort_by_number)
319                 QSORT(log->list.items, log->list.nr,
320                       log->summary ? compare_by_counter : compare_by_list);
321         for (i = 0; i < log->list.nr; i++) {
322                 const struct string_list_item *item = &log->list.items[i];
323                 if (log->summary) {
324                         fprintf(log->file, "%6d\t%s\n",
325                                 (int)UTIL_TO_INT(item), item->string);
326                 } else {
327                         struct string_list *onelines = item->util;
328                         fprintf(log->file, "%s (%d):\n",
329                                 item->string, onelines->nr);
330                         for (j = onelines->nr - 1; j >= 0; j--) {
331                                 const char *msg = onelines->items[j].string;
332
333                                 if (log->wrap_lines) {
334                                         strbuf_reset(&sb);
335                                         add_wrapped_shortlog_msg(&sb, msg, log);
336                                         fwrite(sb.buf, sb.len, 1, log->file);
337                                 }
338                                 else
339                                         fprintf(log->file, "      %s\n", msg);
340                         }
341                         putc('\n', log->file);
342                         onelines->strdup_strings = 1;
343                         string_list_clear(onelines, 0);
344                         free(onelines);
345                 }
346
347                 log->list.items[i].util = NULL;
348         }
349
350         strbuf_release(&sb);
351         log->list.strdup_strings = 1;
352         string_list_clear(&log->list, 1);
353         clear_mailmap(&log->mailmap);
354 }