Replace a pair of patches with updated ones for subproject support.
[git/jnareb-git/bp-gitweb.git] / builtin-shortlog.c
blob29343aefc843c4dd22095f559262bc6b5e381440
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"
8 static const char shortlog_usage[] =
9 "git-shortlog [-n] [-s] [<commit-id>... ]";
11 static char *common_repo_prefix;
13 static int compare_by_number(const void *a1, const void *a2)
15 const struct path_list_item *i1 = a1, *i2 = a2;
16 const struct path_list *l1 = i1->util, *l2 = i2->util;
18 if (l1->nr < l2->nr)
19 return 1;
20 else if (l1->nr == l2->nr)
21 return 0;
22 else
23 return -1;
26 static struct path_list mailmap = {NULL, 0, 0, 0};
28 static int read_mailmap(const char *filename)
30 char buffer[1024];
31 FILE *f = fopen(filename, "r");
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;
38 int i;
39 if (buffer[0] == '#') {
40 static const char abbrev[] = "# repo-abbrev:";
41 int abblen = sizeof(abbrev) - 1;
42 int len = strlen(buffer);
44 if (len && buffer[len - 1] == '\n')
45 buffer[--len] = 0;
46 if (!strncmp(buffer, abbrev, abblen)) {
47 char *cp;
49 if (common_repo_prefix)
50 free(common_repo_prefix);
51 common_repo_prefix = xmalloc(len);
53 for (cp = buffer + abblen; isspace(*cp); cp++)
54 ; /* nothing */
55 strcpy(common_repo_prefix, cp);
57 continue;
59 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);
73 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';
76 path_list_insert(email, &mailmap)->util = name;
78 fclose(f);
79 return 0;
82 static int map_email(char *email, char *name, int maxlen)
84 char *p;
85 struct path_list_item *item;
87 /* autocomplete common developers */
88 p = strchr(email, '>');
89 if (!p)
90 return 0;
92 *p = '\0';
93 /* downcase the email address */
94 for (p = email; *p; p++)
95 *p = tolower(*p);
96 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;
102 return 0;
105 static void insert_author_oneline(struct path_list *list,
106 const char *author, int authorlen,
107 const char *oneline, int onelinelen)
109 const char *dot3 = common_repo_prefix;
110 char *buffer, *p;
111 struct path_list_item *item;
112 struct path_list *onelines;
114 while (authorlen > 0 && isspace(author[authorlen - 1]))
115 authorlen--;
117 buffer = xmalloc(authorlen + 1);
118 memcpy(buffer, author, authorlen);
119 buffer[authorlen] = '\0';
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);
127 if (!prefixcmp(oneline, "[PATCH")) {
128 char *eob = strchr(oneline, ']');
130 if (eob) {
131 while (isspace(eob[1]) && eob[1] != '\n')
132 eob++;
133 if (eob - oneline < onelinelen) {
134 onelinelen -= eob - oneline;
135 oneline = eob;
140 while (onelinelen > 0 && isspace(oneline[0])) {
141 oneline++;
142 onelinelen--;
145 while (onelinelen > 0 && isspace(oneline[onelinelen - 1]))
146 onelinelen--;
148 buffer = xmalloc(onelinelen + 1);
149 memcpy(buffer, oneline, onelinelen);
150 buffer[onelinelen] = '\0';
152 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);
163 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));
171 onelines->items[onelines->nr].util = NULL;
172 onelines->items[onelines->nr++].path = buffer;
175 static void read_from_stdin(struct path_list *list)
177 char buffer[1024];
179 while (fgets(buffer, sizeof(buffer), stdin) != NULL) {
180 char *bob;
181 if ((buffer[0] == 'A' || buffer[0] == 'a') &&
182 !prefixcmp(buffer + 1, "uthor: ") &&
183 (bob = strchr(buffer + 7, '<')) != NULL) {
184 char buffer2[1024], offset = 0;
186 if (map_email(bob + 1, buffer, sizeof(buffer)))
187 bob = buffer + strlen(buffer);
188 else {
189 offset = 8;
190 while (buffer + offset < bob &&
191 isspace(bob[-1]))
192 bob--;
195 while (fgets(buffer2, sizeof(buffer2), stdin) &&
196 buffer2[0] != '\n')
197 ; /* chomp input */
198 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;
204 insert_author_oneline(list,
205 buffer + offset,
206 bob - buffer - offset,
207 buffer2 + i, l2 - i);
213 static void get_from_rev(struct rev_info *rev, struct path_list *list)
215 char scratch[1024];
216 struct commit *commit;
218 prepare_revision_walk(rev);
219 while ((commit = get_revision(rev)) != NULL) {
220 const char *author = NULL, *oneline, *buffer;
221 int authorlen = authorlen, onelinelen;
223 /* get author and oneline */
224 for (buffer = commit->buffer; buffer && *buffer != '\0' &&
225 *buffer != '\n'; ) {
226 const char *eol = strchr(buffer, '\n');
228 if (eol == NULL)
229 eol = buffer + strlen(buffer);
230 else
231 eol++;
233 if (!prefixcmp(buffer, "author ")) {
234 char *bracket = strchr(buffer, '<');
236 if (bracket == NULL || bracket > eol)
237 die("Invalid commit buffer: %s",
238 sha1_to_hex(commit->object.sha1));
240 if (map_email(bracket + 1, scratch,
241 sizeof(scratch))) {
242 author = scratch;
243 authorlen = strlen(scratch);
244 } else {
245 if (bracket[-1] == ' ')
246 bracket--;
248 author = buffer + 7;
249 authorlen = bracket - buffer - 7;
252 buffer = eol;
255 if (author == NULL)
256 die ("Missing author: %s",
257 sha1_to_hex(commit->object.sha1));
259 if (buffer == NULL || *buffer == '\0') {
260 oneline = "<none>";
261 onelinelen = sizeof(oneline) + 1;
262 } else {
263 char *eol;
265 oneline = buffer + 1;
266 eol = strchr(oneline, '\n');
267 if (eol == NULL)
268 onelinelen = strlen(oneline);
269 else
270 onelinelen = eol - oneline;
273 insert_author_oneline(list,
274 author, authorlen, oneline, onelinelen);
279 int cmd_shortlog(int argc, const char **argv, const char *prefix)
281 struct rev_info rev;
282 struct path_list list = { NULL, 0, 0, 1 };
283 int i, j, sort_by_number = 0, summary = 0;
285 /* since -n is a shadowed rev argument, parse our args first */
286 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
295 break;
296 argv++;
297 argc--;
299 init_revisions(&rev, prefix);
300 argc = setup_revisions(argc, argv, &rev, NULL);
301 if (argc > 1)
302 die ("unrecognized argument: %s", argv[1]);
304 if (!access(".mailmap", R_OK))
305 read_mailmap(".mailmap");
307 if (rev.pending.nr == 0) {
308 if (isatty(0))
309 fprintf(stderr, "(reading log to summarize from standard input)\n");
310 read_from_stdin(&list);
312 else
313 get_from_rev(&rev, &list);
315 if (sort_by_number)
316 qsort(list.items, list.nr, sizeof(struct path_list_item),
317 compare_by_number);
319 for (i = 0; i < list.nr; i++) {
320 struct path_list *onelines = list.items[i].util;
322 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);
326 for (j = onelines->nr - 1; j >= 0; j--)
327 printf(" %s\n", onelines->items[j].path);
328 printf("\n");
331 onelines->strdup_paths = 1;
332 path_list_clear(onelines, 1);
333 free(onelines);
334 list.items[i].util = NULL;
337 list.strdup_paths = 1;
338 path_list_clear(&list, 1);
339 mailmap.strdup_paths = 1;
340 path_list_clear(&mailmap, 1);
342 return 0;