5 #include "string-list.h"
10 #include "parse-options.h"
12 static char const * const shortlog_usage
[] = {
13 N_("git shortlog [-n] [-s] [-e] [-w] [rev-opts] [--] [<commit-id>... ]"),
15 N_("[rev-opts] are documented in git-rev-list(1)"),
19 static int compare_by_number(const void *a1
, const void *a2
)
21 const struct string_list_item
*i1
= a1
, *i2
= a2
;
22 const struct string_list
*l1
= i1
->util
, *l2
= i2
->util
;
26 else if (l1
->nr
== l2
->nr
)
32 static void insert_one_record(struct shortlog
*log
,
36 const char *dot3
= log
->common_repo_prefix
;
38 struct string_list_item
*item
;
43 struct strbuf subject
= STRBUF_INIT
;
44 struct ident_split ident
;
46 if (split_ident_line(&ident
, author
, strlen(author
)))
49 /* copy author name to namebuf, to support matching on both name and email */
50 len
= ident
.name_end
- ident
.name_begin
;
51 memcpy(namebuf
, ident
.name_begin
, len
);
54 /* copy email name to emailbuf, to allow email replacement as well */
55 len
= ident
.mail_end
- ident
.mail_begin
;
56 memcpy(emailbuf
, ident
.mail_begin
, len
);
59 map_user(&log
->mailmap
, emailbuf
, sizeof(emailbuf
), namebuf
, sizeof(namebuf
));
60 len
= strlen(namebuf
);
63 size_t room
= sizeof(namebuf
) - len
- 1;
64 int maillen
= strlen(emailbuf
);
65 snprintf(namebuf
+ len
, room
, " <%.*s>", maillen
, emailbuf
);
68 item
= string_list_insert(&log
->list
, namebuf
);
69 if (item
->util
== NULL
)
70 item
->util
= xcalloc(1, sizeof(struct string_list
));
72 /* Skip any leading whitespace, including any blank lines. */
73 while (*oneline
&& isspace(*oneline
))
75 eol
= strchr(oneline
, '\n');
77 eol
= oneline
+ strlen(oneline
);
78 if (!prefixcmp(oneline
, "[PATCH")) {
79 char *eob
= strchr(oneline
, ']');
80 if (eob
&& (!eol
|| eob
< eol
))
83 while (*oneline
&& isspace(*oneline
) && *oneline
!= '\n')
85 format_subject(&subject
, oneline
, " ");
86 buffer
= strbuf_detach(&subject
, NULL
);
89 int dot3len
= strlen(dot3
);
91 while ((p
= strstr(buffer
, dot3
)) != NULL
) {
92 int taillen
= strlen(p
) - dot3len
;
93 memcpy(p
, "/.../", 5);
94 memmove(p
+ 5, p
+ dot3len
, taillen
+ 1);
99 string_list_append(item
->util
, buffer
);
102 static void read_from_stdin(struct shortlog
*log
)
104 char author
[1024], oneline
[1024];
106 while (fgets(author
, sizeof(author
), stdin
) != NULL
) {
107 if (!(author
[0] == 'A' || author
[0] == 'a') ||
108 prefixcmp(author
+ 1, "uthor: "))
110 while (fgets(oneline
, sizeof(oneline
), stdin
) &&
112 ; /* discard headers */
113 while (fgets(oneline
, sizeof(oneline
), stdin
) &&
115 ; /* discard blanks */
116 insert_one_record(log
, author
+ 8, oneline
);
120 void shortlog_add_commit(struct shortlog
*log
, struct commit
*commit
)
122 const char *author
= NULL
, *buffer
;
123 struct strbuf buf
= STRBUF_INIT
;
124 struct strbuf ufbuf
= STRBUF_INIT
;
126 pp_commit_easy(CMIT_FMT_RAW
, commit
, &buf
);
128 while (*buffer
&& *buffer
!= '\n') {
129 const char *eol
= strchr(buffer
, '\n');
132 eol
= buffer
+ strlen(buffer
);
136 if (!prefixcmp(buffer
, "author "))
141 die(_("Missing author: %s"),
142 sha1_to_hex(commit
->object
.sha1
));
143 if (log
->user_format
) {
144 struct pretty_print_context ctx
= {0};
145 ctx
.fmt
= CMIT_FMT_USERFORMAT
;
146 ctx
.abbrev
= log
->abbrev
;
148 ctx
.after_subject
= "";
149 ctx
.date_mode
= DATE_NORMAL
;
150 pretty_print_commit(&ctx
, commit
, &ufbuf
);
152 } else if (*buffer
) {
155 insert_one_record(log
, author
, !*buffer
? "<none>" : buffer
);
156 strbuf_release(&ufbuf
);
157 strbuf_release(&buf
);
160 static void get_from_rev(struct rev_info
*rev
, struct shortlog
*log
)
162 struct commit
*commit
;
164 if (prepare_revision_walk(rev
))
165 die(_("revision walk setup failed"));
166 while ((commit
= get_revision(rev
)) != NULL
)
167 shortlog_add_commit(log
, commit
);
170 static int parse_uint(char const **arg
, int comma
, int defval
)
176 ul
= strtoul(*arg
, &endp
, 10);
177 if (*endp
&& *endp
!= comma
)
181 ret
= *arg
== endp
? defval
: (int)ul
;
182 *arg
= *endp
? endp
+ 1 : endp
;
186 static const char wrap_arg_usage
[] = "-w[<width>[,<indent1>[,<indent2>]]]";
187 #define DEFAULT_WRAPLEN 76
188 #define DEFAULT_INDENT1 6
189 #define DEFAULT_INDENT2 9
191 static int parse_wrap_args(const struct option
*opt
, const char *arg
, int unset
)
193 struct shortlog
*log
= opt
->value
;
195 log
->wrap_lines
= !unset
;
199 log
->wrap
= DEFAULT_WRAPLEN
;
200 log
->in1
= DEFAULT_INDENT1
;
201 log
->in2
= DEFAULT_INDENT2
;
205 log
->wrap
= parse_uint(&arg
, ',', DEFAULT_WRAPLEN
);
206 log
->in1
= parse_uint(&arg
, ',', DEFAULT_INDENT1
);
207 log
->in2
= parse_uint(&arg
, '\0', DEFAULT_INDENT2
);
208 if (log
->wrap
< 0 || log
->in1
< 0 || log
->in2
< 0)
209 return error(wrap_arg_usage
);
211 ((log
->in1
&& log
->wrap
<= log
->in1
) ||
212 (log
->in2
&& log
->wrap
<= log
->in2
)))
213 return error(wrap_arg_usage
);
217 void shortlog_init(struct shortlog
*log
)
219 memset(log
, 0, sizeof(*log
));
221 read_mailmap(&log
->mailmap
, &log
->common_repo_prefix
);
223 log
->list
.strdup_strings
= 1;
224 log
->wrap
= DEFAULT_WRAPLEN
;
225 log
->in1
= DEFAULT_INDENT1
;
226 log
->in2
= DEFAULT_INDENT2
;
229 int cmd_shortlog(int argc
, const char **argv
, const char *prefix
)
231 static struct shortlog log
;
232 static struct rev_info rev
;
233 int nongit
= !startup_info
->have_repository
;
235 static const struct option options
[] = {
236 OPT_BOOLEAN('n', "numbered", &log
.sort_by_number
,
237 N_("sort output according to the number of commits per author")),
238 OPT_BOOLEAN('s', "summary", &log
.summary
,
239 N_("Suppress commit descriptions, only provides commit count")),
240 OPT_BOOLEAN('e', "email", &log
.email
,
241 N_("Show the email address of each author")),
242 { OPTION_CALLBACK
, 'w', NULL
, &log
, N_("w[,i1[,i2]]"),
243 N_("Linewrap output"), PARSE_OPT_OPTARG
, &parse_wrap_args
},
247 struct parse_opt_ctx_t ctx
;
249 git_config(git_default_config
, NULL
);
251 init_revisions(&rev
, prefix
);
252 parse_options_start(&ctx
, argc
, argv
, prefix
, options
,
253 PARSE_OPT_KEEP_DASHDASH
| PARSE_OPT_KEEP_ARGV0
);
256 switch (parse_options_step(&ctx
, options
, shortlog_usage
)) {
262 parse_revision_opt(&rev
, &ctx
, options
, shortlog_usage
);
265 argc
= parse_options_end(&ctx
);
267 if (setup_revisions(argc
, argv
, &rev
, NULL
) != 1) {
268 error(_("unrecognized argument: %s"), argv
[1]);
269 usage_with_options(shortlog_usage
, options
);
272 log
.user_format
= rev
.commit_format
== CMIT_FMT_USERFORMAT
;
273 log
.abbrev
= rev
.abbrev
;
275 /* assume HEAD if from a tty */
276 if (!nongit
&& !rev
.pending
.nr
&& isatty(0))
277 add_head_to_pending(&rev
);
278 if (rev
.pending
.nr
== 0) {
280 fprintf(stderr
, _("(reading log message from standard input)\n"));
281 read_from_stdin(&log
);
284 get_from_rev(&rev
, &log
);
286 shortlog_output(&log
);
290 static void add_wrapped_shortlog_msg(struct strbuf
*sb
, const char *s
,
291 const struct shortlog
*log
)
293 int col
= strbuf_add_wrapped_text(sb
, s
, log
->in1
, log
->in2
, log
->wrap
);
294 if (col
!= log
->wrap
)
295 strbuf_addch(sb
, '\n');
298 void shortlog_output(struct shortlog
*log
)
301 struct strbuf sb
= STRBUF_INIT
;
303 if (log
->sort_by_number
)
304 qsort(log
->list
.items
, log
->list
.nr
, sizeof(struct string_list_item
),
306 for (i
= 0; i
< log
->list
.nr
; i
++) {
307 struct string_list
*onelines
= log
->list
.items
[i
].util
;
310 printf("%6d\t%s\n", onelines
->nr
, log
->list
.items
[i
].string
);
312 printf("%s (%d):\n", log
->list
.items
[i
].string
, onelines
->nr
);
313 for (j
= onelines
->nr
- 1; j
>= 0; j
--) {
314 const char *msg
= onelines
->items
[j
].string
;
316 if (log
->wrap_lines
) {
318 add_wrapped_shortlog_msg(&sb
, msg
, log
);
319 fwrite(sb
.buf
, sb
.len
, 1, stdout
);
322 printf(" %s\n", msg
);
327 onelines
->strdup_strings
= 1;
328 string_list_clear(onelines
, 0);
330 log
->list
.items
[i
].util
= NULL
;
334 log
->list
.strdup_strings
= 1;
335 string_list_clear(&log
->list
, 1);
336 clear_mailmap(&log
->mailmap
);