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"
7#include "utf8.h"
8#include "mailmap.h"
9
10static const char shortlog_usage[] =
11"git-shortlog [-n] [-s] [-e] [<commit-id>... ]";
12
13static char *common_repo_prefix;
14static int email;
15
16static int compare_by_number(const void *a1, const void *a2)
17{
18 const struct path_list_item *i1 = a1, *i2 = a2;
19 const struct path_list *l1 = i1->util, *l2 = i2->util;
20
21 if (l1->nr < l2->nr)
22 return 1;
23 else if (l1->nr == l2->nr)
24 return 0;
25 else
26 return -1;
27}
28
29static struct path_list mailmap = {NULL, 0, 0, 0};
30
31static void insert_one_record(struct path_list *list,
32 const char *author,
33 const char *oneline)
34{
35 const char *dot3 = common_repo_prefix;
36 char *buffer, *p;
37 struct path_list_item *item;
38 struct path_list *onelines;
39 char namebuf[1024];
40 size_t len;
41 const char *eol;
42 const char *boemail, *eoemail;
43
44 boemail = strchr(author, '<');
45 if (!boemail)
46 return;
47 eoemail = strchr(boemail, '>');
48 if (!eoemail)
49 return;
50 if (!map_email(&mailmap, boemail+1, namebuf, sizeof(namebuf))) {
51 while (author < boemail && isspace(*author))
52 author++;
53 for (len = 0;
54 len < sizeof(namebuf) - 1 && author + len < boemail;
55 len++)
56 namebuf[len] = author[len];
57 while (0 < len && isspace(namebuf[len-1]))
58 len--;
59 namebuf[len] = '\0';
60 }
61 else
62 len = strlen(namebuf);
63
64 if (email) {
65 size_t room = sizeof(namebuf) - len - 1;
66 int maillen = eoemail - boemail + 1;
67 snprintf(namebuf + len, room, " %.*s", maillen, boemail);
68 }
69
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));
74 else
75 free(buffer);
76
77 /* Skip any leading whitespace, including any blank lines. */
78 while (*oneline && isspace(*oneline))
79 oneline++;
80 eol = strchr(oneline, '\n');
81 if (!eol)
82 eol = oneline + strlen(oneline);
83 if (!prefixcmp(oneline, "[PATCH")) {
84 char *eob = strchr(oneline, ']');
85 if (eob && (!eol || eob < eol))
86 oneline = eob + 1;
87 }
88 while (*oneline && isspace(*oneline) && *oneline != '\n')
89 oneline++;
90 len = eol - oneline;
91 while (len && isspace(oneline[len-1]))
92 len--;
93 buffer = xmemdupz(oneline, len);
94
95 if (dot3) {
96 int dot3len = strlen(dot3);
97 if (dot3len > 5) {
98 while ((p = strstr(buffer, dot3)) != NULL) {
99 int taillen = strlen(p) - dot3len;
100 memcpy(p, "/.../", 5);
101 memmove(p + 5, p + dot3len, taillen + 1);
102 }
103 }
104 }
105
106 onelines = item->util;
107 if (onelines->nr >= onelines->alloc) {
108 onelines->alloc = alloc_nr(onelines->nr);
109 onelines->items = xrealloc(onelines->items,
110 onelines->alloc
111 * sizeof(struct path_list_item));
112 }
113
114 onelines->items[onelines->nr].util = NULL;
115 onelines->items[onelines->nr++].path = buffer;
116}
117
118static void read_from_stdin(struct path_list *list)
119{
120 char author[1024], oneline[1024];
121
122 while (fgets(author, sizeof(author), stdin) != NULL) {
123 if (!(author[0] == 'A' || author[0] == 'a') ||
124 prefixcmp(author + 1, "uthor: "))
125 continue;
126 while (fgets(oneline, sizeof(oneline), stdin) &&
127 oneline[0] != '\n')
128 ; /* discard headers */
129 while (fgets(oneline, sizeof(oneline), stdin) &&
130 oneline[0] == '\n')
131 ; /* discard blanks */
132 insert_one_record(list, author + 8, oneline);
133 }
134}
135
136static void get_from_rev(struct rev_info *rev, struct path_list *list)
137{
138 struct commit *commit;
139
140 prepare_revision_walk(rev);
141 while ((commit = get_revision(rev)) != NULL) {
142 const char *author = NULL, *buffer;
143
144 buffer = commit->buffer;
145 while (*buffer && *buffer != '\n') {
146 const char *eol = strchr(buffer, '\n');
147
148 if (eol == NULL)
149 eol = buffer + strlen(buffer);
150 else
151 eol++;
152
153 if (!prefixcmp(buffer, "author "))
154 author = buffer + 7;
155 buffer = eol;
156 }
157 if (!author)
158 die("Missing author: %s",
159 sha1_to_hex(commit->object.sha1));
160 if (*buffer)
161 buffer++;
162 insert_one_record(list, author, !*buffer ? "<none>" : buffer);
163 }
164}
165
166static int parse_uint(char const **arg, int comma)
167{
168 unsigned long ul;
169 int ret;
170 char *endp;
171
172 ul = strtoul(*arg, &endp, 10);
173 if (endp != *arg && *endp && *endp != comma)
174 return -1;
175 ret = (int) ul;
176 if (ret != ul)
177 return -1;
178 *arg = endp;
179 if (**arg)
180 (*arg)++;
181 return ret;
182}
183
184static const char wrap_arg_usage[] = "-w[<width>[,<indent1>[,<indent2>]]]";
185#define DEFAULT_WRAPLEN 76
186#define DEFAULT_INDENT1 6
187#define DEFAULT_INDENT2 9
188
189static void parse_wrap_args(const char *arg, int *in1, int *in2, int *wrap)
190{
191 arg += 2; /* skip -w */
192
193 *wrap = parse_uint(&arg, ',');
194 if (*wrap < 0)
195 die(wrap_arg_usage);
196 *in1 = parse_uint(&arg, ',');
197 if (*in1 < 0)
198 die(wrap_arg_usage);
199 *in2 = parse_uint(&arg, '\0');
200 if (*in2 < 0)
201 die(wrap_arg_usage);
202
203 if (!*wrap)
204 *wrap = DEFAULT_WRAPLEN;
205 if (!*in1)
206 *in1 = DEFAULT_INDENT1;
207 if (!*in2)
208 *in2 = DEFAULT_INDENT2;
209 if (*wrap &&
210 ((*in1 && *wrap <= *in1) ||
211 (*in2 && *wrap <= *in2)))
212 die(wrap_arg_usage);
213}
214
215int cmd_shortlog(int argc, const char **argv, const char *prefix)
216{
217 struct rev_info rev;
218 struct path_list list = { NULL, 0, 0, 1 };
219 int i, j, sort_by_number = 0, summary = 0;
220 int wrap_lines = 0;
221 int wrap = DEFAULT_WRAPLEN;
222 int in1 = DEFAULT_INDENT1;
223 int in2 = DEFAULT_INDENT2;
224
225 /* since -n is a shadowed rev argument, parse our args first */
226 while (argc > 1) {
227 if (!strcmp(argv[1], "-n") || !strcmp(argv[1], "--numbered"))
228 sort_by_number = 1;
229 else if (!strcmp(argv[1], "-s") ||
230 !strcmp(argv[1], "--summary"))
231 summary = 1;
232 else if (!strcmp(argv[1], "-e") ||
233 !strcmp(argv[1], "--email"))
234 email = 1;
235 else if (!prefixcmp(argv[1], "-w")) {
236 wrap_lines = 1;
237 parse_wrap_args(argv[1], &in1, &in2, &wrap);
238 }
239 else if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))
240 usage(shortlog_usage);
241 else
242 break;
243 argv++;
244 argc--;
245 }
246 init_revisions(&rev, prefix);
247 argc = setup_revisions(argc, argv, &rev, NULL);
248 if (argc > 1)
249 die ("unrecognized argument: %s", argv[1]);
250
251 read_mailmap(&mailmap, ".mailmap", &common_repo_prefix);
252
253 /* assume HEAD if from a tty */
254 if (!rev.pending.nr && isatty(0))
255 add_head_to_pending(&rev);
256 if (rev.pending.nr == 0) {
257 read_from_stdin(&list);
258 }
259 else
260 get_from_rev(&rev, &list);
261
262 if (sort_by_number)
263 qsort(list.items, list.nr, sizeof(struct path_list_item),
264 compare_by_number);
265
266 for (i = 0; i < list.nr; i++) {
267 struct path_list *onelines = list.items[i].util;
268
269 if (summary) {
270 printf("%6d\t%s\n", onelines->nr, list.items[i].path);
271 } else {
272 printf("%s (%d):\n", list.items[i].path, onelines->nr);
273 for (j = onelines->nr - 1; j >= 0; j--) {
274 const char *msg = onelines->items[j].path;
275
276 if (wrap_lines) {
277 int col = print_wrapped_text(msg, in1, in2, wrap);
278 if (col != wrap)
279 putchar('\n');
280 }
281 else
282 printf(" %s\n", msg);
283 }
284 putchar('\n');
285 }
286
287 onelines->strdup_paths = 1;
288 path_list_clear(onelines, 1);
289 free(onelines);
290 list.items[i].util = NULL;
291 }
292
293 list.strdup_paths = 1;
294 path_list_clear(&list, 1);
295 mailmap.strdup_paths = 1;
296 path_list_clear(&mailmap, 1);
297
298 return 0;
299}