shortlog: add '--sort-key' and '--sort-key-regexp' options
[git/dscho.git] / builtin-shortlog.c
blob2cab5e4e67b16c5739248c5d1520ea67ce765bd4
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_record1(struct shortlog *log,
36 const char *name, const char *oneline)
38 const char *dot3 = log->common_repo_prefix;
39 char *buffer, *p;
40 struct string_list_item *item;
41 const char *eol;
42 struct strbuf subject = STRBUF_INIT;
44 item = string_list_insert(name, &log->list);
45 if (item->util == NULL)
46 item->util = xcalloc(1, sizeof(struct string_list));
48 /* Skip any leading whitespace, including any blank lines. */
49 while (*oneline && isspace(*oneline))
50 oneline++;
51 eol = strchr(oneline, '\n');
52 if (!eol)
53 eol = oneline + strlen(oneline);
54 if (!prefixcmp(oneline, "[PATCH")) {
55 char *eob = strchr(oneline, ']');
56 if (eob && (!eol || eob < eol))
57 oneline = eob + 1;
59 while (*oneline && isspace(*oneline) && *oneline != '\n')
60 oneline++;
61 format_subject(&subject, oneline, " ");
62 buffer = strbuf_detach(&subject, NULL);
64 if (dot3) {
65 int dot3len = strlen(dot3);
66 if (dot3len > 5) {
67 while ((p = strstr(buffer, dot3)) != NULL) {
68 int taillen = strlen(p) - dot3len;
69 memcpy(p, "/.../", 5);
70 memmove(p + 5, p + dot3len, taillen + 1);
75 string_list_append(buffer, item->util);
78 static void insert_one_record(struct shortlog *log,
79 const char *author,
80 const char *oneline)
82 char namebuf[1024];
83 char emailbuf[1024];
84 size_t len;
85 const char *boemail, *eoemail;
87 boemail = strchr(author, '<');
88 if (!boemail)
89 return;
90 eoemail = strchr(boemail, '>');
91 if (!eoemail)
92 return;
94 /* copy author name to namebuf, to support matching on both name and email */
95 memcpy(namebuf, author, boemail - author);
96 len = boemail - author;
97 while(len > 0 && isspace(namebuf[len-1]))
98 len--;
99 namebuf[len] = 0;
101 /* copy email name to emailbuf, to allow email replacement as well */
102 memcpy(emailbuf, boemail+1, eoemail - boemail);
103 emailbuf[eoemail - boemail - 1] = 0;
105 if (!map_user(&log->mailmap, emailbuf, sizeof(emailbuf), namebuf, sizeof(namebuf))) {
106 while (author < boemail && isspace(*author))
107 author++;
108 for (len = 0;
109 len < sizeof(namebuf) - 1 && author + len < boemail;
110 len++)
111 namebuf[len] = author[len];
112 while (0 < len && isspace(namebuf[len-1]))
113 len--;
114 namebuf[len] = '\0';
116 else
117 len = strlen(namebuf);
119 if (log->email) {
120 size_t room = sizeof(namebuf) - len - 1;
121 int maillen = strlen(emailbuf);
122 snprintf(namebuf + len, room, " <%.*s>", maillen, emailbuf);
125 insert_one_record1(log, namebuf, oneline);
128 static void read_from_stdin(struct shortlog *log)
130 char author[1024], oneline[1024];
132 while (fgets(author, sizeof(author), stdin) != NULL) {
133 if (!(author[0] == 'A' || author[0] == 'a') ||
134 prefixcmp(author + 1, "uthor: "))
135 continue;
136 while (fgets(oneline, sizeof(oneline), stdin) &&
137 oneline[0] != '\n')
138 ; /* discard headers */
139 while (fgets(oneline, sizeof(oneline), stdin) &&
140 oneline[0] == '\n')
141 ; /* discard blanks */
142 insert_one_record(log, author + 8, oneline);
146 void shortlog_add_commit(struct shortlog *log, struct commit *commit)
148 struct strbuf buf = STRBUF_INIT;
149 char *key = NULL;
150 const char *author = NULL, *buffer;
152 buffer = commit->buffer;
153 if (log->user_format)
154 pretty_print_commit(CMIT_FMT_USERFORMAT, commit, &buf,
155 DEFAULT_ABBREV, "", "", DATE_NORMAL, 0);
157 if (log->sort_key) {
158 const char *p = buf.buf;
159 regmatch_t match[2];
161 if (!log->user_format) {
162 p = strstr(buffer, "\n\n");
163 if (!p)
164 return;
165 p += 2;
168 if (!regexec(log->sort_key, p, 2, match, 0) &&
169 match[1].rm_so >= 0)
170 author = key = xstrndup(p + match[1].rm_so,
171 match[1].rm_eo - match[1].rm_so);
172 else
173 author = "<null>";
174 insert_one_record1(log, key, p);
175 strbuf_release(&buf);
176 return;
179 while (*buffer && *buffer != '\n') {
180 const char *eol = strchr(buffer, '\n');
182 if (eol == NULL)
183 eol = buffer + strlen(buffer);
184 else
185 eol++;
187 if (!prefixcmp(buffer, "author "))
188 author = buffer + 7;
189 buffer = eol;
191 if (!author)
192 die("Missing author: %s",
193 sha1_to_hex(commit->object.sha1));
194 if (log->user_format) {
195 insert_one_record(log, author, buf.buf);
196 strbuf_release(&buf);
197 return;
199 if (*buffer)
200 buffer++;
201 insert_one_record(log, author, !*buffer ? "<none>" : buffer);
202 free(key);
205 static void get_from_rev(struct rev_info *rev, struct shortlog *log)
207 struct commit *commit;
209 if (prepare_revision_walk(rev))
210 die("revision walk setup failed");
211 while ((commit = get_revision(rev)) != NULL)
212 shortlog_add_commit(log, commit);
215 static int parse_uint(char const **arg, int comma, int defval)
217 unsigned long ul;
218 int ret;
219 char *endp;
221 ul = strtoul(*arg, &endp, 10);
222 if (*endp && *endp != comma)
223 return -1;
224 if (ul > INT_MAX)
225 return -1;
226 ret = *arg == endp ? defval : (int)ul;
227 *arg = *endp ? endp + 1 : endp;
228 return ret;
231 static const char wrap_arg_usage[] = "-w[<width>[,<indent1>[,<indent2>]]]";
232 #define DEFAULT_WRAPLEN 76
233 #define DEFAULT_INDENT1 6
234 #define DEFAULT_INDENT2 9
236 static int parse_wrap_args(const struct option *opt, const char *arg, int unset)
238 struct shortlog *log = opt->value;
240 log->wrap_lines = !unset;
241 if (unset)
242 return 0;
243 if (!arg) {
244 log->wrap = DEFAULT_WRAPLEN;
245 log->in1 = DEFAULT_INDENT1;
246 log->in2 = DEFAULT_INDENT2;
247 return 0;
250 log->wrap = parse_uint(&arg, ',', DEFAULT_WRAPLEN);
251 log->in1 = parse_uint(&arg, ',', DEFAULT_INDENT1);
252 log->in2 = parse_uint(&arg, '\0', DEFAULT_INDENT2);
253 if (log->wrap < 0 || log->in1 < 0 || log->in2 < 0)
254 return error(wrap_arg_usage);
255 if (log->wrap &&
256 ((log->in1 && log->wrap <= log->in1) ||
257 (log->in2 && log->wrap <= log->in2)))
258 return error(wrap_arg_usage);
259 return 0;
262 void shortlog_init(struct shortlog *log)
264 memset(log, 0, sizeof(*log));
266 read_mailmap(&log->mailmap, &log->common_repo_prefix);
268 log->list.strdup_strings = 1;
269 log->wrap = DEFAULT_WRAPLEN;
270 log->in1 = DEFAULT_INDENT1;
271 log->in2 = DEFAULT_INDENT2;
274 int cmd_shortlog(int argc, const char **argv, const char *prefix)
276 static struct shortlog log;
277 static struct rev_info rev;
278 static const char *sort_key_regexp = NULL;
279 static int sort_key_field = 0;
280 int nongit;
282 static const struct option options[] = {
283 OPT_BOOLEAN('n', "numbered", &log.sort_by_number,
284 "sort output according to the number of commits per author"),
285 OPT_BOOLEAN('s', "summary", &log.summary,
286 "Suppress commit descriptions, only provides commit count"),
287 OPT_BOOLEAN('e', "email", &log.email,
288 "Show the email address of each author"),
289 { OPTION_CALLBACK, 'w', NULL, &log, "w[,i1[,i2]]",
290 "Linewrap output", PARSE_OPT_OPTARG, &parse_wrap_args },
291 OPT_STRING('K', "sort-key-regexp", &sort_key_regexp, "<regexp>",
292 "Sort shortlog by the given regular expression"),
293 OPT_INTEGER('k', "sort-key", &sort_key_field,
294 "Sort shortlog by the given field "
295 "(whitespace-delimited)"),
296 OPT_END(),
299 struct parse_opt_ctx_t ctx;
301 prefix = setup_git_directory_gently(&nongit);
302 git_config(git_default_config, NULL);
303 shortlog_init(&log);
304 init_revisions(&rev, prefix);
305 parse_options_start(&ctx, argc, argv, prefix, PARSE_OPT_KEEP_DASHDASH |
306 PARSE_OPT_KEEP_ARGV0);
308 for (;;) {
309 switch (parse_options_step(&ctx, options, shortlog_usage)) {
310 case PARSE_OPT_HELP:
311 exit(129);
312 case PARSE_OPT_DONE:
313 goto parse_done;
315 parse_revision_opt(&rev, &ctx, options, shortlog_usage);
317 parse_done:
318 argc = parse_options_end(&ctx);
320 if (sort_key_regexp && sort_key_field > 0)
321 die ("--sort-key-regexp and --sort-key-field are incompatible");
323 if (sort_key_regexp) {
324 log.sort_key = xmalloc(sizeof(*log.sort_key));
325 if (regcomp(log.sort_key, sort_key_regexp, 0))
326 die ("Invalid regular expression: '%s'",
327 sort_key_regexp);
329 if (sort_key_field > 0) {
330 struct strbuf regexp = STRBUF_INIT;
331 strbuf_addstr(&regexp, "^[ \t\n]*");
332 while (--sort_key_field)
333 strbuf_addstr(&regexp, "[^ \t\n]*[ \t\n]*");
334 strbuf_addstr(&regexp, "\\([^ \t\n]*\\)[ \t\n]*.*$");
335 log.sort_key = xmalloc(sizeof(*log.sort_key));
336 if (regcomp(log.sort_key, regexp.buf, 0))
337 die ("Invalid regular expression: '%s'", regexp.buf);
338 strbuf_release(&regexp);
341 if (setup_revisions(argc, argv, &rev, NULL) != 1) {
342 error("unrecognized argument: %s", argv[1]);
343 usage_with_options(shortlog_usage, options);
346 log.user_format = rev.commit_format == CMIT_FMT_USERFORMAT;
348 /* assume HEAD if from a tty */
349 if (!nongit && !rev.pending.nr && isatty(0))
350 add_head_to_pending(&rev);
351 if (rev.pending.nr == 0) {
352 if (log.sort_key)
353 die ("Specifying a sort key is incompatible with "
354 "reading from stdin.");
355 read_from_stdin(&log);
357 else
358 get_from_rev(&rev, &log);
360 shortlog_output(&log);
361 return 0;
364 void shortlog_output(struct shortlog *log)
366 int i, j;
367 if (log->sort_by_number)
368 qsort(log->list.items, log->list.nr, sizeof(struct string_list_item),
369 compare_by_number);
370 for (i = 0; i < log->list.nr; i++) {
371 struct string_list *onelines = log->list.items[i].util;
373 if (log->summary) {
374 printf("%6d\t%s\n", onelines->nr, log->list.items[i].string);
375 } else {
376 printf("%s (%d):\n", log->list.items[i].string, onelines->nr);
377 for (j = onelines->nr - 1; j >= 0; j--) {
378 const char *msg = onelines->items[j].string;
380 if (log->wrap_lines) {
381 int col = print_wrapped_text(msg, log->in1, log->in2, log->wrap);
382 if (col != log->wrap)
383 putchar('\n');
385 else
386 printf(" %s\n", msg);
388 putchar('\n');
391 onelines->strdup_strings = 1;
392 string_list_clear(onelines, 0);
393 free(onelines);
394 log->list.items[i].util = NULL;
397 log->list.strdup_strings = 1;
398 string_list_clear(&log->list, 1);
399 clear_mailmap(&log->mailmap);