Remove post-upload-hook
[git/dscho.git] / builtin-shortlog.c
blobb98edc3ba6c155b632d5e140d0b5a3e8a2d4eb9b
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 char emailbuf[1024];
44 size_t len;
45 const char *eol;
46 const char *boemail, *eoemail;
47 struct strbuf subject = STRBUF_INIT;
49 boemail = strchr(author, '<');
50 if (!boemail)
51 return;
52 eoemail = strchr(boemail, '>');
53 if (!eoemail)
54 return;
56 /* copy author name to namebuf, to support matching on both name and email */
57 memcpy(namebuf, author, boemail - author);
58 len = boemail - author;
59 while (len > 0 && isspace(namebuf[len-1]))
60 len--;
61 namebuf[len] = 0;
63 /* copy email name to emailbuf, to allow email replacement as well */
64 memcpy(emailbuf, boemail+1, eoemail - boemail);
65 emailbuf[eoemail - boemail - 1] = 0;
67 if (!map_user(&log->mailmap, emailbuf, sizeof(emailbuf), namebuf, sizeof(namebuf))) {
68 while (author < boemail && isspace(*author))
69 author++;
70 for (len = 0;
71 len < sizeof(namebuf) - 1 && author + len < boemail;
72 len++)
73 namebuf[len] = author[len];
74 while (0 < len && isspace(namebuf[len-1]))
75 len--;
76 namebuf[len] = '\0';
78 else
79 len = strlen(namebuf);
81 if (log->email) {
82 size_t room = sizeof(namebuf) - len - 1;
83 int maillen = strlen(emailbuf);
84 snprintf(namebuf + len, room, " <%.*s>", maillen, emailbuf);
87 item = string_list_insert(namebuf, &log->list);
88 if (item->util == NULL)
89 item->util = xcalloc(1, sizeof(struct string_list));
91 /* Skip any leading whitespace, including any blank lines. */
92 while (*oneline && isspace(*oneline))
93 oneline++;
94 eol = strchr(oneline, '\n');
95 if (!eol)
96 eol = oneline + strlen(oneline);
97 if (!prefixcmp(oneline, "[PATCH")) {
98 char *eob = strchr(oneline, ']');
99 if (eob && (!eol || eob < eol))
100 oneline = eob + 1;
102 while (*oneline && isspace(*oneline) && *oneline != '\n')
103 oneline++;
104 format_subject(&subject, oneline, " ");
105 buffer = strbuf_detach(&subject, NULL);
107 if (dot3) {
108 int dot3len = strlen(dot3);
109 if (dot3len > 5) {
110 while ((p = strstr(buffer, dot3)) != NULL) {
111 int taillen = strlen(p) - dot3len;
112 memcpy(p, "/.../", 5);
113 memmove(p + 5, p + dot3len, taillen + 1);
118 string_list_append(buffer, item->util);
121 static void read_from_stdin(struct shortlog *log)
123 char author[1024], oneline[1024];
125 while (fgets(author, sizeof(author), stdin) != NULL) {
126 if (!(author[0] == 'A' || author[0] == 'a') ||
127 prefixcmp(author + 1, "uthor: "))
128 continue;
129 while (fgets(oneline, sizeof(oneline), stdin) &&
130 oneline[0] != '\n')
131 ; /* discard headers */
132 while (fgets(oneline, sizeof(oneline), stdin) &&
133 oneline[0] == '\n')
134 ; /* discard blanks */
135 insert_one_record(log, author + 8, oneline);
139 void shortlog_add_commit(struct shortlog *log, struct commit *commit)
141 const char *author = NULL, *buffer;
142 struct strbuf buf = STRBUF_INIT;
143 struct strbuf ufbuf = STRBUF_INIT;
145 pretty_print_commit(CMIT_FMT_RAW, commit, &buf,
146 0, NULL, NULL, DATE_NORMAL, 0);
147 buffer = buf.buf;
148 while (*buffer && *buffer != '\n') {
149 const char *eol = strchr(buffer, '\n');
151 if (eol == NULL)
152 eol = buffer + strlen(buffer);
153 else
154 eol++;
156 if (!prefixcmp(buffer, "author "))
157 author = buffer + 7;
158 buffer = eol;
160 if (!author)
161 die("Missing author: %s",
162 sha1_to_hex(commit->object.sha1));
163 if (log->user_format) {
164 pretty_print_commit(CMIT_FMT_USERFORMAT, commit, &ufbuf,
165 DEFAULT_ABBREV, "", "", DATE_NORMAL, 0);
166 buffer = ufbuf.buf;
167 } else if (*buffer) {
168 buffer++;
170 insert_one_record(log, author, !*buffer ? "<none>" : buffer);
171 strbuf_release(&ufbuf);
172 strbuf_release(&buf);
175 static void get_from_rev(struct rev_info *rev, struct shortlog *log)
177 struct commit *commit;
179 if (prepare_revision_walk(rev))
180 die("revision walk setup failed");
181 while ((commit = get_revision(rev)) != NULL)
182 shortlog_add_commit(log, commit);
185 static int parse_uint(char const **arg, int comma, int defval)
187 unsigned long ul;
188 int ret;
189 char *endp;
191 ul = strtoul(*arg, &endp, 10);
192 if (*endp && *endp != comma)
193 return -1;
194 if (ul > INT_MAX)
195 return -1;
196 ret = *arg == endp ? defval : (int)ul;
197 *arg = *endp ? endp + 1 : endp;
198 return ret;
201 static const char wrap_arg_usage[] = "-w[<width>[,<indent1>[,<indent2>]]]";
202 #define DEFAULT_WRAPLEN 76
203 #define DEFAULT_INDENT1 6
204 #define DEFAULT_INDENT2 9
206 static int parse_wrap_args(const struct option *opt, const char *arg, int unset)
208 struct shortlog *log = opt->value;
210 log->wrap_lines = !unset;
211 if (unset)
212 return 0;
213 if (!arg) {
214 log->wrap = DEFAULT_WRAPLEN;
215 log->in1 = DEFAULT_INDENT1;
216 log->in2 = DEFAULT_INDENT2;
217 return 0;
220 log->wrap = parse_uint(&arg, ',', DEFAULT_WRAPLEN);
221 log->in1 = parse_uint(&arg, ',', DEFAULT_INDENT1);
222 log->in2 = parse_uint(&arg, '\0', DEFAULT_INDENT2);
223 if (log->wrap < 0 || log->in1 < 0 || log->in2 < 0)
224 return error(wrap_arg_usage);
225 if (log->wrap &&
226 ((log->in1 && log->wrap <= log->in1) ||
227 (log->in2 && log->wrap <= log->in2)))
228 return error(wrap_arg_usage);
229 return 0;
232 void shortlog_init(struct shortlog *log)
234 memset(log, 0, sizeof(*log));
236 read_mailmap(&log->mailmap, &log->common_repo_prefix);
238 log->list.strdup_strings = 1;
239 log->wrap = DEFAULT_WRAPLEN;
240 log->in1 = DEFAULT_INDENT1;
241 log->in2 = DEFAULT_INDENT2;
244 int cmd_shortlog(int argc, const char **argv, const char *prefix)
246 static struct shortlog log;
247 static struct rev_info rev;
248 int nongit;
250 static const struct option options[] = {
251 OPT_BOOLEAN('n', "numbered", &log.sort_by_number,
252 "sort output according to the number of commits per author"),
253 OPT_BOOLEAN('s', "summary", &log.summary,
254 "Suppress commit descriptions, only provides commit count"),
255 OPT_BOOLEAN('e', "email", &log.email,
256 "Show the email address of each author"),
257 { OPTION_CALLBACK, 'w', NULL, &log, "w[,i1[,i2]]",
258 "Linewrap output", PARSE_OPT_OPTARG, &parse_wrap_args },
259 OPT_END(),
262 struct parse_opt_ctx_t ctx;
264 prefix = setup_git_directory_gently(&nongit);
265 git_config(git_default_config, NULL);
266 shortlog_init(&log);
267 init_revisions(&rev, prefix);
268 parse_options_start(&ctx, argc, argv, prefix, PARSE_OPT_KEEP_DASHDASH |
269 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;
290 /* assume HEAD if from a tty */
291 if (!nongit && !rev.pending.nr && isatty(0))
292 add_head_to_pending(&rev);
293 if (rev.pending.nr == 0) {
294 read_from_stdin(&log);
296 else
297 get_from_rev(&rev, &log);
299 shortlog_output(&log);
300 return 0;
303 void shortlog_output(struct shortlog *log)
305 int i, j;
306 if (log->sort_by_number)
307 qsort(log->list.items, log->list.nr, sizeof(struct string_list_item),
308 compare_by_number);
309 for (i = 0; i < log->list.nr; i++) {
310 struct string_list *onelines = log->list.items[i].util;
312 if (log->summary) {
313 printf("%6d\t%s\n", onelines->nr, log->list.items[i].string);
314 } else {
315 printf("%s (%d):\n", log->list.items[i].string, onelines->nr);
316 for (j = onelines->nr - 1; j >= 0; j--) {
317 const char *msg = onelines->items[j].string;
319 if (log->wrap_lines) {
320 int col = print_wrapped_text(msg, log->in1, log->in2, log->wrap);
321 if (col != log->wrap)
322 putchar('\n');
324 else
325 printf(" %s\n", msg);
327 putchar('\n');
330 onelines->strdup_strings = 1;
331 string_list_clear(onelines, 0);
332 free(onelines);
333 log->list.items[i].util = NULL;
336 log->list.strdup_strings = 1;
337 string_list_clear(&log->list, 1);
338 clear_mailmap(&log->mailmap);