]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/shortlog.c
shortlog: use strbufs to read from stdin
[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[] = {
9c9b4f2f 13 N_("git shortlog [<options>] [<revision-range>] [[--] [<path>...]]"),
14ec9cbd
PH
14 NULL
15};
b8ec5923
JS
16
17static int compare_by_number(const void *a1, const void *a2)
18{
c455c87c
JS
19 const struct string_list_item *i1 = a1, *i2 = a2;
20 const struct string_list *l1 = i1->util, *l2 = i2->util;
b8ec5923
JS
21
22 if (l1->nr < l2->nr)
6d6ab610 23 return 1;
b8ec5923
JS
24 else if (l1->nr == l2->nr)
25 return 0;
26 else
6d6ab610 27 return -1;
b8ec5923
JS
28}
29
552bcac3 30static void insert_one_record(struct shortlog *log,
1e931cb4
JH
31 const char *author,
32 const char *oneline)
b8ec5923 33{
552bcac3 34 const char *dot3 = log->common_repo_prefix;
b8ec5923 35 char *buffer, *p;
c455c87c 36 struct string_list_item *item;
ea02ffa3
AP
37 const char *mailbuf, *namebuf;
38 size_t namelen, maillen;
1e931cb4 39 const char *eol;
cec08717 40 struct strbuf subject = STRBUF_INIT;
ea02ffa3 41 struct strbuf namemailbuf = STRBUF_INIT;
3c020bd5 42 struct ident_split ident;
1e931cb4 43
3c020bd5 44 if (split_ident_line(&ident, author, strlen(author)))
1e931cb4 45 return;
d20d654f 46
ea02ffa3
AP
47 namebuf = ident.name_begin;
48 mailbuf = ident.mail_begin;
49 namelen = ident.name_end - ident.name_begin;
50 maillen = ident.mail_end - ident.mail_begin;
d20d654f 51
ea02ffa3
AP
52 map_user(&log->mailmap, &mailbuf, &maillen, &namebuf, &namelen);
53 strbuf_add(&namemailbuf, namebuf, namelen);
d20d654f 54
ea02ffa3
AP
55 if (log->email)
56 strbuf_addf(&namemailbuf, " <%.*s>", (int)maillen, mailbuf);
b8ec5923 57
ea02ffa3 58 item = string_list_insert(&log->list, namemailbuf.buf);
b8ec5923 59 if (item->util == NULL)
c455c87c 60 item->util = xcalloc(1, sizeof(struct string_list));
b8ec5923 61
c1ce83a5
AW
62 /* Skip any leading whitespace, including any blank lines. */
63 while (*oneline && isspace(*oneline))
64 oneline++;
1e931cb4
JH
65 eol = strchr(oneline, '\n');
66 if (!eol)
67 eol = oneline + strlen(oneline);
59556548 68 if (starts_with(oneline, "[PATCH")) {
72019cde 69 char *eob = strchr(oneline, ']');
1e931cb4
JH
70 if (eob && (!eol || eob < eol))
71 oneline = eob + 1;
b8ec5923 72 }
1e931cb4 73 while (*oneline && isspace(*oneline) && *oneline != '\n')
b8ec5923 74 oneline++;
cec08717
RS
75 format_subject(&subject, oneline, " ");
76 buffer = strbuf_detach(&subject, NULL);
b8ec5923 77
c95044d4
JH
78 if (dot3) {
79 int dot3len = strlen(dot3);
80 if (dot3len > 5) {
81 while ((p = strstr(buffer, dot3)) != NULL) {
82 int taillen = strlen(p) - dot3len;
83 memcpy(p, "/.../", 5);
84 memmove(p + 5, p + dot3len, taillen + 1);
85 }
86 }
b8ec5923
JS
87 }
88
1d2f80fa 89 string_list_append(item->util, buffer);
b8ec5923
JS
90}
91
552bcac3 92static void read_from_stdin(struct shortlog *log)
b8ec5923 93{
50250491
JK
94 struct strbuf author = STRBUF_INIT;
95 struct strbuf oneline = STRBUF_INIT;
1e931cb4 96
50250491 97 while (strbuf_getline(&author, stdin, '\n') != EOF) {
5c3894c3 98 const char *v;
50250491
JK
99 if (!skip_prefix(author.buf, "Author: ", &v) &&
100 !skip_prefix(author.buf, "author ", &v))
1e931cb4 101 continue;
50250491
JK
102 while (strbuf_getline(&oneline, stdin, '\n') != EOF &&
103 oneline.len)
1e931cb4 104 ; /* discard headers */
50250491
JK
105 while (strbuf_getline(&oneline, stdin, '\n') != EOF &&
106 !oneline.len)
1e931cb4 107 ; /* discard blanks */
50250491 108 insert_one_record(log, v, oneline.buf);
b8ec5923 109 }
50250491
JK
110 strbuf_release(&author);
111 strbuf_release(&oneline);
b8ec5923
JS
112}
113
552bcac3 114void shortlog_add_commit(struct shortlog *log, struct commit *commit)
b8ec5923 115{
552bcac3 116 const char *author = NULL, *buffer;
79f7ca06
UKK
117 struct strbuf buf = STRBUF_INIT;
118 struct strbuf ufbuf = STRBUF_INIT;
b8ec5923 119
8b8a5374 120 pp_commit_easy(CMIT_FMT_RAW, commit, &buf);
79f7ca06 121 buffer = buf.buf;
552bcac3
DB
122 while (*buffer && *buffer != '\n') {
123 const char *eol = strchr(buffer, '\n');
b8ec5923 124
552bcac3
DB
125 if (eol == NULL)
126 eol = buffer + strlen(buffer);
127 else
128 eol++;
b8ec5923 129
59556548 130 if (starts_with(buffer, "author "))
552bcac3
DB
131 author = buffer + 7;
132 buffer = eol;
b8ec5923 133 }
cd4f09e3
JK
134 if (!author) {
135 warning(_("Missing author: %s"),
f2fd0760 136 oid_to_hex(&commit->object.oid));
cd4f09e3
JK
137 return;
138 }
b526f8ed 139 if (log->user_format) {
dd2e794a 140 struct pretty_print_context ctx = {0};
6bf13944 141 ctx.fmt = CMIT_FMT_USERFORMAT;
c1977021 142 ctx.abbrev = log->abbrev;
dd2e794a
TR
143 ctx.subject = "";
144 ctx.after_subject = "";
a5481a6c 145 ctx.date_mode.type = DATE_NORMAL;
ecaee805 146 ctx.output_encoding = get_log_output_encoding();
6bf13944 147 pretty_print_commit(&ctx, commit, &ufbuf);
79f7ca06
UKK
148 buffer = ufbuf.buf;
149 } else if (*buffer) {
552bcac3 150 buffer++;
79f7ca06 151 }
552bcac3 152 insert_one_record(log, author, !*buffer ? "<none>" : buffer);
79f7ca06
UKK
153 strbuf_release(&ufbuf);
154 strbuf_release(&buf);
552bcac3
DB
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))
ab8b53bb 162 die(_("revision walk setup failed"));
552bcac3
DB
163 while ((commit = get_revision(rev)) != NULL)
164 shortlog_add_commit(log, commit);
b8ec5923
JS
165}
166
14ec9cbd 167static int parse_uint(char const **arg, int comma, int defval)
3d711d97
JH
168{
169 unsigned long ul;
170 int ret;
171 char *endp;
172
173 ul = strtoul(*arg, &endp, 10);
14ec9cbd 174 if (*endp && *endp != comma)
3d711d97 175 return -1;
14ec9cbd 176 if (ul > INT_MAX)
3d711d97 177 return -1;
14ec9cbd
PH
178 ret = *arg == endp ? defval : (int)ul;
179 *arg = *endp ? endp + 1 : endp;
3d711d97
JH
180 return ret;
181}
182
183static const char wrap_arg_usage[] = "-w[<width>[,<indent1>[,<indent2>]]]";
184#define DEFAULT_WRAPLEN 76
185#define DEFAULT_INDENT1 6
186#define DEFAULT_INDENT2 9
187
14ec9cbd 188static int parse_wrap_args(const struct option *opt, const char *arg, int unset)
3d711d97 189{
14ec9cbd
PH
190 struct shortlog *log = opt->value;
191
192 log->wrap_lines = !unset;
193 if (unset)
194 return 0;
195 if (!arg) {
196 log->wrap = DEFAULT_WRAPLEN;
197 log->in1 = DEFAULT_INDENT1;
198 log->in2 = DEFAULT_INDENT2;
199 return 0;
200 }
201
202 log->wrap = parse_uint(&arg, ',', DEFAULT_WRAPLEN);
203 log->in1 = parse_uint(&arg, ',', DEFAULT_INDENT1);
204 log->in2 = parse_uint(&arg, '\0', DEFAULT_INDENT2);
205 if (log->wrap < 0 || log->in1 < 0 || log->in2 < 0)
206 return error(wrap_arg_usage);
207 if (log->wrap &&
208 ((log->in1 && log->wrap <= log->in1) ||
209 (log->in2 && log->wrap <= log->in2)))
210 return error(wrap_arg_usage);
211 return 0;
3d711d97
JH
212}
213
552bcac3
DB
214void shortlog_init(struct shortlog *log)
215{
216 memset(log, 0, sizeof(*log));
217
d551a488 218 read_mailmap(&log->mailmap, &log->common_repo_prefix);
552bcac3 219
c455c87c 220 log->list.strdup_strings = 1;
552bcac3
DB
221 log->wrap = DEFAULT_WRAPLEN;
222 log->in1 = DEFAULT_INDENT1;
223 log->in2 = DEFAULT_INDENT2;
224}
225
b8ec5923
JS
226int cmd_shortlog(int argc, const char **argv, const char *prefix)
227{
14ec9cbd
PH
228 static struct shortlog log;
229 static struct rev_info rev;
773b69bf 230 int nongit = !startup_info->have_repository;
552bcac3 231
14ec9cbd 232 static const struct option options[] = {
d5d09d47
SB
233 OPT_BOOL('n', "numbered", &log.sort_by_number,
234 N_("sort output according to the number of commits per author")),
235 OPT_BOOL('s', "summary", &log.summary,
236 N_("Suppress commit descriptions, only provides commit count")),
237 OPT_BOOL('e', "email", &log.email,
238 N_("Show the email address of each author")),
c48bd2c1
NTND
239 { OPTION_CALLBACK, 'w', NULL, &log, N_("w[,i1[,i2]]"),
240 N_("Linewrap output"), PARSE_OPT_OPTARG, &parse_wrap_args },
14ec9cbd
PH
241 OPT_END(),
242 };
243
244 struct parse_opt_ctx_t ctx;
245
d551a488 246 git_config(git_default_config, NULL);
552bcac3 247 shortlog_init(&log);
14ec9cbd 248 init_revisions(&rev, prefix);
9ca1169f
SB
249 parse_options_start(&ctx, argc, argv, prefix, options,
250 PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0);
14ec9cbd
PH
251
252 for (;;) {
14ec9cbd
PH
253 switch (parse_options_step(&ctx, options, shortlog_usage)) {
254 case PARSE_OPT_HELP:
255 exit(129);
256 case PARSE_OPT_DONE:
257 goto parse_done;
3d711d97 258 }
6b61ec05 259 parse_revision_opt(&rev, &ctx, options, shortlog_usage);
14ec9cbd
PH
260 }
261parse_done:
262 argc = parse_options_end(&ctx);
263
264 if (setup_revisions(argc, argv, &rev, NULL) != 1) {
ab8b53bb 265 error(_("unrecognized argument: %s"), argv[1]);
14ec9cbd 266 usage_with_options(shortlog_usage, options);
b8ec5923
JS
267 }
268
b526f8ed 269 log.user_format = rev.commit_format == CMIT_FMT_USERFORMAT;
c1977021 270 log.abbrev = rev.abbrev;
b526f8ed 271
3384a2df 272 /* assume HEAD if from a tty */
abe549e1 273 if (!nongit && !rev.pending.nr && isatty(0))
3384a2df 274 add_head_to_pending(&rev);
0497c620 275 if (rev.pending.nr == 0) {
37314495 276 if (isatty(0))
ab8b53bb 277 fprintf(stderr, _("(reading log message from standard input)\n"));
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
bb96a2c9
RS
287static void add_wrapped_shortlog_msg(struct strbuf *sb, const char *s,
288 const struct shortlog *log)
289{
5b597082
SP
290 strbuf_add_wrapped_text(sb, s, log->in1, log->in2, log->wrap);
291 strbuf_addch(sb, '\n');
bb96a2c9
RS
292}
293
552bcac3
DB
294void shortlog_output(struct shortlog *log)
295{
296 int i, j;
bb96a2c9
RS
297 struct strbuf sb = STRBUF_INIT;
298
552bcac3 299 if (log->sort_by_number)
c455c87c 300 qsort(log->list.items, log->list.nr, sizeof(struct string_list_item),
552bcac3
DB
301 compare_by_number);
302 for (i = 0; i < log->list.nr; i++) {
c455c87c 303 struct string_list *onelines = log->list.items[i].util;
b8ec5923 304
552bcac3 305 if (log->summary) {
c455c87c 306 printf("%6d\t%s\n", onelines->nr, log->list.items[i].string);
ac60c94d 307 } else {
c455c87c 308 printf("%s (%d):\n", log->list.items[i].string, onelines->nr);
3714e7c8 309 for (j = onelines->nr - 1; j >= 0; j--) {
c455c87c 310 const char *msg = onelines->items[j].string;
3d711d97 311
552bcac3 312 if (log->wrap_lines) {
bb96a2c9
RS
313 strbuf_reset(&sb);
314 add_wrapped_shortlog_msg(&sb, msg, log);
315 fwrite(sb.buf, sb.len, 1, stdout);
3d711d97
JH
316 }
317 else
318 printf(" %s\n", msg);
3714e7c8
JS
319 }
320 putchar('\n');
b8ec5923
JS
321 }
322
c455c87c 323 onelines->strdup_strings = 1;
fe73fc1a 324 string_list_clear(onelines, 0);
b8ec5923 325 free(onelines);
552bcac3 326 log->list.items[i].util = NULL;
b8ec5923
JS
327 }
328
bb96a2c9 329 strbuf_release(&sb);
c455c87c
JS
330 log->list.strdup_strings = 1;
331 string_list_clear(&log->list, 1);
d20d654f 332 clear_mailmap(&log->mailmap);
b8ec5923 333}