Add mailmap.file as configurational option for mailmap location
[git/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[] = {
588c038a 13 "git shortlog [-n] [-s] [-e] [-w] [rev-opts] [--] [<commit-id>... ]",
14ec9cbd
PH
14 "",
15 "[rev-opts] are documented in git-rev-list(1)",
16 NULL
17};
b8ec5923
JS
18
19static int compare_by_number(const void *a1, const void *a2)
20{
c455c87c
JS
21 const struct string_list_item *i1 = a1, *i2 = a2;
22 const struct string_list *l1 = i1->util, *l2 = i2->util;
b8ec5923
JS
23
24 if (l1->nr < l2->nr)
6d6ab610 25 return 1;
b8ec5923
JS
26 else if (l1->nr == l2->nr)
27 return 0;
28 else
6d6ab610 29 return -1;
b8ec5923
JS
30}
31
cec08717
RS
32const char *format_subject(struct strbuf *sb, const char *msg,
33 const char *line_separator);
34
552bcac3 35static void insert_one_record(struct shortlog *log,
1e931cb4
JH
36 const char *author,
37 const char *oneline)
b8ec5923 38{
552bcac3 39 const char *dot3 = log->common_repo_prefix;
b8ec5923 40 char *buffer, *p;
c455c87c 41 struct string_list_item *item;
1e931cb4
JH
42 char namebuf[1024];
43 size_t len;
44 const char *eol;
45 const char *boemail, *eoemail;
cec08717 46 struct strbuf subject = STRBUF_INIT;
1e931cb4
JH
47
48 boemail = strchr(author, '<');
49 if (!boemail)
50 return;
51 eoemail = strchr(boemail, '>');
52 if (!eoemail)
53 return;
552bcac3 54 if (!map_email(&log->mailmap, boemail+1, namebuf, sizeof(namebuf))) {
1e931cb4
JH
55 while (author < boemail && isspace(*author))
56 author++;
57 for (len = 0;
58 len < sizeof(namebuf) - 1 && author + len < boemail;
59 len++)
60 namebuf[len] = author[len];
61 while (0 < len && isspace(namebuf[len-1]))
62 len--;
63 namebuf[len] = '\0';
64 }
4602c17d
JH
65 else
66 len = strlen(namebuf);
67
552bcac3 68 if (log->email) {
4602c17d
JH
69 size_t room = sizeof(namebuf) - len - 1;
70 int maillen = eoemail - boemail + 1;
71 snprintf(namebuf + len, room, " %.*s", maillen, boemail);
72 }
b8ec5923 73
fe73fc1a 74 item = string_list_insert(namebuf, &log->list);
b8ec5923 75 if (item->util == NULL)
c455c87c 76 item->util = xcalloc(1, sizeof(struct string_list));
b8ec5923 77
c1ce83a5
AW
78 /* Skip any leading whitespace, including any blank lines. */
79 while (*oneline && isspace(*oneline))
80 oneline++;
1e931cb4
JH
81 eol = strchr(oneline, '\n');
82 if (!eol)
83 eol = oneline + strlen(oneline);
cc44c765 84 if (!prefixcmp(oneline, "[PATCH")) {
72019cde 85 char *eob = strchr(oneline, ']');
1e931cb4
JH
86 if (eob && (!eol || eob < eol))
87 oneline = eob + 1;
b8ec5923 88 }
1e931cb4 89 while (*oneline && isspace(*oneline) && *oneline != '\n')
b8ec5923 90 oneline++;
1e931cb4 91 len = eol - oneline;
cec08717
RS
92 format_subject(&subject, oneline, " ");
93 buffer = strbuf_detach(&subject, NULL);
b8ec5923 94
c95044d4
JH
95 if (dot3) {
96 int dot3len = strlen(dot3);
97 if (dot3len > 5) {
98 while ((p = strstr(buffer, dot3)) != NULL) {
99 int taillen = strlen(p) - dot3len;
100 memcpy(p, "/.../", 5);
101 memmove(p + 5, p + dot3len, taillen + 1);
102 }
103 }
b8ec5923
JS
104 }
105
fe73fc1a 106 string_list_append(buffer, item->util);
b8ec5923
JS
107}
108
552bcac3 109static void read_from_stdin(struct shortlog *log)
b8ec5923 110{
1e931cb4
JH
111 char author[1024], oneline[1024];
112
113 while (fgets(author, sizeof(author), stdin) != NULL) {
114 if (!(author[0] == 'A' || author[0] == 'a') ||
115 prefixcmp(author + 1, "uthor: "))
116 continue;
117 while (fgets(oneline, sizeof(oneline), stdin) &&
118 oneline[0] != '\n')
119 ; /* discard headers */
120 while (fgets(oneline, sizeof(oneline), stdin) &&
121 oneline[0] == '\n')
122 ; /* discard blanks */
552bcac3 123 insert_one_record(log, author + 8, oneline);
b8ec5923
JS
124 }
125}
126
552bcac3 127void shortlog_add_commit(struct shortlog *log, struct commit *commit)
b8ec5923 128{
552bcac3 129 const char *author = NULL, *buffer;
b8ec5923 130
552bcac3
DB
131 buffer = commit->buffer;
132 while (*buffer && *buffer != '\n') {
133 const char *eol = strchr(buffer, '\n');
b8ec5923 134
552bcac3
DB
135 if (eol == NULL)
136 eol = buffer + strlen(buffer);
137 else
138 eol++;
b8ec5923 139
552bcac3
DB
140 if (!prefixcmp(buffer, "author "))
141 author = buffer + 7;
142 buffer = eol;
b8ec5923 143 }
552bcac3
DB
144 if (!author)
145 die("Missing author: %s",
146 sha1_to_hex(commit->object.sha1));
b526f8ed
JS
147 if (log->user_format) {
148 struct strbuf buf = STRBUF_INIT;
149
150 pretty_print_commit(CMIT_FMT_USERFORMAT, commit, &buf,
151 DEFAULT_ABBREV, "", "", DATE_NORMAL, 0);
152 insert_one_record(log, author, buf.buf);
153 strbuf_release(&buf);
154 return;
155 }
552bcac3
DB
156 if (*buffer)
157 buffer++;
158 insert_one_record(log, author, !*buffer ? "<none>" : buffer);
159}
160
161static void get_from_rev(struct rev_info *rev, struct shortlog *log)
162{
163 struct commit *commit;
164
165 if (prepare_revision_walk(rev))
166 die("revision walk setup failed");
167 while ((commit = get_revision(rev)) != NULL)
168 shortlog_add_commit(log, commit);
b8ec5923
JS
169}
170
14ec9cbd 171static int parse_uint(char const **arg, int comma, int defval)
3d711d97
JH
172{
173 unsigned long ul;
174 int ret;
175 char *endp;
176
177 ul = strtoul(*arg, &endp, 10);
14ec9cbd 178 if (*endp && *endp != comma)
3d711d97 179 return -1;
14ec9cbd 180 if (ul > INT_MAX)
3d711d97 181 return -1;
14ec9cbd
PH
182 ret = *arg == endp ? defval : (int)ul;
183 *arg = *endp ? endp + 1 : endp;
3d711d97
JH
184 return ret;
185}
186
187static const char wrap_arg_usage[] = "-w[<width>[,<indent1>[,<indent2>]]]";
188#define DEFAULT_WRAPLEN 76
189#define DEFAULT_INDENT1 6
190#define DEFAULT_INDENT2 9
191
14ec9cbd 192static int parse_wrap_args(const struct option *opt, const char *arg, int unset)
3d711d97 193{
14ec9cbd
PH
194 struct shortlog *log = opt->value;
195
196 log->wrap_lines = !unset;
197 if (unset)
198 return 0;
199 if (!arg) {
200 log->wrap = DEFAULT_WRAPLEN;
201 log->in1 = DEFAULT_INDENT1;
202 log->in2 = DEFAULT_INDENT2;
203 return 0;
204 }
205
206 log->wrap = parse_uint(&arg, ',', DEFAULT_WRAPLEN);
207 log->in1 = parse_uint(&arg, ',', DEFAULT_INDENT1);
208 log->in2 = parse_uint(&arg, '\0', DEFAULT_INDENT2);
209 if (log->wrap < 0 || log->in1 < 0 || log->in2 < 0)
210 return error(wrap_arg_usage);
211 if (log->wrap &&
212 ((log->in1 && log->wrap <= log->in1) ||
213 (log->in2 && log->wrap <= log->in2)))
214 return error(wrap_arg_usage);
215 return 0;
3d711d97
JH
216}
217
552bcac3
DB
218void shortlog_init(struct shortlog *log)
219{
220 memset(log, 0, sizeof(*log));
221
d551a488 222 read_mailmap(&log->mailmap, &log->common_repo_prefix);
552bcac3 223
c455c87c 224 log->list.strdup_strings = 1;
552bcac3
DB
225 log->wrap = DEFAULT_WRAPLEN;
226 log->in1 = DEFAULT_INDENT1;
227 log->in2 = DEFAULT_INDENT2;
228}
229
b8ec5923
JS
230int cmd_shortlog(int argc, const char **argv, const char *prefix)
231{
14ec9cbd
PH
232 static struct shortlog log;
233 static struct rev_info rev;
abe549e1 234 int nongit;
552bcac3 235
14ec9cbd
PH
236 static const struct option options[] = {
237 OPT_BOOLEAN('n', "numbered", &log.sort_by_number,
238 "sort output according to the number of commits per author"),
239 OPT_BOOLEAN('s', "summary", &log.summary,
240 "Suppress commit descriptions, only provides commit count"),
241 OPT_BOOLEAN('e', "email", &log.email,
242 "Show the email address of each author"),
243 { OPTION_CALLBACK, 'w', NULL, &log, "w[,i1[,i2]]",
244 "Linewrap output", PARSE_OPT_OPTARG, &parse_wrap_args },
245 OPT_END(),
246 };
247
248 struct parse_opt_ctx_t ctx;
249
abe549e1 250 prefix = setup_git_directory_gently(&nongit);
d551a488 251 git_config(git_default_config, NULL);
552bcac3 252 shortlog_init(&log);
14ec9cbd
PH
253 init_revisions(&rev, prefix);
254 parse_options_start(&ctx, argc, argv, PARSE_OPT_KEEP_DASHDASH |
255 PARSE_OPT_KEEP_ARGV0);
256
257 for (;;) {
14ec9cbd
PH
258 switch (parse_options_step(&ctx, options, shortlog_usage)) {
259 case PARSE_OPT_HELP:
260 exit(129);
261 case PARSE_OPT_DONE:
262 goto parse_done;
3d711d97 263 }
6b61ec05 264 parse_revision_opt(&rev, &ctx, options, shortlog_usage);
14ec9cbd
PH
265 }
266parse_done:
267 argc = parse_options_end(&ctx);
268
269 if (setup_revisions(argc, argv, &rev, NULL) != 1) {
270 error("unrecognized argument: %s", argv[1]);
271 usage_with_options(shortlog_usage, options);
b8ec5923
JS
272 }
273
b526f8ed
JS
274 log.user_format = rev.commit_format == CMIT_FMT_USERFORMAT;
275
3384a2df 276 /* assume HEAD if from a tty */
abe549e1 277 if (!nongit && !rev.pending.nr && isatty(0))
3384a2df 278 add_head_to_pending(&rev);
0497c620 279 if (rev.pending.nr == 0) {
552bcac3 280 read_from_stdin(&log);
0497c620 281 }
b8ec5923 282 else
552bcac3 283 get_from_rev(&rev, &log);
b8ec5923 284
552bcac3
DB
285 shortlog_output(&log);
286 return 0;
287}
b8ec5923 288
552bcac3
DB
289void shortlog_output(struct shortlog *log)
290{
291 int i, j;
292 if (log->sort_by_number)
c455c87c 293 qsort(log->list.items, log->list.nr, sizeof(struct string_list_item),
552bcac3
DB
294 compare_by_number);
295 for (i = 0; i < log->list.nr; i++) {
c455c87c 296 struct string_list *onelines = log->list.items[i].util;
b8ec5923 297
552bcac3 298 if (log->summary) {
c455c87c 299 printf("%6d\t%s\n", onelines->nr, log->list.items[i].string);
ac60c94d 300 } else {
c455c87c 301 printf("%s (%d):\n", log->list.items[i].string, onelines->nr);
3714e7c8 302 for (j = onelines->nr - 1; j >= 0; j--) {
c455c87c 303 const char *msg = onelines->items[j].string;
3d711d97 304
552bcac3
DB
305 if (log->wrap_lines) {
306 int col = print_wrapped_text(msg, log->in1, log->in2, log->wrap);
307 if (col != log->wrap)
3d711d97
JH
308 putchar('\n');
309 }
310 else
311 printf(" %s\n", msg);
3714e7c8
JS
312 }
313 putchar('\n');
b8ec5923
JS
314 }
315
c455c87c 316 onelines->strdup_strings = 1;
fe73fc1a 317 string_list_clear(onelines, 0);
b8ec5923 318 free(onelines);
552bcac3 319 log->list.items[i].util = NULL;
b8ec5923
JS
320 }
321
c455c87c
JS
322 log->list.strdup_strings = 1;
323 string_list_clear(&log->list, 1);
324 log->mailmap.strdup_strings = 1;
325 string_list_clear(&log->mailmap, 1);
b8ec5923 326}