9 static const char shortlog_usage[] =
10 "git-shortlog [-n] [-s] [<commit-id>... ]\n";
12 static int compare_by_number(const void *a1, const void *a2)
14 const struct path_list_item *i1 = a1, *i2 = a2;
15 const struct path_list *l1 = i1->util, *l2 = i2->util;
19 else if (l1->nr == l2->nr)
25 static struct path_list mailmap = {NULL, 0, 0, 0};
27 static int read_mailmap(const char *filename)
30 FILE *f = fopen(filename, "r");
34 while (fgets(buffer, sizeof(buffer), f) != NULL) {
35 char *end_of_name, *left_bracket, *right_bracket;
39 if ((left_bracket = strchr(buffer, '<')) == NULL)
41 if ((right_bracket = strchr(left_bracket + 1, '>')) == NULL)
43 if (right_bracket == left_bracket + 1)
45 for (end_of_name = left_bracket; end_of_name != buffer
46 && isspace(end_of_name[-1]); end_of_name--)
48 if (end_of_name == buffer)
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;
60 static int map_email(char *email, char *name, int maxlen)
63 struct path_list_item *item;
65 /* autocomplete common developers */
66 p = strchr(email, '>');
71 item = path_list_lookup(email, &mailmap);
73 const char *realname = (const char *)item->util;
74 strncpy(name, realname, maxlen);
80 static void insert_author_oneline(struct path_list *list,
81 const char *author, int authorlen,
82 const char *oneline, int onelinelen)
84 const char *dot3 = "/pub/scm/linux/kernel/git/";
86 struct path_list_item *item;
87 struct path_list *onelines;
89 while (authorlen > 0 && isspace(author[authorlen - 1]))
92 buffer = xmalloc(authorlen + 1);
93 memcpy(buffer, author, authorlen);
94 buffer[authorlen] = '\0';
96 item = path_list_insert(buffer, list);
97 if (item->util == NULL)
98 item->util = xcalloc(1, sizeof(struct path_list));
102 if (!strncmp(oneline, "[PATCH", 6)) {
103 char *eob = strchr(oneline, ']');
106 while (isspace(eob[1]) && eob[1] != '\n')
108 if (eob - oneline < onelinelen) {
109 onelinelen -= eob - oneline;
115 while (onelinelen > 0 && isspace(oneline[0])) {
120 while (onelinelen > 0 && isspace(oneline[onelinelen - 1]))
123 buffer = xmalloc(onelinelen + 1);
124 memcpy(buffer, oneline, onelinelen);
125 buffer[onelinelen] = '\0';
127 while ((p = strstr(buffer, dot3)) != NULL) {
129 strcpy(p + 2, p + sizeof(dot3) - 1);
133 onelines = item->util;
134 if (onelines->nr >= onelines->alloc) {
135 onelines->alloc = alloc_nr(onelines->nr);
136 onelines->items = xrealloc(onelines->items,
138 * sizeof(struct path_list_item));
141 onelines->items[onelines->nr].util = NULL;
142 onelines->items[onelines->nr++].path = buffer;
145 static void read_from_stdin(struct path_list *list)
149 while (fgets(buffer, sizeof(buffer), stdin) != NULL) {
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;
156 if (map_email(bob + 1, buffer, sizeof(buffer)))
157 bob = buffer + strlen(buffer);
160 while (isspace(bob[-1]))
164 while (fgets(buffer2, sizeof(buffer2), stdin) &&
167 if (fgets(buffer2, sizeof(buffer2), stdin))
168 insert_author_oneline(list,
170 bob - buffer - offset,
171 buffer2, strlen(buffer2));
176 static void get_from_rev(struct rev_info *rev, struct path_list *list)
179 struct commit *commit;
181 prepare_revision_walk(rev);
182 while ((commit = get_revision(rev)) != NULL) {
183 char *author = NULL, *oneline, *buffer;
184 int authorlen = authorlen, onelinelen;
186 /* get author and oneline */
187 for (buffer = commit->buffer; buffer && *buffer != '\0' &&
189 char *eol = strchr(buffer, '\n');
192 eol = buffer + strlen(buffer);
196 if (!strncmp(buffer, "author ", 7)) {
197 char *bracket = strchr(buffer, '<');
199 if (bracket == NULL || bracket > eol)
200 die("Invalid commit buffer: %s",
201 sha1_to_hex(commit->object.sha1));
203 if (map_email(bracket + 1, scratch,
206 authorlen = strlen(scratch);
208 while (bracket[-1] == ' ')
212 authorlen = bracket - buffer - 7;
219 die ("Missing author: %s",
220 sha1_to_hex(commit->object.sha1));
222 if (buffer == NULL || *buffer == '\0') {
224 onelinelen = sizeof(oneline) + 1;
228 oneline = buffer + 1;
229 eol = strchr(oneline, '\n');
231 onelinelen = strlen(oneline);
233 onelinelen = eol - oneline;
236 insert_author_oneline(list,
237 author, authorlen, oneline, onelinelen);
242 int cmd_shortlog(int argc, const char **argv, const char *prefix)
245 struct path_list list = { NULL, 0, 0, 1 };
246 int i, j, sort_by_number = 0, summary = 0;
248 init_revisions(&rev, prefix);
249 argc = setup_revisions(argc, argv, &rev, NULL);
251 if (!strcmp(argv[1], "-n") || !strcmp(argv[1], "--numbered"))
253 else if (!strcmp(argv[1], "-s") ||
254 !strcmp(argv[1], "--summary"))
256 else if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))
257 usage(shortlog_usage);
259 die ("unrecognized argument: %s", argv[1]);
264 if (!access(".mailmap", R_OK))
265 read_mailmap(".mailmap");
267 if (rev.pending.nr == 1)
268 die ("Need a range!");
269 else if (rev.pending.nr == 0)
270 read_from_stdin(&list);
272 get_from_rev(&rev, &list);
275 qsort(list.items, sizeof(struct path_list_item), list.nr,
278 for (i = 0; i < list.nr; i++) {
279 struct path_list *onelines = list.items[i].util;
281 printf("%s (%d):\n", list.items[i].path, onelines->nr);
283 for (j = onelines->nr - 1; j >= 0; j--)
284 printf(" %s\n", onelines->items[j].path);
288 onelines->strdup_paths = 1;
289 path_list_clear(onelines, 1);
291 list.items[i].util = NULL;
294 list.strdup_paths = 1;
295 path_list_clear(&list, 1);
296 mailmap.strdup_paths = 1;
297 path_list_clear(&mailmap, 1);