]>
git.ipfire.org Git - thirdparty/git.git/blob - builtin/shortlog.c
6 #include "environment.h"
8 #include "string-list.h"
13 #include "parse-options.h"
17 static char const * const shortlog_usage
[] = {
18 N_("git shortlog [<options>] [<revision-range>] [[--] <path>...]"),
19 N_("git log --pretty=short | git shortlog [<options>]"),
24 * The util field of our string_list_items will contain one of two things:
26 * - if --summary is not in use, it will point to a string list of the
27 * oneline subjects assigned to this author
29 * - if --summary is in use, we don't need that list; we only need to know
30 * its size. So we abuse the pointer slot to store our integer counter.
32 * This macro accesses the latter.
34 #define UTIL_TO_INT(x) ((intptr_t)(x)->util)
36 static int compare_by_counter(const void *a1
, const void *a2
)
38 const struct string_list_item
*i1
= a1
, *i2
= a2
;
39 return UTIL_TO_INT(i2
) - UTIL_TO_INT(i1
);
42 static int compare_by_list(const void *a1
, const void *a2
)
44 const struct string_list_item
*i1
= a1
, *i2
= a2
;
45 const struct string_list
*l1
= i1
->util
, *l2
= i2
->util
;
49 else if (l1
->nr
== l2
->nr
)
55 static void insert_one_record(struct shortlog
*log
,
59 struct string_list_item
*item
;
61 item
= string_list_insert(&log
->list
, ident
);
64 item
->util
= (void *)(UTIL_TO_INT(item
) + 1);
67 struct strbuf subject
= STRBUF_INIT
;
70 /* Skip any leading whitespace, including any blank lines. */
71 while (*oneline
&& isspace(*oneline
))
73 eol
= strchr(oneline
, '\n');
75 eol
= oneline
+ strlen(oneline
);
76 if (starts_with(oneline
, "[PATCH")) {
77 char *eob
= strchr(oneline
, ']');
78 if (eob
&& (!eol
|| eob
< eol
))
81 while (*oneline
&& isspace(*oneline
) && *oneline
!= '\n')
83 format_subject(&subject
, oneline
, " ");
84 buffer
= strbuf_detach(&subject
, NULL
);
87 item
->util
= xmalloc(sizeof(struct string_list
));
88 string_list_init_nodup(item
->util
);
90 string_list_append(item
->util
, buffer
);
94 static int parse_ident(struct shortlog
*log
,
95 struct strbuf
*out
, const char *in
)
97 const char *mailbuf
, *namebuf
;
98 size_t namelen
, maillen
;
99 struct ident_split ident
;
101 if (split_ident_line(&ident
, in
, strlen(in
)))
104 namebuf
= ident
.name_begin
;
105 mailbuf
= ident
.mail_begin
;
106 namelen
= ident
.name_end
- ident
.name_begin
;
107 maillen
= ident
.mail_end
- ident
.mail_begin
;
109 map_user(&log
->mailmap
, &mailbuf
, &maillen
, &namebuf
, &namelen
);
110 strbuf_add(out
, namebuf
, namelen
);
112 strbuf_addf(out
, " <%.*s>", (int)maillen
, mailbuf
);
117 static void read_from_stdin(struct shortlog
*log
)
119 struct strbuf ident
= STRBUF_INIT
;
120 struct strbuf mapped_ident
= STRBUF_INIT
;
121 struct strbuf oneline
= STRBUF_INIT
;
122 static const char *author_match
[2] = { "Author: ", "author " };
123 static const char *committer_match
[2] = { "Commit: ", "committer " };
126 if (HAS_MULTI_BITS(log
->groups
))
127 die(_("using multiple --group options with stdin is not supported"));
129 switch (log
->groups
) {
130 case SHORTLOG_GROUP_AUTHOR
:
131 match
= author_match
;
133 case SHORTLOG_GROUP_COMMITTER
:
134 match
= committer_match
;
136 case SHORTLOG_GROUP_TRAILER
:
137 die(_("using %s with stdin is not supported"), "--group=trailer");
138 case SHORTLOG_GROUP_FORMAT
:
139 die(_("using %s with stdin is not supported"), "--group=format");
141 BUG("unhandled shortlog group");
144 while (strbuf_getline_lf(&ident
, stdin
) != EOF
) {
146 if (!skip_prefix(ident
.buf
, match
[0], &v
) &&
147 !skip_prefix(ident
.buf
, match
[1], &v
))
149 while (strbuf_getline_lf(&oneline
, stdin
) != EOF
&&
151 ; /* discard headers */
152 while (strbuf_getline_lf(&oneline
, stdin
) != EOF
&&
154 ; /* discard blanks */
156 strbuf_reset(&mapped_ident
);
157 if (parse_ident(log
, &mapped_ident
, v
) < 0)
160 insert_one_record(log
, mapped_ident
.buf
, oneline
.buf
);
162 strbuf_release(&ident
);
163 strbuf_release(&mapped_ident
);
164 strbuf_release(&oneline
);
167 static void insert_records_from_trailers(struct shortlog
*log
,
169 struct commit
*commit
,
170 struct pretty_print_context
*ctx
,
173 struct trailer_iterator iter
;
174 const char *commit_buffer
, *body
;
175 struct strbuf ident
= STRBUF_INIT
;
177 if (!log
->trailers
.nr
)
181 * Using format_commit_message("%B") would be simpler here, but
182 * this saves us copying the message.
184 commit_buffer
= logmsg_reencode(commit
, NULL
, ctx
->output_encoding
);
185 body
= strstr(commit_buffer
, "\n\n");
189 trailer_iterator_init(&iter
, body
);
190 while (trailer_iterator_advance(&iter
)) {
191 const char *value
= iter
.val
.buf
;
193 if (!string_list_has_string(&log
->trailers
, iter
.key
.buf
))
196 strbuf_reset(&ident
);
197 if (!parse_ident(log
, &ident
, value
))
200 if (!strset_add(dups
, value
))
202 insert_one_record(log
, value
, oneline
);
204 trailer_iterator_release(&iter
);
206 strbuf_release(&ident
);
207 unuse_commit_buffer(commit
, commit_buffer
);
210 static int shortlog_needs_dedup(const struct shortlog
*log
)
212 return HAS_MULTI_BITS(log
->groups
) || log
->format
.nr
> 1 || log
->trailers
.nr
;
215 static void insert_records_from_format(struct shortlog
*log
,
217 struct commit
*commit
,
218 struct pretty_print_context
*ctx
,
221 struct strbuf buf
= STRBUF_INIT
;
222 struct string_list_item
*item
;
224 for_each_string_list_item(item
, &log
->format
) {
227 format_commit_message(commit
, item
->string
, &buf
, ctx
);
229 if (!shortlog_needs_dedup(log
) || strset_add(dups
, buf
.buf
))
230 insert_one_record(log
, buf
.buf
, oneline
);
233 strbuf_release(&buf
);
236 void shortlog_add_commit(struct shortlog
*log
, struct commit
*commit
)
238 struct strbuf oneline
= STRBUF_INIT
;
239 struct strset dups
= STRSET_INIT
;
240 struct pretty_print_context ctx
= {0};
241 const char *oneline_str
;
243 ctx
.fmt
= CMIT_FMT_USERFORMAT
;
244 ctx
.abbrev
= log
->abbrev
;
245 ctx
.print_email_subject
= 1;
246 ctx
.date_mode
= log
->date_mode
;
247 ctx
.output_encoding
= get_log_output_encoding();
250 if (log
->user_format
)
251 pretty_print_commit(&ctx
, commit
, &oneline
);
253 format_commit_message(commit
, "%s", &oneline
, &ctx
);
255 oneline_str
= oneline
.len
? oneline
.buf
: "<none>";
257 insert_records_from_trailers(log
, &dups
, commit
, &ctx
, oneline_str
);
258 insert_records_from_format(log
, &dups
, commit
, &ctx
, oneline_str
);
261 strbuf_release(&oneline
);
264 static void get_from_rev(struct rev_info
*rev
, struct shortlog
*log
)
266 struct commit
*commit
;
268 if (prepare_revision_walk(rev
))
269 die(_("revision walk setup failed"));
270 while ((commit
= get_revision(rev
)) != NULL
)
271 shortlog_add_commit(log
, commit
);
274 static int parse_uint(char const **arg
, int comma
, int defval
)
280 ul
= strtoul(*arg
, &endp
, 10);
281 if (*endp
&& *endp
!= comma
)
285 ret
= *arg
== endp
? defval
: (int)ul
;
286 *arg
= *endp
? endp
+ 1 : endp
;
290 static const char wrap_arg_usage
[] = "-w[<width>[,<indent1>[,<indent2>]]]";
291 #define DEFAULT_WRAPLEN 76
292 #define DEFAULT_INDENT1 6
293 #define DEFAULT_INDENT2 9
295 static int parse_wrap_args(const struct option
*opt
, const char *arg
, int unset
)
297 struct shortlog
*log
= opt
->value
;
299 log
->wrap_lines
= !unset
;
303 log
->wrap
= DEFAULT_WRAPLEN
;
304 log
->in1
= DEFAULT_INDENT1
;
305 log
->in2
= DEFAULT_INDENT2
;
309 log
->wrap
= parse_uint(&arg
, ',', DEFAULT_WRAPLEN
);
310 log
->in1
= parse_uint(&arg
, ',', DEFAULT_INDENT1
);
311 log
->in2
= parse_uint(&arg
, '\0', DEFAULT_INDENT2
);
312 if (log
->wrap
< 0 || log
->in1
< 0 || log
->in2
< 0)
313 return error(wrap_arg_usage
);
315 ((log
->in1
&& log
->wrap
<= log
->in1
) ||
316 (log
->in2
&& log
->wrap
<= log
->in2
)))
317 return error(wrap_arg_usage
);
321 static int parse_group_option(const struct option
*opt
, const char *arg
, int unset
)
323 struct shortlog
*log
= opt
->value
;
328 string_list_clear(&log
->trailers
, 0);
329 string_list_clear(&log
->format
, 0);
330 } else if (!strcasecmp(arg
, "author"))
331 log
->groups
|= SHORTLOG_GROUP_AUTHOR
;
332 else if (!strcasecmp(arg
, "committer"))
333 log
->groups
|= SHORTLOG_GROUP_COMMITTER
;
334 else if (skip_prefix(arg
, "trailer:", &field
)) {
335 log
->groups
|= SHORTLOG_GROUP_TRAILER
;
336 string_list_append(&log
->trailers
, field
);
337 } else if (skip_prefix(arg
, "format:", &field
)) {
338 log
->groups
|= SHORTLOG_GROUP_FORMAT
;
339 string_list_append(&log
->format
, field
);
340 } else if (strchr(arg
, '%')) {
341 log
->groups
|= SHORTLOG_GROUP_FORMAT
;
342 string_list_append(&log
->format
, arg
);
344 return error(_("unknown group type: %s"), arg
);
351 void shortlog_init(struct shortlog
*log
)
353 memset(log
, 0, sizeof(*log
));
355 read_mailmap(&log
->mailmap
);
357 log
->list
.strdup_strings
= 1;
358 log
->wrap
= DEFAULT_WRAPLEN
;
359 log
->in1
= DEFAULT_INDENT1
;
360 log
->in2
= DEFAULT_INDENT2
;
361 log
->trailers
.strdup_strings
= 1;
362 log
->trailers
.cmp
= strcasecmp
;
363 log
->format
.strdup_strings
= 1;
366 void shortlog_finish_setup(struct shortlog
*log
)
368 if (log
->groups
& SHORTLOG_GROUP_AUTHOR
)
369 string_list_append(&log
->format
,
370 log
->email
? "%aN <%aE>" : "%aN");
371 if (log
->groups
& SHORTLOG_GROUP_COMMITTER
)
372 string_list_append(&log
->format
,
373 log
->email
? "%cN <%cE>" : "%cN");
375 string_list_sort(&log
->trailers
);
378 int cmd_shortlog(int argc
, const char **argv
, const char *prefix
)
380 struct shortlog log
= { STRING_LIST_INIT_NODUP
};
382 int nongit
= !startup_info
->have_repository
;
384 const struct option options
[] = {
385 OPT_BIT('c', "committer", &log
.groups
,
386 N_("group by committer rather than author"),
387 SHORTLOG_GROUP_COMMITTER
),
388 OPT_BOOL('n', "numbered", &log
.sort_by_number
,
389 N_("sort output according to the number of commits per author")),
390 OPT_BOOL('s', "summary", &log
.summary
,
391 N_("suppress commit descriptions, only provides commit count")),
392 OPT_BOOL('e', "email", &log
.email
,
393 N_("show the email address of each author")),
394 OPT_CALLBACK_F('w', NULL
, &log
, N_("<w>[,<i1>[,<i2>]]"),
395 N_("linewrap output"), PARSE_OPT_OPTARG
,
397 OPT_CALLBACK(0, "group", &log
, N_("field"),
398 N_("group by field"), parse_group_option
),
402 struct parse_opt_ctx_t ctx
;
404 git_config(git_default_config
, NULL
);
406 repo_init_revisions(the_repository
, &rev
, prefix
);
407 parse_options_start(&ctx
, argc
, argv
, prefix
, options
,
408 PARSE_OPT_KEEP_DASHDASH
| PARSE_OPT_KEEP_ARGV0
);
411 switch (parse_options_step(&ctx
, options
, shortlog_usage
)) {
412 case PARSE_OPT_NON_OPTION
:
413 case PARSE_OPT_UNKNOWN
:
416 case PARSE_OPT_ERROR
:
417 case PARSE_OPT_SUBCOMMAND
:
419 case PARSE_OPT_COMPLETE
:
424 parse_revision_opt(&rev
, &ctx
, options
, shortlog_usage
);
427 revision_opts_finish(&rev
);
428 argc
= parse_options_end(&ctx
);
430 if (nongit
&& argc
> 1) {
431 error(_("too many arguments given outside repository"));
432 usage_with_options(shortlog_usage
, options
);
435 if (setup_revisions(argc
, argv
, &rev
, NULL
) != 1) {
436 error(_("unrecognized argument: %s"), argv
[1]);
437 usage_with_options(shortlog_usage
, options
);
440 log
.user_format
= rev
.commit_format
== CMIT_FMT_USERFORMAT
;
441 log
.abbrev
= rev
.abbrev
;
442 log
.file
= rev
.diffopt
.file
;
443 log
.date_mode
= rev
.date_mode
;
446 log
.groups
= SHORTLOG_GROUP_AUTHOR
;
447 shortlog_finish_setup(&log
);
449 /* assume HEAD if from a tty */
450 if (!nongit
&& !rev
.pending
.nr
&& isatty(0))
451 add_head_to_pending(&rev
);
452 if (rev
.pending
.nr
== 0) {
454 fprintf(stderr
, _("(reading log message from standard input)\n"));
455 read_from_stdin(&log
);
458 get_from_rev(&rev
, &log
);
460 release_revisions(&rev
);
462 shortlog_output(&log
);
463 if (log
.file
!= stdout
)
468 static void add_wrapped_shortlog_msg(struct strbuf
*sb
, const char *s
,
469 const struct shortlog
*log
)
471 strbuf_add_wrapped_text(sb
, s
, log
->in1
, log
->in2
, log
->wrap
);
472 strbuf_addch(sb
, '\n');
475 void shortlog_output(struct shortlog
*log
)
478 struct strbuf sb
= STRBUF_INIT
;
480 if (log
->sort_by_number
)
481 STABLE_QSORT(log
->list
.items
, log
->list
.nr
,
482 log
->summary
? compare_by_counter
: compare_by_list
);
483 for (i
= 0; i
< log
->list
.nr
; i
++) {
484 const struct string_list_item
*item
= &log
->list
.items
[i
];
486 fprintf(log
->file
, "%6d\t%s\n",
487 (int)UTIL_TO_INT(item
), item
->string
);
489 struct string_list
*onelines
= item
->util
;
490 fprintf(log
->file
, "%s (%"PRIuMAX
"):\n",
491 item
->string
, (uintmax_t)onelines
->nr
);
492 for (j
= onelines
->nr
; j
>= 1; j
--) {
493 const char *msg
= onelines
->items
[j
- 1].string
;
495 if (log
->wrap_lines
) {
497 add_wrapped_shortlog_msg(&sb
, msg
, log
);
498 fwrite(sb
.buf
, sb
.len
, 1, log
->file
);
501 fprintf(log
->file
, " %s\n", msg
);
503 putc('\n', log
->file
);
504 onelines
->strdup_strings
= 1;
505 string_list_clear(onelines
, 0);
509 log
->list
.items
[i
].util
= NULL
;
513 log
->list
.strdup_strings
= 1;
514 string_list_clear(&log
->list
, 1);
515 clear_mailmap(&log
->mailmap
);
516 string_list_clear(&log
->format
, 0);