shortlog: optimize "--summary" mode
[git.git] / builtin / shortlog.c
blob973b50d417753618da50b9d74d290280bc914ff7
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 N_("git shortlog [<options>] [<revision-range>] [[--] [<path>...]]"),
14 NULL
17 static int compare_by_number(const void *a1, const void *a2)
19 const struct string_list_item *i1 = a1, *i2 = a2;
20 const struct string_list *l1 = i1->util, *l2 = i2->util;
22 if (l1->nr < l2->nr)
23 return 1;
24 else if (l1->nr == l2->nr)
25 return 0;
26 else
27 return -1;
30 static void insert_one_record(struct shortlog *log,
31 const char *author,
32 const char *oneline)
34 const char *dot3 = log->common_repo_prefix;
35 char *buffer, *p;
36 struct string_list_item *item;
37 const char *mailbuf, *namebuf;
38 size_t namelen, maillen;
39 const char *eol;
40 struct strbuf subject = STRBUF_INIT;
41 struct strbuf namemailbuf = STRBUF_INIT;
42 struct ident_split ident;
44 if (split_ident_line(&ident, author, strlen(author)))
45 return;
47 namebuf = ident.name_begin;
48 mailbuf = ident.mail_begin;
49 namelen = ident.name_end - ident.name_begin;
50 maillen = ident.mail_end - ident.mail_begin;
52 map_user(&log->mailmap, &mailbuf, &maillen, &namebuf, &namelen);
53 strbuf_add(&namemailbuf, namebuf, namelen);
55 if (log->email)
56 strbuf_addf(&namemailbuf, " <%.*s>", (int)maillen, mailbuf);
58 item = string_list_insert(&log->list, namemailbuf.buf);
59 if (item->util == NULL)
60 item->util = xcalloc(1, sizeof(struct string_list));
62 /* Skip any leading whitespace, including any blank lines. */
63 while (*oneline && isspace(*oneline))
64 oneline++;
65 eol = strchr(oneline, '\n');
66 if (!eol)
67 eol = oneline + strlen(oneline);
68 if (starts_with(oneline, "[PATCH")) {
69 char *eob = strchr(oneline, ']');
70 if (eob && (!eol || eob < eol))
71 oneline = eob + 1;
73 while (*oneline && isspace(*oneline) && *oneline != '\n')
74 oneline++;
75 format_subject(&subject, oneline, " ");
76 buffer = strbuf_detach(&subject, NULL);
78 if (dot3) {
79 int dot3len = strlen(dot3);
80 if (dot3len > 5) {
81 while ((p = strstr(buffer, dot3)) != NULL) {
82 int taillen = strlen(p) - dot3len;
83 memcpy(p, "/.../", 5);
84 memmove(p + 5, p + dot3len, taillen + 1);
89 string_list_append(item->util, buffer);
92 static void read_from_stdin(struct shortlog *log)
94 struct strbuf author = STRBUF_INIT;
95 struct strbuf oneline = STRBUF_INIT;
97 while (strbuf_getline(&author, stdin, '\n') != EOF) {
98 const char *v;
99 if (!skip_prefix(author.buf, "Author: ", &v) &&
100 !skip_prefix(author.buf, "author ", &v))
101 continue;
102 while (strbuf_getline(&oneline, stdin, '\n') != EOF &&
103 oneline.len)
104 ; /* discard headers */
105 while (strbuf_getline(&oneline, stdin, '\n') != EOF &&
106 !oneline.len)
107 ; /* discard blanks */
108 insert_one_record(log, v, oneline.buf);
110 strbuf_release(&author);
111 strbuf_release(&oneline);
114 void shortlog_add_commit(struct shortlog *log, struct commit *commit)
116 struct strbuf author = STRBUF_INIT;
117 struct strbuf oneline = STRBUF_INIT;
118 struct pretty_print_context ctx = {0};
120 ctx.fmt = CMIT_FMT_USERFORMAT;
121 ctx.abbrev = log->abbrev;
122 ctx.subject = "";
123 ctx.after_subject = "";
124 ctx.date_mode.type = DATE_NORMAL;
125 ctx.output_encoding = get_log_output_encoding();
127 format_commit_message(commit, "%an <%ae>", &author, &ctx);
128 /* we can detect a total failure only by seeing " <>" in the output */
129 if (author.len <= 3) {
130 warning(_("Missing author: %s"),
131 oid_to_hex(&commit->object.oid));
132 goto out;
135 if (!log->summary) {
136 if (log->user_format)
137 pretty_print_commit(&ctx, commit, &oneline);
138 else
139 format_commit_message(commit, "%s", &oneline, &ctx);
142 insert_one_record(log, author.buf, oneline.len ? oneline.buf : "<none>");
144 out:
145 strbuf_release(&author);
146 strbuf_release(&oneline);
149 static void get_from_rev(struct rev_info *rev, struct shortlog *log)
151 struct commit *commit;
153 if (prepare_revision_walk(rev))
154 die(_("revision walk setup failed"));
155 while ((commit = get_revision(rev)) != NULL)
156 shortlog_add_commit(log, commit);
159 static int parse_uint(char const **arg, int comma, int defval)
161 unsigned long ul;
162 int ret;
163 char *endp;
165 ul = strtoul(*arg, &endp, 10);
166 if (*endp && *endp != comma)
167 return -1;
168 if (ul > INT_MAX)
169 return -1;
170 ret = *arg == endp ? defval : (int)ul;
171 *arg = *endp ? endp + 1 : endp;
172 return ret;
175 static const char wrap_arg_usage[] = "-w[<width>[,<indent1>[,<indent2>]]]";
176 #define DEFAULT_WRAPLEN 76
177 #define DEFAULT_INDENT1 6
178 #define DEFAULT_INDENT2 9
180 static int parse_wrap_args(const struct option *opt, const char *arg, int unset)
182 struct shortlog *log = opt->value;
184 log->wrap_lines = !unset;
185 if (unset)
186 return 0;
187 if (!arg) {
188 log->wrap = DEFAULT_WRAPLEN;
189 log->in1 = DEFAULT_INDENT1;
190 log->in2 = DEFAULT_INDENT2;
191 return 0;
194 log->wrap = parse_uint(&arg, ',', DEFAULT_WRAPLEN);
195 log->in1 = parse_uint(&arg, ',', DEFAULT_INDENT1);
196 log->in2 = parse_uint(&arg, '\0', DEFAULT_INDENT2);
197 if (log->wrap < 0 || log->in1 < 0 || log->in2 < 0)
198 return error(wrap_arg_usage);
199 if (log->wrap &&
200 ((log->in1 && log->wrap <= log->in1) ||
201 (log->in2 && log->wrap <= log->in2)))
202 return error(wrap_arg_usage);
203 return 0;
206 void shortlog_init(struct shortlog *log)
208 memset(log, 0, sizeof(*log));
210 read_mailmap(&log->mailmap, &log->common_repo_prefix);
212 log->list.strdup_strings = 1;
213 log->wrap = DEFAULT_WRAPLEN;
214 log->in1 = DEFAULT_INDENT1;
215 log->in2 = DEFAULT_INDENT2;
218 int cmd_shortlog(int argc, const char **argv, const char *prefix)
220 static struct shortlog log;
221 static struct rev_info rev;
222 int nongit = !startup_info->have_repository;
224 static const struct option options[] = {
225 OPT_BOOL('n', "numbered", &log.sort_by_number,
226 N_("sort output according to the number of commits per author")),
227 OPT_BOOL('s', "summary", &log.summary,
228 N_("Suppress commit descriptions, only provides commit count")),
229 OPT_BOOL('e', "email", &log.email,
230 N_("Show the email address of each author")),
231 { OPTION_CALLBACK, 'w', NULL, &log, N_("w[,i1[,i2]]"),
232 N_("Linewrap output"), PARSE_OPT_OPTARG, &parse_wrap_args },
233 OPT_END(),
236 struct parse_opt_ctx_t ctx;
238 git_config(git_default_config, NULL);
239 shortlog_init(&log);
240 init_revisions(&rev, prefix);
241 parse_options_start(&ctx, argc, argv, prefix, options,
242 PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0);
244 for (;;) {
245 switch (parse_options_step(&ctx, options, shortlog_usage)) {
246 case PARSE_OPT_HELP:
247 exit(129);
248 case PARSE_OPT_DONE:
249 goto parse_done;
251 parse_revision_opt(&rev, &ctx, options, shortlog_usage);
253 parse_done:
254 argc = parse_options_end(&ctx);
256 if (setup_revisions(argc, argv, &rev, NULL) != 1) {
257 error(_("unrecognized argument: %s"), argv[1]);
258 usage_with_options(shortlog_usage, options);
261 log.user_format = rev.commit_format == CMIT_FMT_USERFORMAT;
262 log.abbrev = rev.abbrev;
264 /* assume HEAD if from a tty */
265 if (!nongit && !rev.pending.nr && isatty(0))
266 add_head_to_pending(&rev);
267 if (rev.pending.nr == 0) {
268 if (isatty(0))
269 fprintf(stderr, _("(reading log message from standard input)\n"));
270 read_from_stdin(&log);
272 else
273 get_from_rev(&rev, &log);
275 shortlog_output(&log);
276 return 0;
279 static void add_wrapped_shortlog_msg(struct strbuf *sb, const char *s,
280 const struct shortlog *log)
282 strbuf_add_wrapped_text(sb, s, log->in1, log->in2, log->wrap);
283 strbuf_addch(sb, '\n');
286 void shortlog_output(struct shortlog *log)
288 int i, j;
289 struct strbuf sb = STRBUF_INIT;
291 if (log->sort_by_number)
292 qsort(log->list.items, log->list.nr, sizeof(struct string_list_item),
293 compare_by_number);
294 for (i = 0; i < log->list.nr; i++) {
295 struct string_list *onelines = log->list.items[i].util;
297 if (log->summary) {
298 printf("%6d\t%s\n", onelines->nr, log->list.items[i].string);
299 } else {
300 printf("%s (%d):\n", log->list.items[i].string, onelines->nr);
301 for (j = onelines->nr - 1; j >= 0; j--) {
302 const char *msg = onelines->items[j].string;
304 if (log->wrap_lines) {
305 strbuf_reset(&sb);
306 add_wrapped_shortlog_msg(&sb, msg, log);
307 fwrite(sb.buf, sb.len, 1, stdout);
309 else
310 printf(" %s\n", msg);
312 putchar('\n');
315 onelines->strdup_strings = 1;
316 string_list_clear(onelines, 0);
317 free(onelines);
318 log->list.items[i].util = NULL;
321 strbuf_release(&sb);
322 log->list.strdup_strings = 1;
323 string_list_clear(&log->list, 1);
324 clear_mailmap(&log->mailmap);