blob: e7f7af5de3ff005150680d19761828c7b9984ecf [file] [log] [blame]
Johannes Schindelinb8ec5922006-10-22 13:23:31 +02001#include "builtin.h"
2#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07003#include "config.h"
Johannes Schindelinb8ec5922006-10-22 13:23:31 +02004#include "commit.h"
5#include "diff.h"
Johannes Schindelinc455c872008-07-21 19:03:49 +01006#include "string-list.h"
Johannes Schindelinb8ec5922006-10-22 13:23:31 +02007#include "revision.h"
Johannes Schindelin3714e7c2006-12-22 22:15:59 +01008#include "utf8.h"
Junio C Hamano7c1c6782007-04-27 00:41:15 -07009#include "mailmap.h"
Daniel Barkalow552bcac2008-02-25 18:24:14 -050010#include "shortlog.h"
Pierre Habouzit14ec9cb2008-07-09 23:38:33 +020011#include "parse-options.h"
Jeff King47beb372020-09-27 04:40:04 -040012#include "trailer.h"
Elijah Newren449a9002020-11-11 20:02:21 +000013#include "strmap.h"
Johannes Schindelinb8ec5922006-10-22 13:23:31 +020014
Pierre Habouzit14ec9cb2008-07-09 23:38:33 +020015static char const * const shortlog_usage[] = {
Martin Ågrencd56d4e2018-03-10 12:52:11 +010016 N_("git shortlog [<options>] [<revision-range>] [[--] <path>...]"),
17 N_("git log --pretty=short | git shortlog [<options>]"),
Pierre Habouzit14ec9cb2008-07-09 23:38:33 +020018 NULL
19};
Johannes Schindelinb8ec5922006-10-22 13:23:31 +020020
Jeff King9b21a342016-01-18 15:02:59 -050021/*
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
34static 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
40static int compare_by_list(const void *a1, const void *a2)
Johannes Schindelinb8ec5922006-10-22 13:23:31 +020041{
Johannes Schindelinc455c872008-07-21 19:03:49 +010042 const struct string_list_item *i1 = a1, *i2 = a2;
43 const struct string_list *l1 = i1->util, *l2 = i2->util;
Johannes Schindelinb8ec5922006-10-22 13:23:31 +020044
45 if (l1->nr < l2->nr)
Johannes Schindelin6d6ab612006-11-21 21:12:06 +010046 return 1;
Johannes Schindelinb8ec5922006-10-22 13:23:31 +020047 else if (l1->nr == l2->nr)
48 return 0;
49 else
Johannes Schindelin6d6ab612006-11-21 21:12:06 +010050 return -1;
Johannes Schindelinb8ec5922006-10-22 13:23:31 +020051}
52
Daniel Barkalow552bcac2008-02-25 18:24:14 -050053static void insert_one_record(struct shortlog *log,
Jeff King45d93eb2020-09-25 03:01:50 -040054 const char *ident,
Junio C Hamano1e931cb2007-12-07 17:07:41 -080055 const char *oneline)
Johannes Schindelinb8ec5922006-10-22 13:23:31 +020056{
Johannes Schindelinc455c872008-07-21 19:03:49 +010057 struct string_list_item *item;
Johannes Schindelinb8ec5922006-10-22 13:23:31 +020058
Jeff King45d93eb2020-09-25 03:01:50 -040059 item = string_list_insert(&log->list, ident);
Johannes Schindelinb8ec5922006-10-22 13:23:31 +020060
Jeff Kinged7eba92016-01-18 15:02:56 -050061 if (log->summary)
Jeff King9b21a342016-01-18 15:02:59 -050062 item->util = (void *)(UTIL_TO_INT(item) + 1);
Jeff Kinged7eba92016-01-18 15:02:56 -050063 else {
Ævar Arnfjörð Bjarmason4e168332021-01-12 21:18:06 +010064 char *buffer;
Jeff Kinged7eba92016-01-18 15:02:56 -050065 struct strbuf subject = STRBUF_INIT;
66 const char *eol;
Johannes Schindelinb8ec5922006-10-22 13:23:31 +020067
Jeff Kinged7eba92016-01-18 15:02:56 -050068 /* 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;
78 }
79 while (*oneline && isspace(*oneline) && *oneline != '\n')
80 oneline++;
81 format_subject(&subject, oneline, " ");
82 buffer = strbuf_detach(&subject, NULL);
83
Jeff King9b21a342016-01-18 15:02:59 -050084 if (item->util == NULL)
85 item->util = xcalloc(1, sizeof(struct string_list));
Jeff Kinged7eba92016-01-18 15:02:56 -050086 string_list_append(item->util, buffer);
87 }
Johannes Schindelinb8ec5922006-10-22 13:23:31 +020088}
89
Jeff King87abb962020-09-27 04:40:09 -040090static int parse_ident(struct shortlog *log,
91 struct strbuf *out, const char *in)
Jeff King1ab03a52017-09-08 05:21:27 -040092{
93 const char *mailbuf, *namebuf;
94 size_t namelen, maillen;
95 struct ident_split ident;
96
97 if (split_ident_line(&ident, in, strlen(in)))
98 return -1;
99
100 namebuf = ident.name_begin;
101 mailbuf = ident.mail_begin;
102 namelen = ident.name_end - ident.name_begin;
103 maillen = ident.mail_end - ident.mail_begin;
104
105 map_user(&log->mailmap, &mailbuf, &maillen, &namebuf, &namelen);
106 strbuf_add(out, namebuf, namelen);
107 if (log->email)
108 strbuf_addf(out, " <%.*s>", (int)maillen, mailbuf);
109
110 return 0;
111}
112
Daniel Barkalow552bcac2008-02-25 18:24:14 -0500113static void read_from_stdin(struct shortlog *log)
Johannes Schindelinb8ec5922006-10-22 13:23:31 +0200114{
Jeff King45d93eb2020-09-25 03:01:50 -0400115 struct strbuf ident = STRBUF_INIT;
116 struct strbuf mapped_ident = STRBUF_INIT;
Jeff King50250492016-01-18 15:02:44 -0500117 struct strbuf oneline = STRBUF_INIT;
Linus Torvaldsfbfda152016-10-11 11:45:58 -0700118 static const char *author_match[2] = { "Author: ", "author " };
119 static const char *committer_match[2] = { "Commit: ", "committer " };
120 const char **match;
Johannes Schindelinb8ec5922006-10-22 13:23:31 +0200121
Jeff King63d24fa2020-09-27 04:40:15 -0400122 if (HAS_MULTI_BITS(log->groups))
123 die(_("using multiple --group options with stdin is not supported"));
124
125 switch (log->groups) {
Jeff King92338c42020-09-27 04:39:59 -0400126 case SHORTLOG_GROUP_AUTHOR:
127 match = author_match;
128 break;
129 case SHORTLOG_GROUP_COMMITTER:
130 match = committer_match;
131 break;
Jeff King47beb372020-09-27 04:40:04 -0400132 case SHORTLOG_GROUP_TRAILER:
133 die(_("using --group=trailer with stdin is not supported"));
Jeff King92338c42020-09-27 04:39:59 -0400134 default:
135 BUG("unhandled shortlog group");
136 }
137
Jeff King45d93eb2020-09-25 03:01:50 -0400138 while (strbuf_getline_lf(&ident, stdin) != EOF) {
Jeff King5c3894c2016-01-18 15:02:40 -0500139 const char *v;
Jeff King45d93eb2020-09-25 03:01:50 -0400140 if (!skip_prefix(ident.buf, match[0], &v) &&
141 !skip_prefix(ident.buf, match[1], &v))
Junio C Hamano1e931cb2007-12-07 17:07:41 -0800142 continue;
Junio C Hamanoa1c54052016-01-28 16:10:14 -0800143 while (strbuf_getline_lf(&oneline, stdin) != EOF &&
Jeff King50250492016-01-18 15:02:44 -0500144 oneline.len)
Junio C Hamano1e931cb2007-12-07 17:07:41 -0800145 ; /* discard headers */
Junio C Hamanoa1c54052016-01-28 16:10:14 -0800146 while (strbuf_getline_lf(&oneline, stdin) != EOF &&
Jeff King50250492016-01-18 15:02:44 -0500147 !oneline.len)
Junio C Hamano1e931cb2007-12-07 17:07:41 -0800148 ; /* discard blanks */
Jeff King1ab03a52017-09-08 05:21:27 -0400149
Jeff King45d93eb2020-09-25 03:01:50 -0400150 strbuf_reset(&mapped_ident);
Jeff King87abb962020-09-27 04:40:09 -0400151 if (parse_ident(log, &mapped_ident, v) < 0)
Jeff King1ab03a52017-09-08 05:21:27 -0400152 continue;
153
Jeff King45d93eb2020-09-25 03:01:50 -0400154 insert_one_record(log, mapped_ident.buf, oneline.buf);
Johannes Schindelinb8ec5922006-10-22 13:23:31 +0200155 }
Jeff King45d93eb2020-09-25 03:01:50 -0400156 strbuf_release(&ident);
157 strbuf_release(&mapped_ident);
Jeff King50250492016-01-18 15:02:44 -0500158 strbuf_release(&oneline);
Johannes Schindelinb8ec5922006-10-22 13:23:31 +0200159}
160
Jeff King47beb372020-09-27 04:40:04 -0400161static void insert_records_from_trailers(struct shortlog *log,
Jeff King63d24fa2020-09-27 04:40:15 -0400162 struct strset *dups,
Jeff King47beb372020-09-27 04:40:04 -0400163 struct commit *commit,
164 struct pretty_print_context *ctx,
165 const char *oneline)
166{
167 struct trailer_iterator iter;
168 const char *commit_buffer, *body;
Jeff King56d5dde2020-09-27 04:40:11 -0400169 struct strbuf ident = STRBUF_INIT;
Jeff King47beb372020-09-27 04:40:04 -0400170
171 /*
172 * Using format_commit_message("%B") would be simpler here, but
173 * this saves us copying the message.
174 */
175 commit_buffer = logmsg_reencode(commit, NULL, ctx->output_encoding);
176 body = strstr(commit_buffer, "\n\n");
177 if (!body)
178 return;
179
180 trailer_iterator_init(&iter, body);
181 while (trailer_iterator_advance(&iter)) {
182 const char *value = iter.val.buf;
183
Jeff King63d24fa2020-09-27 04:40:15 -0400184 if (!string_list_has_string(&log->trailers, iter.key.buf))
Jeff King47beb372020-09-27 04:40:04 -0400185 continue;
186
Jeff King56d5dde2020-09-27 04:40:11 -0400187 strbuf_reset(&ident);
188 if (!parse_ident(log, &ident, value))
189 value = ident.buf;
190
Elijah Newren449a9002020-11-11 20:02:21 +0000191 if (!strset_add(dups, value))
Jeff Kingf17b0b92020-09-27 04:40:07 -0400192 continue;
Jeff King47beb372020-09-27 04:40:04 -0400193 insert_one_record(log, value, oneline);
194 }
195 trailer_iterator_release(&iter);
196
Jeff King56d5dde2020-09-27 04:40:11 -0400197 strbuf_release(&ident);
Jeff King47beb372020-09-27 04:40:04 -0400198 unuse_commit_buffer(commit, commit_buffer);
199}
200
Daniel Barkalow552bcac2008-02-25 18:24:14 -0500201void shortlog_add_commit(struct shortlog *log, struct commit *commit)
202{
Jeff King45d93eb2020-09-25 03:01:50 -0400203 struct strbuf ident = STRBUF_INIT;
Jeff King2db6b832016-01-18 15:02:48 -0500204 struct strbuf oneline = STRBUF_INIT;
Jeff King63d24fa2020-09-27 04:40:15 -0400205 struct strset dups = STRSET_INIT;
Jeff King2db6b832016-01-18 15:02:48 -0500206 struct pretty_print_context ctx = {0};
Jeff King92338c42020-09-27 04:39:59 -0400207 const char *oneline_str;
Daniel Barkalow552bcac2008-02-25 18:24:14 -0500208
Jeff King2db6b832016-01-18 15:02:48 -0500209 ctx.fmt = CMIT_FMT_USERFORMAT;
210 ctx.abbrev = log->abbrev;
René Scharfe6d167fd2017-03-01 12:37:07 +0100211 ctx.print_email_subject = 1;
Jeff King2db6b832016-01-18 15:02:48 -0500212 ctx.date_mode.type = DATE_NORMAL;
213 ctx.output_encoding = get_log_output_encoding();
Daniel Barkalow552bcac2008-02-25 18:24:14 -0500214
Jeff King4e1d1a22016-01-18 15:02:52 -0500215 if (!log->summary) {
216 if (log->user_format)
217 pretty_print_commit(&ctx, commit, &oneline);
Daniel Barkalow552bcac2008-02-25 18:24:14 -0500218 else
Jeff King4e1d1a22016-01-18 15:02:52 -0500219 format_commit_message(commit, "%s", &oneline, &ctx);
220 }
Jeff King92338c42020-09-27 04:39:59 -0400221 oneline_str = oneline.len ? oneline.buf : "<none>";
Daniel Barkalow552bcac2008-02-25 18:24:14 -0500222
Jeff King63d24fa2020-09-27 04:40:15 -0400223 if (log->groups & SHORTLOG_GROUP_AUTHOR) {
224 strbuf_reset(&ident);
Jeff King92338c42020-09-27 04:39:59 -0400225 format_commit_message(commit,
226 log->email ? "%aN <%aE>" : "%aN",
227 &ident, &ctx);
Jeff King63d24fa2020-09-27 04:40:15 -0400228 if (!HAS_MULTI_BITS(log->groups) ||
Elijah Newren449a9002020-11-11 20:02:21 +0000229 strset_add(&dups, ident.buf))
Jeff King63d24fa2020-09-27 04:40:15 -0400230 insert_one_record(log, ident.buf, oneline_str);
231 }
232 if (log->groups & SHORTLOG_GROUP_COMMITTER) {
233 strbuf_reset(&ident);
Jeff King92338c42020-09-27 04:39:59 -0400234 format_commit_message(commit,
235 log->email ? "%cN <%cE>" : "%cN",
236 &ident, &ctx);
Jeff King63d24fa2020-09-27 04:40:15 -0400237 if (!HAS_MULTI_BITS(log->groups) ||
Elijah Newren449a9002020-11-11 20:02:21 +0000238 strset_add(&dups, ident.buf))
Jeff King63d24fa2020-09-27 04:40:15 -0400239 insert_one_record(log, ident.buf, oneline_str);
240 }
241 if (log->groups & SHORTLOG_GROUP_TRAILER) {
242 insert_records_from_trailers(log, &dups, commit, &ctx, oneline_str);
Jeff King92338c42020-09-27 04:39:59 -0400243 }
Jeff King2db6b832016-01-18 15:02:48 -0500244
Jeff King63d24fa2020-09-27 04:40:15 -0400245 strset_clear(&dups);
Jeff King45d93eb2020-09-25 03:01:50 -0400246 strbuf_release(&ident);
Jeff King2db6b832016-01-18 15:02:48 -0500247 strbuf_release(&oneline);
Daniel Barkalow552bcac2008-02-25 18:24:14 -0500248}
249
250static void get_from_rev(struct rev_info *rev, struct shortlog *log)
Johannes Schindelinb8ec5922006-10-22 13:23:31 +0200251{
Johannes Schindelinb8ec5922006-10-22 13:23:31 +0200252 struct commit *commit;
253
Daniel Barkalow552bcac2008-02-25 18:24:14 -0500254 if (prepare_revision_walk(rev))
Ævar Arnfjörð Bjarmasonab8b53b2011-02-22 23:42:32 +0000255 die(_("revision walk setup failed"));
Daniel Barkalow552bcac2008-02-25 18:24:14 -0500256 while ((commit = get_revision(rev)) != NULL)
257 shortlog_add_commit(log, commit);
Johannes Schindelinb8ec5922006-10-22 13:23:31 +0200258}
259
Pierre Habouzit14ec9cb2008-07-09 23:38:33 +0200260static int parse_uint(char const **arg, int comma, int defval)
Junio C Hamano3d711d92007-04-08 01:28:00 -0700261{
262 unsigned long ul;
263 int ret;
264 char *endp;
265
266 ul = strtoul(*arg, &endp, 10);
Pierre Habouzit14ec9cb2008-07-09 23:38:33 +0200267 if (*endp && *endp != comma)
Junio C Hamano3d711d92007-04-08 01:28:00 -0700268 return -1;
Pierre Habouzit14ec9cb2008-07-09 23:38:33 +0200269 if (ul > INT_MAX)
Junio C Hamano3d711d92007-04-08 01:28:00 -0700270 return -1;
Pierre Habouzit14ec9cb2008-07-09 23:38:33 +0200271 ret = *arg == endp ? defval : (int)ul;
272 *arg = *endp ? endp + 1 : endp;
Junio C Hamano3d711d92007-04-08 01:28:00 -0700273 return ret;
274}
275
276static const char wrap_arg_usage[] = "-w[<width>[,<indent1>[,<indent2>]]]";
277#define DEFAULT_WRAPLEN 76
278#define DEFAULT_INDENT1 6
279#define DEFAULT_INDENT2 9
280
Pierre Habouzit14ec9cb2008-07-09 23:38:33 +0200281static int parse_wrap_args(const struct option *opt, const char *arg, int unset)
Junio C Hamano3d711d92007-04-08 01:28:00 -0700282{
Pierre Habouzit14ec9cb2008-07-09 23:38:33 +0200283 struct shortlog *log = opt->value;
Junio C Hamano3d711d92007-04-08 01:28:00 -0700284
Pierre Habouzit14ec9cb2008-07-09 23:38:33 +0200285 log->wrap_lines = !unset;
286 if (unset)
287 return 0;
288 if (!arg) {
289 log->wrap = DEFAULT_WRAPLEN;
290 log->in1 = DEFAULT_INDENT1;
291 log->in2 = DEFAULT_INDENT2;
292 return 0;
293 }
Junio C Hamano3d711d92007-04-08 01:28:00 -0700294
Pierre Habouzit14ec9cb2008-07-09 23:38:33 +0200295 log->wrap = parse_uint(&arg, ',', DEFAULT_WRAPLEN);
296 log->in1 = parse_uint(&arg, ',', DEFAULT_INDENT1);
297 log->in2 = parse_uint(&arg, '\0', DEFAULT_INDENT2);
298 if (log->wrap < 0 || log->in1 < 0 || log->in2 < 0)
299 return error(wrap_arg_usage);
300 if (log->wrap &&
301 ((log->in1 && log->wrap <= log->in1) ||
302 (log->in2 && log->wrap <= log->in2)))
303 return error(wrap_arg_usage);
304 return 0;
Junio C Hamano3d711d92007-04-08 01:28:00 -0700305}
306
Jeff King92338c42020-09-27 04:39:59 -0400307static int parse_group_option(const struct option *opt, const char *arg, int unset)
308{
309 struct shortlog *log = opt->value;
Jeff King47beb372020-09-27 04:40:04 -0400310 const char *field;
Jeff King92338c42020-09-27 04:39:59 -0400311
Jeff King63d24fa2020-09-27 04:40:15 -0400312 if (unset) {
313 log->groups = 0;
314 string_list_clear(&log->trailers, 0);
315 } else if (!strcasecmp(arg, "author"))
316 log->groups |= SHORTLOG_GROUP_AUTHOR;
Jeff King92338c42020-09-27 04:39:59 -0400317 else if (!strcasecmp(arg, "committer"))
Jeff King63d24fa2020-09-27 04:40:15 -0400318 log->groups |= SHORTLOG_GROUP_COMMITTER;
Jeff King47beb372020-09-27 04:40:04 -0400319 else if (skip_prefix(arg, "trailer:", &field)) {
Jeff King63d24fa2020-09-27 04:40:15 -0400320 log->groups |= SHORTLOG_GROUP_TRAILER;
321 string_list_append(&log->trailers, field);
Jeff King47beb372020-09-27 04:40:04 -0400322 } else
Jeff King92338c42020-09-27 04:39:59 -0400323 return error(_("unknown group type: %s"), arg);
324
325 return 0;
326}
327
328
Daniel Barkalow552bcac2008-02-25 18:24:14 -0500329void shortlog_init(struct shortlog *log)
330{
331 memset(log, 0, sizeof(*log));
332
Ævar Arnfjörð Bjarmason4e168332021-01-12 21:18:06 +0100333 read_mailmap(&log->mailmap);
Daniel Barkalow552bcac2008-02-25 18:24:14 -0500334
Johannes Schindelinc455c872008-07-21 19:03:49 +0100335 log->list.strdup_strings = 1;
Daniel Barkalow552bcac2008-02-25 18:24:14 -0500336 log->wrap = DEFAULT_WRAPLEN;
337 log->in1 = DEFAULT_INDENT1;
338 log->in2 = DEFAULT_INDENT2;
Jeff King63d24fa2020-09-27 04:40:15 -0400339 log->trailers.strdup_strings = 1;
340 log->trailers.cmp = strcasecmp;
Daniel Barkalow552bcac2008-02-25 18:24:14 -0500341}
342
Johannes Schindelinb8ec5922006-10-22 13:23:31 +0200343int cmd_shortlog(int argc, const char **argv, const char *prefix)
344{
Jeff King64093fc2016-06-13 01:39:28 -0400345 struct shortlog log = { STRING_LIST_INIT_NODUP };
346 struct rev_info rev;
Nguyễn Thái Ngọc Duy773b69b2010-08-05 22:01:37 -0500347 int nongit = !startup_info->have_repository;
Daniel Barkalow552bcac2008-02-25 18:24:14 -0500348
Jeff King64093fc2016-06-13 01:39:28 -0400349 const struct option options[] = {
Jeff King63d24fa2020-09-27 04:40:15 -0400350 OPT_BIT('c', "committer", &log.groups,
ZheNing Hue73fe3d2021-01-06 14:44:03 +0000351 N_("group by committer rather than author"),
Jeff King63d24fa2020-09-27 04:40:15 -0400352 SHORTLOG_GROUP_COMMITTER),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200353 OPT_BOOL('n', "numbered", &log.sort_by_number,
354 N_("sort output according to the number of commits per author")),
355 OPT_BOOL('s', "summary", &log.summary,
ZheNing Hue73fe3d2021-01-06 14:44:03 +0000356 N_("suppress commit descriptions, only provides commit count")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200357 OPT_BOOL('e', "email", &log.email,
ZheNing Hue73fe3d2021-01-06 14:44:03 +0000358 N_("show the email address of each author")),
Denton Liu203c8532020-04-28 04:36:28 -0400359 OPT_CALLBACK_F('w', NULL, &log, N_("<w>[,<i1>[,<i2>]]"),
ZheNing Hue73fe3d2021-01-06 14:44:03 +0000360 N_("linewrap output"), PARSE_OPT_OPTARG,
Denton Liu203c8532020-04-28 04:36:28 -0400361 &parse_wrap_args),
Jeff King92338c42020-09-27 04:39:59 -0400362 OPT_CALLBACK(0, "group", &log, N_("field"),
ZheNing Hue73fe3d2021-01-06 14:44:03 +0000363 N_("group by field"), parse_group_option),
Pierre Habouzit14ec9cb2008-07-09 23:38:33 +0200364 OPT_END(),
365 };
366
367 struct parse_opt_ctx_t ctx;
368
Marius Storm-Olsend551a482009-02-08 15:34:27 +0100369 git_config(git_default_config, NULL);
Daniel Barkalow552bcac2008-02-25 18:24:14 -0500370 shortlog_init(&log);
Nguyễn Thái Ngọc Duy2abf3502018-09-21 17:57:38 +0200371 repo_init_revisions(the_repository, &rev, prefix);
Stephen Boyd9ca11692010-12-05 23:57:42 -0800372 parse_options_start(&ctx, argc, argv, prefix, options,
373 PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0);
Pierre Habouzit14ec9cb2008-07-09 23:38:33 +0200374
375 for (;;) {
Pierre Habouzit14ec9cb2008-07-09 23:38:33 +0200376 switch (parse_options_step(&ctx, options, shortlog_usage)) {
Ævar Arnfjörð Bjarmason352e7612021-10-08 21:07:39 +0200377 case PARSE_OPT_NON_OPTION:
378 case PARSE_OPT_UNKNOWN:
379 break;
Pierre Habouzit14ec9cb2008-07-09 23:38:33 +0200380 case PARSE_OPT_HELP:
Paul-Sebastian Ungureanu3bb09232018-03-22 20:43:51 +0200381 case PARSE_OPT_ERROR:
Pierre Habouzit14ec9cb2008-07-09 23:38:33 +0200382 exit(129);
Nguyễn Thái Ngọc Duya92ec7e2018-12-11 16:35:01 +0100383 case PARSE_OPT_COMPLETE:
384 exit(0);
Pierre Habouzit14ec9cb2008-07-09 23:38:33 +0200385 case PARSE_OPT_DONE:
386 goto parse_done;
387 }
Pierre Habouzit6b61ec02008-07-09 23:38:34 +0200388 parse_revision_opt(&rev, &ctx, options, shortlog_usage);
Pierre Habouzit14ec9cb2008-07-09 23:38:33 +0200389 }
390parse_done:
391 argc = parse_options_end(&ctx);
392
Martin Ågren4aa01612018-03-14 22:34:19 +0100393 if (nongit && argc > 1) {
394 error(_("too many arguments given outside repository"));
395 usage_with_options(shortlog_usage, options);
396 }
397
Pierre Habouzit14ec9cb2008-07-09 23:38:33 +0200398 if (setup_revisions(argc, argv, &rev, NULL) != 1) {
Ævar Arnfjörð Bjarmasonab8b53b2011-02-22 23:42:32 +0000399 error(_("unrecognized argument: %s"), argv[1]);
Pierre Habouzit14ec9cb2008-07-09 23:38:33 +0200400 usage_with_options(shortlog_usage, options);
401 }
Johannes Schindelinb8ec5922006-10-22 13:23:31 +0200402
Johannes Schindelinb526f8e2008-07-14 19:08:52 +0100403 log.user_format = rev.commit_format == CMIT_FMT_USERFORMAT;
Will Palmerc1977022010-05-03 22:18:57 -0500404 log.abbrev = rev.abbrev;
Johannes Schindelin7f7d7122016-06-22 17:02:07 +0200405 log.file = rev.diffopt.file;
Johannes Schindelinb526f8e2008-07-14 19:08:52 +0100406
Jeff King63d24fa2020-09-27 04:40:15 -0400407 if (!log.groups)
408 log.groups = SHORTLOG_GROUP_AUTHOR;
409 string_list_sort(&log.trailers);
410
Junio C Hamano3384a2d2007-12-11 10:09:04 -0800411 /* assume HEAD if from a tty */
Jonas Fonsecaabe549e2008-03-14 22:35:24 +0100412 if (!nongit && !rev.pending.nr && isatty(0))
Junio C Hamano3384a2d2007-12-11 10:09:04 -0800413 add_head_to_pending(&rev);
Junio C Hamano0497c622007-03-08 02:12:06 -0800414 if (rev.pending.nr == 0) {
Michele Ballabio37314492010-02-24 21:49:03 +0100415 if (isatty(0))
Ævar Arnfjörð Bjarmasonab8b53b2011-02-22 23:42:32 +0000416 fprintf(stderr, _("(reading log message from standard input)\n"));
Daniel Barkalow552bcac2008-02-25 18:24:14 -0500417 read_from_stdin(&log);
Junio C Hamano0497c622007-03-08 02:12:06 -0800418 }
Johannes Schindelinb8ec5922006-10-22 13:23:31 +0200419 else
Daniel Barkalow552bcac2008-02-25 18:24:14 -0500420 get_from_rev(&rev, &log);
Johannes Schindelinb8ec5922006-10-22 13:23:31 +0200421
Daniel Barkalow552bcac2008-02-25 18:24:14 -0500422 shortlog_output(&log);
Johannes Schindelin7f7d7122016-06-22 17:02:07 +0200423 if (log.file != stdout)
424 fclose(log.file);
Daniel Barkalow552bcac2008-02-25 18:24:14 -0500425 return 0;
426}
427
René Scharfebb96a2c2010-02-19 23:15:01 +0100428static void add_wrapped_shortlog_msg(struct strbuf *sb, const char *s,
429 const struct shortlog *log)
430{
Steffen Prohaska5b597082012-12-11 06:59:21 +0100431 strbuf_add_wrapped_text(sb, s, log->in1, log->in2, log->wrap);
432 strbuf_addch(sb, '\n');
René Scharfebb96a2c2010-02-19 23:15:01 +0100433}
434
Daniel Barkalow552bcac2008-02-25 18:24:14 -0500435void shortlog_output(struct shortlog *log)
436{
437 int i, j;
René Scharfebb96a2c2010-02-19 23:15:01 +0100438 struct strbuf sb = STRBUF_INIT;
439
Daniel Barkalow552bcac2008-02-25 18:24:14 -0500440 if (log->sort_by_number)
René Scharfe1b5294d2016-09-30 01:40:14 +0200441 QSORT(log->list.items, log->list.nr,
Jeff King9b21a342016-01-18 15:02:59 -0500442 log->summary ? compare_by_counter : compare_by_list);
Daniel Barkalow552bcac2008-02-25 18:24:14 -0500443 for (i = 0; i < log->list.nr; i++) {
Jeff King9b21a342016-01-18 15:02:59 -0500444 const struct string_list_item *item = &log->list.items[i];
Daniel Barkalow552bcac2008-02-25 18:24:14 -0500445 if (log->summary) {
Johannes Schindelin0a7b3572016-06-22 17:01:49 +0200446 fprintf(log->file, "%6d\t%s\n",
447 (int)UTIL_TO_INT(item), item->string);
Nicolas Pitreac60c942006-11-21 15:49:45 -0500448 } else {
Jeff King9b21a342016-01-18 15:02:59 -0500449 struct string_list *onelines = item->util;
Johannes Schindelin0a7b3572016-06-22 17:01:49 +0200450 fprintf(log->file, "%s (%d):\n",
451 item->string, onelines->nr);
Johannes Schindelin3714e7c2006-12-22 22:15:59 +0100452 for (j = onelines->nr - 1; j >= 0; j--) {
Johannes Schindelinc455c872008-07-21 19:03:49 +0100453 const char *msg = onelines->items[j].string;
Junio C Hamano3d711d92007-04-08 01:28:00 -0700454
Daniel Barkalow552bcac2008-02-25 18:24:14 -0500455 if (log->wrap_lines) {
René Scharfebb96a2c2010-02-19 23:15:01 +0100456 strbuf_reset(&sb);
457 add_wrapped_shortlog_msg(&sb, msg, log);
Johannes Schindelin0a7b3572016-06-22 17:01:49 +0200458 fwrite(sb.buf, sb.len, 1, log->file);
Junio C Hamano3d711d92007-04-08 01:28:00 -0700459 }
460 else
Johannes Schindelin0a7b3572016-06-22 17:01:49 +0200461 fprintf(log->file, " %s\n", msg);
Johannes Schindelin3714e7c2006-12-22 22:15:59 +0100462 }
Johannes Schindelin0a7b3572016-06-22 17:01:49 +0200463 putc('\n', log->file);
Jeff King9b21a342016-01-18 15:02:59 -0500464 onelines->strdup_strings = 1;
465 string_list_clear(onelines, 0);
466 free(onelines);
Johannes Schindelinb8ec5922006-10-22 13:23:31 +0200467 }
468
Daniel Barkalow552bcac2008-02-25 18:24:14 -0500469 log->list.items[i].util = NULL;
Johannes Schindelinb8ec5922006-10-22 13:23:31 +0200470 }
471
René Scharfebb96a2c2010-02-19 23:15:01 +0100472 strbuf_release(&sb);
Johannes Schindelinc455c872008-07-21 19:03:49 +0100473 log->list.strdup_strings = 1;
474 string_list_clear(&log->list, 1);
Marius Storm-Olsend20d6542009-02-08 15:34:30 +0100475 clear_mailmap(&log->mailmap);
Johannes Schindelinb8ec5922006-10-22 13:23:31 +0200476}