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