]>
git.ipfire.org Git - thirdparty/git.git/blob - builtin/shortlog.c
7 #include "string-list.h"
12 #include "parse-options.h"
16 static char const * const shortlog_usage
[] = {
17 N_("git shortlog [<options>] [<revision-range>] [[--] <path>...]"),
18 N_("git log --pretty=short | git shortlog [<options>]"),
23 * The util field of our string_list_items will contain one of two things:
25 * - if --summary is not in use, it will point to a string list of the
26 * oneline subjects assigned to this author
28 * - if --summary is in use, we don't need that list; we only need to know
29 * its size. So we abuse the pointer slot to store our integer counter.
31 * This macro accesses the latter.
33 #define UTIL_TO_INT(x) ((intptr_t)(x)->util)
35 static int compare_by_counter(const void *a1
, const void *a2
)
37 const struct string_list_item
*i1
= a1
, *i2
= a2
;
38 return UTIL_TO_INT(i2
) - UTIL_TO_INT(i1
);
41 static int compare_by_list(const void *a1
, const void *a2
)
43 const struct string_list_item
*i1
= a1
, *i2
= a2
;
44 const struct string_list
*l1
= i1
->util
, *l2
= i2
->util
;
48 else if (l1
->nr
== l2
->nr
)
54 static void insert_one_record(struct shortlog
*log
,
58 struct string_list_item
*item
;
60 item
= string_list_insert(&log
->list
, ident
);
63 item
->util
= (void *)(UTIL_TO_INT(item
) + 1);
66 struct strbuf subject
= STRBUF_INIT
;
69 /* Skip any leading whitespace, including any blank lines. */
70 while (*oneline
&& isspace(*oneline
))
72 eol
= strchr(oneline
, '\n');
74 eol
= oneline
+ strlen(oneline
);
75 if (starts_with(oneline
, "[PATCH")) {
76 char *eob
= strchr(oneline
, ']');
77 if (eob
&& (!eol
|| eob
< eol
))
80 while (*oneline
&& isspace(*oneline
) && *oneline
!= '\n')
82 format_subject(&subject
, oneline
, " ");
83 buffer
= strbuf_detach(&subject
, NULL
);
86 item
->util
= xmalloc(sizeof(struct string_list
));
87 string_list_init_nodup(item
->util
);
89 string_list_append(item
->util
, buffer
);
93 static int parse_ident(struct shortlog
*log
,
94 struct strbuf
*out
, const char *in
)
96 const char *mailbuf
, *namebuf
;
97 size_t namelen
, maillen
;
98 struct ident_split ident
;
100 if (split_ident_line(&ident
, in
, strlen(in
)))
103 namebuf
= ident
.name_begin
;
104 mailbuf
= ident
.mail_begin
;
105 namelen
= ident
.name_end
- ident
.name_begin
;
106 maillen
= ident
.mail_end
- ident
.mail_begin
;
108 map_user(&log
->mailmap
, &mailbuf
, &maillen
, &namebuf
, &namelen
);
109 strbuf_add(out
, namebuf
, namelen
);
111 strbuf_addf(out
, " <%.*s>", (int)maillen
, mailbuf
);
116 static void read_from_stdin(struct shortlog
*log
)
118 struct strbuf ident
= STRBUF_INIT
;
119 struct strbuf mapped_ident
= STRBUF_INIT
;
120 struct strbuf oneline
= STRBUF_INIT
;
121 static const char *author_match
[2] = { "Author: ", "author " };
122 static const char *committer_match
[2] = { "Commit: ", "committer " };
125 if (HAS_MULTI_BITS(log
->groups
))
126 die(_("using multiple --group options with stdin is not supported"));
128 switch (log
->groups
) {
129 case SHORTLOG_GROUP_AUTHOR
:
130 match
= author_match
;
132 case SHORTLOG_GROUP_COMMITTER
:
133 match
= committer_match
;
135 case SHORTLOG_GROUP_TRAILER
:
136 die(_("using %s with stdin is not supported"), "--group=trailer");
137 case SHORTLOG_GROUP_FORMAT
:
138 die(_("using %s with stdin is not supported"), "--group=format");
140 BUG("unhandled shortlog group");
143 while (strbuf_getline_lf(&ident
, stdin
) != EOF
) {
145 if (!skip_prefix(ident
.buf
, match
[0], &v
) &&
146 !skip_prefix(ident
.buf
, match
[1], &v
))
148 while (strbuf_getline_lf(&oneline
, stdin
) != EOF
&&
150 ; /* discard headers */
151 while (strbuf_getline_lf(&oneline
, stdin
) != EOF
&&
153 ; /* discard blanks */
155 strbuf_reset(&mapped_ident
);
156 if (parse_ident(log
, &mapped_ident
, v
) < 0)
159 insert_one_record(log
, mapped_ident
.buf
, oneline
.buf
);
161 strbuf_release(&ident
);
162 strbuf_release(&mapped_ident
);
163 strbuf_release(&oneline
);
166 static void insert_records_from_trailers(struct shortlog
*log
,
168 struct commit
*commit
,
169 struct pretty_print_context
*ctx
,
172 struct trailer_iterator iter
;
173 const char *commit_buffer
, *body
;
174 struct strbuf ident
= STRBUF_INIT
;
176 if (!log
->trailers
.nr
)
180 * Using format_commit_message("%B") would be simpler here, but
181 * this saves us copying the message.
183 commit_buffer
= logmsg_reencode(commit
, NULL
, ctx
->output_encoding
);
184 body
= strstr(commit_buffer
, "\n\n");
188 trailer_iterator_init(&iter
, body
);
189 while (trailer_iterator_advance(&iter
)) {
190 const char *value
= iter
.val
.buf
;
192 if (!string_list_has_string(&log
->trailers
, iter
.key
.buf
))
195 strbuf_reset(&ident
);
196 if (!parse_ident(log
, &ident
, value
))
199 if (!strset_add(dups
, value
))
201 insert_one_record(log
, value
, oneline
);
203 trailer_iterator_release(&iter
);
205 strbuf_release(&ident
);
206 unuse_commit_buffer(commit
, commit_buffer
);
209 static int shortlog_needs_dedup(const struct shortlog
*log
)
211 return HAS_MULTI_BITS(log
->groups
) || log
->format
.nr
> 1 || log
->trailers
.nr
;
214 static void insert_records_from_format(struct shortlog
*log
,
216 struct commit
*commit
,
217 struct pretty_print_context
*ctx
,
220 struct strbuf buf
= STRBUF_INIT
;
221 struct string_list_item
*item
;
223 for_each_string_list_item(item
, &log
->format
) {
226 format_commit_message(commit
, item
->string
, &buf
, ctx
);
228 if (!shortlog_needs_dedup(log
) || strset_add(dups
, buf
.buf
))
229 insert_one_record(log
, buf
.buf
, oneline
);
232 strbuf_release(&buf
);
235 void shortlog_add_commit(struct shortlog
*log
, struct commit
*commit
)
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 insert_records_from_trailers(log
, &dups
, commit
, &ctx
, oneline_str
);
257 insert_records_from_format(log
, &dups
, commit
, &ctx
, oneline_str
);
260 strbuf_release(&oneline
);
263 static void get_from_rev(struct rev_info
*rev
, struct shortlog
*log
)
265 struct commit
*commit
;
267 if (prepare_revision_walk(rev
))
268 die(_("revision walk setup failed"));
269 while ((commit
= get_revision(rev
)) != NULL
)
270 shortlog_add_commit(log
, commit
);
273 static int parse_uint(char const **arg
, int comma
, int defval
)
279 ul
= strtoul(*arg
, &endp
, 10);
280 if (*endp
&& *endp
!= comma
)
284 ret
= *arg
== endp
? defval
: (int)ul
;
285 *arg
= *endp
? endp
+ 1 : endp
;
289 static const char wrap_arg_usage
[] = "-w[<width>[,<indent1>[,<indent2>]]]";
290 #define DEFAULT_WRAPLEN 76
291 #define DEFAULT_INDENT1 6
292 #define DEFAULT_INDENT2 9
294 static int parse_wrap_args(const struct option
*opt
, const char *arg
, int unset
)
296 struct shortlog
*log
= opt
->value
;
298 log
->wrap_lines
= !unset
;
302 log
->wrap
= DEFAULT_WRAPLEN
;
303 log
->in1
= DEFAULT_INDENT1
;
304 log
->in2
= DEFAULT_INDENT2
;
308 log
->wrap
= parse_uint(&arg
, ',', DEFAULT_WRAPLEN
);
309 log
->in1
= parse_uint(&arg
, ',', DEFAULT_INDENT1
);
310 log
->in2
= parse_uint(&arg
, '\0', DEFAULT_INDENT2
);
311 if (log
->wrap
< 0 || log
->in1
< 0 || log
->in2
< 0)
312 return error(wrap_arg_usage
);
314 ((log
->in1
&& log
->wrap
<= log
->in1
) ||
315 (log
->in2
&& log
->wrap
<= log
->in2
)))
316 return error(wrap_arg_usage
);
320 static int parse_group_option(const struct option
*opt
, const char *arg
, int unset
)
322 struct shortlog
*log
= opt
->value
;
327 string_list_clear(&log
->trailers
, 0);
328 string_list_clear(&log
->format
, 0);
329 } else if (!strcasecmp(arg
, "author"))
330 log
->groups
|= SHORTLOG_GROUP_AUTHOR
;
331 else if (!strcasecmp(arg
, "committer"))
332 log
->groups
|= SHORTLOG_GROUP_COMMITTER
;
333 else if (skip_prefix(arg
, "trailer:", &field
)) {
334 log
->groups
|= SHORTLOG_GROUP_TRAILER
;
335 string_list_append(&log
->trailers
, field
);
336 } else if (skip_prefix(arg
, "format:", &field
)) {
337 log
->groups
|= SHORTLOG_GROUP_FORMAT
;
338 string_list_append(&log
->format
, field
);
339 } else if (strchr(arg
, '%')) {
340 log
->groups
|= SHORTLOG_GROUP_FORMAT
;
341 string_list_append(&log
->format
, arg
);
343 return error(_("unknown group type: %s"), arg
);
350 void shortlog_init(struct shortlog
*log
)
352 memset(log
, 0, sizeof(*log
));
354 read_mailmap(&log
->mailmap
);
356 log
->list
.strdup_strings
= 1;
357 log
->wrap
= DEFAULT_WRAPLEN
;
358 log
->in1
= DEFAULT_INDENT1
;
359 log
->in2
= DEFAULT_INDENT2
;
360 log
->trailers
.strdup_strings
= 1;
361 log
->trailers
.cmp
= strcasecmp
;
362 log
->format
.strdup_strings
= 1;
365 void shortlog_finish_setup(struct shortlog
*log
)
367 if (log
->groups
& SHORTLOG_GROUP_AUTHOR
)
368 string_list_append(&log
->format
,
369 log
->email
? "%aN <%aE>" : "%aN");
370 if (log
->groups
& SHORTLOG_GROUP_COMMITTER
)
371 string_list_append(&log
->format
,
372 log
->email
? "%cN <%cE>" : "%cN");
374 string_list_sort(&log
->trailers
);
377 int cmd_shortlog(int argc
, const char **argv
, const char *prefix
)
379 struct shortlog log
= { STRING_LIST_INIT_NODUP
};
381 int nongit
= !startup_info
->have_repository
;
383 const struct option options
[] = {
384 OPT_BIT('c', "committer", &log
.groups
,
385 N_("group by committer rather than author"),
386 SHORTLOG_GROUP_COMMITTER
),
387 OPT_BOOL('n', "numbered", &log
.sort_by_number
,
388 N_("sort output according to the number of commits per author")),
389 OPT_BOOL('s', "summary", &log
.summary
,
390 N_("suppress commit descriptions, only provides commit count")),
391 OPT_BOOL('e', "email", &log
.email
,
392 N_("show the email address of each author")),
393 OPT_CALLBACK_F('w', NULL
, &log
, N_("<w>[,<i1>[,<i2>]]"),
394 N_("linewrap output"), PARSE_OPT_OPTARG
,
396 OPT_CALLBACK(0, "group", &log
, N_("field"),
397 N_("group by field"), parse_group_option
),
401 struct parse_opt_ctx_t ctx
;
403 git_config(git_default_config
, NULL
);
405 repo_init_revisions(the_repository
, &rev
, prefix
);
406 parse_options_start(&ctx
, argc
, argv
, prefix
, options
,
407 PARSE_OPT_KEEP_DASHDASH
| PARSE_OPT_KEEP_ARGV0
);
410 switch (parse_options_step(&ctx
, options
, shortlog_usage
)) {
411 case PARSE_OPT_NON_OPTION
:
412 case PARSE_OPT_UNKNOWN
:
415 case PARSE_OPT_ERROR
:
416 case PARSE_OPT_SUBCOMMAND
:
418 case PARSE_OPT_COMPLETE
:
423 parse_revision_opt(&rev
, &ctx
, options
, shortlog_usage
);
426 revision_opts_finish(&rev
);
427 argc
= parse_options_end(&ctx
);
429 if (nongit
&& argc
> 1) {
430 error(_("too many arguments given outside repository"));
431 usage_with_options(shortlog_usage
, options
);
434 if (setup_revisions(argc
, argv
, &rev
, NULL
) != 1) {
435 error(_("unrecognized argument: %s"), argv
[1]);
436 usage_with_options(shortlog_usage
, options
);
439 log
.user_format
= rev
.commit_format
== CMIT_FMT_USERFORMAT
;
440 log
.abbrev
= rev
.abbrev
;
441 log
.file
= rev
.diffopt
.file
;
442 log
.date_mode
= rev
.date_mode
;
445 log
.groups
= SHORTLOG_GROUP_AUTHOR
;
446 shortlog_finish_setup(&log
);
448 /* assume HEAD if from a tty */
449 if (!nongit
&& !rev
.pending
.nr
&& isatty(0))
450 add_head_to_pending(&rev
);
451 if (rev
.pending
.nr
== 0) {
453 fprintf(stderr
, _("(reading log message from standard input)\n"));
454 read_from_stdin(&log
);
457 get_from_rev(&rev
, &log
);
459 release_revisions(&rev
);
461 shortlog_output(&log
);
462 if (log
.file
!= stdout
)
467 static void add_wrapped_shortlog_msg(struct strbuf
*sb
, const char *s
,
468 const struct shortlog
*log
)
470 strbuf_add_wrapped_text(sb
, s
, log
->in1
, log
->in2
, log
->wrap
);
471 strbuf_addch(sb
, '\n');
474 void shortlog_output(struct shortlog
*log
)
477 struct strbuf sb
= STRBUF_INIT
;
479 if (log
->sort_by_number
)
480 STABLE_QSORT(log
->list
.items
, log
->list
.nr
,
481 log
->summary
? compare_by_counter
: compare_by_list
);
482 for (i
= 0; i
< log
->list
.nr
; i
++) {
483 const struct string_list_item
*item
= &log
->list
.items
[i
];
485 fprintf(log
->file
, "%6d\t%s\n",
486 (int)UTIL_TO_INT(item
), item
->string
);
488 struct string_list
*onelines
= item
->util
;
489 fprintf(log
->file
, "%s (%"PRIuMAX
"):\n",
490 item
->string
, (uintmax_t)onelines
->nr
);
491 for (j
= onelines
->nr
; j
>= 1; j
--) {
492 const char *msg
= onelines
->items
[j
- 1].string
;
494 if (log
->wrap_lines
) {
496 add_wrapped_shortlog_msg(&sb
, msg
, log
);
497 fwrite(sb
.buf
, sb
.len
, 1, log
->file
);
500 fprintf(log
->file
, " %s\n", msg
);
502 putc('\n', log
->file
);
503 onelines
->strdup_strings
= 1;
504 string_list_clear(onelines
, 0);
508 log
->list
.items
[i
].util
= NULL
;
512 log
->list
.strdup_strings
= 1;
513 string_list_clear(&log
->list
, 1);
514 clear_mailmap(&log
->mailmap
);
515 string_list_clear(&log
->format
, 0);