Teach the "@{-1} syntax to "git branch"
[git/jrn.git] / builtin-shortlog.c
blob5f9f3f09b11b2e7f54a26bdbce7cb0e6974740d5
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_record(struct shortlog *log,
36 const char *author,
37 const char *oneline)
39 const char *dot3 = log->common_repo_prefix;
40 char *buffer, *p;
41 struct string_list_item *item;
42 char namebuf[1024];
43 size_t len;
44 const char *eol;
45 const char *boemail, *eoemail;
46 struct strbuf subject = STRBUF_INIT;
48 boemail = strchr(author, '<');
49 if (!boemail)
50 return;
51 eoemail = strchr(boemail, '>');
52 if (!eoemail)
53 return;
54 if (!map_email(&log->mailmap, boemail+1, namebuf, sizeof(namebuf))) {
55 while (author < boemail && isspace(*author))
56 author++;
57 for (len = 0;
58 len < sizeof(namebuf) - 1 && author + len < boemail;
59 len++)
60 namebuf[len] = author[len];
61 while (0 < len && isspace(namebuf[len-1]))
62 len--;
63 namebuf[len] = '\0';
65 else
66 len = strlen(namebuf);
68 if (log->email) {
69 size_t room = sizeof(namebuf) - len - 1;
70 int maillen = eoemail - boemail + 1;
71 snprintf(namebuf + len, room, " %.*s", maillen, boemail);
74 item = string_list_insert(namebuf, &log->list);
75 if (item->util == NULL)
76 item->util = xcalloc(1, sizeof(struct string_list));
78 /* Skip any leading whitespace, including any blank lines. */
79 while (*oneline && isspace(*oneline))
80 oneline++;
81 eol = strchr(oneline, '\n');
82 if (!eol)
83 eol = oneline + strlen(oneline);
84 if (!prefixcmp(oneline, "[PATCH")) {
85 char *eob = strchr(oneline, ']');
86 if (eob && (!eol || eob < eol))
87 oneline = eob + 1;
89 while (*oneline && isspace(*oneline) && *oneline != '\n')
90 oneline++;
91 len = eol - oneline;
92 format_subject(&subject, oneline, " ");
93 buffer = strbuf_detach(&subject, NULL);
95 if (dot3) {
96 int dot3len = strlen(dot3);
97 if (dot3len > 5) {
98 while ((p = strstr(buffer, dot3)) != NULL) {
99 int taillen = strlen(p) - dot3len;
100 memcpy(p, "/.../", 5);
101 memmove(p + 5, p + dot3len, taillen + 1);
106 string_list_append(buffer, item->util);
109 static void read_from_stdin(struct shortlog *log)
111 char author[1024], oneline[1024];
113 while (fgets(author, sizeof(author), stdin) != NULL) {
114 if (!(author[0] == 'A' || author[0] == 'a') ||
115 prefixcmp(author + 1, "uthor: "))
116 continue;
117 while (fgets(oneline, sizeof(oneline), stdin) &&
118 oneline[0] != '\n')
119 ; /* discard headers */
120 while (fgets(oneline, sizeof(oneline), stdin) &&
121 oneline[0] == '\n')
122 ; /* discard blanks */
123 insert_one_record(log, author + 8, oneline);
127 void shortlog_add_commit(struct shortlog *log, struct commit *commit)
129 const char *author = NULL, *buffer;
131 buffer = commit->buffer;
132 while (*buffer && *buffer != '\n') {
133 const char *eol = strchr(buffer, '\n');
135 if (eol == NULL)
136 eol = buffer + strlen(buffer);
137 else
138 eol++;
140 if (!prefixcmp(buffer, "author "))
141 author = buffer + 7;
142 buffer = eol;
144 if (!author)
145 die("Missing author: %s",
146 sha1_to_hex(commit->object.sha1));
147 if (log->user_format) {
148 struct strbuf buf = STRBUF_INIT;
150 pretty_print_commit(CMIT_FMT_USERFORMAT, commit, &buf,
151 DEFAULT_ABBREV, "", "", DATE_NORMAL, 0);
152 insert_one_record(log, author, buf.buf);
153 strbuf_release(&buf);
154 return;
156 if (*buffer)
157 buffer++;
158 insert_one_record(log, author, !*buffer ? "<none>" : buffer);
161 static void get_from_rev(struct rev_info *rev, struct shortlog *log)
163 struct commit *commit;
165 if (prepare_revision_walk(rev))
166 die("revision walk setup failed");
167 while ((commit = get_revision(rev)) != NULL)
168 shortlog_add_commit(log, commit);
171 static int parse_uint(char const **arg, int comma, int defval)
173 unsigned long ul;
174 int ret;
175 char *endp;
177 ul = strtoul(*arg, &endp, 10);
178 if (*endp && *endp != comma)
179 return -1;
180 if (ul > INT_MAX)
181 return -1;
182 ret = *arg == endp ? defval : (int)ul;
183 *arg = *endp ? endp + 1 : endp;
184 return ret;
187 static const char wrap_arg_usage[] = "-w[<width>[,<indent1>[,<indent2>]]]";
188 #define DEFAULT_WRAPLEN 76
189 #define DEFAULT_INDENT1 6
190 #define DEFAULT_INDENT2 9
192 static int parse_wrap_args(const struct option *opt, const char *arg, int unset)
194 struct shortlog *log = opt->value;
196 log->wrap_lines = !unset;
197 if (unset)
198 return 0;
199 if (!arg) {
200 log->wrap = DEFAULT_WRAPLEN;
201 log->in1 = DEFAULT_INDENT1;
202 log->in2 = DEFAULT_INDENT2;
203 return 0;
206 log->wrap = parse_uint(&arg, ',', DEFAULT_WRAPLEN);
207 log->in1 = parse_uint(&arg, ',', DEFAULT_INDENT1);
208 log->in2 = parse_uint(&arg, '\0', DEFAULT_INDENT2);
209 if (log->wrap < 0 || log->in1 < 0 || log->in2 < 0)
210 return error(wrap_arg_usage);
211 if (log->wrap &&
212 ((log->in1 && log->wrap <= log->in1) ||
213 (log->in2 && log->wrap <= log->in2)))
214 return error(wrap_arg_usage);
215 return 0;
218 void shortlog_init(struct shortlog *log)
220 memset(log, 0, sizeof(*log));
222 read_mailmap(&log->mailmap, ".mailmap", &log->common_repo_prefix);
224 log->list.strdup_strings = 1;
225 log->wrap = DEFAULT_WRAPLEN;
226 log->in1 = DEFAULT_INDENT1;
227 log->in2 = DEFAULT_INDENT2;
230 int cmd_shortlog(int argc, const char **argv, const char *prefix)
232 static struct shortlog log;
233 static struct rev_info rev;
234 int nongit;
236 static const struct option options[] = {
237 OPT_BOOLEAN('n', "numbered", &log.sort_by_number,
238 "sort output according to the number of commits per author"),
239 OPT_BOOLEAN('s', "summary", &log.summary,
240 "Suppress commit descriptions, only provides commit count"),
241 OPT_BOOLEAN('e', "email", &log.email,
242 "Show the email address of each author"),
243 { OPTION_CALLBACK, 'w', NULL, &log, "w[,i1[,i2]]",
244 "Linewrap output", PARSE_OPT_OPTARG, &parse_wrap_args },
245 OPT_END(),
248 struct parse_opt_ctx_t ctx;
250 prefix = setup_git_directory_gently(&nongit);
251 shortlog_init(&log);
252 init_revisions(&rev, prefix);
253 parse_options_start(&ctx, argc, argv, PARSE_OPT_KEEP_DASHDASH |
254 PARSE_OPT_KEEP_ARGV0);
256 for (;;) {
257 switch (parse_options_step(&ctx, options, shortlog_usage)) {
258 case PARSE_OPT_HELP:
259 exit(129);
260 case PARSE_OPT_DONE:
261 goto parse_done;
263 parse_revision_opt(&rev, &ctx, options, shortlog_usage);
265 parse_done:
266 argc = parse_options_end(&ctx);
268 if (setup_revisions(argc, argv, &rev, NULL) != 1) {
269 error("unrecognized argument: %s", argv[1]);
270 usage_with_options(shortlog_usage, options);
273 log.user_format = rev.commit_format == CMIT_FMT_USERFORMAT;
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 read_from_stdin(&log);
281 else
282 get_from_rev(&rev, &log);
284 shortlog_output(&log);
285 return 0;
288 void shortlog_output(struct shortlog *log)
290 int i, j;
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 int col = print_wrapped_text(msg, log->in1, log->in2, log->wrap);
306 if (col != log->wrap)
307 putchar('\n');
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 log->list.strdup_strings = 1;
322 string_list_clear(&log->list, 1);
323 log->mailmap.strdup_strings = 1;
324 string_list_clear(&log->mailmap, 1);