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 --group=trailer with stdin is not supported"));
137 BUG("unhandled shortlog group");
140 while (strbuf_getline_lf(&ident
, stdin
) != EOF
) {
142 if (!skip_prefix(ident
.buf
, match
[0], &v
) &&
143 !skip_prefix(ident
.buf
, match
[1], &v
))
145 while (strbuf_getline_lf(&oneline
, stdin
) != EOF
&&
147 ; /* discard headers */
148 while (strbuf_getline_lf(&oneline
, stdin
) != EOF
&&
150 ; /* discard blanks */
152 strbuf_reset(&mapped_ident
);
153 if (parse_ident(log
, &mapped_ident
, v
) < 0)
156 insert_one_record(log
, mapped_ident
.buf
, oneline
.buf
);
158 strbuf_release(&ident
);
159 strbuf_release(&mapped_ident
);
160 strbuf_release(&oneline
);
163 static void insert_records_from_trailers(struct shortlog
*log
,
165 struct commit
*commit
,
166 struct pretty_print_context
*ctx
,
169 struct trailer_iterator iter
;
170 const char *commit_buffer
, *body
;
171 struct strbuf ident
= STRBUF_INIT
;
173 if (!log
->trailers
.nr
)
177 * Using format_commit_message("%B") would be simpler here, but
178 * this saves us copying the message.
180 commit_buffer
= logmsg_reencode(commit
, NULL
, ctx
->output_encoding
);
181 body
= strstr(commit_buffer
, "\n\n");
185 trailer_iterator_init(&iter
, body
);
186 while (trailer_iterator_advance(&iter
)) {
187 const char *value
= iter
.val
.buf
;
189 if (!string_list_has_string(&log
->trailers
, iter
.key
.buf
))
192 strbuf_reset(&ident
);
193 if (!parse_ident(log
, &ident
, value
))
196 if (!strset_add(dups
, value
))
198 insert_one_record(log
, value
, oneline
);
200 trailer_iterator_release(&iter
);
202 strbuf_release(&ident
);
203 unuse_commit_buffer(commit
, commit_buffer
);
206 void shortlog_add_commit(struct shortlog
*log
, struct commit
*commit
)
208 struct strbuf ident
= STRBUF_INIT
;
209 struct strbuf oneline
= STRBUF_INIT
;
210 struct strset dups
= STRSET_INIT
;
211 struct pretty_print_context ctx
= {0};
212 const char *oneline_str
;
214 ctx
.fmt
= CMIT_FMT_USERFORMAT
;
215 ctx
.abbrev
= log
->abbrev
;
216 ctx
.print_email_subject
= 1;
217 ctx
.date_mode
= log
->date_mode
;
218 ctx
.output_encoding
= get_log_output_encoding();
221 if (log
->user_format
)
222 pretty_print_commit(&ctx
, commit
, &oneline
);
224 format_commit_message(commit
, "%s", &oneline
, &ctx
);
226 oneline_str
= oneline
.len
? oneline
.buf
: "<none>";
228 if (log
->groups
& SHORTLOG_GROUP_AUTHOR
) {
229 strbuf_reset(&ident
);
230 format_commit_message(commit
,
231 log
->email
? "%aN <%aE>" : "%aN",
233 if (!HAS_MULTI_BITS(log
->groups
) ||
234 strset_add(&dups
, ident
.buf
))
235 insert_one_record(log
, ident
.buf
, oneline_str
);
237 if (log
->groups
& SHORTLOG_GROUP_COMMITTER
) {
238 strbuf_reset(&ident
);
239 format_commit_message(commit
,
240 log
->email
? "%cN <%cE>" : "%cN",
242 if (!HAS_MULTI_BITS(log
->groups
) ||
243 strset_add(&dups
, ident
.buf
))
244 insert_one_record(log
, ident
.buf
, oneline_str
);
246 insert_records_from_trailers(log
, &dups
, commit
, &ctx
, oneline_str
);
249 strbuf_release(&ident
);
250 strbuf_release(&oneline
);
253 static void get_from_rev(struct rev_info
*rev
, struct shortlog
*log
)
255 struct commit
*commit
;
257 if (prepare_revision_walk(rev
))
258 die(_("revision walk setup failed"));
259 while ((commit
= get_revision(rev
)) != NULL
)
260 shortlog_add_commit(log
, commit
);
263 static int parse_uint(char const **arg
, int comma
, int defval
)
269 ul
= strtoul(*arg
, &endp
, 10);
270 if (*endp
&& *endp
!= comma
)
274 ret
= *arg
== endp
? defval
: (int)ul
;
275 *arg
= *endp
? endp
+ 1 : endp
;
279 static const char wrap_arg_usage
[] = "-w[<width>[,<indent1>[,<indent2>]]]";
280 #define DEFAULT_WRAPLEN 76
281 #define DEFAULT_INDENT1 6
282 #define DEFAULT_INDENT2 9
284 static int parse_wrap_args(const struct option
*opt
, const char *arg
, int unset
)
286 struct shortlog
*log
= opt
->value
;
288 log
->wrap_lines
= !unset
;
292 log
->wrap
= DEFAULT_WRAPLEN
;
293 log
->in1
= DEFAULT_INDENT1
;
294 log
->in2
= DEFAULT_INDENT2
;
298 log
->wrap
= parse_uint(&arg
, ',', DEFAULT_WRAPLEN
);
299 log
->in1
= parse_uint(&arg
, ',', DEFAULT_INDENT1
);
300 log
->in2
= parse_uint(&arg
, '\0', DEFAULT_INDENT2
);
301 if (log
->wrap
< 0 || log
->in1
< 0 || log
->in2
< 0)
302 return error(wrap_arg_usage
);
304 ((log
->in1
&& log
->wrap
<= log
->in1
) ||
305 (log
->in2
&& log
->wrap
<= log
->in2
)))
306 return error(wrap_arg_usage
);
310 static int parse_group_option(const struct option
*opt
, const char *arg
, int unset
)
312 struct shortlog
*log
= opt
->value
;
317 string_list_clear(&log
->trailers
, 0);
318 } else if (!strcasecmp(arg
, "author"))
319 log
->groups
|= SHORTLOG_GROUP_AUTHOR
;
320 else if (!strcasecmp(arg
, "committer"))
321 log
->groups
|= SHORTLOG_GROUP_COMMITTER
;
322 else if (skip_prefix(arg
, "trailer:", &field
)) {
323 log
->groups
|= SHORTLOG_GROUP_TRAILER
;
324 string_list_append(&log
->trailers
, field
);
326 return error(_("unknown group type: %s"), arg
);
332 void shortlog_init(struct shortlog
*log
)
334 memset(log
, 0, sizeof(*log
));
336 read_mailmap(&log
->mailmap
);
338 log
->list
.strdup_strings
= 1;
339 log
->wrap
= DEFAULT_WRAPLEN
;
340 log
->in1
= DEFAULT_INDENT1
;
341 log
->in2
= DEFAULT_INDENT2
;
342 log
->trailers
.strdup_strings
= 1;
343 log
->trailers
.cmp
= strcasecmp
;
346 int cmd_shortlog(int argc
, const char **argv
, const char *prefix
)
348 struct shortlog log
= { STRING_LIST_INIT_NODUP
};
350 int nongit
= !startup_info
->have_repository
;
352 const struct option options
[] = {
353 OPT_BIT('c', "committer", &log
.groups
,
354 N_("group by committer rather than author"),
355 SHORTLOG_GROUP_COMMITTER
),
356 OPT_BOOL('n', "numbered", &log
.sort_by_number
,
357 N_("sort output according to the number of commits per author")),
358 OPT_BOOL('s', "summary", &log
.summary
,
359 N_("suppress commit descriptions, only provides commit count")),
360 OPT_BOOL('e', "email", &log
.email
,
361 N_("show the email address of each author")),
362 OPT_CALLBACK_F('w', NULL
, &log
, N_("<w>[,<i1>[,<i2>]]"),
363 N_("linewrap output"), PARSE_OPT_OPTARG
,
365 OPT_CALLBACK(0, "group", &log
, N_("field"),
366 N_("group by field"), parse_group_option
),
370 struct parse_opt_ctx_t ctx
;
372 git_config(git_default_config
, NULL
);
374 repo_init_revisions(the_repository
, &rev
, prefix
);
375 parse_options_start(&ctx
, argc
, argv
, prefix
, options
,
376 PARSE_OPT_KEEP_DASHDASH
| PARSE_OPT_KEEP_ARGV0
);
379 switch (parse_options_step(&ctx
, options
, shortlog_usage
)) {
380 case PARSE_OPT_NON_OPTION
:
381 case PARSE_OPT_UNKNOWN
:
384 case PARSE_OPT_ERROR
:
385 case PARSE_OPT_SUBCOMMAND
:
387 case PARSE_OPT_COMPLETE
:
392 parse_revision_opt(&rev
, &ctx
, options
, shortlog_usage
);
395 revision_opts_finish(&rev
);
396 argc
= parse_options_end(&ctx
);
398 if (nongit
&& argc
> 1) {
399 error(_("too many arguments given outside repository"));
400 usage_with_options(shortlog_usage
, options
);
403 if (setup_revisions(argc
, argv
, &rev
, NULL
) != 1) {
404 error(_("unrecognized argument: %s"), argv
[1]);
405 usage_with_options(shortlog_usage
, options
);
408 log
.user_format
= rev
.commit_format
== CMIT_FMT_USERFORMAT
;
409 log
.abbrev
= rev
.abbrev
;
410 log
.file
= rev
.diffopt
.file
;
411 log
.date_mode
= rev
.date_mode
;
414 log
.groups
= SHORTLOG_GROUP_AUTHOR
;
415 string_list_sort(&log
.trailers
);
417 /* assume HEAD if from a tty */
418 if (!nongit
&& !rev
.pending
.nr
&& isatty(0))
419 add_head_to_pending(&rev
);
420 if (rev
.pending
.nr
== 0) {
422 fprintf(stderr
, _("(reading log message from standard input)\n"));
423 read_from_stdin(&log
);
426 get_from_rev(&rev
, &log
);
428 release_revisions(&rev
);
430 shortlog_output(&log
);
431 if (log
.file
!= stdout
)
436 static void add_wrapped_shortlog_msg(struct strbuf
*sb
, const char *s
,
437 const struct shortlog
*log
)
439 strbuf_add_wrapped_text(sb
, s
, log
->in1
, log
->in2
, log
->wrap
);
440 strbuf_addch(sb
, '\n');
443 void shortlog_output(struct shortlog
*log
)
446 struct strbuf sb
= STRBUF_INIT
;
448 if (log
->sort_by_number
)
449 STABLE_QSORT(log
->list
.items
, log
->list
.nr
,
450 log
->summary
? compare_by_counter
: compare_by_list
);
451 for (i
= 0; i
< log
->list
.nr
; i
++) {
452 const struct string_list_item
*item
= &log
->list
.items
[i
];
454 fprintf(log
->file
, "%6d\t%s\n",
455 (int)UTIL_TO_INT(item
), item
->string
);
457 struct string_list
*onelines
= item
->util
;
458 fprintf(log
->file
, "%s (%"PRIuMAX
"):\n",
459 item
->string
, (uintmax_t)onelines
->nr
);
460 for (j
= onelines
->nr
; j
>= 1; j
--) {
461 const char *msg
= onelines
->items
[j
- 1].string
;
463 if (log
->wrap_lines
) {
465 add_wrapped_shortlog_msg(&sb
, msg
, log
);
466 fwrite(sb
.buf
, sb
.len
, 1, log
->file
);
469 fprintf(log
->file
, " %s\n", msg
);
471 putc('\n', log
->file
);
472 onelines
->strdup_strings
= 1;
473 string_list_clear(onelines
, 0);
477 log
->list
.items
[i
].util
= NULL
;
481 log
->list
.strdup_strings
= 1;
482 string_list_clear(&log
->list
, 1);
483 clear_mailmap(&log
->mailmap
);