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