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