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