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