pack-objects: use fixup_pack_header_footer()'s validation mode
[git/platforms.git] / builtin-shortlog.c
blobd03f14fdad3d17dde06734d78ddb4aade6ed4f2b
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 struct string_list *onelines;
40 char namebuf[1024];
41 size_t len;
42 const char *eol;
43 const char *boemail, *eoemail;
45 boemail = strchr(author, '<');
46 if (!boemail)
47 return;
48 eoemail = strchr(boemail, '>');
49 if (!eoemail)
50 return;
51 if (!map_email(&log->mailmap, boemail+1, namebuf, sizeof(namebuf))) {
52 while (author < boemail && isspace(*author))
53 author++;
54 for (len = 0;
55 len < sizeof(namebuf) - 1 && author + len < boemail;
56 len++)
57 namebuf[len] = author[len];
58 while (0 < len && isspace(namebuf[len-1]))
59 len--;
60 namebuf[len] = '\0';
62 else
63 len = strlen(namebuf);
65 if (log->email) {
66 size_t room = sizeof(namebuf) - len - 1;
67 int maillen = eoemail - boemail + 1;
68 snprintf(namebuf + len, room, " %.*s", maillen, boemail);
71 buffer = xstrdup(namebuf);
72 item = string_list_insert(buffer, &log->list);
73 if (item->util == NULL)
74 item->util = xcalloc(1, sizeof(struct string_list));
75 else
76 free(buffer);
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 while (len && isspace(oneline[len-1]))
93 len--;
94 buffer = xmemdupz(oneline, len);
96 if (dot3) {
97 int dot3len = strlen(dot3);
98 if (dot3len > 5) {
99 while ((p = strstr(buffer, dot3)) != NULL) {
100 int taillen = strlen(p) - dot3len;
101 memcpy(p, "/.../", 5);
102 memmove(p + 5, p + dot3len, taillen + 1);
107 onelines = item->util;
108 if (onelines->nr >= onelines->alloc) {
109 onelines->alloc = alloc_nr(onelines->nr);
110 onelines->items = xrealloc(onelines->items,
111 onelines->alloc
112 * sizeof(struct string_list_item));
115 onelines->items[onelines->nr].util = NULL;
116 onelines->items[onelines->nr++].string = buffer;
119 static void read_from_stdin(struct shortlog *log)
121 char author[1024], oneline[1024];
123 while (fgets(author, sizeof(author), stdin) != NULL) {
124 if (!(author[0] == 'A' || author[0] == 'a') ||
125 prefixcmp(author + 1, "uthor: "))
126 continue;
127 while (fgets(oneline, sizeof(oneline), stdin) &&
128 oneline[0] != '\n')
129 ; /* discard headers */
130 while (fgets(oneline, sizeof(oneline), stdin) &&
131 oneline[0] == '\n')
132 ; /* discard blanks */
133 insert_one_record(log, author + 8, oneline);
137 void shortlog_add_commit(struct shortlog *log, struct commit *commit)
139 const char *author = NULL, *buffer;
141 buffer = commit->buffer;
142 while (*buffer && *buffer != '\n') {
143 const char *eol = strchr(buffer, '\n');
145 if (eol == NULL)
146 eol = buffer + strlen(buffer);
147 else
148 eol++;
150 if (!prefixcmp(buffer, "author "))
151 author = buffer + 7;
152 buffer = eol;
154 if (!author)
155 die("Missing author: %s",
156 sha1_to_hex(commit->object.sha1));
157 if (log->user_format) {
158 struct strbuf buf = STRBUF_INIT;
160 pretty_print_commit(CMIT_FMT_USERFORMAT, commit, &buf,
161 DEFAULT_ABBREV, "", "", DATE_NORMAL, 0);
162 insert_one_record(log, author, buf.buf);
163 strbuf_release(&buf);
164 return;
166 if (*buffer)
167 buffer++;
168 insert_one_record(log, author, !*buffer ? "<none>" : buffer);
171 static void get_from_rev(struct rev_info *rev, struct shortlog *log)
173 struct commit *commit;
175 if (prepare_revision_walk(rev))
176 die("revision walk setup failed");
177 while ((commit = get_revision(rev)) != NULL)
178 shortlog_add_commit(log, commit);
181 static int parse_uint(char const **arg, int comma, int defval)
183 unsigned long ul;
184 int ret;
185 char *endp;
187 ul = strtoul(*arg, &endp, 10);
188 if (*endp && *endp != comma)
189 return -1;
190 if (ul > INT_MAX)
191 return -1;
192 ret = *arg == endp ? defval : (int)ul;
193 *arg = *endp ? endp + 1 : endp;
194 return ret;
197 static const char wrap_arg_usage[] = "-w[<width>[,<indent1>[,<indent2>]]]";
198 #define DEFAULT_WRAPLEN 76
199 #define DEFAULT_INDENT1 6
200 #define DEFAULT_INDENT2 9
202 static int parse_wrap_args(const struct option *opt, const char *arg, int unset)
204 struct shortlog *log = opt->value;
206 log->wrap_lines = !unset;
207 if (unset)
208 return 0;
209 if (!arg) {
210 log->wrap = DEFAULT_WRAPLEN;
211 log->in1 = DEFAULT_INDENT1;
212 log->in2 = DEFAULT_INDENT2;
213 return 0;
216 log->wrap = parse_uint(&arg, ',', DEFAULT_WRAPLEN);
217 log->in1 = parse_uint(&arg, ',', DEFAULT_INDENT1);
218 log->in2 = parse_uint(&arg, '\0', DEFAULT_INDENT2);
219 if (log->wrap < 0 || log->in1 < 0 || log->in2 < 0)
220 return error(wrap_arg_usage);
221 if (log->wrap &&
222 ((log->in1 && log->wrap <= log->in1) ||
223 (log->in2 && log->wrap <= log->in2)))
224 return error(wrap_arg_usage);
225 return 0;
228 void shortlog_init(struct shortlog *log)
230 memset(log, 0, sizeof(*log));
232 read_mailmap(&log->mailmap, ".mailmap", &log->common_repo_prefix);
234 log->list.strdup_strings = 1;
235 log->wrap = DEFAULT_WRAPLEN;
236 log->in1 = DEFAULT_INDENT1;
237 log->in2 = DEFAULT_INDENT2;
240 int cmd_shortlog(int argc, const char **argv, const char *prefix)
242 static struct shortlog log;
243 static struct rev_info rev;
244 int nongit;
246 static const struct option options[] = {
247 OPT_BOOLEAN('n', "numbered", &log.sort_by_number,
248 "sort output according to the number of commits per author"),
249 OPT_BOOLEAN('s', "summary", &log.summary,
250 "Suppress commit descriptions, only provides commit count"),
251 OPT_BOOLEAN('e', "email", &log.email,
252 "Show the email address of each author"),
253 { OPTION_CALLBACK, 'w', NULL, &log, "w[,i1[,i2]]",
254 "Linewrap output", PARSE_OPT_OPTARG, &parse_wrap_args },
255 OPT_END(),
258 struct parse_opt_ctx_t ctx;
260 prefix = setup_git_directory_gently(&nongit);
261 shortlog_init(&log);
262 init_revisions(&rev, prefix);
263 parse_options_start(&ctx, argc, argv, PARSE_OPT_KEEP_DASHDASH |
264 PARSE_OPT_KEEP_ARGV0);
266 for (;;) {
267 switch (parse_options_step(&ctx, options, shortlog_usage)) {
268 case PARSE_OPT_HELP:
269 exit(129);
270 case PARSE_OPT_DONE:
271 goto parse_done;
273 parse_revision_opt(&rev, &ctx, options, shortlog_usage);
275 parse_done:
276 argc = parse_options_end(&ctx);
278 if (setup_revisions(argc, argv, &rev, NULL) != 1) {
279 error("unrecognized argument: %s", argv[1]);
280 usage_with_options(shortlog_usage, options);
283 log.user_format = rev.commit_format == CMIT_FMT_USERFORMAT;
285 /* assume HEAD if from a tty */
286 if (!nongit && !rev.pending.nr && isatty(0))
287 add_head_to_pending(&rev);
288 if (rev.pending.nr == 0) {
289 read_from_stdin(&log);
291 else
292 get_from_rev(&rev, &log);
294 shortlog_output(&log);
295 return 0;
298 void shortlog_output(struct shortlog *log)
300 int i, j;
301 if (log->sort_by_number)
302 qsort(log->list.items, log->list.nr, sizeof(struct string_list_item),
303 compare_by_number);
304 for (i = 0; i < log->list.nr; i++) {
305 struct string_list *onelines = log->list.items[i].util;
307 if (log->summary) {
308 printf("%6d\t%s\n", onelines->nr, log->list.items[i].string);
309 } else {
310 printf("%s (%d):\n", log->list.items[i].string, onelines->nr);
311 for (j = onelines->nr - 1; j >= 0; j--) {
312 const char *msg = onelines->items[j].string;
314 if (log->wrap_lines) {
315 int col = print_wrapped_text(msg, log->in1, log->in2, log->wrap);
316 if (col != log->wrap)
317 putchar('\n');
319 else
320 printf(" %s\n", msg);
322 putchar('\n');
325 onelines->strdup_strings = 1;
326 string_list_clear(onelines, 1);
327 free(onelines);
328 log->list.items[i].util = NULL;
331 log->list.strdup_strings = 1;
332 string_list_clear(&log->list, 1);
333 log->mailmap.strdup_strings = 1;
334 string_list_clear(&log->mailmap, 1);