bash: remove unnecessary checks for long options with argument
[git/spearce.git] / builtin-shortlog.c
blob90e76ae420389da34c43fd00a665b913d6d503e3
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 size_t len;
41 const char *eol;
42 const char *boemail, *eoemail;
44 boemail = strchr(author, '<');
45 if (!boemail)
46 return;
47 eoemail = strchr(boemail, '>');
48 if (!eoemail)
49 return;
50 if (!map_email(&log->mailmap, boemail+1, namebuf, sizeof(namebuf))) {
51 while (author < boemail && isspace(*author))
52 author++;
53 for (len = 0;
54 len < sizeof(namebuf) - 1 && author + len < boemail;
55 len++)
56 namebuf[len] = author[len];
57 while (0 < len && isspace(namebuf[len-1]))
58 len--;
59 namebuf[len] = '\0';
61 else
62 len = strlen(namebuf);
64 if (log->email) {
65 size_t room = sizeof(namebuf) - len - 1;
66 int maillen = eoemail - boemail + 1;
67 snprintf(namebuf + len, room, " %.*s", maillen, boemail);
70 item = string_list_insert(namebuf, &log->list);
71 if (item->util == NULL)
72 item->util = xcalloc(1, sizeof(struct string_list));
74 /* Skip any leading whitespace, including any blank lines. */
75 while (*oneline && isspace(*oneline))
76 oneline++;
77 eol = strchr(oneline, '\n');
78 if (!eol)
79 eol = oneline + strlen(oneline);
80 if (!prefixcmp(oneline, "[PATCH")) {
81 char *eob = strchr(oneline, ']');
82 if (eob && (!eol || eob < eol))
83 oneline = eob + 1;
85 while (*oneline && isspace(*oneline) && *oneline != '\n')
86 oneline++;
87 len = eol - oneline;
88 while (len && isspace(oneline[len-1]))
89 len--;
90 buffer = xmemdupz(oneline, len);
92 if (dot3) {
93 int dot3len = strlen(dot3);
94 if (dot3len > 5) {
95 while ((p = strstr(buffer, dot3)) != NULL) {
96 int taillen = strlen(p) - dot3len;
97 memcpy(p, "/.../", 5);
98 memmove(p + 5, p + dot3len, taillen + 1);
103 string_list_append(buffer, item->util);
106 static void read_from_stdin(struct shortlog *log)
108 char author[1024], oneline[1024];
110 while (fgets(author, sizeof(author), stdin) != NULL) {
111 if (!(author[0] == 'A' || author[0] == 'a') ||
112 prefixcmp(author + 1, "uthor: "))
113 continue;
114 while (fgets(oneline, sizeof(oneline), stdin) &&
115 oneline[0] != '\n')
116 ; /* discard headers */
117 while (fgets(oneline, sizeof(oneline), stdin) &&
118 oneline[0] == '\n')
119 ; /* discard blanks */
120 insert_one_record(log, author + 8, oneline);
124 void shortlog_add_commit(struct shortlog *log, struct commit *commit)
126 const char *author = NULL, *buffer;
128 buffer = commit->buffer;
129 while (*buffer && *buffer != '\n') {
130 const char *eol = strchr(buffer, '\n');
132 if (eol == NULL)
133 eol = buffer + strlen(buffer);
134 else
135 eol++;
137 if (!prefixcmp(buffer, "author "))
138 author = buffer + 7;
139 buffer = eol;
141 if (!author)
142 die("Missing author: %s",
143 sha1_to_hex(commit->object.sha1));
144 if (log->user_format) {
145 struct strbuf buf = STRBUF_INIT;
147 pretty_print_commit(CMIT_FMT_USERFORMAT, commit, &buf,
148 DEFAULT_ABBREV, "", "", DATE_NORMAL, 0);
149 insert_one_record(log, author, buf.buf);
150 strbuf_release(&buf);
151 return;
153 if (*buffer)
154 buffer++;
155 insert_one_record(log, author, !*buffer ? "<none>" : buffer);
158 static void get_from_rev(struct rev_info *rev, struct shortlog *log)
160 struct commit *commit;
162 if (prepare_revision_walk(rev))
163 die("revision walk setup failed");
164 while ((commit = get_revision(rev)) != NULL)
165 shortlog_add_commit(log, commit);
168 static int parse_uint(char const **arg, int comma, int defval)
170 unsigned long ul;
171 int ret;
172 char *endp;
174 ul = strtoul(*arg, &endp, 10);
175 if (*endp && *endp != comma)
176 return -1;
177 if (ul > INT_MAX)
178 return -1;
179 ret = *arg == endp ? defval : (int)ul;
180 *arg = *endp ? endp + 1 : endp;
181 return ret;
184 static const char wrap_arg_usage[] = "-w[<width>[,<indent1>[,<indent2>]]]";
185 #define DEFAULT_WRAPLEN 76
186 #define DEFAULT_INDENT1 6
187 #define DEFAULT_INDENT2 9
189 static int parse_wrap_args(const struct option *opt, const char *arg, int unset)
191 struct shortlog *log = opt->value;
193 log->wrap_lines = !unset;
194 if (unset)
195 return 0;
196 if (!arg) {
197 log->wrap = DEFAULT_WRAPLEN;
198 log->in1 = DEFAULT_INDENT1;
199 log->in2 = DEFAULT_INDENT2;
200 return 0;
203 log->wrap = parse_uint(&arg, ',', DEFAULT_WRAPLEN);
204 log->in1 = parse_uint(&arg, ',', DEFAULT_INDENT1);
205 log->in2 = parse_uint(&arg, '\0', DEFAULT_INDENT2);
206 if (log->wrap < 0 || log->in1 < 0 || log->in2 < 0)
207 return error(wrap_arg_usage);
208 if (log->wrap &&
209 ((log->in1 && log->wrap <= log->in1) ||
210 (log->in2 && log->wrap <= log->in2)))
211 return error(wrap_arg_usage);
212 return 0;
215 void shortlog_init(struct shortlog *log)
217 memset(log, 0, sizeof(*log));
219 read_mailmap(&log->mailmap, ".mailmap", &log->common_repo_prefix);
221 log->list.strdup_strings = 1;
222 log->wrap = DEFAULT_WRAPLEN;
223 log->in1 = DEFAULT_INDENT1;
224 log->in2 = DEFAULT_INDENT2;
227 int cmd_shortlog(int argc, const char **argv, const char *prefix)
229 static struct shortlog log;
230 static struct rev_info rev;
231 int nongit;
233 static const struct option options[] = {
234 OPT_BOOLEAN('n', "numbered", &log.sort_by_number,
235 "sort output according to the number of commits per author"),
236 OPT_BOOLEAN('s', "summary", &log.summary,
237 "Suppress commit descriptions, only provides commit count"),
238 OPT_BOOLEAN('e', "email", &log.email,
239 "Show the email address of each author"),
240 { OPTION_CALLBACK, 'w', NULL, &log, "w[,i1[,i2]]",
241 "Linewrap output", PARSE_OPT_OPTARG, &parse_wrap_args },
242 OPT_END(),
245 struct parse_opt_ctx_t ctx;
247 prefix = setup_git_directory_gently(&nongit);
248 shortlog_init(&log);
249 init_revisions(&rev, prefix);
250 parse_options_start(&ctx, argc, argv, PARSE_OPT_KEEP_DASHDASH |
251 PARSE_OPT_KEEP_ARGV0);
253 for (;;) {
254 switch (parse_options_step(&ctx, options, shortlog_usage)) {
255 case PARSE_OPT_HELP:
256 exit(129);
257 case PARSE_OPT_DONE:
258 goto parse_done;
260 parse_revision_opt(&rev, &ctx, options, shortlog_usage);
262 parse_done:
263 argc = parse_options_end(&ctx);
265 if (setup_revisions(argc, argv, &rev, NULL) != 1) {
266 error("unrecognized argument: %s", argv[1]);
267 usage_with_options(shortlog_usage, options);
270 log.user_format = rev.commit_format == CMIT_FMT_USERFORMAT;
272 /* assume HEAD if from a tty */
273 if (!nongit && !rev.pending.nr && isatty(0))
274 add_head_to_pending(&rev);
275 if (rev.pending.nr == 0) {
276 read_from_stdin(&log);
278 else
279 get_from_rev(&rev, &log);
281 shortlog_output(&log);
282 return 0;
285 void shortlog_output(struct shortlog *log)
287 int i, j;
288 if (log->sort_by_number)
289 qsort(log->list.items, log->list.nr, sizeof(struct string_list_item),
290 compare_by_number);
291 for (i = 0; i < log->list.nr; i++) {
292 struct string_list *onelines = log->list.items[i].util;
294 if (log->summary) {
295 printf("%6d\t%s\n", onelines->nr, log->list.items[i].string);
296 } else {
297 printf("%s (%d):\n", log->list.items[i].string, onelines->nr);
298 for (j = onelines->nr - 1; j >= 0; j--) {
299 const char *msg = onelines->items[j].string;
301 if (log->wrap_lines) {
302 int col = print_wrapped_text(msg, log->in1, log->in2, log->wrap);
303 if (col != log->wrap)
304 putchar('\n');
306 else
307 printf(" %s\n", msg);
309 putchar('\n');
312 onelines->strdup_strings = 1;
313 string_list_clear(onelines, 0);
314 free(onelines);
315 log->list.items[i].util = NULL;
318 log->list.strdup_strings = 1;
319 string_list_clear(&log->list, 1);
320 log->mailmap.strdup_strings = 1;
321 string_list_clear(&log->mailmap, 1);