6 #include "string-list.h"
11 #include "parse-options.h"
15 static char const * const shortlog_usage
[] = {
16 N_("git shortlog [<options>] [<revision-range>] [[--] <path>...]"),
17 N_("git log --pretty=short | git shortlog [<options>]"),
22 * The util field of our string_list_items will contain one of two things:
24 * - if --summary is not in use, it will point to a string list of the
25 * oneline subjects assigned to this author
27 * - if --summary is in use, we don't need that list; we only need to know
28 * its size. So we abuse the pointer slot to store our integer counter.
30 * This macro accesses the latter.
32 #define UTIL_TO_INT(x) ((intptr_t)(x)->util)
34 static int compare_by_counter(const void *a1
, const void *a2
)
36 const struct string_list_item
*i1
= a1
, *i2
= a2
;
37 return UTIL_TO_INT(i2
) - UTIL_TO_INT(i1
);
40 static int compare_by_list(const void *a1
, const void *a2
)
42 const struct string_list_item
*i1
= a1
, *i2
= a2
;
43 const struct string_list
*l1
= i1
->util
, *l2
= i2
->util
;
47 else if (l1
->nr
== l2
->nr
)
53 static void insert_one_record(struct shortlog
*log
,
57 struct string_list_item
*item
;
59 item
= string_list_insert(&log
->list
, ident
);
62 item
->util
= (void *)(UTIL_TO_INT(item
) + 1);
65 struct strbuf subject
= STRBUF_INIT
;
68 /* Skip any leading whitespace, including any blank lines. */
69 while (*oneline
&& isspace(*oneline
))
71 eol
= strchr(oneline
, '\n');
73 eol
= oneline
+ strlen(oneline
);
74 if (starts_with(oneline
, "[PATCH")) {
75 char *eob
= strchr(oneline
, ']');
76 if (eob
&& (!eol
|| eob
< eol
))
79 while (*oneline
&& isspace(*oneline
) && *oneline
!= '\n')
81 format_subject(&subject
, oneline
, " ");
82 buffer
= strbuf_detach(&subject
, NULL
);
85 item
->util
= xmalloc(sizeof(struct string_list
));
86 string_list_init_nodup(item
->util
);
88 string_list_append(item
->util
, buffer
);
92 static int parse_ident(struct shortlog
*log
,
93 struct strbuf
*out
, const char *in
)
95 const char *mailbuf
, *namebuf
;
96 size_t namelen
, maillen
;
97 struct ident_split ident
;
99 if (split_ident_line(&ident
, in
, strlen(in
)))
102 namebuf
= ident
.name_begin
;
103 mailbuf
= ident
.mail_begin
;
104 namelen
= ident
.name_end
- ident
.name_begin
;
105 maillen
= ident
.mail_end
- ident
.mail_begin
;
107 map_user(&log
->mailmap
, &mailbuf
, &maillen
, &namebuf
, &namelen
);
108 strbuf_add(out
, namebuf
, namelen
);
110 strbuf_addf(out
, " <%.*s>", (int)maillen
, mailbuf
);
115 static void read_from_stdin(struct shortlog
*log
)
117 struct strbuf ident
= STRBUF_INIT
;
118 struct strbuf mapped_ident
= STRBUF_INIT
;
119 struct strbuf oneline
= STRBUF_INIT
;
120 static const char *author_match
[2] = { "Author: ", "author " };
121 static const char *committer_match
[2] = { "Commit: ", "committer " };
124 if (HAS_MULTI_BITS(log
->groups
))
125 die(_("using multiple --group options with stdin is not supported"));
127 switch (log
->groups
) {
128 case SHORTLOG_GROUP_AUTHOR
:
129 match
= author_match
;
131 case SHORTLOG_GROUP_COMMITTER
:
132 match
= committer_match
;
134 case SHORTLOG_GROUP_TRAILER
:
135 die(_("using %s with stdin is not supported"), "--group=trailer");
136 case SHORTLOG_GROUP_FORMAT
:
137 die(_("using %s with stdin is not supported"), "--group=format");
139 BUG("unhandled shortlog group");
142 while (strbuf_getline_lf(&ident
, stdin
) != EOF
) {
144 if (!skip_prefix(ident
.buf
, match
[0], &v
) &&
145 !skip_prefix(ident
.buf
, match
[1], &v
))
147 while (strbuf_getline_lf(&oneline
, stdin
) != EOF
&&
149 ; /* discard headers */
150 while (strbuf_getline_lf(&oneline
, stdin
) != EOF
&&
152 ; /* discard blanks */
154 strbuf_reset(&mapped_ident
);
155 if (parse_ident(log
, &mapped_ident
, v
) < 0)
158 insert_one_record(log
, mapped_ident
.buf
, oneline
.buf
);
160 strbuf_release(&ident
);
161 strbuf_release(&mapped_ident
);
162 strbuf_release(&oneline
);
165 static void insert_records_from_trailers(struct shortlog
*log
,
167 struct commit
*commit
,
168 struct pretty_print_context
*ctx
,
171 struct trailer_iterator iter
;
172 const char *commit_buffer
, *body
;
173 struct strbuf ident
= STRBUF_INIT
;
175 if (!log
->trailers
.nr
)
179 * Using format_commit_message("%B") would be simpler here, but
180 * this saves us copying the message.
182 commit_buffer
= logmsg_reencode(commit
, NULL
, ctx
->output_encoding
);
183 body
= strstr(commit_buffer
, "\n\n");
187 trailer_iterator_init(&iter
, body
);
188 while (trailer_iterator_advance(&iter
)) {
189 const char *value
= iter
.val
.buf
;
191 if (!string_list_has_string(&log
->trailers
, iter
.key
.buf
))
194 strbuf_reset(&ident
);
195 if (!parse_ident(log
, &ident
, value
))
198 if (!strset_add(dups
, value
))
200 insert_one_record(log
, value
, oneline
);
202 trailer_iterator_release(&iter
);
204 strbuf_release(&ident
);
205 unuse_commit_buffer(commit
, commit_buffer
);
208 static int shortlog_needs_dedup(const struct shortlog
*log
)
210 return HAS_MULTI_BITS(log
->groups
) || log
->format
.nr
> 1 || log
->trailers
.nr
;
213 static void insert_records_from_format(struct shortlog
*log
,
215 struct commit
*commit
,
216 struct pretty_print_context
*ctx
,
219 struct strbuf buf
= STRBUF_INIT
;
220 struct string_list_item
*item
;
222 for_each_string_list_item(item
, &log
->format
) {
225 format_commit_message(commit
, item
->string
, &buf
, ctx
);
227 if (!shortlog_needs_dedup(log
) || strset_add(dups
, buf
.buf
))
228 insert_one_record(log
, buf
.buf
, oneline
);
231 strbuf_release(&buf
);
234 void shortlog_add_commit(struct shortlog
*log
, struct commit
*commit
)
236 struct strbuf ident
= STRBUF_INIT
;
237 struct strbuf oneline
= STRBUF_INIT
;
238 struct strset dups
= STRSET_INIT
;
239 struct pretty_print_context ctx
= {0};
240 const char *oneline_str
;
242 ctx
.fmt
= CMIT_FMT_USERFORMAT
;
243 ctx
.abbrev
= log
->abbrev
;
244 ctx
.print_email_subject
= 1;
245 ctx
.date_mode
= log
->date_mode
;
246 ctx
.output_encoding
= get_log_output_encoding();
249 if (log
->user_format
)
250 pretty_print_commit(&ctx
, commit
, &oneline
);
252 format_commit_message(commit
, "%s", &oneline
, &ctx
);
254 oneline_str
= oneline
.len
? oneline
.buf
: "<none>";
256 if (log
->groups
& SHORTLOG_GROUP_AUTHOR
) {
257 strbuf_reset(&ident
);
258 format_commit_message(commit
,
259 log
->email
? "%aN <%aE>" : "%aN",
261 if (!HAS_MULTI_BITS(log
->groups
) ||
262 strset_add(&dups
, ident
.buf
))
263 insert_one_record(log
, ident
.buf
, oneline_str
);
265 if (log
->groups
& SHORTLOG_GROUP_COMMITTER
) {
266 strbuf_reset(&ident
);
267 format_commit_message(commit
,
268 log
->email
? "%cN <%cE>" : "%cN",
270 if (!HAS_MULTI_BITS(log
->groups
) ||
271 strset_add(&dups
, ident
.buf
))
272 insert_one_record(log
, ident
.buf
, oneline_str
);
274 insert_records_from_trailers(log
, &dups
, commit
, &ctx
, oneline_str
);
275 insert_records_from_format(log
, &dups
, commit
, &ctx
, oneline_str
);
278 strbuf_release(&ident
);
279 strbuf_release(&oneline
);
282 static void get_from_rev(struct rev_info
*rev
, struct shortlog
*log
)
284 struct commit
*commit
;
286 if (prepare_revision_walk(rev
))
287 die(_("revision walk setup failed"));
288 while ((commit
= get_revision(rev
)) != NULL
)
289 shortlog_add_commit(log
, commit
);
292 static int parse_uint(char const **arg
, int comma
, int defval
)
298 ul
= strtoul(*arg
, &endp
, 10);
299 if (*endp
&& *endp
!= comma
)
303 ret
= *arg
== endp
? defval
: (int)ul
;
304 *arg
= *endp
? endp
+ 1 : endp
;
308 static const char wrap_arg_usage
[] = "-w[<width>[,<indent1>[,<indent2>]]]";
309 #define DEFAULT_WRAPLEN 76
310 #define DEFAULT_INDENT1 6
311 #define DEFAULT_INDENT2 9
313 static int parse_wrap_args(const struct option
*opt
, const char *arg
, int unset
)
315 struct shortlog
*log
= opt
->value
;
317 log
->wrap_lines
= !unset
;
321 log
->wrap
= DEFAULT_WRAPLEN
;
322 log
->in1
= DEFAULT_INDENT1
;
323 log
->in2
= DEFAULT_INDENT2
;
327 log
->wrap
= parse_uint(&arg
, ',', DEFAULT_WRAPLEN
);
328 log
->in1
= parse_uint(&arg
, ',', DEFAULT_INDENT1
);
329 log
->in2
= parse_uint(&arg
, '\0', DEFAULT_INDENT2
);
330 if (log
->wrap
< 0 || log
->in1
< 0 || log
->in2
< 0)
331 return error(wrap_arg_usage
);
333 ((log
->in1
&& log
->wrap
<= log
->in1
) ||
334 (log
->in2
&& log
->wrap
<= log
->in2
)))
335 return error(wrap_arg_usage
);
339 static int parse_group_option(const struct option
*opt
, const char *arg
, int unset
)
341 struct shortlog
*log
= opt
->value
;
346 string_list_clear(&log
->trailers
, 0);
347 string_list_clear(&log
->format
, 0);
348 } else if (!strcasecmp(arg
, "author"))
349 log
->groups
|= SHORTLOG_GROUP_AUTHOR
;
350 else if (!strcasecmp(arg
, "committer"))
351 log
->groups
|= SHORTLOG_GROUP_COMMITTER
;
352 else if (skip_prefix(arg
, "trailer:", &field
)) {
353 log
->groups
|= SHORTLOG_GROUP_TRAILER
;
354 string_list_append(&log
->trailers
, field
);
355 } else if (skip_prefix(arg
, "format:", &field
)) {
356 log
->groups
|= SHORTLOG_GROUP_FORMAT
;
357 string_list_append(&log
->format
, field
);
358 } else if (strchr(arg
, '%')) {
359 log
->groups
|= SHORTLOG_GROUP_FORMAT
;
360 string_list_append(&log
->format
, arg
);
362 return error(_("unknown group type: %s"), arg
);
369 void shortlog_init(struct shortlog
*log
)
371 memset(log
, 0, sizeof(*log
));
373 read_mailmap(&log
->mailmap
);
375 log
->list
.strdup_strings
= 1;
376 log
->wrap
= DEFAULT_WRAPLEN
;
377 log
->in1
= DEFAULT_INDENT1
;
378 log
->in2
= DEFAULT_INDENT2
;
379 log
->trailers
.strdup_strings
= 1;
380 log
->trailers
.cmp
= strcasecmp
;
381 log
->format
.strdup_strings
= 1;
384 int cmd_shortlog(int argc
, const char **argv
, const char *prefix
)
386 struct shortlog log
= { STRING_LIST_INIT_NODUP
};
388 int nongit
= !startup_info
->have_repository
;
390 const struct option options
[] = {
391 OPT_BIT('c', "committer", &log
.groups
,
392 N_("group by committer rather than author"),
393 SHORTLOG_GROUP_COMMITTER
),
394 OPT_BOOL('n', "numbered", &log
.sort_by_number
,
395 N_("sort output according to the number of commits per author")),
396 OPT_BOOL('s', "summary", &log
.summary
,
397 N_("suppress commit descriptions, only provides commit count")),
398 OPT_BOOL('e', "email", &log
.email
,
399 N_("show the email address of each author")),
400 OPT_CALLBACK_F('w', NULL
, &log
, N_("<w>[,<i1>[,<i2>]]"),
401 N_("linewrap output"), PARSE_OPT_OPTARG
,
403 OPT_CALLBACK(0, "group", &log
, N_("field"),
404 N_("group by field"), parse_group_option
),
408 struct parse_opt_ctx_t ctx
;
410 git_config(git_default_config
, NULL
);
412 repo_init_revisions(the_repository
, &rev
, prefix
);
413 parse_options_start(&ctx
, argc
, argv
, prefix
, options
,
414 PARSE_OPT_KEEP_DASHDASH
| PARSE_OPT_KEEP_ARGV0
);
417 switch (parse_options_step(&ctx
, options
, shortlog_usage
)) {
418 case PARSE_OPT_NON_OPTION
:
419 case PARSE_OPT_UNKNOWN
:
422 case PARSE_OPT_ERROR
:
423 case PARSE_OPT_SUBCOMMAND
:
425 case PARSE_OPT_COMPLETE
:
430 parse_revision_opt(&rev
, &ctx
, options
, shortlog_usage
);
433 revision_opts_finish(&rev
);
434 argc
= parse_options_end(&ctx
);
436 if (nongit
&& argc
> 1) {
437 error(_("too many arguments given outside repository"));
438 usage_with_options(shortlog_usage
, options
);
441 if (setup_revisions(argc
, argv
, &rev
, NULL
) != 1) {
442 error(_("unrecognized argument: %s"), argv
[1]);
443 usage_with_options(shortlog_usage
, options
);
446 log
.user_format
= rev
.commit_format
== CMIT_FMT_USERFORMAT
;
447 log
.abbrev
= rev
.abbrev
;
448 log
.file
= rev
.diffopt
.file
;
449 log
.date_mode
= rev
.date_mode
;
452 log
.groups
= SHORTLOG_GROUP_AUTHOR
;
453 string_list_sort(&log
.trailers
);
455 /* assume HEAD if from a tty */
456 if (!nongit
&& !rev
.pending
.nr
&& isatty(0))
457 add_head_to_pending(&rev
);
458 if (rev
.pending
.nr
== 0) {
460 fprintf(stderr
, _("(reading log message from standard input)\n"));
461 read_from_stdin(&log
);
464 get_from_rev(&rev
, &log
);
466 release_revisions(&rev
);
468 shortlog_output(&log
);
469 if (log
.file
!= stdout
)
474 static void add_wrapped_shortlog_msg(struct strbuf
*sb
, const char *s
,
475 const struct shortlog
*log
)
477 strbuf_add_wrapped_text(sb
, s
, log
->in1
, log
->in2
, log
->wrap
);
478 strbuf_addch(sb
, '\n');
481 void shortlog_output(struct shortlog
*log
)
484 struct strbuf sb
= STRBUF_INIT
;
486 if (log
->sort_by_number
)
487 STABLE_QSORT(log
->list
.items
, log
->list
.nr
,
488 log
->summary
? compare_by_counter
: compare_by_list
);
489 for (i
= 0; i
< log
->list
.nr
; i
++) {
490 const struct string_list_item
*item
= &log
->list
.items
[i
];
492 fprintf(log
->file
, "%6d\t%s\n",
493 (int)UTIL_TO_INT(item
), item
->string
);
495 struct string_list
*onelines
= item
->util
;
496 fprintf(log
->file
, "%s (%"PRIuMAX
"):\n",
497 item
->string
, (uintmax_t)onelines
->nr
);
498 for (j
= onelines
->nr
; j
>= 1; j
--) {
499 const char *msg
= onelines
->items
[j
- 1].string
;
501 if (log
->wrap_lines
) {
503 add_wrapped_shortlog_msg(&sb
, msg
, log
);
504 fwrite(sb
.buf
, sb
.len
, 1, log
->file
);
507 fprintf(log
->file
, " %s\n", msg
);
509 putc('\n', log
->file
);
510 onelines
->strdup_strings
= 1;
511 string_list_clear(onelines
, 0);
515 log
->list
.items
[i
].util
= NULL
;
519 log
->list
.strdup_strings
= 1;
520 string_list_clear(&log
->list
, 1);
521 clear_mailmap(&log
->mailmap
);
522 string_list_clear(&log
->format
, 0);