10 static const char shortlog_usage[] =
11 "git-shortlog [-n] [-s] [-e] [<commit-id>... ]";
13 static char *common_repo_prefix;
16 static int compare_by_number(const void *a1, const void *a2)
18 const struct path_list_item *i1 = a1, *i2 = a2;
19 const struct path_list *l1 = i1->util, *l2 = i2->util;
23 else if (l1->nr == l2->nr)
29 static struct path_list mailmap = {NULL, 0, 0, 0};
31 static void insert_one_record(struct path_list *list,
35 const char *dot3 = common_repo_prefix;
37 struct path_list_item *item;
38 struct path_list *onelines;
42 const char *boemail, *eoemail;
44 boemail = strchr(author, '<');
47 eoemail = strchr(boemail, '>');
50 if (!map_email(&mailmap, boemail+1, namebuf, sizeof(namebuf))) {
51 while (author < boemail && isspace(*author))
54 len < sizeof(namebuf) - 1 && author + len < boemail;
56 namebuf[len] = author[len];
57 while (0 < len && isspace(namebuf[len-1]))
62 len = strlen(namebuf);
65 size_t room = sizeof(namebuf) - len - 1;
66 int maillen = eoemail - boemail + 1;
67 snprintf(namebuf + len, room, " %.*s", maillen, boemail);
70 buffer = xstrdup(namebuf);
71 item = path_list_insert(buffer, list);
72 if (item->util == NULL)
73 item->util = xcalloc(1, sizeof(struct path_list));
77 eol = strchr(oneline, '\n');
79 eol = oneline + strlen(oneline);
80 while (*oneline && isspace(*oneline) && *oneline != '\n')
82 if (!prefixcmp(oneline, "[PATCH")) {
83 char *eob = strchr(oneline, ']');
84 if (eob && (!eol || eob < eol))
87 while (*oneline && isspace(*oneline) && *oneline != '\n')
90 while (len && isspace(oneline[len-1]))
92 buffer = xmemdupz(oneline, len);
95 int dot3len = strlen(dot3);
97 while ((p = strstr(buffer, dot3)) != NULL) {
98 int taillen = strlen(p) - dot3len;
99 memcpy(p, "/.../", 5);
100 memmove(p + 5, p + dot3len, taillen + 1);
105 onelines = item->util;
106 if (onelines->nr >= onelines->alloc) {
107 onelines->alloc = alloc_nr(onelines->nr);
108 onelines->items = xrealloc(onelines->items,
110 * sizeof(struct path_list_item));
113 onelines->items[onelines->nr].util = NULL;
114 onelines->items[onelines->nr++].path = buffer;
117 static void read_from_stdin(struct path_list *list)
119 char author[1024], oneline[1024];
121 while (fgets(author, sizeof(author), stdin) != NULL) {
122 if (!(author[0] == 'A' || author[0] == 'a') ||
123 prefixcmp(author + 1, "uthor: "))
125 while (fgets(oneline, sizeof(oneline), stdin) &&
127 ; /* discard headers */
128 while (fgets(oneline, sizeof(oneline), stdin) &&
130 ; /* discard blanks */
131 insert_one_record(list, author + 8, oneline);
135 static void get_from_rev(struct rev_info *rev, struct path_list *list)
137 struct commit *commit;
139 prepare_revision_walk(rev);
140 while ((commit = get_revision(rev)) != NULL) {
141 const char *author = NULL, *buffer;
143 buffer = commit->buffer;
144 while (*buffer && *buffer != '\n') {
145 const char *eol = strchr(buffer, '\n');
148 eol = buffer + strlen(buffer);
152 if (!prefixcmp(buffer, "author "))
157 die("Missing author: %s",
158 sha1_to_hex(commit->object.sha1));
161 insert_one_record(list, author, !*buffer ? "<none>" : buffer);
165 static int parse_uint(char const **arg, int comma)
171 ul = strtoul(*arg, &endp, 10);
172 if (endp != *arg && *endp && *endp != comma)
183 static const char wrap_arg_usage[] = "-w[<width>[,<indent1>[,<indent2>]]]";
184 #define DEFAULT_WRAPLEN 76
185 #define DEFAULT_INDENT1 6
186 #define DEFAULT_INDENT2 9
188 static void parse_wrap_args(const char *arg, int *in1, int *in2, int *wrap)
190 arg += 2; /* skip -w */
192 *wrap = parse_uint(&arg, ',');
195 *in1 = parse_uint(&arg, ',');
198 *in2 = parse_uint(&arg, '\0');
203 *wrap = DEFAULT_WRAPLEN;
205 *in1 = DEFAULT_INDENT1;
207 *in2 = DEFAULT_INDENT2;
209 ((*in1 && *wrap <= *in1) ||
210 (*in2 && *wrap <= *in2)))
214 int cmd_shortlog(int argc, const char **argv, const char *prefix)
217 struct path_list list = { NULL, 0, 0, 1 };
218 int i, j, sort_by_number = 0, summary = 0;
220 int wrap = DEFAULT_WRAPLEN;
221 int in1 = DEFAULT_INDENT1;
222 int in2 = DEFAULT_INDENT2;
224 /* since -n is a shadowed rev argument, parse our args first */
226 if (!strcmp(argv[1], "-n") || !strcmp(argv[1], "--numbered"))
228 else if (!strcmp(argv[1], "-s") ||
229 !strcmp(argv[1], "--summary"))
231 else if (!strcmp(argv[1], "-e") ||
232 !strcmp(argv[1], "--email"))
234 else if (!prefixcmp(argv[1], "-w")) {
236 parse_wrap_args(argv[1], &in1, &in2, &wrap);
238 else if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))
239 usage(shortlog_usage);
245 init_revisions(&rev, prefix);
246 argc = setup_revisions(argc, argv, &rev, NULL);
248 die ("unrecognized argument: %s", argv[1]);
250 read_mailmap(&mailmap, ".mailmap", &common_repo_prefix);
252 /* assume HEAD if from a tty */
253 if (!rev.pending.nr && isatty(0))
254 add_head_to_pending(&rev);
255 if (rev.pending.nr == 0) {
256 read_from_stdin(&list);
259 get_from_rev(&rev, &list);
262 qsort(list.items, list.nr, sizeof(struct path_list_item),
265 for (i = 0; i < list.nr; i++) {
266 struct path_list *onelines = list.items[i].util;
269 printf("%6d\t%s\n", onelines->nr, list.items[i].path);
271 printf("%s (%d):\n", list.items[i].path, onelines->nr);
272 for (j = onelines->nr - 1; j >= 0; j--) {
273 const char *msg = onelines->items[j].path;
276 int col = print_wrapped_text(msg, in1, in2, wrap);
281 printf(" %s\n", msg);
286 onelines->strdup_paths = 1;
287 path_list_clear(onelines, 1);
289 list.items[i].util = NULL;
292 list.strdup_paths = 1;
293 path_list_clear(&list, 1);
294 mailmap.strdup_paths = 1;
295 path_list_clear(&mailmap, 1);