pull: pass --strategy along to to rebase
[git/dscho.git] / builtin-shortlog.c
blob0055a57aeb24392de0dcf337bb93f30fceccd88f
1 #include "builtin.h"
2 #include "cache.h"
3 #include "commit.h"
4 #include "diff.h"
5 #include "path-list.h"
6 #include "revision.h"
7 #include "utf8.h"
8 #include "mailmap.h"
10 static const char shortlog_usage[] =
11 "git-shortlog [-n] [-s] [-e] [<commit-id>... ]";
13 static char *common_repo_prefix;
14 static int email;
16 static int compare_by_number(const void *a1, const void *a2)
18 const struct path_list_item *i1 = a1, *i2 = a2;
19 const struct path_list *l1 = i1->util, *l2 = i2->util;
21 if (l1->nr < l2->nr)
22 return 1;
23 else if (l1->nr == l2->nr)
24 return 0;
25 else
26 return -1;
29 static struct path_list mailmap = {NULL, 0, 0, 0};
31 static void insert_one_record(struct path_list *list,
32 const char *author,
33 const char *oneline)
35 const char *dot3 = common_repo_prefix;
36 char *buffer, *p;
37 struct path_list_item *item;
38 struct path_list *onelines;
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(&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 (email) {
65 size_t room = sizeof(namebuf) - len - 1;
66 int maillen = eoemail - boemail + 1;
67 snprintf(namebuf + len, room, " %.*s", maillen, boemail);
70 buffer = xstrdup(namebuf);
71 item = path_list_insert(buffer, list);
72 if (item->util == NULL)
73 item->util = xcalloc(1, sizeof(struct path_list));
74 else
75 free(buffer);
77 eol = strchr(oneline, '\n');
78 if (!eol)
79 eol = oneline + strlen(oneline);
80 while (*oneline && isspace(*oneline) && *oneline != '\n')
81 oneline++;
82 if (!prefixcmp(oneline, "[PATCH")) {
83 char *eob = strchr(oneline, ']');
84 if (eob && (!eol || eob < eol))
85 oneline = eob + 1;
87 while (*oneline && isspace(*oneline) && *oneline != '\n')
88 oneline++;
89 len = eol - oneline;
90 while (len && isspace(oneline[len-1]))
91 len--;
92 buffer = xmemdupz(oneline, len);
94 if (dot3) {
95 int dot3len = strlen(dot3);
96 if (dot3len > 5) {
97 while ((p = strstr(buffer, dot3)) != NULL) {
98 int taillen = strlen(p) - dot3len;
99 memcpy(p, "/.../", 5);
100 memmove(p + 5, p + dot3len, taillen + 1);
105 onelines = item->util;
106 if (onelines->nr >= onelines->alloc) {
107 onelines->alloc = alloc_nr(onelines->nr);
108 onelines->items = xrealloc(onelines->items,
109 onelines->alloc
110 * sizeof(struct path_list_item));
113 onelines->items[onelines->nr].util = NULL;
114 onelines->items[onelines->nr++].path = buffer;
117 static void read_from_stdin(struct path_list *list)
119 char author[1024], oneline[1024];
121 while (fgets(author, sizeof(author), stdin) != NULL) {
122 if (!(author[0] == 'A' || author[0] == 'a') ||
123 prefixcmp(author + 1, "uthor: "))
124 continue;
125 while (fgets(oneline, sizeof(oneline), stdin) &&
126 oneline[0] != '\n')
127 ; /* discard headers */
128 while (fgets(oneline, sizeof(oneline), stdin) &&
129 oneline[0] == '\n')
130 ; /* discard blanks */
131 insert_one_record(list, author + 8, oneline);
135 static void get_from_rev(struct rev_info *rev, struct path_list *list)
137 struct commit *commit;
139 if (prepare_revision_walk(rev))
140 die("revision walk setup failed");
141 while ((commit = get_revision(rev)) != NULL) {
142 const char *author = NULL, *buffer;
144 buffer = commit->buffer;
145 while (*buffer && *buffer != '\n') {
146 const char *eol = strchr(buffer, '\n');
148 if (eol == NULL)
149 eol = buffer + strlen(buffer);
150 else
151 eol++;
153 if (!prefixcmp(buffer, "author "))
154 author = buffer + 7;
155 buffer = eol;
157 if (!author)
158 die("Missing author: %s",
159 sha1_to_hex(commit->object.sha1));
160 if (*buffer)
161 buffer++;
162 insert_one_record(list, author, !*buffer ? "<none>" : buffer);
166 static int parse_uint(char const **arg, int comma)
168 unsigned long ul;
169 int ret;
170 char *endp;
172 ul = strtoul(*arg, &endp, 10);
173 if (endp != *arg && *endp && *endp != comma)
174 return -1;
175 ret = (int) ul;
176 if (ret != ul)
177 return -1;
178 *arg = endp;
179 if (**arg)
180 (*arg)++;
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 void parse_wrap_args(const char *arg, int *in1, int *in2, int *wrap)
191 arg += 2; /* skip -w */
193 *wrap = parse_uint(&arg, ',');
194 if (*wrap < 0)
195 die(wrap_arg_usage);
196 *in1 = parse_uint(&arg, ',');
197 if (*in1 < 0)
198 die(wrap_arg_usage);
199 *in2 = parse_uint(&arg, '\0');
200 if (*in2 < 0)
201 die(wrap_arg_usage);
203 if (!*wrap)
204 *wrap = DEFAULT_WRAPLEN;
205 if (!*in1)
206 *in1 = DEFAULT_INDENT1;
207 if (!*in2)
208 *in2 = DEFAULT_INDENT2;
209 if (*wrap &&
210 ((*in1 && *wrap <= *in1) ||
211 (*in2 && *wrap <= *in2)))
212 die(wrap_arg_usage);
215 int cmd_shortlog(int argc, const char **argv, const char *prefix)
217 struct rev_info rev;
218 struct path_list list = { NULL, 0, 0, 1 };
219 int i, j, sort_by_number = 0, summary = 0;
220 int wrap_lines = 0;
221 int wrap = DEFAULT_WRAPLEN;
222 int in1 = DEFAULT_INDENT1;
223 int in2 = DEFAULT_INDENT2;
225 /* since -n is a shadowed rev argument, parse our args first */
226 while (argc > 1) {
227 if (!strcmp(argv[1], "-n") || !strcmp(argv[1], "--numbered"))
228 sort_by_number = 1;
229 else if (!strcmp(argv[1], "-s") ||
230 !strcmp(argv[1], "--summary"))
231 summary = 1;
232 else if (!strcmp(argv[1], "-e") ||
233 !strcmp(argv[1], "--email"))
234 email = 1;
235 else if (!prefixcmp(argv[1], "-w")) {
236 wrap_lines = 1;
237 parse_wrap_args(argv[1], &in1, &in2, &wrap);
239 else if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))
240 usage(shortlog_usage);
241 else
242 break;
243 argv++;
244 argc--;
246 init_revisions(&rev, prefix);
247 argc = setup_revisions(argc, argv, &rev, NULL);
248 if (argc > 1)
249 die ("unrecognized argument: %s", argv[1]);
251 read_mailmap(&mailmap, ".mailmap", &common_repo_prefix);
253 /* assume HEAD if from a tty */
254 if (!rev.pending.nr && isatty(0))
255 add_head_to_pending(&rev);
256 if (rev.pending.nr == 0) {
257 read_from_stdin(&list);
259 else
260 get_from_rev(&rev, &list);
262 if (sort_by_number)
263 qsort(list.items, list.nr, sizeof(struct path_list_item),
264 compare_by_number);
266 for (i = 0; i < list.nr; i++) {
267 struct path_list *onelines = list.items[i].util;
269 if (summary) {
270 printf("%6d\t%s\n", onelines->nr, list.items[i].path);
271 } else {
272 printf("%s (%d):\n", list.items[i].path, onelines->nr);
273 for (j = onelines->nr - 1; j >= 0; j--) {
274 const char *msg = onelines->items[j].path;
276 if (wrap_lines) {
277 int col = print_wrapped_text(msg, in1, in2, wrap);
278 if (col != wrap)
279 putchar('\n');
281 else
282 printf(" %s\n", msg);
284 putchar('\n');
287 onelines->strdup_paths = 1;
288 path_list_clear(onelines, 1);
289 free(onelines);
290 list.items[i].util = NULL;
293 list.strdup_paths = 1;
294 path_list_clear(&list, 1);
295 mailmap.strdup_paths = 1;
296 path_list_clear(&mailmap, 1);
298 return 0;