]> git.ipfire.org Git - thirdparty/git.git/blob - builtin-shortlog.c
git-shortlog: fix common repository prefix abbreviation.
[thirdparty/git.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 <string.h>
8
9 static const char shortlog_usage[] =
10 "git-shortlog [-n] [-s] [<commit-id>... ]";
11
12 static 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
25 static struct path_list mailmap = {NULL, 0, 0, 0};
26
27 static 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 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;
58 }
59 fclose(f);
60 return 0;
61 }
62
63 static int map_email(char *email, char *name, int maxlen)
64 {
65 char *p;
66 struct path_list_item *item;
67
68 /* autocomplete common developers */
69 p = strchr(email, '>');
70 if (!p)
71 return 0;
72
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;
82 }
83 return 0;
84 }
85
86 static void insert_author_oneline(struct path_list *list,
87 const char *author, int authorlen,
88 const char *oneline, int onelinelen)
89 {
90 const char *dot3 = "/pub/scm/linux/kernel/git/";
91 char *buffer, *p;
92 struct path_list_item *item;
93 struct path_list *onelines;
94
95 while (authorlen > 0 && isspace(author[authorlen - 1]))
96 authorlen--;
97
98 buffer = xmalloc(authorlen + 1);
99 memcpy(buffer, author, authorlen);
100 buffer[authorlen] = '\0';
101
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);
107
108 if (!strncmp(oneline, "[PATCH", 6)) {
109 char *eob = strchr(oneline, ']');
110
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;
117 }
118 }
119 }
120
121 while (onelinelen > 0 && isspace(oneline[0])) {
122 oneline++;
123 onelinelen--;
124 }
125
126 while (onelinelen > 0 && isspace(oneline[onelinelen - 1]))
127 onelinelen--;
128
129 buffer = xmalloc(onelinelen + 1);
130 memcpy(buffer, oneline, onelinelen);
131 buffer[onelinelen] = '\0';
132
133 if (dot3) {
134 int dot3len = strlen(dot3);
135 if (dot3len > 5) {
136 while ((p = strstr(buffer, dot3)) != NULL) {
137 int taillen = strlen(p) - dot3len;
138 memcpy(p, "/.../", 5);
139 memmove(p + 5, p + dot3len, taillen + 1);
140 }
141 }
142 }
143
144 onelines = item->util;
145 if (onelines->nr >= onelines->alloc) {
146 onelines->alloc = alloc_nr(onelines->nr);
147 onelines->items = xrealloc(onelines->items,
148 onelines->alloc
149 * sizeof(struct path_list_item));
150 }
151
152 onelines->items[onelines->nr].util = NULL;
153 onelines->items[onelines->nr++].path = buffer;
154 }
155
156 static void read_from_stdin(struct path_list *list)
157 {
158 char buffer[1024];
159
160 while (fgets(buffer, sizeof(buffer), stdin) != NULL) {
161 char *bob;
162 if ((buffer[0] == 'A' || buffer[0] == 'a') &&
163 !strncmp(buffer + 1, "uthor: ", 7) &&
164 (bob = strchr(buffer + 7, '<')) != NULL) {
165 char buffer2[1024], offset = 0;
166
167 if (map_email(bob + 1, buffer, sizeof(buffer)))
168 bob = buffer + strlen(buffer);
169 else {
170 offset = 8;
171 while (isspace(bob[-1]))
172 bob--;
173 }
174
175 while (fgets(buffer2, sizeof(buffer2), stdin) &&
176 buffer2[0] != '\n')
177 ; /* chomp input */
178 if (fgets(buffer2, sizeof(buffer2), stdin))
179 insert_author_oneline(list,
180 buffer + offset,
181 bob - buffer - offset,
182 buffer2, strlen(buffer2));
183 }
184 }
185 }
186
187 static void get_from_rev(struct rev_info *rev, struct path_list *list)
188 {
189 char scratch[1024];
190 struct commit *commit;
191
192 prepare_revision_walk(rev);
193 while ((commit = get_revision(rev)) != NULL) {
194 char *author = NULL, *oneline, *buffer;
195 int authorlen = authorlen, onelinelen;
196
197 /* get author and oneline */
198 for (buffer = commit->buffer; buffer && *buffer != '\0' &&
199 *buffer != '\n'; ) {
200 char *eol = strchr(buffer, '\n');
201
202 if (eol == NULL)
203 eol = buffer + strlen(buffer);
204 else
205 eol++;
206
207 if (!strncmp(buffer, "author ", 7)) {
208 char *bracket = strchr(buffer, '<');
209
210 if (bracket == NULL || bracket > eol)
211 die("Invalid commit buffer: %s",
212 sha1_to_hex(commit->object.sha1));
213
214 if (map_email(bracket + 1, scratch,
215 sizeof(scratch))) {
216 author = scratch;
217 authorlen = strlen(scratch);
218 } else {
219 while (bracket[-1] == ' ')
220 bracket--;
221
222 author = buffer + 7;
223 authorlen = bracket - buffer - 7;
224 }
225 }
226 buffer = eol;
227 }
228
229 if (author == NULL)
230 die ("Missing author: %s",
231 sha1_to_hex(commit->object.sha1));
232
233 if (buffer == NULL || *buffer == '\0') {
234 oneline = "<none>";
235 onelinelen = sizeof(oneline) + 1;
236 } else {
237 char *eol;
238
239 oneline = buffer + 1;
240 eol = strchr(oneline, '\n');
241 if (eol == NULL)
242 onelinelen = strlen(oneline);
243 else
244 onelinelen = eol - oneline;
245 }
246
247 insert_author_oneline(list,
248 author, authorlen, oneline, onelinelen);
249 }
250
251 }
252
253 int cmd_shortlog(int argc, const char **argv, const char *prefix)
254 {
255 struct rev_info rev;
256 struct path_list list = { NULL, 0, 0, 1 };
257 int i, j, sort_by_number = 0, summary = 0;
258
259 /* since -n is a shadowed rev argument, parse our args first */
260 while (argc > 1) {
261 if (!strcmp(argv[1], "-n") || !strcmp(argv[1], "--numbered"))
262 sort_by_number = 1;
263 else if (!strcmp(argv[1], "-s") ||
264 !strcmp(argv[1], "--summary"))
265 summary = 1;
266 else if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))
267 usage(shortlog_usage);
268 else
269 break;
270 argv++;
271 argc--;
272 }
273 init_revisions(&rev, prefix);
274 argc = setup_revisions(argc, argv, &rev, NULL);
275 if (argc > 1)
276 die ("unrecognized argument: %s", argv[1]);
277
278 if (!access(".mailmap", R_OK))
279 read_mailmap(".mailmap");
280
281 if (rev.pending.nr == 1)
282 die ("Need a range!");
283 else if (rev.pending.nr == 0)
284 read_from_stdin(&list);
285 else
286 get_from_rev(&rev, &list);
287
288 if (sort_by_number)
289 qsort(list.items, list.nr, sizeof(struct path_list_item),
290 compare_by_number);
291
292 for (i = 0; i < list.nr; i++) {
293 struct path_list *onelines = list.items[i].util;
294
295 if (summary) {
296 printf("%s: %d\n", list.items[i].path, onelines->nr);
297 } else {
298 printf("%s (%d):\n", list.items[i].path, onelines->nr);
299 for (j = onelines->nr - 1; j >= 0; j--)
300 printf(" %s\n", onelines->items[j].path);
301 printf("\n");
302 }
303
304 onelines->strdup_paths = 1;
305 path_list_clear(onelines, 1);
306 free(onelines);
307 list.items[i].util = NULL;
308 }
309
310 list.strdup_paths = 1;
311 path_list_clear(&list, 1);
312 mailmap.strdup_paths = 1;
313 path_list_clear(&mailmap, 1);
314
315 return 0;
316 }
317