10 static const char shortlog_usage[] =
11 "git-shortlog [-n] [-s] [<commit-id>... ]";
13 static char *common_repo_prefix;
15 static int compare_by_number(const void *a1, const void *a2)
17 const struct path_list_item *i1 = a1, *i2 = a2;
18 const struct path_list *l1 = i1->util, *l2 = i2->util;
22 else if (l1->nr == l2->nr)
28 static struct path_list mailmap = {NULL, 0, 0, 0};
30 static void insert_author_oneline(struct path_list *list,
31 const char *author, int authorlen,
32 const char *oneline, int onelinelen)
34 const char *dot3 = common_repo_prefix;
36 struct path_list_item *item;
37 struct path_list *onelines;
39 while (authorlen > 0 && isspace(author[authorlen - 1]))
42 buffer = xmemdupz(author, authorlen);
43 item = path_list_insert(buffer, list);
44 if (item->util == NULL)
45 item->util = xcalloc(1, sizeof(struct path_list));
49 if (!prefixcmp(oneline, "[PATCH")) {
50 char *eob = strchr(oneline, ']');
53 while (isspace(eob[1]) && eob[1] != '\n')
55 if (eob - oneline < onelinelen) {
56 onelinelen -= eob - oneline;
62 while (onelinelen > 0 && isspace(oneline[0])) {
66 while (onelinelen > 0 && isspace(oneline[onelinelen - 1]))
68 buffer = xmemdupz(oneline, onelinelen);
71 int dot3len = strlen(dot3);
73 while ((p = strstr(buffer, dot3)) != NULL) {
74 int taillen = strlen(p) - dot3len;
75 memcpy(p, "/.../", 5);
76 memmove(p + 5, p + dot3len, taillen + 1);
81 onelines = item->util;
82 if (onelines->nr >= onelines->alloc) {
83 onelines->alloc = alloc_nr(onelines->nr);
84 onelines->items = xrealloc(onelines->items,
86 * sizeof(struct path_list_item));
89 onelines->items[onelines->nr].util = NULL;
90 onelines->items[onelines->nr++].path = buffer;
93 static void read_from_stdin(struct path_list *list)
97 while (fgets(buffer, sizeof(buffer), stdin) != NULL) {
99 if ((buffer[0] == 'A' || buffer[0] == 'a') &&
100 !prefixcmp(buffer + 1, "uthor: ") &&
101 (bob = strchr(buffer + 7, '<')) != NULL) {
102 char buffer2[1024], offset = 0;
104 if (map_email(&mailmap, bob + 1, buffer, sizeof(buffer)))
105 bob = buffer + strlen(buffer);
108 while (buffer + offset < bob &&
113 while (fgets(buffer2, sizeof(buffer2), stdin) &&
116 if (fgets(buffer2, sizeof(buffer2), stdin)) {
117 int l2 = strlen(buffer2);
119 for (i = 0; i < l2; i++)
120 if (!isspace(buffer2[i]))
122 insert_author_oneline(list,
124 bob - buffer - offset,
125 buffer2 + i, l2 - i);
131 static void get_from_rev(struct rev_info *rev, struct path_list *list)
134 struct commit *commit;
136 prepare_revision_walk(rev);
137 while ((commit = get_revision(rev)) != NULL) {
138 const char *author = NULL, *oneline, *buffer;
139 int authorlen = authorlen, onelinelen;
141 /* get author and oneline */
142 for (buffer = commit->buffer; buffer && *buffer != '\0' &&
144 const char *eol = strchr(buffer, '\n');
147 eol = buffer + strlen(buffer);
151 if (!prefixcmp(buffer, "author ")) {
152 char *bracket = strchr(buffer, '<');
154 if (bracket == NULL || bracket > eol)
155 die("Invalid commit buffer: %s",
156 sha1_to_hex(commit->object.sha1));
158 if (map_email(&mailmap, bracket + 1, scratch,
161 authorlen = strlen(scratch);
163 if (bracket[-1] == ' ')
167 authorlen = bracket - buffer - 7;
174 die ("Missing author: %s",
175 sha1_to_hex(commit->object.sha1));
177 if (buffer == NULL || *buffer == '\0') {
179 onelinelen = sizeof(oneline) + 1;
183 oneline = buffer + 1;
184 eol = strchr(oneline, '\n');
186 onelinelen = strlen(oneline);
188 onelinelen = eol - oneline;
191 insert_author_oneline(list,
192 author, authorlen, oneline, onelinelen);
197 static int parse_uint(char const **arg, int comma)
203 ul = strtoul(*arg, &endp, 10);
204 if (endp != *arg && *endp && *endp != comma)
215 static const char wrap_arg_usage[] = "-w[<width>[,<indent1>[,<indent2>]]]";
216 #define DEFAULT_WRAPLEN 76
217 #define DEFAULT_INDENT1 6
218 #define DEFAULT_INDENT2 9
220 static void parse_wrap_args(const char *arg, int *in1, int *in2, int *wrap)
222 arg += 2; /* skip -w */
224 *wrap = parse_uint(&arg, ',');
227 *in1 = parse_uint(&arg, ',');
230 *in2 = parse_uint(&arg, '\0');
235 *wrap = DEFAULT_WRAPLEN;
237 *in1 = DEFAULT_INDENT1;
239 *in2 = DEFAULT_INDENT2;
241 ((*in1 && *wrap <= *in1) ||
242 (*in2 && *wrap <= *in2)))
246 int cmd_shortlog(int argc, const char **argv, const char *prefix)
249 struct path_list list = { NULL, 0, 0, 1 };
250 int i, j, sort_by_number = 0, summary = 0;
252 int wrap = DEFAULT_WRAPLEN;
253 int in1 = DEFAULT_INDENT1;
254 int in2 = DEFAULT_INDENT2;
256 /* since -n is a shadowed rev argument, parse our args first */
258 if (!strcmp(argv[1], "-n") || !strcmp(argv[1], "--numbered"))
260 else if (!strcmp(argv[1], "-s") ||
261 !strcmp(argv[1], "--summary"))
263 else if (!prefixcmp(argv[1], "-w")) {
265 parse_wrap_args(argv[1], &in1, &in2, &wrap);
267 else if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))
268 usage(shortlog_usage);
274 init_revisions(&rev, prefix);
275 argc = setup_revisions(argc, argv, &rev, NULL);
277 die ("unrecognized argument: %s", argv[1]);
279 read_mailmap(&mailmap, ".mailmap", &common_repo_prefix);
281 if (rev.pending.nr == 0) {
283 fprintf(stderr, "(reading log to summarize from standard input)\n");
284 read_from_stdin(&list);
287 get_from_rev(&rev, &list);
290 qsort(list.items, list.nr, sizeof(struct path_list_item),
293 for (i = 0; i < list.nr; i++) {
294 struct path_list *onelines = list.items[i].util;
297 printf("%s: %d\n", list.items[i].path, onelines->nr);
299 printf("%s (%d):\n", list.items[i].path, onelines->nr);
300 for (j = onelines->nr - 1; j >= 0; j--) {
301 const char *msg = onelines->items[j].path;
304 int col = print_wrapped_text(msg, in1, in2, wrap);
309 printf(" %s\n", msg);
314 onelines->strdup_paths = 1;
315 path_list_clear(onelines, 1);
317 list.items[i].util = NULL;
320 list.strdup_paths = 1;
321 path_list_clear(&list, 1);
322 mailmap.strdup_paths = 1;
323 path_list_clear(&mailmap, 1);