Use split_ident_line to parse author and committer
[git/gitweb.git] / builtin / shortlog.c
blob03c6cd7e868ff7451a719586534105a9b6c0fc40
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 [-n] [-s] [-e] [-w] [rev-opts] [--] [<commit-id>... ]"),
14 "",
15 N_("[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 static void insert_one_record(struct shortlog *log,
33 const char *author,
34 const char *oneline)
36 const char *dot3 = log->common_repo_prefix;
37 char *buffer, *p;
38 struct string_list_item *item;
39 char namebuf[1024];
40 char emailbuf[1024];
41 size_t len;
42 const char *eol;
43 struct strbuf subject = STRBUF_INIT;
44 struct ident_split ident;
46 if (split_ident_line(&ident, author, strlen(author)))
47 return;
49 /* copy author name to namebuf, to support matching on both name and email */
50 len = ident.name_end - ident.name_begin;
51 memcpy(namebuf, ident.name_begin, len);
52 namebuf[len] = 0;
54 /* copy email name to emailbuf, to allow email replacement as well */
55 len = ident.mail_end - ident.mail_begin;
56 memcpy(emailbuf, ident.mail_begin, len);
57 emailbuf[len] = 0;
59 map_user(&log->mailmap, emailbuf, sizeof(emailbuf), namebuf, sizeof(namebuf));
60 len = strlen(namebuf);
62 if (log->email) {
63 size_t room = sizeof(namebuf) - len - 1;
64 int maillen = strlen(emailbuf);
65 snprintf(namebuf + len, room, " <%.*s>", maillen, emailbuf);
68 item = string_list_insert(&log->list, namebuf);
69 if (item->util == NULL)
70 item->util = xcalloc(1, sizeof(struct string_list));
72 /* Skip any leading whitespace, including any blank lines. */
73 while (*oneline && isspace(*oneline))
74 oneline++;
75 eol = strchr(oneline, '\n');
76 if (!eol)
77 eol = oneline + strlen(oneline);
78 if (!prefixcmp(oneline, "[PATCH")) {
79 char *eob = strchr(oneline, ']');
80 if (eob && (!eol || eob < eol))
81 oneline = eob + 1;
83 while (*oneline && isspace(*oneline) && *oneline != '\n')
84 oneline++;
85 format_subject(&subject, oneline, " ");
86 buffer = strbuf_detach(&subject, NULL);
88 if (dot3) {
89 int dot3len = strlen(dot3);
90 if (dot3len > 5) {
91 while ((p = strstr(buffer, dot3)) != NULL) {
92 int taillen = strlen(p) - dot3len;
93 memcpy(p, "/.../", 5);
94 memmove(p + 5, p + dot3len, taillen + 1);
99 string_list_append(item->util, buffer);
102 static void read_from_stdin(struct shortlog *log)
104 char author[1024], oneline[1024];
106 while (fgets(author, sizeof(author), stdin) != NULL) {
107 if (!(author[0] == 'A' || author[0] == 'a') ||
108 prefixcmp(author + 1, "uthor: "))
109 continue;
110 while (fgets(oneline, sizeof(oneline), stdin) &&
111 oneline[0] != '\n')
112 ; /* discard headers */
113 while (fgets(oneline, sizeof(oneline), stdin) &&
114 oneline[0] == '\n')
115 ; /* discard blanks */
116 insert_one_record(log, author + 8, oneline);
120 void shortlog_add_commit(struct shortlog *log, struct commit *commit)
122 const char *author = NULL, *buffer;
123 struct strbuf buf = STRBUF_INIT;
124 struct strbuf ufbuf = STRBUF_INIT;
126 pp_commit_easy(CMIT_FMT_RAW, commit, &buf);
127 buffer = buf.buf;
128 while (*buffer && *buffer != '\n') {
129 const char *eol = strchr(buffer, '\n');
131 if (eol == NULL)
132 eol = buffer + strlen(buffer);
133 else
134 eol++;
136 if (!prefixcmp(buffer, "author "))
137 author = buffer + 7;
138 buffer = eol;
140 if (!author)
141 die(_("Missing author: %s"),
142 sha1_to_hex(commit->object.sha1));
143 if (log->user_format) {
144 struct pretty_print_context ctx = {0};
145 ctx.fmt = CMIT_FMT_USERFORMAT;
146 ctx.abbrev = log->abbrev;
147 ctx.subject = "";
148 ctx.after_subject = "";
149 ctx.date_mode = DATE_NORMAL;
150 pretty_print_commit(&ctx, commit, &ufbuf);
151 buffer = ufbuf.buf;
152 } else if (*buffer) {
153 buffer++;
155 insert_one_record(log, author, !*buffer ? "<none>" : buffer);
156 strbuf_release(&ufbuf);
157 strbuf_release(&buf);
160 static void get_from_rev(struct rev_info *rev, struct shortlog *log)
162 struct commit *commit;
164 if (prepare_revision_walk(rev))
165 die(_("revision walk setup failed"));
166 while ((commit = get_revision(rev)) != NULL)
167 shortlog_add_commit(log, commit);
170 static int parse_uint(char const **arg, int comma, int defval)
172 unsigned long ul;
173 int ret;
174 char *endp;
176 ul = strtoul(*arg, &endp, 10);
177 if (*endp && *endp != comma)
178 return -1;
179 if (ul > INT_MAX)
180 return -1;
181 ret = *arg == endp ? defval : (int)ul;
182 *arg = *endp ? endp + 1 : endp;
183 return ret;
186 static const char wrap_arg_usage[] = "-w[<width>[,<indent1>[,<indent2>]]]";
187 #define DEFAULT_WRAPLEN 76
188 #define DEFAULT_INDENT1 6
189 #define DEFAULT_INDENT2 9
191 static int parse_wrap_args(const struct option *opt, const char *arg, int unset)
193 struct shortlog *log = opt->value;
195 log->wrap_lines = !unset;
196 if (unset)
197 return 0;
198 if (!arg) {
199 log->wrap = DEFAULT_WRAPLEN;
200 log->in1 = DEFAULT_INDENT1;
201 log->in2 = DEFAULT_INDENT2;
202 return 0;
205 log->wrap = parse_uint(&arg, ',', DEFAULT_WRAPLEN);
206 log->in1 = parse_uint(&arg, ',', DEFAULT_INDENT1);
207 log->in2 = parse_uint(&arg, '\0', DEFAULT_INDENT2);
208 if (log->wrap < 0 || log->in1 < 0 || log->in2 < 0)
209 return error(wrap_arg_usage);
210 if (log->wrap &&
211 ((log->in1 && log->wrap <= log->in1) ||
212 (log->in2 && log->wrap <= log->in2)))
213 return error(wrap_arg_usage);
214 return 0;
217 void shortlog_init(struct shortlog *log)
219 memset(log, 0, sizeof(*log));
221 read_mailmap(&log->mailmap, &log->common_repo_prefix);
223 log->list.strdup_strings = 1;
224 log->wrap = DEFAULT_WRAPLEN;
225 log->in1 = DEFAULT_INDENT1;
226 log->in2 = DEFAULT_INDENT2;
229 int cmd_shortlog(int argc, const char **argv, const char *prefix)
231 static struct shortlog log;
232 static struct rev_info rev;
233 int nongit = !startup_info->have_repository;
235 static const struct option options[] = {
236 OPT_BOOLEAN('n', "numbered", &log.sort_by_number,
237 N_("sort output according to the number of commits per author")),
238 OPT_BOOLEAN('s', "summary", &log.summary,
239 N_("Suppress commit descriptions, only provides commit count")),
240 OPT_BOOLEAN('e', "email", &log.email,
241 N_("Show the email address of each author")),
242 { OPTION_CALLBACK, 'w', NULL, &log, N_("w[,i1[,i2]]"),
243 N_("Linewrap output"), PARSE_OPT_OPTARG, &parse_wrap_args },
244 OPT_END(),
247 struct parse_opt_ctx_t ctx;
249 git_config(git_default_config, NULL);
250 shortlog_init(&log);
251 init_revisions(&rev, prefix);
252 parse_options_start(&ctx, argc, argv, prefix, options,
253 PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0);
255 for (;;) {
256 switch (parse_options_step(&ctx, options, shortlog_usage)) {
257 case PARSE_OPT_HELP:
258 exit(129);
259 case PARSE_OPT_DONE:
260 goto parse_done;
262 parse_revision_opt(&rev, &ctx, options, shortlog_usage);
264 parse_done:
265 argc = parse_options_end(&ctx);
267 if (setup_revisions(argc, argv, &rev, NULL) != 1) {
268 error(_("unrecognized argument: %s"), argv[1]);
269 usage_with_options(shortlog_usage, options);
272 log.user_format = rev.commit_format == CMIT_FMT_USERFORMAT;
273 log.abbrev = rev.abbrev;
275 /* assume HEAD if from a tty */
276 if (!nongit && !rev.pending.nr && isatty(0))
277 add_head_to_pending(&rev);
278 if (rev.pending.nr == 0) {
279 if (isatty(0))
280 fprintf(stderr, _("(reading log message from standard input)\n"));
281 read_from_stdin(&log);
283 else
284 get_from_rev(&rev, &log);
286 shortlog_output(&log);
287 return 0;
290 static void add_wrapped_shortlog_msg(struct strbuf *sb, const char *s,
291 const struct shortlog *log)
293 int col = strbuf_add_wrapped_text(sb, s, log->in1, log->in2, log->wrap);
294 if (col != log->wrap)
295 strbuf_addch(sb, '\n');
298 void shortlog_output(struct shortlog *log)
300 int i, j;
301 struct strbuf sb = STRBUF_INIT;
303 if (log->sort_by_number)
304 qsort(log->list.items, log->list.nr, sizeof(struct string_list_item),
305 compare_by_number);
306 for (i = 0; i < log->list.nr; i++) {
307 struct string_list *onelines = log->list.items[i].util;
309 if (log->summary) {
310 printf("%6d\t%s\n", onelines->nr, log->list.items[i].string);
311 } else {
312 printf("%s (%d):\n", log->list.items[i].string, onelines->nr);
313 for (j = onelines->nr - 1; j >= 0; j--) {
314 const char *msg = onelines->items[j].string;
316 if (log->wrap_lines) {
317 strbuf_reset(&sb);
318 add_wrapped_shortlog_msg(&sb, msg, log);
319 fwrite(sb.buf, sb.len, 1, stdout);
321 else
322 printf(" %s\n", msg);
324 putchar('\n');
327 onelines->strdup_strings = 1;
328 string_list_clear(onelines, 0);
329 free(onelines);
330 log->list.items[i].util = NULL;
333 strbuf_release(&sb);
334 log->list.strdup_strings = 1;
335 string_list_clear(&log->list, 1);
336 clear_mailmap(&log->mailmap);