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