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