6 #include "string-list.h"
11 #include "parse-options.h"
13 static char const * const shortlog_usage[] = {
14 N_("git shortlog [<options>] [<revision-range>] [[--] <path>...]"),
15 N_("git log --pretty=short | git shortlog [<options>]"),
20 * The util field of our string_list_items will contain one of two things:
22 * - if --summary is not in use, it will point to a string list of the
23 * oneline subjects assigned to this author
25 * - if --summary is in use, we don't need that list; we only need to know
26 * its size. So we abuse the pointer slot to store our integer counter.
28 * This macro accesses the latter.
30 #define UTIL_TO_INT(x) ((intptr_t)(x)->util)
32 static int compare_by_counter(const void *a1, const void *a2)
34 const struct string_list_item *i1 = a1, *i2 = a2;
35 return UTIL_TO_INT(i2) - UTIL_TO_INT(i1);
38 static int compare_by_list(const void *a1, const void *a2)
40 const struct string_list_item *i1 = a1, *i2 = a2;
41 const struct string_list *l1 = i1->util, *l2 = i2->util;
45 else if (l1->nr == l2->nr)
51 static void insert_one_record(struct shortlog *log,
55 struct string_list_item *item;
57 item = string_list_insert(&log->list, author);
60 item->util = (void *)(UTIL_TO_INT(item) + 1);
62 const char *dot3 = log->common_repo_prefix;
64 struct strbuf subject = STRBUF_INIT;
67 /* Skip any leading whitespace, including any blank lines. */
68 while (*oneline && isspace(*oneline))
70 eol = strchr(oneline, '\n');
72 eol = oneline + strlen(oneline);
73 if (starts_with(oneline, "[PATCH")) {
74 char *eob = strchr(oneline, ']');
75 if (eob && (!eol || eob < eol))
78 while (*oneline && isspace(*oneline) && *oneline != '\n')
80 format_subject(&subject, oneline, " ");
81 buffer = strbuf_detach(&subject, NULL);
84 int dot3len = strlen(dot3);
86 while ((p = strstr(buffer, dot3)) != NULL) {
87 int taillen = strlen(p) - dot3len;
88 memcpy(p, "/.../", 5);
89 memmove(p + 5, p + dot3len, taillen + 1);
94 if (item->util == NULL)
95 item->util = xcalloc(1, sizeof(struct string_list));
96 string_list_append(item->util, buffer);
100 static int parse_stdin_author(struct shortlog *log,
101 struct strbuf *out, const char *in)
103 const char *mailbuf, *namebuf;
104 size_t namelen, maillen;
105 struct ident_split ident;
107 if (split_ident_line(&ident, in, strlen(in)))
110 namebuf = ident.name_begin;
111 mailbuf = ident.mail_begin;
112 namelen = ident.name_end - ident.name_begin;
113 maillen = ident.mail_end - ident.mail_begin;
115 map_user(&log->mailmap, &mailbuf, &maillen, &namebuf, &namelen);
116 strbuf_add(out, namebuf, namelen);
118 strbuf_addf(out, " <%.*s>", (int)maillen, mailbuf);
123 static void read_from_stdin(struct shortlog *log)
125 struct strbuf author = STRBUF_INIT;
126 struct strbuf mapped_author = STRBUF_INIT;
127 struct strbuf oneline = STRBUF_INIT;
128 static const char *author_match[2] = { "Author: ", "author " };
129 static const char *committer_match[2] = { "Commit: ", "committer " };
132 match = log->committer ? committer_match : author_match;
133 while (strbuf_getline_lf(&author, stdin) != EOF) {
135 if (!skip_prefix(author.buf, match[0], &v) &&
136 !skip_prefix(author.buf, match[1], &v))
138 while (strbuf_getline_lf(&oneline, stdin) != EOF &&
140 ; /* discard headers */
141 while (strbuf_getline_lf(&oneline, stdin) != EOF &&
143 ; /* discard blanks */
145 strbuf_reset(&mapped_author);
146 if (parse_stdin_author(log, &mapped_author, v) < 0)
149 insert_one_record(log, mapped_author.buf, oneline.buf);
151 strbuf_release(&author);
152 strbuf_release(&mapped_author);
153 strbuf_release(&oneline);
156 void shortlog_add_commit(struct shortlog *log, struct commit *commit)
158 struct strbuf author = STRBUF_INIT;
159 struct strbuf oneline = STRBUF_INIT;
160 struct pretty_print_context ctx = {0};
163 ctx.fmt = CMIT_FMT_USERFORMAT;
164 ctx.abbrev = log->abbrev;
165 ctx.print_email_subject = 1;
166 ctx.date_mode.type = DATE_NORMAL;
167 ctx.output_encoding = get_log_output_encoding();
169 fmt = log->committer ?
170 (log->email ? "%cN <%cE>" : "%cN") :
171 (log->email ? "%aN <%aE>" : "%aN");
173 format_commit_message(commit, fmt, &author, &ctx);
175 if (log->user_format)
176 pretty_print_commit(&ctx, commit, &oneline);
178 format_commit_message(commit, "%s", &oneline, &ctx);
181 insert_one_record(log, author.buf, oneline.len ? oneline.buf : "<none>");
183 strbuf_release(&author);
184 strbuf_release(&oneline);
187 static void get_from_rev(struct rev_info *rev, struct shortlog *log)
189 struct commit *commit;
191 if (prepare_revision_walk(rev))
192 die(_("revision walk setup failed"));
193 while ((commit = get_revision(rev)) != NULL)
194 shortlog_add_commit(log, commit);
197 static int parse_uint(char const **arg, int comma, int defval)
203 ul = strtoul(*arg, &endp, 10);
204 if (*endp && *endp != comma)
208 ret = *arg == endp ? defval : (int)ul;
209 *arg = *endp ? endp + 1 : endp;
213 static const char wrap_arg_usage[] = "-w[<width>[,<indent1>[,<indent2>]]]";
214 #define DEFAULT_WRAPLEN 76
215 #define DEFAULT_INDENT1 6
216 #define DEFAULT_INDENT2 9
218 static int parse_wrap_args(const struct option *opt, const char *arg, int unset)
220 struct shortlog *log = opt->value;
222 log->wrap_lines = !unset;
226 log->wrap = DEFAULT_WRAPLEN;
227 log->in1 = DEFAULT_INDENT1;
228 log->in2 = DEFAULT_INDENT2;
232 log->wrap = parse_uint(&arg, ',', DEFAULT_WRAPLEN);
233 log->in1 = parse_uint(&arg, ',', DEFAULT_INDENT1);
234 log->in2 = parse_uint(&arg, '\0', DEFAULT_INDENT2);
235 if (log->wrap < 0 || log->in1 < 0 || log->in2 < 0)
236 return error(wrap_arg_usage);
238 ((log->in1 && log->wrap <= log->in1) ||
239 (log->in2 && log->wrap <= log->in2)))
240 return error(wrap_arg_usage);
244 void shortlog_init(struct shortlog *log)
246 memset(log, 0, sizeof(*log));
248 read_mailmap(&log->mailmap, &log->common_repo_prefix);
250 log->list.strdup_strings = 1;
251 log->wrap = DEFAULT_WRAPLEN;
252 log->in1 = DEFAULT_INDENT1;
253 log->in2 = DEFAULT_INDENT2;
256 int cmd_shortlog(int argc, const char **argv, const char *prefix)
258 struct shortlog log = { STRING_LIST_INIT_NODUP };
260 int nongit = !startup_info->have_repository;
262 const struct option options[] = {
263 OPT_BOOL('c', "committer", &log.committer,
264 N_("Group by committer rather than author")),
265 OPT_BOOL('n', "numbered", &log.sort_by_number,
266 N_("sort output according to the number of commits per author")),
267 OPT_BOOL('s', "summary", &log.summary,
268 N_("Suppress commit descriptions, only provides commit count")),
269 OPT_BOOL('e', "email", &log.email,
270 N_("Show the email address of each author")),
271 OPT_CALLBACK_F('w', NULL, &log, N_("<w>[,<i1>[,<i2>]]"),
272 N_("Linewrap output"), PARSE_OPT_OPTARG,
277 struct parse_opt_ctx_t ctx;
279 git_config(git_default_config, NULL);
281 repo_init_revisions(the_repository, &rev, prefix);
282 parse_options_start(&ctx, argc, argv, prefix, options,
283 PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0);
286 switch (parse_options_step(&ctx, options, shortlog_usage)) {
288 case PARSE_OPT_ERROR:
290 case PARSE_OPT_COMPLETE:
295 parse_revision_opt(&rev, &ctx, options, shortlog_usage);
298 argc = parse_options_end(&ctx);
300 if (nongit && argc > 1) {
301 error(_("too many arguments given outside repository"));
302 usage_with_options(shortlog_usage, options);
305 if (setup_revisions(argc, argv, &rev, NULL) != 1) {
306 error(_("unrecognized argument: %s"), argv[1]);
307 usage_with_options(shortlog_usage, options);
310 log.user_format = rev.commit_format == CMIT_FMT_USERFORMAT;
311 log.abbrev = rev.abbrev;
312 log.file = rev.diffopt.file;
314 /* assume HEAD if from a tty */
315 if (!nongit && !rev.pending.nr && isatty(0))
316 add_head_to_pending(&rev);
317 if (rev.pending.nr == 0) {
319 fprintf(stderr, _("(reading log message from standard input)\n"));
320 read_from_stdin(&log);
323 get_from_rev(&rev, &log);
325 shortlog_output(&log);
326 if (log.file != stdout)
331 static void add_wrapped_shortlog_msg(struct strbuf *sb, const char *s,
332 const struct shortlog *log)
334 strbuf_add_wrapped_text(sb, s, log->in1, log->in2, log->wrap);
335 strbuf_addch(sb, '\n');
338 void shortlog_output(struct shortlog *log)
341 struct strbuf sb = STRBUF_INIT;
343 if (log->sort_by_number)
344 QSORT(log->list.items, log->list.nr,
345 log->summary ? compare_by_counter : compare_by_list);
346 for (i = 0; i < log->list.nr; i++) {
347 const struct string_list_item *item = &log->list.items[i];
349 fprintf(log->file, "%6d\t%s\n",
350 (int)UTIL_TO_INT(item), item->string);
352 struct string_list *onelines = item->util;
353 fprintf(log->file, "%s (%d):\n",
354 item->string, onelines->nr);
355 for (j = onelines->nr - 1; j >= 0; j--) {
356 const char *msg = onelines->items[j].string;
358 if (log->wrap_lines) {
360 add_wrapped_shortlog_msg(&sb, msg, log);
361 fwrite(sb.buf, sb.len, 1, log->file);
364 fprintf(log->file, " %s\n", msg);
366 putc('\n', log->file);
367 onelines->strdup_strings = 1;
368 string_list_clear(onelines, 0);
372 log->list.items[i].util = NULL;
376 log->list.strdup_strings = 1;
377 string_list_clear(&log->list, 1);
378 clear_mailmap(&log->mailmap);