6 #include "string-list.h"
11 #include "parse-options.h"
13 static char const * const shortlog_usage
[] = {
14 N_("git shortlog [<options>] [<revision-range>] [[--] [<path>...]]"),
19 * The util field of our string_list_items will contain one of two things:
21 * - if --summary is not in use, it will point to a string list of the
22 * oneline subjects assigned to this author
24 * - if --summary is in use, we don't need that list; we only need to know
25 * its size. So we abuse the pointer slot to store our integer counter.
27 * This macro accesses the latter.
29 #define UTIL_TO_INT(x) ((intptr_t)(x)->util)
31 static int compare_by_counter(const void *a1
, const void *a2
)
33 const struct string_list_item
*i1
= a1
, *i2
= a2
;
34 return UTIL_TO_INT(i2
) - UTIL_TO_INT(i1
);
37 static int compare_by_list(const void *a1
, const void *a2
)
39 const struct string_list_item
*i1
= a1
, *i2
= a2
;
40 const struct string_list
*l1
= i1
->util
, *l2
= i2
->util
;
44 else if (l1
->nr
== l2
->nr
)
50 static void insert_one_record(struct shortlog
*log
,
54 struct string_list_item
*item
;
55 const char *mailbuf
, *namebuf
;
56 size_t namelen
, maillen
;
57 struct strbuf namemailbuf
= STRBUF_INIT
;
58 struct ident_split ident
;
60 if (split_ident_line(&ident
, author
, strlen(author
)))
63 namebuf
= ident
.name_begin
;
64 mailbuf
= ident
.mail_begin
;
65 namelen
= ident
.name_end
- ident
.name_begin
;
66 maillen
= ident
.mail_end
- ident
.mail_begin
;
68 map_user(&log
->mailmap
, &mailbuf
, &maillen
, &namebuf
, &namelen
);
69 strbuf_add(&namemailbuf
, namebuf
, namelen
);
72 strbuf_addf(&namemailbuf
, " <%.*s>", (int)maillen
, mailbuf
);
74 item
= string_list_insert(&log
->list
, namemailbuf
.buf
);
77 item
->util
= (void *)(UTIL_TO_INT(item
) + 1);
79 const char *dot3
= log
->common_repo_prefix
;
81 struct strbuf subject
= STRBUF_INIT
;
84 /* Skip any leading whitespace, including any blank lines. */
85 while (*oneline
&& isspace(*oneline
))
87 eol
= strchr(oneline
, '\n');
89 eol
= oneline
+ strlen(oneline
);
90 if (starts_with(oneline
, "[PATCH")) {
91 char *eob
= strchr(oneline
, ']');
92 if (eob
&& (!eol
|| eob
< eol
))
95 while (*oneline
&& isspace(*oneline
) && *oneline
!= '\n')
97 format_subject(&subject
, oneline
, " ");
98 buffer
= strbuf_detach(&subject
, NULL
);
101 int dot3len
= strlen(dot3
);
103 while ((p
= strstr(buffer
, dot3
)) != NULL
) {
104 int taillen
= strlen(p
) - dot3len
;
105 memcpy(p
, "/.../", 5);
106 memmove(p
+ 5, p
+ dot3len
, taillen
+ 1);
111 if (item
->util
== NULL
)
112 item
->util
= xcalloc(1, sizeof(struct string_list
));
113 string_list_append(item
->util
, buffer
);
117 static void read_from_stdin(struct shortlog
*log
)
119 struct strbuf author
= 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 match
= log
->committer
? committer_match
: author_match
;
126 while (strbuf_getline_lf(&author
, stdin
) != EOF
) {
128 if (!skip_prefix(author
.buf
, match
[0], &v
) &&
129 !skip_prefix(author
.buf
, match
[1], &v
))
131 while (strbuf_getline_lf(&oneline
, stdin
) != EOF
&&
133 ; /* discard headers */
134 while (strbuf_getline_lf(&oneline
, stdin
) != EOF
&&
136 ; /* discard blanks */
137 insert_one_record(log
, v
, oneline
.buf
);
139 strbuf_release(&author
);
140 strbuf_release(&oneline
);
143 void shortlog_add_commit(struct shortlog
*log
, struct commit
*commit
)
145 struct strbuf author
= STRBUF_INIT
;
146 struct strbuf oneline
= STRBUF_INIT
;
147 struct pretty_print_context ctx
= {0};
150 ctx
.fmt
= CMIT_FMT_USERFORMAT
;
151 ctx
.abbrev
= log
->abbrev
;
152 ctx
.print_email_subject
= 1;
153 ctx
.date_mode
.type
= DATE_NORMAL
;
154 ctx
.output_encoding
= get_log_output_encoding();
156 fmt
= log
->committer
? "%cn <%ce>" : "%an <%ae>";
158 format_commit_message(commit
, fmt
, &author
, &ctx
);
160 if (log
->user_format
)
161 pretty_print_commit(&ctx
, commit
, &oneline
);
163 format_commit_message(commit
, "%s", &oneline
, &ctx
);
166 insert_one_record(log
, author
.buf
, oneline
.len
? oneline
.buf
: "<none>");
168 strbuf_release(&author
);
169 strbuf_release(&oneline
);
172 static void get_from_rev(struct rev_info
*rev
, struct shortlog
*log
)
174 struct commit
*commit
;
176 if (prepare_revision_walk(rev
))
177 die(_("revision walk setup failed"));
178 while ((commit
= get_revision(rev
)) != NULL
)
179 shortlog_add_commit(log
, commit
);
182 static int parse_uint(char const **arg
, int comma
, int defval
)
188 ul
= strtoul(*arg
, &endp
, 10);
189 if (*endp
&& *endp
!= comma
)
193 ret
= *arg
== endp
? defval
: (int)ul
;
194 *arg
= *endp
? endp
+ 1 : endp
;
198 static const char wrap_arg_usage
[] = "-w[<width>[,<indent1>[,<indent2>]]]";
199 #define DEFAULT_WRAPLEN 76
200 #define DEFAULT_INDENT1 6
201 #define DEFAULT_INDENT2 9
203 static int parse_wrap_args(const struct option
*opt
, const char *arg
, int unset
)
205 struct shortlog
*log
= opt
->value
;
207 log
->wrap_lines
= !unset
;
211 log
->wrap
= DEFAULT_WRAPLEN
;
212 log
->in1
= DEFAULT_INDENT1
;
213 log
->in2
= DEFAULT_INDENT2
;
217 log
->wrap
= parse_uint(&arg
, ',', DEFAULT_WRAPLEN
);
218 log
->in1
= parse_uint(&arg
, ',', DEFAULT_INDENT1
);
219 log
->in2
= parse_uint(&arg
, '\0', DEFAULT_INDENT2
);
220 if (log
->wrap
< 0 || log
->in1
< 0 || log
->in2
< 0)
221 return error(wrap_arg_usage
);
223 ((log
->in1
&& log
->wrap
<= log
->in1
) ||
224 (log
->in2
&& log
->wrap
<= log
->in2
)))
225 return error(wrap_arg_usage
);
229 void shortlog_init(struct shortlog
*log
)
231 memset(log
, 0, sizeof(*log
));
233 read_mailmap(&log
->mailmap
, &log
->common_repo_prefix
);
235 log
->list
.strdup_strings
= 1;
236 log
->wrap
= DEFAULT_WRAPLEN
;
237 log
->in1
= DEFAULT_INDENT1
;
238 log
->in2
= DEFAULT_INDENT2
;
241 int cmd_shortlog(int argc
, const char **argv
, const char *prefix
)
243 struct shortlog log
= { STRING_LIST_INIT_NODUP
};
245 int nongit
= !startup_info
->have_repository
;
247 const struct option options
[] = {
248 OPT_BOOL('c', "committer", &log
.committer
,
249 N_("Group by committer rather than author")),
250 OPT_BOOL('n', "numbered", &log
.sort_by_number
,
251 N_("sort output according to the number of commits per author")),
252 OPT_BOOL('s', "summary", &log
.summary
,
253 N_("Suppress commit descriptions, only provides commit count")),
254 OPT_BOOL('e', "email", &log
.email
,
255 N_("Show the email address of each author")),
256 { OPTION_CALLBACK
, 'w', NULL
, &log
, N_("w[,i1[,i2]]"),
257 N_("Linewrap output"), PARSE_OPT_OPTARG
, &parse_wrap_args
},
261 struct parse_opt_ctx_t ctx
;
263 git_config(git_default_config
, NULL
);
265 init_revisions(&rev
, prefix
);
266 parse_options_start(&ctx
, argc
, argv
, prefix
, options
,
267 PARSE_OPT_KEEP_DASHDASH
| PARSE_OPT_KEEP_ARGV0
);
270 switch (parse_options_step(&ctx
, options
, shortlog_usage
)) {
276 parse_revision_opt(&rev
, &ctx
, options
, shortlog_usage
);
279 argc
= parse_options_end(&ctx
);
281 if (setup_revisions(argc
, argv
, &rev
, NULL
) != 1) {
282 error(_("unrecognized argument: %s"), argv
[1]);
283 usage_with_options(shortlog_usage
, options
);
286 log
.user_format
= rev
.commit_format
== CMIT_FMT_USERFORMAT
;
287 log
.abbrev
= rev
.abbrev
;
288 log
.file
= rev
.diffopt
.file
;
290 /* assume HEAD if from a tty */
291 if (!nongit
&& !rev
.pending
.nr
&& isatty(0))
292 add_head_to_pending(&rev
);
293 if (rev
.pending
.nr
== 0) {
295 fprintf(stderr
, _("(reading log message from standard input)\n"));
296 read_from_stdin(&log
);
299 get_from_rev(&rev
, &log
);
301 shortlog_output(&log
);
302 if (log
.file
!= stdout
)
307 static void add_wrapped_shortlog_msg(struct strbuf
*sb
, const char *s
,
308 const struct shortlog
*log
)
310 strbuf_add_wrapped_text(sb
, s
, log
->in1
, log
->in2
, log
->wrap
);
311 strbuf_addch(sb
, '\n');
314 void shortlog_output(struct shortlog
*log
)
317 struct strbuf sb
= STRBUF_INIT
;
319 if (log
->sort_by_number
)
320 QSORT(log
->list
.items
, log
->list
.nr
,
321 log
->summary
? compare_by_counter
: compare_by_list
);
322 for (i
= 0; i
< log
->list
.nr
; i
++) {
323 const struct string_list_item
*item
= &log
->list
.items
[i
];
325 fprintf(log
->file
, "%6d\t%s\n",
326 (int)UTIL_TO_INT(item
), item
->string
);
328 struct string_list
*onelines
= item
->util
;
329 fprintf(log
->file
, "%s (%d):\n",
330 item
->string
, onelines
->nr
);
331 for (j
= onelines
->nr
- 1; j
>= 0; j
--) {
332 const char *msg
= onelines
->items
[j
].string
;
334 if (log
->wrap_lines
) {
336 add_wrapped_shortlog_msg(&sb
, msg
, log
);
337 fwrite(sb
.buf
, sb
.len
, 1, log
->file
);
340 fprintf(log
->file
, " %s\n", msg
);
342 putc('\n', log
->file
);
343 onelines
->strdup_strings
= 1;
344 string_list_clear(onelines
, 0);
348 log
->list
.items
[i
].util
= NULL
;
352 log
->list
.strdup_strings
= 1;
353 string_list_clear(&log
->list
, 1);
354 clear_mailmap(&log
->mailmap
);