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 const char *dot3
= log
->common_repo_prefix
;
36 struct string_list_item
*item
;
37 const char *mailbuf
, *namebuf
;
38 size_t namelen
, maillen
;
40 struct strbuf subject
= STRBUF_INIT
;
41 struct strbuf namemailbuf
= STRBUF_INIT
;
42 struct ident_split ident
;
44 if (split_ident_line(&ident
, author
, strlen(author
)))
47 namebuf
= ident
.name_begin
;
48 mailbuf
= ident
.mail_begin
;
49 namelen
= ident
.name_end
- ident
.name_begin
;
50 maillen
= ident
.mail_end
- ident
.mail_begin
;
52 map_user(&log
->mailmap
, &mailbuf
, &maillen
, &namebuf
, &namelen
);
53 strbuf_add(&namemailbuf
, namebuf
, namelen
);
56 strbuf_addf(&namemailbuf
, " <%.*s>", (int)maillen
, mailbuf
);
58 item
= string_list_insert(&log
->list
, namemailbuf
.buf
);
59 if (item
->util
== NULL
)
60 item
->util
= xcalloc(1, sizeof(struct string_list
));
62 /* Skip any leading whitespace, including any blank lines. */
63 while (*oneline
&& isspace(*oneline
))
65 eol
= strchr(oneline
, '\n');
67 eol
= oneline
+ strlen(oneline
);
68 if (starts_with(oneline
, "[PATCH")) {
69 char *eob
= strchr(oneline
, ']');
70 if (eob
&& (!eol
|| eob
< eol
))
73 while (*oneline
&& isspace(*oneline
) && *oneline
!= '\n')
75 format_subject(&subject
, oneline
, " ");
76 buffer
= strbuf_detach(&subject
, NULL
);
79 int dot3len
= strlen(dot3
);
81 while ((p
= strstr(buffer
, dot3
)) != NULL
) {
82 int taillen
= strlen(p
) - dot3len
;
83 memcpy(p
, "/.../", 5);
84 memmove(p
+ 5, p
+ dot3len
, taillen
+ 1);
89 string_list_append(item
->util
, buffer
);
92 static void read_from_stdin(struct shortlog
*log
)
94 struct strbuf author
= STRBUF_INIT
;
95 struct strbuf oneline
= STRBUF_INIT
;
97 while (strbuf_getline(&author
, stdin
, '\n') != EOF
) {
99 if (!skip_prefix(author
.buf
, "Author: ", &v
) &&
100 !skip_prefix(author
.buf
, "author ", &v
))
102 while (strbuf_getline(&oneline
, stdin
, '\n') != EOF
&&
104 ; /* discard headers */
105 while (strbuf_getline(&oneline
, stdin
, '\n') != EOF
&&
107 ; /* discard blanks */
108 insert_one_record(log
, v
, oneline
.buf
);
110 strbuf_release(&author
);
111 strbuf_release(&oneline
);
114 void shortlog_add_commit(struct shortlog
*log
, struct commit
*commit
)
116 const char *author
= NULL
, *buffer
;
117 struct strbuf buf
= STRBUF_INIT
;
118 struct strbuf ufbuf
= STRBUF_INIT
;
120 pp_commit_easy(CMIT_FMT_RAW
, commit
, &buf
);
122 while (*buffer
&& *buffer
!= '\n') {
123 const char *eol
= strchr(buffer
, '\n');
126 eol
= buffer
+ strlen(buffer
);
130 if (starts_with(buffer
, "author "))
135 warning(_("Missing author: %s"),
136 oid_to_hex(&commit
->object
.oid
));
139 if (log
->user_format
) {
140 struct pretty_print_context ctx
= {0};
141 ctx
.fmt
= CMIT_FMT_USERFORMAT
;
142 ctx
.abbrev
= log
->abbrev
;
144 ctx
.after_subject
= "";
145 ctx
.date_mode
.type
= DATE_NORMAL
;
146 ctx
.output_encoding
= get_log_output_encoding();
147 pretty_print_commit(&ctx
, commit
, &ufbuf
);
149 } else if (*buffer
) {
152 insert_one_record(log
, author
, !*buffer
? "<none>" : buffer
);
153 strbuf_release(&ufbuf
);
154 strbuf_release(&buf
);
157 static void get_from_rev(struct rev_info
*rev
, struct shortlog
*log
)
159 struct commit
*commit
;
161 if (prepare_revision_walk(rev
))
162 die(_("revision walk setup failed"));
163 while ((commit
= get_revision(rev
)) != NULL
)
164 shortlog_add_commit(log
, commit
);
167 static int parse_uint(char const **arg
, int comma
, int defval
)
173 ul
= strtoul(*arg
, &endp
, 10);
174 if (*endp
&& *endp
!= comma
)
178 ret
= *arg
== endp
? defval
: (int)ul
;
179 *arg
= *endp
? endp
+ 1 : endp
;
183 static const char wrap_arg_usage
[] = "-w[<width>[,<indent1>[,<indent2>]]]";
184 #define DEFAULT_WRAPLEN 76
185 #define DEFAULT_INDENT1 6
186 #define DEFAULT_INDENT2 9
188 static int parse_wrap_args(const struct option
*opt
, const char *arg
, int unset
)
190 struct shortlog
*log
= opt
->value
;
192 log
->wrap_lines
= !unset
;
196 log
->wrap
= DEFAULT_WRAPLEN
;
197 log
->in1
= DEFAULT_INDENT1
;
198 log
->in2
= DEFAULT_INDENT2
;
202 log
->wrap
= parse_uint(&arg
, ',', DEFAULT_WRAPLEN
);
203 log
->in1
= parse_uint(&arg
, ',', DEFAULT_INDENT1
);
204 log
->in2
= parse_uint(&arg
, '\0', DEFAULT_INDENT2
);
205 if (log
->wrap
< 0 || log
->in1
< 0 || log
->in2
< 0)
206 return error(wrap_arg_usage
);
208 ((log
->in1
&& log
->wrap
<= log
->in1
) ||
209 (log
->in2
&& log
->wrap
<= log
->in2
)))
210 return error(wrap_arg_usage
);
214 void shortlog_init(struct shortlog
*log
)
216 memset(log
, 0, sizeof(*log
));
218 read_mailmap(&log
->mailmap
, &log
->common_repo_prefix
);
220 log
->list
.strdup_strings
= 1;
221 log
->wrap
= DEFAULT_WRAPLEN
;
222 log
->in1
= DEFAULT_INDENT1
;
223 log
->in2
= DEFAULT_INDENT2
;
226 int cmd_shortlog(int argc
, const char **argv
, const char *prefix
)
228 static struct shortlog log
;
229 static struct rev_info rev
;
230 int nongit
= !startup_info
->have_repository
;
232 static const struct option options
[] = {
233 OPT_BOOL('n', "numbered", &log
.sort_by_number
,
234 N_("sort output according to the number of commits per author")),
235 OPT_BOOL('s', "summary", &log
.summary
,
236 N_("Suppress commit descriptions, only provides commit count")),
237 OPT_BOOL('e', "email", &log
.email
,
238 N_("Show the email address of each author")),
239 { OPTION_CALLBACK
, 'w', NULL
, &log
, N_("w[,i1[,i2]]"),
240 N_("Linewrap output"), PARSE_OPT_OPTARG
, &parse_wrap_args
},
244 struct parse_opt_ctx_t ctx
;
246 git_config(git_default_config
, NULL
);
248 init_revisions(&rev
, prefix
);
249 parse_options_start(&ctx
, argc
, argv
, prefix
, options
,
250 PARSE_OPT_KEEP_DASHDASH
| PARSE_OPT_KEEP_ARGV0
);
253 switch (parse_options_step(&ctx
, options
, shortlog_usage
)) {
259 parse_revision_opt(&rev
, &ctx
, options
, shortlog_usage
);
262 argc
= parse_options_end(&ctx
);
264 if (setup_revisions(argc
, argv
, &rev
, NULL
) != 1) {
265 error(_("unrecognized argument: %s"), argv
[1]);
266 usage_with_options(shortlog_usage
, options
);
269 log
.user_format
= rev
.commit_format
== CMIT_FMT_USERFORMAT
;
270 log
.abbrev
= rev
.abbrev
;
272 /* assume HEAD if from a tty */
273 if (!nongit
&& !rev
.pending
.nr
&& isatty(0))
274 add_head_to_pending(&rev
);
275 if (rev
.pending
.nr
== 0) {
277 fprintf(stderr
, _("(reading log message from standard input)\n"));
278 read_from_stdin(&log
);
281 get_from_rev(&rev
, &log
);
283 shortlog_output(&log
);
287 static void add_wrapped_shortlog_msg(struct strbuf
*sb
, const char *s
,
288 const struct shortlog
*log
)
290 strbuf_add_wrapped_text(sb
, s
, log
->in1
, log
->in2
, log
->wrap
);
291 strbuf_addch(sb
, '\n');
294 void shortlog_output(struct shortlog
*log
)
297 struct strbuf sb
= STRBUF_INIT
;
299 if (log
->sort_by_number
)
300 qsort(log
->list
.items
, log
->list
.nr
, sizeof(struct string_list_item
),
302 for (i
= 0; i
< log
->list
.nr
; i
++) {
303 struct string_list
*onelines
= log
->list
.items
[i
].util
;
306 printf("%6d\t%s\n", onelines
->nr
, log
->list
.items
[i
].string
);
308 printf("%s (%d):\n", log
->list
.items
[i
].string
, onelines
->nr
);
309 for (j
= onelines
->nr
- 1; j
>= 0; j
--) {
310 const char *msg
= onelines
->items
[j
].string
;
312 if (log
->wrap_lines
) {
314 add_wrapped_shortlog_msg(&sb
, msg
, log
);
315 fwrite(sb
.buf
, sb
.len
, 1, stdout
);
318 printf(" %s\n", msg
);
323 onelines
->strdup_strings
= 1;
324 string_list_clear(onelines
, 0);
326 log
->list
.items
[i
].util
= NULL
;
330 log
->list
.strdup_strings
= 1;
331 string_list_clear(&log
->list
, 1);
332 clear_mailmap(&log
->mailmap
);