Fix tests under GETTEXT_POISON on parseopt
[git.git] / builtin / shortlog.c
blob37f3193a3366725a6b11bdb55db7b6ffc6e9b3ce
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 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 const char *boemail, *eoemail;
44 struct strbuf subject = STRBUF_INIT;
46 boemail = strchr(author, '<');
47 if (!boemail)
48 return;
49 eoemail = strchr(boemail, '>');
50 if (!eoemail)
51 return;
53 /* copy author name to namebuf, to support matching on both name and email */
54 memcpy(namebuf, author, boemail - author);
55 len = boemail - author;
56 while (len > 0 && isspace(namebuf[len-1]))
57 len--;
58 namebuf[len] = 0;
60 /* copy email name to emailbuf, to allow email replacement as well */
61 memcpy(emailbuf, boemail+1, eoemail - boemail);
62 emailbuf[eoemail - boemail - 1] = 0;
64 if (!map_user(&log->mailmap, emailbuf, sizeof(emailbuf), namebuf, sizeof(namebuf))) {
65 while (author < boemail && isspace(*author))
66 author++;
67 for (len = 0;
68 len < sizeof(namebuf) - 1 && author + len < boemail;
69 len++)
70 namebuf[len] = author[len];
71 while (0 < len && isspace(namebuf[len-1]))
72 len--;
73 namebuf[len] = '\0';
75 else
76 len = strlen(namebuf);
78 if (log->email) {
79 size_t room = sizeof(namebuf) - len - 1;
80 int maillen = strlen(emailbuf);
81 snprintf(namebuf + len, room, " <%.*s>", maillen, emailbuf);
84 item = string_list_insert(&log->list, namebuf);
85 if (item->util == NULL)
86 item->util = xcalloc(1, sizeof(struct string_list));
88 /* Skip any leading whitespace, including any blank lines. */
89 while (*oneline && isspace(*oneline))
90 oneline++;
91 eol = strchr(oneline, '\n');
92 if (!eol)
93 eol = oneline + strlen(oneline);
94 if (!prefixcmp(oneline, "[PATCH")) {
95 char *eob = strchr(oneline, ']');
96 if (eob && (!eol || eob < eol))
97 oneline = eob + 1;
99 while (*oneline && isspace(*oneline) && *oneline != '\n')
100 oneline++;
101 format_subject(&subject, oneline, " ");
102 buffer = strbuf_detach(&subject, NULL);
104 if (dot3) {
105 int dot3len = strlen(dot3);
106 if (dot3len > 5) {
107 while ((p = strstr(buffer, dot3)) != NULL) {
108 int taillen = strlen(p) - dot3len;
109 memcpy(p, "/.../", 5);
110 memmove(p + 5, p + dot3len, taillen + 1);
115 string_list_append(item->util, buffer);
118 static void read_from_stdin(struct shortlog *log)
120 char author[1024], oneline[1024];
122 while (fgets(author, sizeof(author), stdin) != NULL) {
123 if (!(author[0] == 'A' || author[0] == 'a') ||
124 prefixcmp(author + 1, "uthor: "))
125 continue;
126 while (fgets(oneline, sizeof(oneline), stdin) &&
127 oneline[0] != '\n')
128 ; /* discard headers */
129 while (fgets(oneline, sizeof(oneline), stdin) &&
130 oneline[0] == '\n')
131 ; /* discard blanks */
132 insert_one_record(log, author + 8, oneline);
136 void shortlog_add_commit(struct shortlog *log, struct commit *commit)
138 const char *author = NULL, *buffer;
139 struct strbuf buf = STRBUF_INIT;
140 struct strbuf ufbuf = STRBUF_INIT;
142 pp_commit_easy(CMIT_FMT_RAW, commit, &buf);
143 buffer = buf.buf;
144 while (*buffer && *buffer != '\n') {
145 const char *eol = strchr(buffer, '\n');
147 if (eol == NULL)
148 eol = buffer + strlen(buffer);
149 else
150 eol++;
152 if (!prefixcmp(buffer, "author "))
153 author = buffer + 7;
154 buffer = eol;
156 if (!author)
157 die(_("Missing author: %s"),
158 sha1_to_hex(commit->object.sha1));
159 if (log->user_format) {
160 struct pretty_print_context ctx = {0};
161 ctx.fmt = CMIT_FMT_USERFORMAT;
162 ctx.abbrev = log->abbrev;
163 ctx.subject = "";
164 ctx.after_subject = "";
165 ctx.date_mode = DATE_NORMAL;
166 pretty_print_commit(&ctx, commit, &ufbuf);
167 buffer = ufbuf.buf;
168 } else if (*buffer) {
169 buffer++;
171 insert_one_record(log, author, !*buffer ? "<none>" : buffer);
172 strbuf_release(&ufbuf);
173 strbuf_release(&buf);
176 static void get_from_rev(struct rev_info *rev, struct shortlog *log)
178 struct commit *commit;
180 if (prepare_revision_walk(rev))
181 die(_("revision walk setup failed"));
182 while ((commit = get_revision(rev)) != NULL)
183 shortlog_add_commit(log, commit);
186 static int parse_uint(char const **arg, int comma, int defval)
188 unsigned long ul;
189 int ret;
190 char *endp;
192 ul = strtoul(*arg, &endp, 10);
193 if (*endp && *endp != comma)
194 return -1;
195 if (ul > INT_MAX)
196 return -1;
197 ret = *arg == endp ? defval : (int)ul;
198 *arg = *endp ? endp + 1 : endp;
199 return ret;
202 static const char wrap_arg_usage[] = "-w[<width>[,<indent1>[,<indent2>]]]";
203 #define DEFAULT_WRAPLEN 76
204 #define DEFAULT_INDENT1 6
205 #define DEFAULT_INDENT2 9
207 static int parse_wrap_args(const struct option *opt, const char *arg, int unset)
209 struct shortlog *log = opt->value;
211 log->wrap_lines = !unset;
212 if (unset)
213 return 0;
214 if (!arg) {
215 log->wrap = DEFAULT_WRAPLEN;
216 log->in1 = DEFAULT_INDENT1;
217 log->in2 = DEFAULT_INDENT2;
218 return 0;
221 log->wrap = parse_uint(&arg, ',', DEFAULT_WRAPLEN);
222 log->in1 = parse_uint(&arg, ',', DEFAULT_INDENT1);
223 log->in2 = parse_uint(&arg, '\0', DEFAULT_INDENT2);
224 if (log->wrap < 0 || log->in1 < 0 || log->in2 < 0)
225 return error(wrap_arg_usage);
226 if (log->wrap &&
227 ((log->in1 && log->wrap <= log->in1) ||
228 (log->in2 && log->wrap <= log->in2)))
229 return error(wrap_arg_usage);
230 return 0;
233 void shortlog_init(struct shortlog *log)
235 memset(log, 0, sizeof(*log));
237 read_mailmap(&log->mailmap, &log->common_repo_prefix);
239 log->list.strdup_strings = 1;
240 log->wrap = DEFAULT_WRAPLEN;
241 log->in1 = DEFAULT_INDENT1;
242 log->in2 = DEFAULT_INDENT2;
245 int cmd_shortlog(int argc, const char **argv, const char *prefix)
247 static struct shortlog log;
248 static struct rev_info rev;
249 int nongit = !startup_info->have_repository;
251 static const struct option options[] = {
252 OPT_BOOLEAN('n', "numbered", &log.sort_by_number,
253 "sort output according to the number of commits per author"),
254 OPT_BOOLEAN('s', "summary", &log.summary,
255 "Suppress commit descriptions, only provides commit count"),
256 OPT_BOOLEAN('e', "email", &log.email,
257 "Show the email address of each author"),
258 { OPTION_CALLBACK, 'w', NULL, &log, "w[,i1[,i2]]",
259 "Linewrap output", PARSE_OPT_OPTARG, &parse_wrap_args },
260 OPT_END(),
263 struct parse_opt_ctx_t ctx;
265 git_config(git_default_config, NULL);
266 shortlog_init(&log);
267 init_revisions(&rev, prefix);
268 parse_options_start(&ctx, argc, argv, prefix, options,
269 PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0);
271 for (;;) {
272 switch (parse_options_step(&ctx, options, shortlog_usage)) {
273 case PARSE_OPT_HELP:
274 exit(129);
275 case PARSE_OPT_DONE:
276 goto parse_done;
278 parse_revision_opt(&rev, &ctx, options, shortlog_usage);
280 parse_done:
281 argc = parse_options_end(&ctx);
283 if (setup_revisions(argc, argv, &rev, NULL) != 1) {
284 error(_("unrecognized argument: %s"), argv[1]);
285 usage_with_options(shortlog_usage, options);
288 log.user_format = rev.commit_format == CMIT_FMT_USERFORMAT;
289 log.abbrev = rev.abbrev;
291 /* assume HEAD if from a tty */
292 if (!nongit && !rev.pending.nr && isatty(0))
293 add_head_to_pending(&rev);
294 if (rev.pending.nr == 0) {
295 if (isatty(0))
296 fprintf(stderr, _("(reading log message from standard input)\n"));
297 read_from_stdin(&log);
299 else
300 get_from_rev(&rev, &log);
302 shortlog_output(&log);
303 return 0;
306 static void add_wrapped_shortlog_msg(struct strbuf *sb, const char *s,
307 const struct shortlog *log)
309 int col = strbuf_add_wrapped_text(sb, s, log->in1, log->in2, log->wrap);
310 if (col != log->wrap)
311 strbuf_addch(sb, '\n');
314 void shortlog_output(struct shortlog *log)
316 int i, j;
317 struct strbuf sb = STRBUF_INIT;
319 if (log->sort_by_number)
320 qsort(log->list.items, log->list.nr, sizeof(struct string_list_item),
321 compare_by_number);
322 for (i = 0; i < log->list.nr; i++) {
323 struct string_list *onelines = log->list.items[i].util;
325 if (log->summary) {
326 printf("%6d\t%s\n", onelines->nr, log->list.items[i].string);
327 } else {
328 printf("%s (%d):\n", log->list.items[i].string, onelines->nr);
329 for (j = onelines->nr - 1; j >= 0; j--) {
330 const char *msg = onelines->items[j].string;
332 if (log->wrap_lines) {
333 strbuf_reset(&sb);
334 add_wrapped_shortlog_msg(&sb, msg, log);
335 fwrite(sb.buf, sb.len, 1, stdout);
337 else
338 printf(" %s\n", msg);
340 putchar('\n');
343 onelines->strdup_strings = 1;
344 string_list_clear(onelines, 0);
345 free(onelines);
346 log->list.items[i].util = NULL;
349 strbuf_release(&sb);
350 log->list.strdup_strings = 1;
351 string_list_clear(&log->list, 1);
352 clear_mailmap(&log->mailmap);