Merge branch 'maint-1.6.0' into maint-1.6.1
[git/dscho.git] / builtin-shortlog.c
blobe49290687f2a0c55317398ef8b04ac319018c91b
1 #include "builtin.h"
2 #include "cache.h"
3 #include "commit.h"
4 #include "diff.h"
5 #include "string-list.h"
6 #include "revision.h"
7 #include "utf8.h"
8 #include "mailmap.h"
9 #include "shortlog.h"
10 #include "parse-options.h"
12 static char const * const shortlog_usage[] = {
13 "git shortlog [-n] [-s] [-e] [-w] [rev-opts] [--] [<commit-id>... ]",
14 "",
15 "[rev-opts] are documented in git-rev-list(1)",
16 NULL
19 static int compare_by_number(const void *a1, const void *a2)
21 const struct string_list_item *i1 = a1, *i2 = a2;
22 const struct string_list *l1 = i1->util, *l2 = i2->util;
24 if (l1->nr < l2->nr)
25 return 1;
26 else if (l1->nr == l2->nr)
27 return 0;
28 else
29 return -1;
32 const char *format_subject(struct strbuf *sb, const char *msg,
33 const char *line_separator);
35 static void insert_one_record(struct shortlog *log,
36 const char *author,
37 const char *oneline)
39 const char *dot3 = log->common_repo_prefix;
40 char *buffer, *p;
41 struct string_list_item *item;
42 struct string_list *onelines;
43 char namebuf[1024];
44 size_t len;
45 const char *eol;
46 const char *boemail, *eoemail;
47 struct strbuf subject = STRBUF_INIT;
49 boemail = strchr(author, '<');
50 if (!boemail)
51 return;
52 eoemail = strchr(boemail, '>');
53 if (!eoemail)
54 return;
55 if (!map_email(&log->mailmap, boemail+1, namebuf, sizeof(namebuf))) {
56 while (author < boemail && isspace(*author))
57 author++;
58 for (len = 0;
59 len < sizeof(namebuf) - 1 && author + len < boemail;
60 len++)
61 namebuf[len] = author[len];
62 while (0 < len && isspace(namebuf[len-1]))
63 len--;
64 namebuf[len] = '\0';
66 else
67 len = strlen(namebuf);
69 if (log->email) {
70 size_t room = sizeof(namebuf) - len - 1;
71 int maillen = eoemail - boemail + 1;
72 snprintf(namebuf + len, room, " %.*s", maillen, boemail);
75 buffer = xstrdup(namebuf);
76 item = string_list_insert(buffer, &log->list);
77 if (item->util == NULL)
78 item->util = xcalloc(1, sizeof(struct string_list));
79 else
80 free(buffer);
82 /* Skip any leading whitespace, including any blank lines. */
83 while (*oneline && isspace(*oneline))
84 oneline++;
85 eol = strchr(oneline, '\n');
86 if (!eol)
87 eol = oneline + strlen(oneline);
88 if (!prefixcmp(oneline, "[PATCH")) {
89 char *eob = strchr(oneline, ']');
90 if (eob && (!eol || eob < eol))
91 oneline = eob + 1;
93 while (*oneline && isspace(*oneline) && *oneline != '\n')
94 oneline++;
95 len = eol - oneline;
96 format_subject(&subject, oneline, " ");
97 buffer = strbuf_detach(&subject, NULL);
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);
110 onelines = item->util;
111 if (onelines->nr >= onelines->alloc) {
112 onelines->alloc = alloc_nr(onelines->nr);
113 onelines->items = xrealloc(onelines->items,
114 onelines->alloc
115 * sizeof(struct string_list_item));
118 onelines->items[onelines->nr].util = NULL;
119 onelines->items[onelines->nr++].string = buffer;
122 static void read_from_stdin(struct shortlog *log)
124 char author[1024], oneline[1024];
126 while (fgets(author, sizeof(author), stdin) != NULL) {
127 if (!(author[0] == 'A' || author[0] == 'a') ||
128 prefixcmp(author + 1, "uthor: "))
129 continue;
130 while (fgets(oneline, sizeof(oneline), stdin) &&
131 oneline[0] != '\n')
132 ; /* discard headers */
133 while (fgets(oneline, sizeof(oneline), stdin) &&
134 oneline[0] == '\n')
135 ; /* discard blanks */
136 insert_one_record(log, author + 8, oneline);
140 void shortlog_add_commit(struct shortlog *log, struct commit *commit)
142 const char *author = NULL, *buffer;
144 buffer = commit->buffer;
145 while (*buffer && *buffer != '\n') {
146 const char *eol = strchr(buffer, '\n');
148 if (eol == NULL)
149 eol = buffer + strlen(buffer);
150 else
151 eol++;
153 if (!prefixcmp(buffer, "author "))
154 author = buffer + 7;
155 buffer = eol;
157 if (!author)
158 die("Missing author: %s",
159 sha1_to_hex(commit->object.sha1));
160 if (log->user_format) {
161 struct strbuf buf = STRBUF_INIT;
163 pretty_print_commit(CMIT_FMT_USERFORMAT, commit, &buf,
164 DEFAULT_ABBREV, "", "", DATE_NORMAL, 0);
165 insert_one_record(log, author, buf.buf);
166 strbuf_release(&buf);
167 return;
169 if (*buffer)
170 buffer++;
171 insert_one_record(log, author, !*buffer ? "<none>" : buffer);
174 static void get_from_rev(struct rev_info *rev, struct shortlog *log)
176 struct commit *commit;
178 if (prepare_revision_walk(rev))
179 die("revision walk setup failed");
180 while ((commit = get_revision(rev)) != NULL)
181 shortlog_add_commit(log, commit);
184 static int parse_uint(char const **arg, int comma, int defval)
186 unsigned long ul;
187 int ret;
188 char *endp;
190 ul = strtoul(*arg, &endp, 10);
191 if (*endp && *endp != comma)
192 return -1;
193 if (ul > INT_MAX)
194 return -1;
195 ret = *arg == endp ? defval : (int)ul;
196 *arg = *endp ? endp + 1 : endp;
197 return ret;
200 static const char wrap_arg_usage[] = "-w[<width>[,<indent1>[,<indent2>]]]";
201 #define DEFAULT_WRAPLEN 76
202 #define DEFAULT_INDENT1 6
203 #define DEFAULT_INDENT2 9
205 static int parse_wrap_args(const struct option *opt, const char *arg, int unset)
207 struct shortlog *log = opt->value;
209 log->wrap_lines = !unset;
210 if (unset)
211 return 0;
212 if (!arg) {
213 log->wrap = DEFAULT_WRAPLEN;
214 log->in1 = DEFAULT_INDENT1;
215 log->in2 = DEFAULT_INDENT2;
216 return 0;
219 log->wrap = parse_uint(&arg, ',', DEFAULT_WRAPLEN);
220 log->in1 = parse_uint(&arg, ',', DEFAULT_INDENT1);
221 log->in2 = parse_uint(&arg, '\0', DEFAULT_INDENT2);
222 if (log->wrap < 0 || log->in1 < 0 || log->in2 < 0)
223 return error(wrap_arg_usage);
224 if (log->wrap &&
225 ((log->in1 && log->wrap <= log->in1) ||
226 (log->in2 && log->wrap <= log->in2)))
227 return error(wrap_arg_usage);
228 return 0;
231 void shortlog_init(struct shortlog *log)
233 memset(log, 0, sizeof(*log));
235 read_mailmap(&log->mailmap, ".mailmap", &log->common_repo_prefix);
237 log->list.strdup_strings = 1;
238 log->wrap = DEFAULT_WRAPLEN;
239 log->in1 = DEFAULT_INDENT1;
240 log->in2 = DEFAULT_INDENT2;
243 int cmd_shortlog(int argc, const char **argv, const char *prefix)
245 static struct shortlog log;
246 static struct rev_info rev;
247 int nongit;
249 static const struct option options[] = {
250 OPT_BOOLEAN('n', "numbered", &log.sort_by_number,
251 "sort output according to the number of commits per author"),
252 OPT_BOOLEAN('s', "summary", &log.summary,
253 "Suppress commit descriptions, only provides commit count"),
254 OPT_BOOLEAN('e', "email", &log.email,
255 "Show the email address of each author"),
256 { OPTION_CALLBACK, 'w', NULL, &log, "w[,i1[,i2]]",
257 "Linewrap output", PARSE_OPT_OPTARG, &parse_wrap_args },
258 OPT_END(),
261 struct parse_opt_ctx_t ctx;
263 prefix = setup_git_directory_gently(&nongit);
264 shortlog_init(&log);
265 init_revisions(&rev, prefix);
266 parse_options_start(&ctx, argc, argv, PARSE_OPT_KEEP_DASHDASH |
267 PARSE_OPT_KEEP_ARGV0);
269 for (;;) {
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;
276 parse_revision_opt(&rev, &ctx, options, shortlog_usage);
278 parse_done:
279 argc = parse_options_end(&ctx);
281 if (setup_revisions(argc, argv, &rev, NULL) != 1) {
282 error("unrecognized argument: %s", argv[1]);
283 usage_with_options(shortlog_usage, options);
286 log.user_format = rev.commit_format == CMIT_FMT_USERFORMAT;
288 /* assume HEAD if from a tty */
289 if (!nongit && !rev.pending.nr && isatty(0))
290 add_head_to_pending(&rev);
291 if (rev.pending.nr == 0) {
292 read_from_stdin(&log);
294 else
295 get_from_rev(&rev, &log);
297 shortlog_output(&log);
298 return 0;
301 void shortlog_output(struct shortlog *log)
303 int i, j;
304 if (log->sort_by_number)
305 qsort(log->list.items, log->list.nr, sizeof(struct string_list_item),
306 compare_by_number);
307 for (i = 0; i < log->list.nr; i++) {
308 struct string_list *onelines = log->list.items[i].util;
310 if (log->summary) {
311 printf("%6d\t%s\n", onelines->nr, log->list.items[i].string);
312 } else {
313 printf("%s (%d):\n", log->list.items[i].string, onelines->nr);
314 for (j = onelines->nr - 1; j >= 0; j--) {
315 const char *msg = onelines->items[j].string;
317 if (log->wrap_lines) {
318 int col = print_wrapped_text(msg, log->in1, log->in2, log->wrap);
319 if (col != log->wrap)
320 putchar('\n');
322 else
323 printf(" %s\n", msg);
325 putchar('\n');
328 onelines->strdup_strings = 1;
329 string_list_clear(onelines, 1);
330 free(onelines);
331 log->list.items[i].util = NULL;
334 log->list.strdup_strings = 1;
335 string_list_clear(&log->list, 1);
336 log->mailmap.strdup_strings = 1;
337 string_list_clear(&log->mailmap, 1);