blob: 29343aefc843c4dd22095f559262bc6b5e381440 [file] [log] [blame]
Johannes Schindelinb8ec5922006-10-22 13:23:31 +02001#include "builtin.h"
2#include "cache.h"
3#include "commit.h"
4#include "diff.h"
5#include "path-list.h"
6#include "revision.h"
Johannes Schindelinb8ec5922006-10-22 13:23:31 +02007
8static const char shortlog_usage[] =
Nicolas Pitreac60c942006-11-21 15:49:45 -05009"git-shortlog [-n] [-s] [<commit-id>... ]";
Johannes Schindelinb8ec5922006-10-22 13:23:31 +020010
Junio C Hamano7595e2e2006-11-25 00:07:54 -080011static char *common_repo_prefix;
12
Johannes Schindelinb8ec5922006-10-22 13:23:31 +020013static int compare_by_number(const void *a1, const void *a2)
14{
15 const struct path_list_item *i1 = a1, *i2 = a2;
16 const struct path_list *l1 = i1->util, *l2 = i2->util;
17
18 if (l1->nr < l2->nr)
Johannes Schindelin6d6ab612006-11-21 21:12:06 +010019 return 1;
Johannes Schindelinb8ec5922006-10-22 13:23:31 +020020 else if (l1->nr == l2->nr)
21 return 0;
22 else
Johannes Schindelin6d6ab612006-11-21 21:12:06 +010023 return -1;
Johannes Schindelinb8ec5922006-10-22 13:23:31 +020024}
25
Johannes Schindelind8e81252006-11-19 17:28:51 +010026static struct path_list mailmap = {NULL, 0, 0, 0};
Johannes Schindelinb8ec5922006-10-22 13:23:31 +020027
Johannes Schindelind8e81252006-11-19 17:28:51 +010028static int read_mailmap(const char *filename)
29{
30 char buffer[1024];
31 FILE *f = fopen(filename, "r");
32
33 if (f == NULL)
34 return 1;
35 while (fgets(buffer, sizeof(buffer), f) != NULL) {
36 char *end_of_name, *left_bracket, *right_bracket;
37 char *name, *email;
Johannes Schindelin54965232006-11-19 17:29:14 +010038 int i;
Junio C Hamano7595e2e2006-11-25 00:07:54 -080039 if (buffer[0] == '#') {
40 static const char abbrev[] = "# repo-abbrev:";
41 int abblen = sizeof(abbrev) - 1;
42 int len = strlen(buffer);
43
44 if (len && buffer[len - 1] == '\n')
45 buffer[--len] = 0;
46 if (!strncmp(buffer, abbrev, abblen)) {
47 char *cp;
48
49 if (common_repo_prefix)
50 free(common_repo_prefix);
51 common_repo_prefix = xmalloc(len);
52
53 for (cp = buffer + abblen; isspace(*cp); cp++)
54 ; /* nothing */
55 strcpy(common_repo_prefix, cp);
56 }
Johannes Schindelind8e81252006-11-19 17:28:51 +010057 continue;
Junio C Hamano7595e2e2006-11-25 00:07:54 -080058 }
Johannes Schindelind8e81252006-11-19 17:28:51 +010059 if ((left_bracket = strchr(buffer, '<')) == NULL)
60 continue;
61 if ((right_bracket = strchr(left_bracket + 1, '>')) == NULL)
62 continue;
63 if (right_bracket == left_bracket + 1)
64 continue;
65 for (end_of_name = left_bracket; end_of_name != buffer
66 && isspace(end_of_name[-1]); end_of_name--)
67 /* keep on looking */
68 if (end_of_name == buffer)
69 continue;
70 name = xmalloc(end_of_name - buffer + 1);
71 strlcpy(name, buffer, end_of_name - buffer + 1);
72 email = xmalloc(right_bracket - left_bracket);
Johannes Schindelin54965232006-11-19 17:29:14 +010073 for (i = 0; i < right_bracket - left_bracket - 1; i++)
74 email[i] = tolower(left_bracket[i + 1]);
75 email[right_bracket - left_bracket - 1] = '\0';
Johannes Schindelind8e81252006-11-19 17:28:51 +010076 path_list_insert(email, &mailmap)->util = name;
77 }
78 fclose(f);
79 return 0;
80}
Johannes Schindelinb8ec5922006-10-22 13:23:31 +020081
82static int map_email(char *email, char *name, int maxlen)
83{
84 char *p;
85 struct path_list_item *item;
86
87 /* autocomplete common developers */
88 p = strchr(email, '>');
89 if (!p)
90 return 0;
91
92 *p = '\0';
Johannes Schindelin54965232006-11-19 17:29:14 +010093 /* downcase the email address */
94 for (p = email; *p; p++)
95 *p = tolower(*p);
Johannes Schindelinb8ec5922006-10-22 13:23:31 +020096 item = path_list_lookup(email, &mailmap);
97 if (item != NULL) {
98 const char *realname = (const char *)item->util;
99 strncpy(name, realname, maxlen);
100 return 1;
101 }
102 return 0;
103}
104
105static void insert_author_oneline(struct path_list *list,
106 const char *author, int authorlen,
107 const char *oneline, int onelinelen)
108{
Junio C Hamano7595e2e2006-11-25 00:07:54 -0800109 const char *dot3 = common_repo_prefix;
Johannes Schindelinb8ec5922006-10-22 13:23:31 +0200110 char *buffer, *p;
111 struct path_list_item *item;
112 struct path_list *onelines;
113
114 while (authorlen > 0 && isspace(author[authorlen - 1]))
115 authorlen--;
116
117 buffer = xmalloc(authorlen + 1);
118 memcpy(buffer, author, authorlen);
119 buffer[authorlen] = '\0';
120
121 item = path_list_insert(buffer, list);
122 if (item->util == NULL)
123 item->util = xcalloc(1, sizeof(struct path_list));
124 else
125 free(buffer);
126
Junio C Hamanocc44c762007-02-20 01:53:29 -0800127 if (!prefixcmp(oneline, "[PATCH")) {
Johannes Schindelin72019cd2006-11-19 17:28:25 +0100128 char *eob = strchr(oneline, ']');
Johannes Schindelinb8ec5922006-10-22 13:23:31 +0200129
Johannes Schindelin72019cd2006-11-19 17:28:25 +0100130 if (eob) {
131 while (isspace(eob[1]) && eob[1] != '\n')
132 eob++;
133 if (eob - oneline < onelinelen) {
134 onelinelen -= eob - oneline;
135 oneline = eob;
136 }
Johannes Schindelinb8ec5922006-10-22 13:23:31 +0200137 }
138 }
139
140 while (onelinelen > 0 && isspace(oneline[0])) {
141 oneline++;
142 onelinelen--;
143 }
144
145 while (onelinelen > 0 && isspace(oneline[onelinelen - 1]))
146 onelinelen--;
147
148 buffer = xmalloc(onelinelen + 1);
149 memcpy(buffer, oneline, onelinelen);
150 buffer[onelinelen] = '\0';
151
Junio C Hamanoc95044d2006-11-25 00:01:27 -0800152 if (dot3) {
153 int dot3len = strlen(dot3);
154 if (dot3len > 5) {
155 while ((p = strstr(buffer, dot3)) != NULL) {
156 int taillen = strlen(p) - dot3len;
157 memcpy(p, "/.../", 5);
158 memmove(p + 5, p + dot3len, taillen + 1);
159 }
160 }
Johannes Schindelinb8ec5922006-10-22 13:23:31 +0200161 }
162
Johannes Schindelinb8ec5922006-10-22 13:23:31 +0200163 onelines = item->util;
164 if (onelines->nr >= onelines->alloc) {
165 onelines->alloc = alloc_nr(onelines->nr);
166 onelines->items = xrealloc(onelines->items,
167 onelines->alloc
168 * sizeof(struct path_list_item));
169 }
170
171 onelines->items[onelines->nr].util = NULL;
172 onelines->items[onelines->nr++].path = buffer;
173}
174
175static void read_from_stdin(struct path_list *list)
176{
177 char buffer[1024];
178
179 while (fgets(buffer, sizeof(buffer), stdin) != NULL) {
180 char *bob;
181 if ((buffer[0] == 'A' || buffer[0] == 'a') &&
Junio C Hamanocc44c762007-02-20 01:53:29 -0800182 !prefixcmp(buffer + 1, "uthor: ") &&
Johannes Schindelinb8ec5922006-10-22 13:23:31 +0200183 (bob = strchr(buffer + 7, '<')) != NULL) {
184 char buffer2[1024], offset = 0;
185
186 if (map_email(bob + 1, buffer, sizeof(buffer)))
187 bob = buffer + strlen(buffer);
188 else {
189 offset = 8;
Junio C Hamano6f987252006-12-10 15:51:54 -0800190 while (buffer + offset < bob &&
191 isspace(bob[-1]))
Johannes Schindelinb8ec5922006-10-22 13:23:31 +0200192 bob--;
193 }
194
195 while (fgets(buffer2, sizeof(buffer2), stdin) &&
196 buffer2[0] != '\n')
197 ; /* chomp input */
Junio C Hamanobca73252006-12-10 15:55:07 -0800198 if (fgets(buffer2, sizeof(buffer2), stdin)) {
199 int l2 = strlen(buffer2);
200 int i;
201 for (i = 0; i < l2; i++)
202 if (!isspace(buffer2[i]))
203 break;
Johannes Schindelinb8ec5922006-10-22 13:23:31 +0200204 insert_author_oneline(list,
205 buffer + offset,
206 bob - buffer - offset,
Junio C Hamanobca73252006-12-10 15:55:07 -0800207 buffer2 + i, l2 - i);
208 }
Johannes Schindelinb8ec5922006-10-22 13:23:31 +0200209 }
210 }
211}
212
213static void get_from_rev(struct rev_info *rev, struct path_list *list)
214{
215 char scratch[1024];
216 struct commit *commit;
217
218 prepare_revision_walk(rev);
219 while ((commit = get_revision(rev)) != NULL) {
Shawn O. Pearce3a556022007-03-06 20:44:17 -0500220 const char *author = NULL, *oneline, *buffer;
Johannes Schindelinb8ec5922006-10-22 13:23:31 +0200221 int authorlen = authorlen, onelinelen;
222
223 /* get author and oneline */
224 for (buffer = commit->buffer; buffer && *buffer != '\0' &&
225 *buffer != '\n'; ) {
Shawn O. Pearce3a556022007-03-06 20:44:17 -0500226 const char *eol = strchr(buffer, '\n');
Johannes Schindelinb8ec5922006-10-22 13:23:31 +0200227
228 if (eol == NULL)
229 eol = buffer + strlen(buffer);
230 else
231 eol++;
232
Junio C Hamanocc44c762007-02-20 01:53:29 -0800233 if (!prefixcmp(buffer, "author ")) {
Johannes Schindelinb8ec5922006-10-22 13:23:31 +0200234 char *bracket = strchr(buffer, '<');
235
236 if (bracket == NULL || bracket > eol)
237 die("Invalid commit buffer: %s",
238 sha1_to_hex(commit->object.sha1));
239
240 if (map_email(bracket + 1, scratch,
241 sizeof(scratch))) {
242 author = scratch;
243 authorlen = strlen(scratch);
244 } else {
Jeff King90ffefe2006-12-08 23:04:21 -0500245 if (bracket[-1] == ' ')
Johannes Schindelinb8ec5922006-10-22 13:23:31 +0200246 bracket--;
247
248 author = buffer + 7;
249 authorlen = bracket - buffer - 7;
250 }
251 }
252 buffer = eol;
253 }
254
255 if (author == NULL)
256 die ("Missing author: %s",
257 sha1_to_hex(commit->object.sha1));
258
259 if (buffer == NULL || *buffer == '\0') {
260 oneline = "<none>";
261 onelinelen = sizeof(oneline) + 1;
262 } else {
263 char *eol;
264
265 oneline = buffer + 1;
266 eol = strchr(oneline, '\n');
267 if (eol == NULL)
268 onelinelen = strlen(oneline);
269 else
270 onelinelen = eol - oneline;
271 }
272
273 insert_author_oneline(list,
274 author, authorlen, oneline, onelinelen);
275 }
276
277}
278
279int cmd_shortlog(int argc, const char **argv, const char *prefix)
280{
281 struct rev_info rev;
282 struct path_list list = { NULL, 0, 0, 1 };
283 int i, j, sort_by_number = 0, summary = 0;
284
Johannes Schindelin6d6ab612006-11-21 21:12:06 +0100285 /* since -n is a shadowed rev argument, parse our args first */
Johannes Schindelinb8ec5922006-10-22 13:23:31 +0200286 while (argc > 1) {
287 if (!strcmp(argv[1], "-n") || !strcmp(argv[1], "--numbered"))
288 sort_by_number = 1;
289 else if (!strcmp(argv[1], "-s") ||
290 !strcmp(argv[1], "--summary"))
291 summary = 1;
292 else if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))
293 usage(shortlog_usage);
294 else
Johannes Schindelin6d6ab612006-11-21 21:12:06 +0100295 break;
Johannes Schindelinb8ec5922006-10-22 13:23:31 +0200296 argv++;
297 argc--;
298 }
Johannes Schindelin6d6ab612006-11-21 21:12:06 +0100299 init_revisions(&rev, prefix);
300 argc = setup_revisions(argc, argv, &rev, NULL);
301 if (argc > 1)
302 die ("unrecognized argument: %s", argv[1]);
Johannes Schindelinb8ec5922006-10-22 13:23:31 +0200303
Johannes Schindelind8e81252006-11-19 17:28:51 +0100304 if (!access(".mailmap", R_OK))
305 read_mailmap(".mailmap");
306
Junio C Hamano0497c622007-03-08 02:12:06 -0800307 if (rev.pending.nr == 0) {
308 if (isatty(0))
309 fprintf(stderr, "(reading log to summarize from standard input)\n");
Johannes Schindelinb8ec5922006-10-22 13:23:31 +0200310 read_from_stdin(&list);
Junio C Hamano0497c622007-03-08 02:12:06 -0800311 }
Johannes Schindelinb8ec5922006-10-22 13:23:31 +0200312 else
313 get_from_rev(&rev, &list);
314
315 if (sort_by_number)
Johannes Schindelin6d6ab612006-11-21 21:12:06 +0100316 qsort(list.items, list.nr, sizeof(struct path_list_item),
Johannes Schindelinb8ec5922006-10-22 13:23:31 +0200317 compare_by_number);
318
319 for (i = 0; i < list.nr; i++) {
320 struct path_list *onelines = list.items[i].util;
321
Nicolas Pitreac60c942006-11-21 15:49:45 -0500322 if (summary) {
323 printf("%s: %d\n", list.items[i].path, onelines->nr);
324 } else {
325 printf("%s (%d):\n", list.items[i].path, onelines->nr);
Johannes Schindelinb8ec5922006-10-22 13:23:31 +0200326 for (j = onelines->nr - 1; j >= 0; j--)
327 printf(" %s\n", onelines->items[j].path);
328 printf("\n");
329 }
330
331 onelines->strdup_paths = 1;
332 path_list_clear(onelines, 1);
333 free(onelines);
334 list.items[i].util = NULL;
335 }
336
337 list.strdup_paths = 1;
338 path_list_clear(&list, 1);
Johannes Schindelind8e81252006-11-19 17:28:51 +0100339 mailmap.strdup_paths = 1;
340 path_list_clear(&mailmap, 1);
Johannes Schindelinb8ec5922006-10-22 13:23:31 +0200341
342 return 0;
343}
344