Update callers of check_ref_format()
[git] / builtin-shortlog.c
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"
9
10 static const char shortlog_usage[] =
11 "git-shortlog [-n] [-s] [<commit-id>... ]";
12
13 static char *common_repo_prefix;
14 static int email;
15
16 static int compare_by_number(const void *a1, const void *a2)
17 {
18         const struct path_list_item *i1 = a1, *i2 = a2;
19         const struct path_list *l1 = i1->util, *l2 = i2->util;
20
21         if (l1->nr < l2->nr)
22                 return 1;
23         else if (l1->nr == l2->nr)
24                 return 0;
25         else
26                 return -1;
27 }
28
29 static struct path_list mailmap = {NULL, 0, 0, 0};
30
31 static void insert_one_record(struct path_list *list,
32                               const char *author,
33                               const char *oneline)
34 {
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;
43
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';
60         }
61         else
62                 len = strlen(namebuf);
63
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);
68         }
69
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);
76
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;
86         }
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);
93
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);
101                         }
102                 }
103         }
104
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));
111         }
112
113         onelines->items[onelines->nr].util = NULL;
114         onelines->items[onelines->nr++].path = buffer;
115 }
116
117 static void read_from_stdin(struct path_list *list)
118 {
119         char author[1024], oneline[1024];
120
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);
132         }
133 }
134
135 static void get_from_rev(struct rev_info *rev, struct path_list *list)
136 {
137         struct commit *commit;
138
139         prepare_revision_walk(rev);
140         while ((commit = get_revision(rev)) != NULL) {
141                 const char *author = NULL, *buffer;
142
143                 buffer = commit->buffer;
144                 while (*buffer && *buffer != '\n') {
145                         const char *eol = strchr(buffer, '\n');
146
147                         if (eol == NULL)
148                                 eol = buffer + strlen(buffer);
149                         else
150                                 eol++;
151
152                         if (!prefixcmp(buffer, "author "))
153                                 author = buffer + 7;
154                         buffer = eol;
155                 }
156                 if (!author)
157                         die("Missing author: %s",
158                             sha1_to_hex(commit->object.sha1));
159                 if (*buffer)
160                         buffer++;
161                 insert_one_record(list, author, !*buffer ? "<none>" : buffer);
162         }
163 }
164
165 static int parse_uint(char const **arg, int comma)
166 {
167         unsigned long ul;
168         int ret;
169         char *endp;
170
171         ul = strtoul(*arg, &endp, 10);
172         if (endp != *arg && *endp && *endp != comma)
173                 return -1;
174         ret = (int) ul;
175         if (ret != ul)
176                 return -1;
177         *arg = endp;
178         if (**arg)
179                 (*arg)++;
180         return ret;
181 }
182
183 static const char wrap_arg_usage[] = "-w[<width>[,<indent1>[,<indent2>]]]";
184 #define DEFAULT_WRAPLEN 76
185 #define DEFAULT_INDENT1 6
186 #define DEFAULT_INDENT2 9
187
188 static void parse_wrap_args(const char *arg, int *in1, int *in2, int *wrap)
189 {
190         arg += 2; /* skip -w */
191
192         *wrap = parse_uint(&arg, ',');
193         if (*wrap < 0)
194                 die(wrap_arg_usage);
195         *in1 = parse_uint(&arg, ',');
196         if (*in1 < 0)
197                 die(wrap_arg_usage);
198         *in2 = parse_uint(&arg, '\0');
199         if (*in2 < 0)
200                 die(wrap_arg_usage);
201
202         if (!*wrap)
203                 *wrap = DEFAULT_WRAPLEN;
204         if (!*in1)
205                 *in1 = DEFAULT_INDENT1;
206         if (!*in2)
207                 *in2 = DEFAULT_INDENT2;
208         if (*wrap &&
209             ((*in1 && *wrap <= *in1) ||
210              (*in2 && *wrap <= *in2)))
211                 die(wrap_arg_usage);
212 }
213
214 int cmd_shortlog(int argc, const char **argv, const char *prefix)
215 {
216         struct rev_info rev;
217         struct path_list list = { NULL, 0, 0, 1 };
218         int i, j, sort_by_number = 0, summary = 0;
219         int wrap_lines = 0;
220         int wrap = DEFAULT_WRAPLEN;
221         int in1 = DEFAULT_INDENT1;
222         int in2 = DEFAULT_INDENT2;
223
224         /* since -n is a shadowed rev argument, parse our args first */
225         while (argc > 1) {
226                 if (!strcmp(argv[1], "-n") || !strcmp(argv[1], "--numbered"))
227                         sort_by_number = 1;
228                 else if (!strcmp(argv[1], "-s") ||
229                                 !strcmp(argv[1], "--summary"))
230                         summary = 1;
231                 else if (!strcmp(argv[1], "-e") ||
232                          !strcmp(argv[1], "--email"))
233                         email = 1;
234                 else if (!prefixcmp(argv[1], "-w")) {
235                         wrap_lines = 1;
236                         parse_wrap_args(argv[1], &in1, &in2, &wrap);
237                 }
238                 else if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))
239                         usage(shortlog_usage);
240                 else
241                         break;
242                 argv++;
243                 argc--;
244         }
245         init_revisions(&rev, prefix);
246         argc = setup_revisions(argc, argv, &rev, NULL);
247         if (argc > 1)
248                 die ("unrecognized argument: %s", argv[1]);
249
250         read_mailmap(&mailmap, ".mailmap", &common_repo_prefix);
251
252         /* assume HEAD if from a tty */
253         if (!rev.pending.nr && isatty(0))
254                 add_head_to_pending(&rev);
255         if (rev.pending.nr == 0) {
256                 read_from_stdin(&list);
257         }
258         else
259                 get_from_rev(&rev, &list);
260
261         if (sort_by_number)
262                 qsort(list.items, list.nr, sizeof(struct path_list_item),
263                         compare_by_number);
264
265         for (i = 0; i < list.nr; i++) {
266                 struct path_list *onelines = list.items[i].util;
267
268                 if (summary) {
269                         printf("%6d\t%s\n", onelines->nr, list.items[i].path);
270                 } else {
271                         printf("%s (%d):\n", list.items[i].path, onelines->nr);
272                         for (j = onelines->nr - 1; j >= 0; j--) {
273                                 const char *msg = onelines->items[j].path;
274
275                                 if (wrap_lines) {
276                                         int col = print_wrapped_text(msg, in1, in2, wrap);
277                                         if (col != wrap)
278                                                 putchar('\n');
279                                 }
280                                 else
281                                         printf("      %s\n", msg);
282                         }
283                         putchar('\n');
284                 }
285
286                 onelines->strdup_paths = 1;
287                 path_list_clear(onelines, 1);
288                 free(onelines);
289                 list.items[i].util = NULL;
290         }
291
292         list.strdup_paths = 1;
293         path_list_clear(&list, 1);
294         mailmap.strdup_paths = 1;
295         path_list_clear(&mailmap, 1);
296
297         return 0;
298 }