shortlog: fix "-n"
[git.git] / builtin-shortlog.c
blob1456e1a1914c0a5a0f0009ef391066d1d67d5280
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 <string.h>
9 static const char shortlog_usage[] =
10 "git-shortlog [-n] [-s] [<commit-id>... ]\n";
12 static int compare_by_number(const void *a1, const void *a2)
14 const struct path_list_item *i1 = a1, *i2 = a2;
15 const struct path_list *l1 = i1->util, *l2 = i2->util;
17 if (l1->nr < l2->nr)
18 return 1;
19 else if (l1->nr == l2->nr)
20 return 0;
21 else
22 return -1;
25 static struct path_list mailmap = {NULL, 0, 0, 0};
27 static int read_mailmap(const char *filename)
29 char buffer[1024];
30 FILE *f = fopen(filename, "r");
32 if (f == NULL)
33 return 1;
34 while (fgets(buffer, sizeof(buffer), f) != NULL) {
35 char *end_of_name, *left_bracket, *right_bracket;
36 char *name, *email;
37 int i;
38 if (buffer[0] == '#')
39 continue;
40 if ((left_bracket = strchr(buffer, '<')) == NULL)
41 continue;
42 if ((right_bracket = strchr(left_bracket + 1, '>')) == NULL)
43 continue;
44 if (right_bracket == left_bracket + 1)
45 continue;
46 for (end_of_name = left_bracket; end_of_name != buffer
47 && isspace(end_of_name[-1]); end_of_name--)
48 /* keep on looking */
49 if (end_of_name == buffer)
50 continue;
51 name = xmalloc(end_of_name - buffer + 1);
52 strlcpy(name, buffer, end_of_name - buffer + 1);
53 email = xmalloc(right_bracket - left_bracket);
54 for (i = 0; i < right_bracket - left_bracket - 1; i++)
55 email[i] = tolower(left_bracket[i + 1]);
56 email[right_bracket - left_bracket - 1] = '\0';
57 path_list_insert(email, &mailmap)->util = name;
59 fclose(f);
60 return 0;
63 static int map_email(char *email, char *name, int maxlen)
65 char *p;
66 struct path_list_item *item;
68 /* autocomplete common developers */
69 p = strchr(email, '>');
70 if (!p)
71 return 0;
73 *p = '\0';
74 /* downcase the email address */
75 for (p = email; *p; p++)
76 *p = tolower(*p);
77 item = path_list_lookup(email, &mailmap);
78 if (item != NULL) {
79 const char *realname = (const char *)item->util;
80 strncpy(name, realname, maxlen);
81 return 1;
83 return 0;
86 static void insert_author_oneline(struct path_list *list,
87 const char *author, int authorlen,
88 const char *oneline, int onelinelen)
90 const char *dot3 = "/pub/scm/linux/kernel/git/";
91 char *buffer, *p;
92 struct path_list_item *item;
93 struct path_list *onelines;
95 while (authorlen > 0 && isspace(author[authorlen - 1]))
96 authorlen--;
98 buffer = xmalloc(authorlen + 1);
99 memcpy(buffer, author, authorlen);
100 buffer[authorlen] = '\0';
102 item = path_list_insert(buffer, list);
103 if (item->util == NULL)
104 item->util = xcalloc(1, sizeof(struct path_list));
105 else
106 free(buffer);
108 if (!strncmp(oneline, "[PATCH", 6)) {
109 char *eob = strchr(oneline, ']');
111 if (eob) {
112 while (isspace(eob[1]) && eob[1] != '\n')
113 eob++;
114 if (eob - oneline < onelinelen) {
115 onelinelen -= eob - oneline;
116 oneline = eob;
121 while (onelinelen > 0 && isspace(oneline[0])) {
122 oneline++;
123 onelinelen--;
126 while (onelinelen > 0 && isspace(oneline[onelinelen - 1]))
127 onelinelen--;
129 buffer = xmalloc(onelinelen + 1);
130 memcpy(buffer, oneline, onelinelen);
131 buffer[onelinelen] = '\0';
133 while ((p = strstr(buffer, dot3)) != NULL) {
134 memcpy(p, "...", 3);
135 strcpy(p + 2, p + sizeof(dot3) - 1);
139 onelines = item->util;
140 if (onelines->nr >= onelines->alloc) {
141 onelines->alloc = alloc_nr(onelines->nr);
142 onelines->items = xrealloc(onelines->items,
143 onelines->alloc
144 * sizeof(struct path_list_item));
147 onelines->items[onelines->nr].util = NULL;
148 onelines->items[onelines->nr++].path = buffer;
151 static void read_from_stdin(struct path_list *list)
153 char buffer[1024];
155 while (fgets(buffer, sizeof(buffer), stdin) != NULL) {
156 char *bob;
157 if ((buffer[0] == 'A' || buffer[0] == 'a') &&
158 !strncmp(buffer + 1, "uthor: ", 7) &&
159 (bob = strchr(buffer + 7, '<')) != NULL) {
160 char buffer2[1024], offset = 0;
162 if (map_email(bob + 1, buffer, sizeof(buffer)))
163 bob = buffer + strlen(buffer);
164 else {
165 offset = 8;
166 while (isspace(bob[-1]))
167 bob--;
170 while (fgets(buffer2, sizeof(buffer2), stdin) &&
171 buffer2[0] != '\n')
172 ; /* chomp input */
173 if (fgets(buffer2, sizeof(buffer2), stdin))
174 insert_author_oneline(list,
175 buffer + offset,
176 bob - buffer - offset,
177 buffer2, strlen(buffer2));
182 static void get_from_rev(struct rev_info *rev, struct path_list *list)
184 char scratch[1024];
185 struct commit *commit;
187 prepare_revision_walk(rev);
188 while ((commit = get_revision(rev)) != NULL) {
189 char *author = NULL, *oneline, *buffer;
190 int authorlen = authorlen, onelinelen;
192 /* get author and oneline */
193 for (buffer = commit->buffer; buffer && *buffer != '\0' &&
194 *buffer != '\n'; ) {
195 char *eol = strchr(buffer, '\n');
197 if (eol == NULL)
198 eol = buffer + strlen(buffer);
199 else
200 eol++;
202 if (!strncmp(buffer, "author ", 7)) {
203 char *bracket = strchr(buffer, '<');
205 if (bracket == NULL || bracket > eol)
206 die("Invalid commit buffer: %s",
207 sha1_to_hex(commit->object.sha1));
209 if (map_email(bracket + 1, scratch,
210 sizeof(scratch))) {
211 author = scratch;
212 authorlen = strlen(scratch);
213 } else {
214 while (bracket[-1] == ' ')
215 bracket--;
217 author = buffer + 7;
218 authorlen = bracket - buffer - 7;
221 buffer = eol;
224 if (author == NULL)
225 die ("Missing author: %s",
226 sha1_to_hex(commit->object.sha1));
228 if (buffer == NULL || *buffer == '\0') {
229 oneline = "<none>";
230 onelinelen = sizeof(oneline) + 1;
231 } else {
232 char *eol;
234 oneline = buffer + 1;
235 eol = strchr(oneline, '\n');
236 if (eol == NULL)
237 onelinelen = strlen(oneline);
238 else
239 onelinelen = eol - oneline;
242 insert_author_oneline(list,
243 author, authorlen, oneline, onelinelen);
248 int cmd_shortlog(int argc, const char **argv, const char *prefix)
250 struct rev_info rev;
251 struct path_list list = { NULL, 0, 0, 1 };
252 int i, j, sort_by_number = 0, summary = 0;
254 /* since -n is a shadowed rev argument, parse our args first */
255 while (argc > 1) {
256 if (!strcmp(argv[1], "-n") || !strcmp(argv[1], "--numbered"))
257 sort_by_number = 1;
258 else if (!strcmp(argv[1], "-s") ||
259 !strcmp(argv[1], "--summary"))
260 summary = 1;
261 else if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))
262 usage(shortlog_usage);
263 else
264 break;
265 argv++;
266 argc--;
268 init_revisions(&rev, prefix);
269 argc = setup_revisions(argc, argv, &rev, NULL);
270 if (argc > 1)
271 die ("unrecognized argument: %s", argv[1]);
273 if (!access(".mailmap", R_OK))
274 read_mailmap(".mailmap");
276 if (rev.pending.nr == 1)
277 die ("Need a range!");
278 else if (rev.pending.nr == 0)
279 read_from_stdin(&list);
280 else
281 get_from_rev(&rev, &list);
283 if (sort_by_number)
284 qsort(list.items, list.nr, sizeof(struct path_list_item),
285 compare_by_number);
287 for (i = 0; i < list.nr; i++) {
288 struct path_list *onelines = list.items[i].util;
290 printf("%s (%d):\n", list.items[i].path, onelines->nr);
291 if (!summary) {
292 for (j = onelines->nr - 1; j >= 0; j--)
293 printf(" %s\n", onelines->items[j].path);
294 printf("\n");
297 onelines->strdup_paths = 1;
298 path_list_clear(onelines, 1);
299 free(onelines);
300 list.items[i].util = NULL;
303 list.strdup_paths = 1;
304 path_list_clear(&list, 1);
305 mailmap.strdup_paths = 1;
306 path_list_clear(&mailmap, 1);
308 return 0;