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>...]]"),
17 static int compare_by_number(const void *a1
, const void *a2
)
19 const struct string_list_item
*i1
= a1
, *i2
= a2
;
20 const struct string_list
*l1
= i1
->util
, *l2
= i2
->util
;
24 else if (l1
->nr
== l2
->nr
)
30 static void insert_one_record(struct shortlog
*log
,
34 struct string_list_item
*item
;
35 const char *mailbuf
, *namebuf
;
36 size_t namelen
, maillen
;
37 struct strbuf namemailbuf
= STRBUF_INIT
;
38 struct ident_split ident
;
40 if (split_ident_line(&ident
, author
, strlen(author
)))
43 namebuf
= ident
.name_begin
;
44 mailbuf
= ident
.mail_begin
;
45 namelen
= ident
.name_end
- ident
.name_begin
;
46 maillen
= ident
.mail_end
- ident
.mail_begin
;
48 map_user(&log
->mailmap
, &mailbuf
, &maillen
, &namebuf
, &namelen
);
49 strbuf_add(&namemailbuf
, namebuf
, namelen
);
52 strbuf_addf(&namemailbuf
, " <%.*s>", (int)maillen
, mailbuf
);
54 item
= string_list_insert(&log
->list
, namemailbuf
.buf
);
55 if (item
->util
== NULL
)
56 item
->util
= xcalloc(1, sizeof(struct string_list
));
59 string_list_append(item
->util
, xstrdup(""));
61 const char *dot3
= log
->common_repo_prefix
;
63 struct strbuf subject
= STRBUF_INIT
;
66 /* Skip any leading whitespace, including any blank lines. */
67 while (*oneline
&& isspace(*oneline
))
69 eol
= strchr(oneline
, '\n');
71 eol
= oneline
+ strlen(oneline
);
72 if (starts_with(oneline
, "[PATCH")) {
73 char *eob
= strchr(oneline
, ']');
74 if (eob
&& (!eol
|| eob
< eol
))
77 while (*oneline
&& isspace(*oneline
) && *oneline
!= '\n')
79 format_subject(&subject
, oneline
, " ");
80 buffer
= strbuf_detach(&subject
, NULL
);
83 int dot3len
= strlen(dot3
);
85 while ((p
= strstr(buffer
, dot3
)) != NULL
) {
86 int taillen
= strlen(p
) - dot3len
;
87 memcpy(p
, "/.../", 5);
88 memmove(p
+ 5, p
+ dot3len
, taillen
+ 1);
93 string_list_append(item
->util
, buffer
);
97 static void read_from_stdin(struct shortlog
*log
)
99 struct strbuf author
= STRBUF_INIT
;
100 struct strbuf oneline
= STRBUF_INIT
;
102 while (strbuf_getline(&author
, stdin
, '\n') != EOF
) {
104 if (!skip_prefix(author
.buf
, "Author: ", &v
) &&
105 !skip_prefix(author
.buf
, "author ", &v
))
107 while (strbuf_getline(&oneline
, stdin
, '\n') != EOF
&&
109 ; /* discard headers */
110 while (strbuf_getline(&oneline
, stdin
, '\n') != EOF
&&
112 ; /* discard blanks */
113 insert_one_record(log
, v
, oneline
.buf
);
115 strbuf_release(&author
);
116 strbuf_release(&oneline
);
119 void shortlog_add_commit(struct shortlog
*log
, struct commit
*commit
)
121 struct strbuf author
= STRBUF_INIT
;
122 struct strbuf oneline
= STRBUF_INIT
;
123 struct pretty_print_context ctx
= {0};
125 ctx
.fmt
= CMIT_FMT_USERFORMAT
;
126 ctx
.abbrev
= log
->abbrev
;
128 ctx
.after_subject
= "";
129 ctx
.date_mode
.type
= DATE_NORMAL
;
130 ctx
.output_encoding
= get_log_output_encoding();
132 format_commit_message(commit
, "%an <%ae>", &author
, &ctx
);
133 /* we can detect a total failure only by seeing " <>" in the output */
134 if (author
.len
<= 3) {
135 warning(_("Missing author: %s"),
136 oid_to_hex(&commit
->object
.oid
));
141 if (log
->user_format
)
142 pretty_print_commit(&ctx
, commit
, &oneline
);
144 format_commit_message(commit
, "%s", &oneline
, &ctx
);
147 insert_one_record(log
, author
.buf
, oneline
.len
? oneline
.buf
: "<none>");
150 strbuf_release(&author
);
151 strbuf_release(&oneline
);
154 static void get_from_rev(struct rev_info
*rev
, struct shortlog
*log
)
156 struct commit
*commit
;
158 if (prepare_revision_walk(rev
))
159 die(_("revision walk setup failed"));
160 while ((commit
= get_revision(rev
)) != NULL
)
161 shortlog_add_commit(log
, commit
);
164 static int parse_uint(char const **arg
, int comma
, int defval
)
170 ul
= strtoul(*arg
, &endp
, 10);
171 if (*endp
&& *endp
!= comma
)
175 ret
= *arg
== endp
? defval
: (int)ul
;
176 *arg
= *endp
? endp
+ 1 : endp
;
180 static const char wrap_arg_usage
[] = "-w[<width>[,<indent1>[,<indent2>]]]";
181 #define DEFAULT_WRAPLEN 76
182 #define DEFAULT_INDENT1 6
183 #define DEFAULT_INDENT2 9
185 static int parse_wrap_args(const struct option
*opt
, const char *arg
, int unset
)
187 struct shortlog
*log
= opt
->value
;
189 log
->wrap_lines
= !unset
;
193 log
->wrap
= DEFAULT_WRAPLEN
;
194 log
->in1
= DEFAULT_INDENT1
;
195 log
->in2
= DEFAULT_INDENT2
;
199 log
->wrap
= parse_uint(&arg
, ',', DEFAULT_WRAPLEN
);
200 log
->in1
= parse_uint(&arg
, ',', DEFAULT_INDENT1
);
201 log
->in2
= parse_uint(&arg
, '\0', DEFAULT_INDENT2
);
202 if (log
->wrap
< 0 || log
->in1
< 0 || log
->in2
< 0)
203 return error(wrap_arg_usage
);
205 ((log
->in1
&& log
->wrap
<= log
->in1
) ||
206 (log
->in2
&& log
->wrap
<= log
->in2
)))
207 return error(wrap_arg_usage
);
211 void shortlog_init(struct shortlog
*log
)
213 memset(log
, 0, sizeof(*log
));
215 read_mailmap(&log
->mailmap
, &log
->common_repo_prefix
);
217 log
->list
.strdup_strings
= 1;
218 log
->wrap
= DEFAULT_WRAPLEN
;
219 log
->in1
= DEFAULT_INDENT1
;
220 log
->in2
= DEFAULT_INDENT2
;
223 int cmd_shortlog(int argc
, const char **argv
, const char *prefix
)
225 static struct shortlog log
;
226 static struct rev_info rev
;
227 int nongit
= !startup_info
->have_repository
;
229 static const struct option options
[] = {
230 OPT_BOOL('n', "numbered", &log
.sort_by_number
,
231 N_("sort output according to the number of commits per author")),
232 OPT_BOOL('s', "summary", &log
.summary
,
233 N_("Suppress commit descriptions, only provides commit count")),
234 OPT_BOOL('e', "email", &log
.email
,
235 N_("Show the email address of each author")),
236 { OPTION_CALLBACK
, 'w', NULL
, &log
, N_("w[,i1[,i2]]"),
237 N_("Linewrap output"), PARSE_OPT_OPTARG
, &parse_wrap_args
},
241 struct parse_opt_ctx_t ctx
;
243 git_config(git_default_config
, NULL
);
245 init_revisions(&rev
, prefix
);
246 parse_options_start(&ctx
, argc
, argv
, prefix
, options
,
247 PARSE_OPT_KEEP_DASHDASH
| PARSE_OPT_KEEP_ARGV0
);
250 switch (parse_options_step(&ctx
, options
, shortlog_usage
)) {
256 parse_revision_opt(&rev
, &ctx
, options
, shortlog_usage
);
259 argc
= parse_options_end(&ctx
);
261 if (setup_revisions(argc
, argv
, &rev
, NULL
) != 1) {
262 error(_("unrecognized argument: %s"), argv
[1]);
263 usage_with_options(shortlog_usage
, options
);
266 log
.user_format
= rev
.commit_format
== CMIT_FMT_USERFORMAT
;
267 log
.abbrev
= rev
.abbrev
;
269 /* assume HEAD if from a tty */
270 if (!nongit
&& !rev
.pending
.nr
&& isatty(0))
271 add_head_to_pending(&rev
);
272 if (rev
.pending
.nr
== 0) {
274 fprintf(stderr
, _("(reading log message from standard input)\n"));
275 read_from_stdin(&log
);
278 get_from_rev(&rev
, &log
);
280 shortlog_output(&log
);
284 static void add_wrapped_shortlog_msg(struct strbuf
*sb
, const char *s
,
285 const struct shortlog
*log
)
287 strbuf_add_wrapped_text(sb
, s
, log
->in1
, log
->in2
, log
->wrap
);
288 strbuf_addch(sb
, '\n');
291 void shortlog_output(struct shortlog
*log
)
294 struct strbuf sb
= STRBUF_INIT
;
296 if (log
->sort_by_number
)
297 qsort(log
->list
.items
, log
->list
.nr
, sizeof(struct string_list_item
),
299 for (i
= 0; i
< log
->list
.nr
; i
++) {
300 struct string_list
*onelines
= log
->list
.items
[i
].util
;
303 printf("%6d\t%s\n", onelines
->nr
, log
->list
.items
[i
].string
);
305 printf("%s (%d):\n", log
->list
.items
[i
].string
, onelines
->nr
);
306 for (j
= onelines
->nr
- 1; j
>= 0; j
--) {
307 const char *msg
= onelines
->items
[j
].string
;
309 if (log
->wrap_lines
) {
311 add_wrapped_shortlog_msg(&sb
, msg
, log
);
312 fwrite(sb
.buf
, sb
.len
, 1, stdout
);
315 printf(" %s\n", msg
);
320 onelines
->strdup_strings
= 1;
321 string_list_clear(onelines
, 0);
323 log
->list
.items
[i
].util
= NULL
;
327 log
->list
.strdup_strings
= 1;
328 string_list_clear(&log
->list
, 1);
329 clear_mailmap(&log
->mailmap
);