]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-shortlog.c
Fix test for cleanup failure in t7300 on Windows
[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
1e931cb4
JH
73 eol = strchr(oneline, '\n');
74 if (!eol)
75 eol = oneline + strlen(oneline);
76 while (*oneline && isspace(*oneline) && *oneline != '\n')
77 oneline++;
cc44c765 78 if (!prefixcmp(oneline, "[PATCH")) {
72019cde 79 char *eob = strchr(oneline, ']');
1e931cb4
JH
80 if (eob && (!eol || eob < eol))
81 oneline = eob + 1;
b8ec5923 82 }
1e931cb4 83 while (*oneline && isspace(*oneline) && *oneline != '\n')
b8ec5923 84 oneline++;
1e931cb4
JH
85 len = eol - oneline;
86 while (len && isspace(oneline[len-1]))
87 len--;
88 buffer = xmemdupz(oneline, len);
b8ec5923 89
c95044d4
JH
90 if (dot3) {
91 int dot3len = strlen(dot3);
92 if (dot3len > 5) {
93 while ((p = strstr(buffer, dot3)) != NULL) {
94 int taillen = strlen(p) - dot3len;
95 memcpy(p, "/.../", 5);
96 memmove(p + 5, p + dot3len, taillen + 1);
97 }
98 }
b8ec5923
JS
99 }
100
b8ec5923
JS
101 onelines = item->util;
102 if (onelines->nr >= onelines->alloc) {
103 onelines->alloc = alloc_nr(onelines->nr);
104 onelines->items = xrealloc(onelines->items,
105 onelines->alloc
106 * sizeof(struct path_list_item));
107 }
108
109 onelines->items[onelines->nr].util = NULL;
110 onelines->items[onelines->nr++].path = buffer;
111}
112
552bcac3 113static void read_from_stdin(struct shortlog *log)
b8ec5923 114{
1e931cb4
JH
115 char author[1024], oneline[1024];
116
117 while (fgets(author, sizeof(author), stdin) != NULL) {
118 if (!(author[0] == 'A' || author[0] == 'a') ||
119 prefixcmp(author + 1, "uthor: "))
120 continue;
121 while (fgets(oneline, sizeof(oneline), stdin) &&
122 oneline[0] != '\n')
123 ; /* discard headers */
124 while (fgets(oneline, sizeof(oneline), stdin) &&
125 oneline[0] == '\n')
126 ; /* discard blanks */
552bcac3 127 insert_one_record(log, author + 8, oneline);
b8ec5923
JS
128 }
129}
130
552bcac3 131void shortlog_add_commit(struct shortlog *log, struct commit *commit)
b8ec5923 132{
552bcac3 133 const char *author = NULL, *buffer;
b8ec5923 134
552bcac3
DB
135 buffer = commit->buffer;
136 while (*buffer && *buffer != '\n') {
137 const char *eol = strchr(buffer, '\n');
b8ec5923 138
552bcac3
DB
139 if (eol == NULL)
140 eol = buffer + strlen(buffer);
141 else
142 eol++;
b8ec5923 143
552bcac3
DB
144 if (!prefixcmp(buffer, "author "))
145 author = buffer + 7;
146 buffer = eol;
b8ec5923 147 }
552bcac3
DB
148 if (!author)
149 die("Missing author: %s",
150 sha1_to_hex(commit->object.sha1));
151 if (*buffer)
152 buffer++;
153 insert_one_record(log, author, !*buffer ? "<none>" : buffer);
154}
155
156static void get_from_rev(struct rev_info *rev, struct shortlog *log)
157{
158 struct commit *commit;
159
160 if (prepare_revision_walk(rev))
161 die("revision walk setup failed");
162 while ((commit = get_revision(rev)) != NULL)
163 shortlog_add_commit(log, commit);
b8ec5923
JS
164}
165
3d711d97
JH
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
552bcac3
DB
215void shortlog_init(struct shortlog *log)
216{
217 memset(log, 0, sizeof(*log));
218
219 read_mailmap(&log->mailmap, ".mailmap", &log->common_repo_prefix);
220
221 log->list.strdup_paths = 1;
222 log->wrap = DEFAULT_WRAPLEN;
223 log->in1 = DEFAULT_INDENT1;
224 log->in2 = DEFAULT_INDENT2;
225}
226
b8ec5923
JS
227int cmd_shortlog(int argc, const char **argv, const char *prefix)
228{
552bcac3 229 struct shortlog log;
b8ec5923 230 struct rev_info rev;
552bcac3
DB
231
232 shortlog_init(&log);
b8ec5923 233
6d6ab610 234 /* since -n is a shadowed rev argument, parse our args first */
b8ec5923
JS
235 while (argc > 1) {
236 if (!strcmp(argv[1], "-n") || !strcmp(argv[1], "--numbered"))
552bcac3 237 log.sort_by_number = 1;
b8ec5923
JS
238 else if (!strcmp(argv[1], "-s") ||
239 !strcmp(argv[1], "--summary"))
552bcac3 240 log.summary = 1;
4602c17d
JH
241 else if (!strcmp(argv[1], "-e") ||
242 !strcmp(argv[1], "--email"))
552bcac3 243 log.email = 1;
3d711d97 244 else if (!prefixcmp(argv[1], "-w")) {
552bcac3
DB
245 log.wrap_lines = 1;
246 parse_wrap_args(argv[1], &log.in1, &log.in2, &log.wrap);
3d711d97 247 }
b8ec5923
JS
248 else if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))
249 usage(shortlog_usage);
250 else
6d6ab610 251 break;
b8ec5923
JS
252 argv++;
253 argc--;
254 }
6d6ab610
JS
255 init_revisions(&rev, prefix);
256 argc = setup_revisions(argc, argv, &rev, NULL);
257 if (argc > 1)
258 die ("unrecognized argument: %s", argv[1]);
b8ec5923 259
3384a2df
JH
260 /* assume HEAD if from a tty */
261 if (!rev.pending.nr && isatty(0))
262 add_head_to_pending(&rev);
0497c620 263 if (rev.pending.nr == 0) {
552bcac3 264 read_from_stdin(&log);
0497c620 265 }
b8ec5923 266 else
552bcac3 267 get_from_rev(&rev, &log);
b8ec5923 268
552bcac3
DB
269 shortlog_output(&log);
270 return 0;
271}
b8ec5923 272
552bcac3
DB
273void shortlog_output(struct shortlog *log)
274{
275 int i, j;
276 if (log->sort_by_number)
277 qsort(log->list.items, log->list.nr, sizeof(struct path_list_item),
278 compare_by_number);
279 for (i = 0; i < log->list.nr; i++) {
280 struct path_list *onelines = log->list.items[i].util;
b8ec5923 281
552bcac3
DB
282 if (log->summary) {
283 printf("%6d\t%s\n", onelines->nr, log->list.items[i].path);
ac60c94d 284 } else {
552bcac3 285 printf("%s (%d):\n", log->list.items[i].path, onelines->nr);
3714e7c8 286 for (j = onelines->nr - 1; j >= 0; j--) {
3d711d97
JH
287 const char *msg = onelines->items[j].path;
288
552bcac3
DB
289 if (log->wrap_lines) {
290 int col = print_wrapped_text(msg, log->in1, log->in2, log->wrap);
291 if (col != log->wrap)
3d711d97
JH
292 putchar('\n');
293 }
294 else
295 printf(" %s\n", msg);
3714e7c8
JS
296 }
297 putchar('\n');
b8ec5923
JS
298 }
299
300 onelines->strdup_paths = 1;
301 path_list_clear(onelines, 1);
302 free(onelines);
552bcac3 303 log->list.items[i].util = NULL;
b8ec5923
JS
304 }
305
552bcac3
DB
306 log->list.strdup_paths = 1;
307 path_list_clear(&log->list, 1);
308 log->mailmap.strdup_paths = 1;
309 path_list_clear(&log->mailmap, 1);
b8ec5923 310}