5 #include "string-list.h"
10 #include "parse-options.h"
12 static char const * const shortlog_usage[] = {
13 N_("git shortlog [<options>] [<revision-range>] [[--] [<path>...]]"),
18 * The util field of our string_list_items will contain one of two things:
20 * - if --summary is not in use, it will point to a string list of the
21 * oneline subjects assigned to this author
23 * - if --summary is in use, we don't need that list; we only need to know
24 * its size. So we abuse the pointer slot to store our integer counter.
26 * This macro accesses the latter.
28 #define UTIL_TO_INT(x) ((intptr_t)(x)->util)
30 static int compare_by_counter(const void *a1, const void *a2)
32 const struct string_list_item *i1 = a1, *i2 = a2;
33 return UTIL_TO_INT(i2) - UTIL_TO_INT(i1);
36 static int compare_by_list(const void *a1, const void *a2)
38 const struct string_list_item *i1 = a1, *i2 = a2;
39 const struct string_list *l1 = i1->util, *l2 = i2->util;
43 else if (l1->nr == l2->nr)
49 static void insert_one_record(struct shortlog *log,
53 struct string_list_item *item;
54 const char *mailbuf, *namebuf;
55 size_t namelen, maillen;
56 struct strbuf namemailbuf = STRBUF_INIT;
57 struct ident_split ident;
59 if (split_ident_line(&ident, author, strlen(author)))
62 namebuf = ident.name_begin;
63 mailbuf = ident.mail_begin;
64 namelen = ident.name_end - ident.name_begin;
65 maillen = ident.mail_end - ident.mail_begin;
67 map_user(&log->mailmap, &mailbuf, &maillen, &namebuf, &namelen);
68 strbuf_add(&namemailbuf, namebuf, namelen);
71 strbuf_addf(&namemailbuf, " <%.*s>", (int)maillen, mailbuf);
73 item = string_list_insert(&log->list, namemailbuf.buf);
76 item->util = (void *)(UTIL_TO_INT(item) + 1);
78 const char *dot3 = log->common_repo_prefix;
80 struct strbuf subject = STRBUF_INIT;
83 /* Skip any leading whitespace, including any blank lines. */
84 while (*oneline && isspace(*oneline))
86 eol = strchr(oneline, '\n');
88 eol = oneline + strlen(oneline);
89 if (starts_with(oneline, "[PATCH")) {
90 char *eob = strchr(oneline, ']');
91 if (eob && (!eol || eob < eol))
94 while (*oneline && isspace(*oneline) && *oneline != '\n')
96 format_subject(&subject, oneline, " ");
97 buffer = strbuf_detach(&subject, NULL);
100 int dot3len = strlen(dot3);
102 while ((p = strstr(buffer, dot3)) != NULL) {
103 int taillen = strlen(p) - dot3len;
104 memcpy(p, "/.../", 5);
105 memmove(p + 5, p + dot3len, taillen + 1);
110 if (item->util == NULL)
111 item->util = xcalloc(1, sizeof(struct string_list));
112 string_list_append(item->util, buffer);
116 static void read_from_stdin(struct shortlog *log)
118 struct strbuf author = STRBUF_INIT;
119 struct strbuf oneline = STRBUF_INIT;
121 while (strbuf_getline_lf(&author, stdin) != EOF) {
123 if (!skip_prefix(author.buf, "Author: ", &v) &&
124 !skip_prefix(author.buf, "author ", &v))
126 while (strbuf_getline_lf(&oneline, stdin) != EOF &&
128 ; /* discard headers */
129 while (strbuf_getline_lf(&oneline, stdin) != EOF &&
131 ; /* discard blanks */
132 insert_one_record(log, v, oneline.buf);
134 strbuf_release(&author);
135 strbuf_release(&oneline);
138 void shortlog_add_commit(struct shortlog *log, struct commit *commit)
140 struct strbuf author = STRBUF_INIT;
141 struct strbuf oneline = STRBUF_INIT;
142 struct pretty_print_context ctx = {0};
144 ctx.fmt = CMIT_FMT_USERFORMAT;
145 ctx.abbrev = log->abbrev;
147 ctx.after_subject = "";
148 ctx.date_mode.type = DATE_NORMAL;
149 ctx.output_encoding = get_log_output_encoding();
151 format_commit_message(commit, "%an <%ae>", &author, &ctx);
153 if (log->user_format)
154 pretty_print_commit(&ctx, commit, &oneline);
156 format_commit_message(commit, "%s", &oneline, &ctx);
159 insert_one_record(log, author.buf, oneline.len ? oneline.buf : "<none>");
161 strbuf_release(&author);
162 strbuf_release(&oneline);
165 static void get_from_rev(struct rev_info *rev, struct shortlog *log)
167 struct commit *commit;
169 if (prepare_revision_walk(rev))
170 die(_("revision walk setup failed"));
171 while ((commit = get_revision(rev)) != NULL)
172 shortlog_add_commit(log, commit);
175 static int parse_uint(char const **arg, int comma, int defval)
181 ul = strtoul(*arg, &endp, 10);
182 if (*endp && *endp != comma)
186 ret = *arg == endp ? defval : (int)ul;
187 *arg = *endp ? endp + 1 : endp;
191 static const char wrap_arg_usage[] = "-w[<width>[,<indent1>[,<indent2>]]]";
192 #define DEFAULT_WRAPLEN 76
193 #define DEFAULT_INDENT1 6
194 #define DEFAULT_INDENT2 9
196 static int parse_wrap_args(const struct option *opt, const char *arg, int unset)
198 struct shortlog *log = opt->value;
200 log->wrap_lines = !unset;
204 log->wrap = DEFAULT_WRAPLEN;
205 log->in1 = DEFAULT_INDENT1;
206 log->in2 = DEFAULT_INDENT2;
210 log->wrap = parse_uint(&arg, ',', DEFAULT_WRAPLEN);
211 log->in1 = parse_uint(&arg, ',', DEFAULT_INDENT1);
212 log->in2 = parse_uint(&arg, '\0', DEFAULT_INDENT2);
213 if (log->wrap < 0 || log->in1 < 0 || log->in2 < 0)
214 return error(wrap_arg_usage);
216 ((log->in1 && log->wrap <= log->in1) ||
217 (log->in2 && log->wrap <= log->in2)))
218 return error(wrap_arg_usage);
222 void shortlog_init(struct shortlog *log)
224 memset(log, 0, sizeof(*log));
226 read_mailmap(&log->mailmap, &log->common_repo_prefix);
228 log->list.strdup_strings = 1;
229 log->wrap = DEFAULT_WRAPLEN;
230 log->in1 = DEFAULT_INDENT1;
231 log->in2 = DEFAULT_INDENT2;
234 int cmd_shortlog(int argc, const char **argv, const char *prefix)
236 struct shortlog log = { STRING_LIST_INIT_NODUP };
238 int nongit = !startup_info->have_repository;
240 const struct option options[] = {
241 OPT_BOOL('n', "numbered", &log.sort_by_number,
242 N_("sort output according to the number of commits per author")),
243 OPT_BOOL('s', "summary", &log.summary,
244 N_("Suppress commit descriptions, only provides commit count")),
245 OPT_BOOL('e', "email", &log.email,
246 N_("Show the email address of each author")),
247 { OPTION_CALLBACK, 'w', NULL, &log, N_("w[,i1[,i2]]"),
248 N_("Linewrap output"), PARSE_OPT_OPTARG, &parse_wrap_args },
252 struct parse_opt_ctx_t ctx;
254 git_config(git_default_config, NULL);
256 init_revisions(&rev, prefix);
257 parse_options_start(&ctx, argc, argv, prefix, options,
258 PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0);
261 switch (parse_options_step(&ctx, options, shortlog_usage)) {
267 parse_revision_opt(&rev, &ctx, options, shortlog_usage);
270 argc = parse_options_end(&ctx);
272 if (setup_revisions(argc, argv, &rev, NULL) != 1) {
273 error(_("unrecognized argument: %s"), argv[1]);
274 usage_with_options(shortlog_usage, options);
277 log.user_format = rev.commit_format == CMIT_FMT_USERFORMAT;
278 log.abbrev = rev.abbrev;
280 /* assume HEAD if from a tty */
281 if (!nongit && !rev.pending.nr && isatty(0))
282 add_head_to_pending(&rev);
283 if (rev.pending.nr == 0) {
285 fprintf(stderr, _("(reading log message from standard input)\n"));
286 read_from_stdin(&log);
289 get_from_rev(&rev, &log);
291 shortlog_output(&log);
295 static void add_wrapped_shortlog_msg(struct strbuf *sb, const char *s,
296 const struct shortlog *log)
298 strbuf_add_wrapped_text(sb, s, log->in1, log->in2, log->wrap);
299 strbuf_addch(sb, '\n');
302 void shortlog_output(struct shortlog *log)
305 struct strbuf sb = STRBUF_INIT;
307 if (log->sort_by_number)
308 qsort(log->list.items, log->list.nr, sizeof(struct string_list_item),
309 log->summary ? compare_by_counter : compare_by_list);
310 for (i = 0; i < log->list.nr; i++) {
311 const struct string_list_item *item = &log->list.items[i];
313 printf("%6d\t%s\n", (int)UTIL_TO_INT(item), item->string);
315 struct string_list *onelines = item->util;
316 printf("%s (%d):\n", item->string, onelines->nr);
317 for (j = onelines->nr - 1; j >= 0; j--) {
318 const char *msg = onelines->items[j].string;
320 if (log->wrap_lines) {
322 add_wrapped_shortlog_msg(&sb, msg, log);
323 fwrite(sb.buf, sb.len, 1, stdout);
326 printf(" %s\n", msg);
329 onelines->strdup_strings = 1;
330 string_list_clear(onelines, 0);
334 log->list.items[i].util = NULL;
338 log->list.strdup_strings = 1;
339 string_list_clear(&log->list, 1);
340 clear_mailmap(&log->mailmap);