afc945663d03ea30691ccc7967d407b3aa173fae
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>
8
9static const char shortlog_usage[] =
10"git-shortlog [-n] [-s] [<commit-id>... ]\n";
11
12static int compare_by_number(const void *a1, const void *a2)
13{
14 const struct path_list_item *i1 = a1, *i2 = a2;
15 const struct path_list *l1 = i1->util, *l2 = i2->util;
16
17 if (l1->nr < l2->nr)
18 return -1;
19 else if (l1->nr == l2->nr)
20 return 0;
21 else
22 return +1;
23}
24
25static struct path_list mailmap = {NULL, 0, 0, 0};
26
27static int read_mailmap(const char *filename)
28{
29 char buffer[1024];
30 FILE *f = fopen(filename, "r");
31
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 if (buffer[0] == '#')
38 continue;
39 if ((left_bracket = strchr(buffer, '<')) == NULL)
40 continue;
41 if ((right_bracket = strchr(left_bracket + 1, '>')) == NULL)
42 continue;
43 if (right_bracket == left_bracket + 1)
44 continue;
45 for (end_of_name = left_bracket; end_of_name != buffer
46 && isspace(end_of_name[-1]); end_of_name--)
47 /* keep on looking */
48 if (end_of_name == buffer)
49 continue;
50 name = xmalloc(end_of_name - buffer + 1);
51 strlcpy(name, buffer, end_of_name - buffer + 1);
52 email = xmalloc(right_bracket - left_bracket);
53 strlcpy(email, left_bracket + 1, right_bracket - left_bracket);
54 path_list_insert(email, &mailmap)->util = name;
55 }
56 fclose(f);
57 return 0;
58}
59
60static int map_email(char *email, char *name, int maxlen)
61{
62 char *p;
63 struct path_list_item *item;
64
65 /* autocomplete common developers */
66 p = strchr(email, '>');
67 if (!p)
68 return 0;
69
70 *p = '\0';
71 item = path_list_lookup(email, &mailmap);
72 if (item != NULL) {
73 const char *realname = (const char *)item->util;
74 strncpy(name, realname, maxlen);
75 return 1;
76 }
77 return 0;
78}
79
80static void insert_author_oneline(struct path_list *list,
81 const char *author, int authorlen,
82 const char *oneline, int onelinelen)
83{
84 const char *dot3 = "/pub/scm/linux/kernel/git/";
85 char *buffer, *p;
86 struct path_list_item *item;
87 struct path_list *onelines;
88
89 while (authorlen > 0 && isspace(author[authorlen - 1]))
90 authorlen--;
91
92 buffer = xmalloc(authorlen + 1);
93 memcpy(buffer, author, authorlen);
94 buffer[authorlen] = '\0';
95
96 item = path_list_insert(buffer, list);
97 if (item->util == NULL)
98 item->util = xcalloc(1, sizeof(struct path_list));
99 else
100 free(buffer);
101
102 if (!strncmp(oneline, "[PATCH", 6)) {
103 char *eob = strchr(oneline, ']');
104
105 if (eob) {
106 while (isspace(eob[1]) && eob[1] != '\n')
107 eob++;
108 if (eob - oneline < onelinelen) {
109 onelinelen -= eob - oneline;
110 oneline = eob;
111 }
112 }
113 }
114
115 while (onelinelen > 0 && isspace(oneline[0])) {
116 oneline++;
117 onelinelen--;
118 }
119
120 while (onelinelen > 0 && isspace(oneline[onelinelen - 1]))
121 onelinelen--;
122
123 buffer = xmalloc(onelinelen + 1);
124 memcpy(buffer, oneline, onelinelen);
125 buffer[onelinelen] = '\0';
126
127 while ((p = strstr(buffer, dot3)) != NULL) {
128 memcpy(p, "...", 3);
129 strcpy(p + 2, p + sizeof(dot3) - 1);
130 }
131
132
133 onelines = item->util;
134 if (onelines->nr >= onelines->alloc) {
135 onelines->alloc = alloc_nr(onelines->nr);
136 onelines->items = xrealloc(onelines->items,
137 onelines->alloc
138 * sizeof(struct path_list_item));
139 }
140
141 onelines->items[onelines->nr].util = NULL;
142 onelines->items[onelines->nr++].path = buffer;
143}
144
145static void read_from_stdin(struct path_list *list)
146{
147 char buffer[1024];
148
149 while (fgets(buffer, sizeof(buffer), stdin) != NULL) {
150 char *bob;
151 if ((buffer[0] == 'A' || buffer[0] == 'a') &&
152 !strncmp(buffer + 1, "uthor: ", 7) &&
153 (bob = strchr(buffer + 7, '<')) != NULL) {
154 char buffer2[1024], offset = 0;
155
156 if (map_email(bob + 1, buffer, sizeof(buffer)))
157 bob = buffer + strlen(buffer);
158 else {
159 offset = 8;
160 while (isspace(bob[-1]))
161 bob--;
162 }
163
164 while (fgets(buffer2, sizeof(buffer2), stdin) &&
165 buffer2[0] != '\n')
166 ; /* chomp input */
167 if (fgets(buffer2, sizeof(buffer2), stdin))
168 insert_author_oneline(list,
169 buffer + offset,
170 bob - buffer - offset,
171 buffer2, strlen(buffer2));
172 }
173 }
174}
175
176static void get_from_rev(struct rev_info *rev, struct path_list *list)
177{
178 char scratch[1024];
179 struct commit *commit;
180
181 prepare_revision_walk(rev);
182 while ((commit = get_revision(rev)) != NULL) {
183 char *author = NULL, *oneline, *buffer;
184 int authorlen = authorlen, onelinelen;
185
186 /* get author and oneline */
187 for (buffer = commit->buffer; buffer && *buffer != '\0' &&
188 *buffer != '\n'; ) {
189 char *eol = strchr(buffer, '\n');
190
191 if (eol == NULL)
192 eol = buffer + strlen(buffer);
193 else
194 eol++;
195
196 if (!strncmp(buffer, "author ", 7)) {
197 char *bracket = strchr(buffer, '<');
198
199 if (bracket == NULL || bracket > eol)
200 die("Invalid commit buffer: %s",
201 sha1_to_hex(commit->object.sha1));
202
203 if (map_email(bracket + 1, scratch,
204 sizeof(scratch))) {
205 author = scratch;
206 authorlen = strlen(scratch);
207 } else {
208 while (bracket[-1] == ' ')
209 bracket--;
210
211 author = buffer + 7;
212 authorlen = bracket - buffer - 7;
213 }
214 }
215 buffer = eol;
216 }
217
218 if (author == NULL)
219 die ("Missing author: %s",
220 sha1_to_hex(commit->object.sha1));
221
222 if (buffer == NULL || *buffer == '\0') {
223 oneline = "<none>";
224 onelinelen = sizeof(oneline) + 1;
225 } else {
226 char *eol;
227
228 oneline = buffer + 1;
229 eol = strchr(oneline, '\n');
230 if (eol == NULL)
231 onelinelen = strlen(oneline);
232 else
233 onelinelen = eol - oneline;
234 }
235
236 insert_author_oneline(list,
237 author, authorlen, oneline, onelinelen);
238 }
239
240}
241
242int cmd_shortlog(int argc, const char **argv, const char *prefix)
243{
244 struct rev_info rev;
245 struct path_list list = { NULL, 0, 0, 1 };
246 int i, j, sort_by_number = 0, summary = 0;
247
248 init_revisions(&rev, prefix);
249 argc = setup_revisions(argc, argv, &rev, NULL);
250 while (argc > 1) {
251 if (!strcmp(argv[1], "-n") || !strcmp(argv[1], "--numbered"))
252 sort_by_number = 1;
253 else if (!strcmp(argv[1], "-s") ||
254 !strcmp(argv[1], "--summary"))
255 summary = 1;
256 else if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))
257 usage(shortlog_usage);
258 else
259 die ("unrecognized argument: %s", argv[1]);
260 argv++;
261 argc--;
262 }
263
264 if (!access(".mailmap", R_OK))
265 read_mailmap(".mailmap");
266
267 if (rev.pending.nr == 1)
268 die ("Need a range!");
269 else if (rev.pending.nr == 0)
270 read_from_stdin(&list);
271 else
272 get_from_rev(&rev, &list);
273
274 if (sort_by_number)
275 qsort(list.items, sizeof(struct path_list_item), list.nr,
276 compare_by_number);
277
278 for (i = 0; i < list.nr; i++) {
279 struct path_list *onelines = list.items[i].util;
280
281 printf("%s (%d):\n", list.items[i].path, onelines->nr);
282 if (!summary) {
283 for (j = onelines->nr - 1; j >= 0; j--)
284 printf(" %s\n", onelines->items[j].path);
285 printf("\n");
286 }
287
288 onelines->strdup_paths = 1;
289 path_list_clear(onelines, 1);
290 free(onelines);
291 list.items[i].util = NULL;
292 }
293
294 list.strdup_paths = 1;
295 path_list_clear(&list, 1);
296 mailmap.strdup_paths = 1;
297 path_list_clear(&mailmap, 1);
298
299 return 0;
300}
301