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