9 static const char shortlog_usage[] =
10 "git-shortlog [-n] [-s] [<commit-id>... ]";
12 static char *common_repo_prefix;
14 static int compare_by_number(const void *a1, const void *a2)
16 const struct path_list_item *i1 = a1, *i2 = a2;
17 const struct path_list *l1 = i1->util, *l2 = i2->util;
21 else if (l1->nr == l2->nr)
27 static struct path_list mailmap = {NULL, 0, 0, 0};
29 static int read_mailmap(const char *filename)
32 FILE *f = fopen(filename, "r");
36 while (fgets(buffer, sizeof(buffer), f) != NULL) {
37 char *end_of_name, *left_bracket, *right_bracket;
40 if (buffer[0] == '#') {
41 static const char abbrev[] = "# repo-abbrev:";
42 int abblen = sizeof(abbrev) - 1;
43 int len = strlen(buffer);
45 if (len && buffer[len - 1] == '\n')
47 if (!strncmp(buffer, abbrev, abblen)) {
50 if (common_repo_prefix)
51 free(common_repo_prefix);
52 common_repo_prefix = xmalloc(len);
54 for (cp = buffer + abblen; isspace(*cp); cp++)
56 strcpy(common_repo_prefix, cp);
60 if ((left_bracket = strchr(buffer, '<')) == NULL)
62 if ((right_bracket = strchr(left_bracket + 1, '>')) == NULL)
64 if (right_bracket == left_bracket + 1)
66 for (end_of_name = left_bracket; end_of_name != buffer
67 && isspace(end_of_name[-1]); end_of_name--)
69 if (end_of_name == buffer)
71 name = xmalloc(end_of_name - buffer + 1);
72 strlcpy(name, buffer, end_of_name - buffer + 1);
73 email = xmalloc(right_bracket - left_bracket);
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';
77 path_list_insert(email, &mailmap)->util = name;
83 static int map_email(char *email, char *name, int maxlen)
86 struct path_list_item *item;
88 /* autocomplete common developers */
89 p = strchr(email, '>');
94 /* downcase the email address */
95 for (p = email; *p; p++)
97 item = path_list_lookup(email, &mailmap);
99 const char *realname = (const char *)item->util;
100 strncpy(name, realname, maxlen);
106 static void insert_author_oneline(struct path_list *list,
107 const char *author, int authorlen,
108 const char *oneline, int onelinelen)
110 const char *dot3 = common_repo_prefix;
112 struct path_list_item *item;
113 struct path_list *onelines;
115 while (authorlen > 0 && isspace(author[authorlen - 1]))
118 buffer = xmalloc(authorlen + 1);
119 memcpy(buffer, author, authorlen);
120 buffer[authorlen] = '\0';
122 item = path_list_insert(buffer, list);
123 if (item->util == NULL)
124 item->util = xcalloc(1, sizeof(struct path_list));
128 if (!prefixcmp(oneline, "[PATCH")) {
129 char *eob = strchr(oneline, ']');
132 while (isspace(eob[1]) && eob[1] != '\n')
134 if (eob - oneline < onelinelen) {
135 onelinelen -= eob - oneline;
141 while (onelinelen > 0 && isspace(oneline[0])) {
146 while (onelinelen > 0 && isspace(oneline[onelinelen - 1]))
149 buffer = xmalloc(onelinelen + 1);
150 memcpy(buffer, oneline, onelinelen);
151 buffer[onelinelen] = '\0';
154 int dot3len = strlen(dot3);
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);
164 onelines = item->util;
165 if (onelines->nr >= onelines->alloc) {
166 onelines->alloc = alloc_nr(onelines->nr);
167 onelines->items = xrealloc(onelines->items,
169 * sizeof(struct path_list_item));
172 onelines->items[onelines->nr].util = NULL;
173 onelines->items[onelines->nr++].path = buffer;
176 static void read_from_stdin(struct path_list *list)
180 while (fgets(buffer, sizeof(buffer), stdin) != NULL) {
182 if ((buffer[0] == 'A' || buffer[0] == 'a') &&
183 !prefixcmp(buffer + 1, "uthor: ") &&
184 (bob = strchr(buffer + 7, '<')) != NULL) {
185 char buffer2[1024], offset = 0;
187 if (map_email(bob + 1, buffer, sizeof(buffer)))
188 bob = buffer + strlen(buffer);
191 while (buffer + offset < bob &&
196 while (fgets(buffer2, sizeof(buffer2), stdin) &&
199 if (fgets(buffer2, sizeof(buffer2), stdin)) {
200 int l2 = strlen(buffer2);
202 for (i = 0; i < l2; i++)
203 if (!isspace(buffer2[i]))
205 insert_author_oneline(list,
207 bob - buffer - offset,
208 buffer2 + i, l2 - i);
214 static void get_from_rev(struct rev_info *rev, struct path_list *list)
217 struct commit *commit;
219 prepare_revision_walk(rev);
220 while ((commit = get_revision(rev)) != NULL) {
221 const char *author = NULL, *oneline, *buffer;
222 int authorlen = authorlen, onelinelen;
224 /* get author and oneline */
225 for (buffer = commit->buffer; buffer && *buffer != '\0' &&
227 const char *eol = strchr(buffer, '\n');
230 eol = buffer + strlen(buffer);
234 if (!prefixcmp(buffer, "author ")) {
235 char *bracket = strchr(buffer, '<');
237 if (bracket == NULL || bracket > eol)
238 die("Invalid commit buffer: %s",
239 sha1_to_hex(commit->object.sha1));
241 if (map_email(bracket + 1, scratch,
244 authorlen = strlen(scratch);
246 if (bracket[-1] == ' ')
250 authorlen = bracket - buffer - 7;
257 die ("Missing author: %s",
258 sha1_to_hex(commit->object.sha1));
260 if (buffer == NULL || *buffer == '\0') {
262 onelinelen = sizeof(oneline) + 1;
266 oneline = buffer + 1;
267 eol = strchr(oneline, '\n');
269 onelinelen = strlen(oneline);
271 onelinelen = eol - oneline;
274 insert_author_oneline(list,
275 author, authorlen, oneline, onelinelen);
280 static int parse_uint(char const **arg, int comma)
286 ul = strtoul(*arg, &endp, 10);
287 if (endp != *arg && *endp && *endp != comma)
298 static const char wrap_arg_usage[] = "-w[<width>[,<indent1>[,<indent2>]]]";
299 #define DEFAULT_WRAPLEN 76
300 #define DEFAULT_INDENT1 6
301 #define DEFAULT_INDENT2 9
303 static void parse_wrap_args(const char *arg, int *in1, int *in2, int *wrap)
305 arg += 2; /* skip -w */
307 *wrap = parse_uint(&arg, ',');
310 *in1 = parse_uint(&arg, ',');
313 *in2 = parse_uint(&arg, '\0');
318 *wrap = DEFAULT_WRAPLEN;
320 *in1 = DEFAULT_INDENT1;
322 *in2 = DEFAULT_INDENT2;
324 ((*in1 && *wrap <= *in1) ||
325 (*in2 && *wrap <= *in2)))
329 int cmd_shortlog(int argc, const char **argv, const char *prefix)
332 struct path_list list = { NULL, 0, 0, 1 };
333 int i, j, sort_by_number = 0, summary = 0;
335 int wrap = DEFAULT_WRAPLEN;
336 int in1 = DEFAULT_INDENT1;
337 int in2 = DEFAULT_INDENT2;
339 /* since -n is a shadowed rev argument, parse our args first */
341 if (!strcmp(argv[1], "-n") || !strcmp(argv[1], "--numbered"))
343 else if (!strcmp(argv[1], "-s") ||
344 !strcmp(argv[1], "--summary"))
346 else if (!prefixcmp(argv[1], "-w")) {
348 parse_wrap_args(argv[1], &in1, &in2, &wrap);
350 else if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))
351 usage(shortlog_usage);
357 init_revisions(&rev, prefix);
358 argc = setup_revisions(argc, argv, &rev, NULL);
360 die ("unrecognized argument: %s", argv[1]);
362 if (!access(".mailmap", R_OK))
363 read_mailmap(".mailmap");
365 if (rev.pending.nr == 0) {
367 fprintf(stderr, "(reading log to summarize from standard input)\n");
368 read_from_stdin(&list);
371 get_from_rev(&rev, &list);
374 qsort(list.items, list.nr, sizeof(struct path_list_item),
377 for (i = 0; i < list.nr; i++) {
378 struct path_list *onelines = list.items[i].util;
381 printf("%s: %d\n", list.items[i].path, onelines->nr);
383 printf("%s (%d):\n", list.items[i].path, onelines->nr);
384 for (j = onelines->nr - 1; j >= 0; j--) {
385 const char *msg = onelines->items[j].path;
388 int col = print_wrapped_text(msg, in1, in2, wrap);
393 printf(" %s\n", msg);
398 onelines->strdup_paths = 1;
399 path_list_clear(onelines, 1);
401 list.items[i].util = NULL;
404 list.strdup_paths = 1;
405 path_list_clear(&list, 1);
406 mailmap.strdup_paths = 1;
407 path_list_clear(&mailmap, 1);