5 #include "string-list.h"
10 #include "parse-options.h"
12 static char const * const shortlog_usage
[] = {
13 "git shortlog [-n] [-s] [-e] [-w] [rev-opts] [--] [<commit-id>... ]",
15 "[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 const char *format_subject(struct strbuf
*sb
, const char *msg
,
33 const char *line_separator
);
35 static void insert_one_record1(struct shortlog
*log
,
36 const char *name
, const char *oneline
)
38 const char *dot3
= log
->common_repo_prefix
;
40 struct string_list_item
*item
;
42 struct strbuf subject
= STRBUF_INIT
;
44 item
= string_list_insert(name
, &log
->list
);
45 if (item
->util
== NULL
)
46 item
->util
= xcalloc(1, sizeof(struct string_list
));
48 /* Skip any leading whitespace, including any blank lines. */
49 while (*oneline
&& isspace(*oneline
))
51 eol
= strchr(oneline
, '\n');
53 eol
= oneline
+ strlen(oneline
);
54 if (!prefixcmp(oneline
, "[PATCH")) {
55 char *eob
= strchr(oneline
, ']');
56 if (eob
&& (!eol
|| eob
< eol
))
59 while (*oneline
&& isspace(*oneline
) && *oneline
!= '\n')
61 format_subject(&subject
, oneline
, " ");
62 buffer
= strbuf_detach(&subject
, NULL
);
65 int dot3len
= strlen(dot3
);
67 while ((p
= strstr(buffer
, dot3
)) != NULL
) {
68 int taillen
= strlen(p
) - dot3len
;
69 memcpy(p
, "/.../", 5);
70 memmove(p
+ 5, p
+ dot3len
, taillen
+ 1);
75 string_list_append(buffer
, item
->util
);
78 static void insert_one_record(struct shortlog
*log
,
85 const char *boemail
, *eoemail
;
87 boemail
= strchr(author
, '<');
90 eoemail
= strchr(boemail
, '>');
94 /* copy author name to namebuf, to support matching on both name and email */
95 memcpy(namebuf
, author
, boemail
- author
);
96 len
= boemail
- author
;
97 while(len
> 0 && isspace(namebuf
[len
-1]))
101 /* copy email name to emailbuf, to allow email replacement as well */
102 memcpy(emailbuf
, boemail
+1, eoemail
- boemail
);
103 emailbuf
[eoemail
- boemail
- 1] = 0;
105 if (!map_user(&log
->mailmap
, emailbuf
, sizeof(emailbuf
), namebuf
, sizeof(namebuf
))) {
106 while (author
< boemail
&& isspace(*author
))
109 len
< sizeof(namebuf
) - 1 && author
+ len
< boemail
;
111 namebuf
[len
] = author
[len
];
112 while (0 < len
&& isspace(namebuf
[len
-1]))
117 len
= strlen(namebuf
);
120 size_t room
= sizeof(namebuf
) - len
- 1;
121 int maillen
= strlen(emailbuf
);
122 snprintf(namebuf
+ len
, room
, " <%.*s>", maillen
, emailbuf
);
125 insert_one_record1(log
, namebuf
, oneline
);
128 static void read_from_stdin(struct shortlog
*log
)
130 char author
[1024], oneline
[1024];
132 while (fgets(author
, sizeof(author
), stdin
) != NULL
) {
133 if (!(author
[0] == 'A' || author
[0] == 'a') ||
134 prefixcmp(author
+ 1, "uthor: "))
136 while (fgets(oneline
, sizeof(oneline
), stdin
) &&
138 ; /* discard headers */
139 while (fgets(oneline
, sizeof(oneline
), stdin
) &&
141 ; /* discard blanks */
142 insert_one_record(log
, author
+ 8, oneline
);
146 void shortlog_add_commit(struct shortlog
*log
, struct commit
*commit
)
148 struct strbuf buf
= STRBUF_INIT
;
150 const char *author
= NULL
, *buffer
;
152 buffer
= commit
->buffer
;
153 if (log
->user_format
)
154 pretty_print_commit(CMIT_FMT_USERFORMAT
, commit
, &buf
,
155 DEFAULT_ABBREV
, "", "", DATE_NORMAL
, 0);
158 const char *p
= buf
.buf
;
161 if (!log
->user_format
) {
162 p
= strstr(buffer
, "\n\n");
168 if (!regexec(log
->sort_key
, p
, 2, match
, 0) &&
170 author
= key
= xstrndup(p
+ match
[1].rm_so
,
171 match
[1].rm_eo
- match
[1].rm_so
);
174 insert_one_record1(log
, key
, p
);
175 strbuf_release(&buf
);
179 while (*buffer
&& *buffer
!= '\n') {
180 const char *eol
= strchr(buffer
, '\n');
183 eol
= buffer
+ strlen(buffer
);
187 if (!prefixcmp(buffer
, "author "))
192 die("Missing author: %s",
193 sha1_to_hex(commit
->object
.sha1
));
194 if (log
->user_format
) {
195 insert_one_record(log
, author
, buf
.buf
);
196 strbuf_release(&buf
);
201 insert_one_record(log
, author
, !*buffer
? "<none>" : buffer
);
205 static void get_from_rev(struct rev_info
*rev
, struct shortlog
*log
)
207 struct commit
*commit
;
209 if (prepare_revision_walk(rev
))
210 die("revision walk setup failed");
211 while ((commit
= get_revision(rev
)) != NULL
)
212 shortlog_add_commit(log
, commit
);
215 static int parse_uint(char const **arg
, int comma
, int defval
)
221 ul
= strtoul(*arg
, &endp
, 10);
222 if (*endp
&& *endp
!= comma
)
226 ret
= *arg
== endp
? defval
: (int)ul
;
227 *arg
= *endp
? endp
+ 1 : endp
;
231 static const char wrap_arg_usage
[] = "-w[<width>[,<indent1>[,<indent2>]]]";
232 #define DEFAULT_WRAPLEN 76
233 #define DEFAULT_INDENT1 6
234 #define DEFAULT_INDENT2 9
236 static int parse_wrap_args(const struct option
*opt
, const char *arg
, int unset
)
238 struct shortlog
*log
= opt
->value
;
240 log
->wrap_lines
= !unset
;
244 log
->wrap
= DEFAULT_WRAPLEN
;
245 log
->in1
= DEFAULT_INDENT1
;
246 log
->in2
= DEFAULT_INDENT2
;
250 log
->wrap
= parse_uint(&arg
, ',', DEFAULT_WRAPLEN
);
251 log
->in1
= parse_uint(&arg
, ',', DEFAULT_INDENT1
);
252 log
->in2
= parse_uint(&arg
, '\0', DEFAULT_INDENT2
);
253 if (log
->wrap
< 0 || log
->in1
< 0 || log
->in2
< 0)
254 return error(wrap_arg_usage
);
256 ((log
->in1
&& log
->wrap
<= log
->in1
) ||
257 (log
->in2
&& log
->wrap
<= log
->in2
)))
258 return error(wrap_arg_usage
);
262 void shortlog_init(struct shortlog
*log
)
264 memset(log
, 0, sizeof(*log
));
266 read_mailmap(&log
->mailmap
, &log
->common_repo_prefix
);
268 log
->list
.strdup_strings
= 1;
269 log
->wrap
= DEFAULT_WRAPLEN
;
270 log
->in1
= DEFAULT_INDENT1
;
271 log
->in2
= DEFAULT_INDENT2
;
274 int cmd_shortlog(int argc
, const char **argv
, const char *prefix
)
276 static struct shortlog log
;
277 static struct rev_info rev
;
278 static const char *sort_key_regexp
= NULL
;
279 static int sort_key_field
= 0;
282 static const struct option options
[] = {
283 OPT_BOOLEAN('n', "numbered", &log
.sort_by_number
,
284 "sort output according to the number of commits per author"),
285 OPT_BOOLEAN('s', "summary", &log
.summary
,
286 "Suppress commit descriptions, only provides commit count"),
287 OPT_BOOLEAN('e', "email", &log
.email
,
288 "Show the email address of each author"),
289 { OPTION_CALLBACK
, 'w', NULL
, &log
, "w[,i1[,i2]]",
290 "Linewrap output", PARSE_OPT_OPTARG
, &parse_wrap_args
},
291 OPT_STRING('K', "sort-key-regexp", &sort_key_regexp
, "<regexp>",
292 "Sort shortlog by the given regular expression"),
293 OPT_INTEGER('k', "sort-key", &sort_key_field
,
294 "Sort shortlog by the given field "
295 "(whitespace-delimited)"),
299 struct parse_opt_ctx_t ctx
;
301 prefix
= setup_git_directory_gently(&nongit
);
302 git_config(git_default_config
, NULL
);
304 init_revisions(&rev
, prefix
);
305 parse_options_start(&ctx
, argc
, argv
, prefix
, PARSE_OPT_KEEP_DASHDASH
|
306 PARSE_OPT_KEEP_ARGV0
);
309 switch (parse_options_step(&ctx
, options
, shortlog_usage
)) {
315 parse_revision_opt(&rev
, &ctx
, options
, shortlog_usage
);
318 argc
= parse_options_end(&ctx
);
320 if (sort_key_regexp
&& sort_key_field
> 0)
321 die ("--sort-key-regexp and --sort-key-field are incompatible");
323 if (sort_key_regexp
) {
324 log
.sort_key
= xmalloc(sizeof(*log
.sort_key
));
325 if (regcomp(log
.sort_key
, sort_key_regexp
, 0))
326 die ("Invalid regular expression: '%s'",
329 if (sort_key_field
> 0) {
330 struct strbuf regexp
= STRBUF_INIT
;
331 strbuf_addstr(®exp
, "^[ \t\n]*");
332 while (--sort_key_field
)
333 strbuf_addstr(®exp
, "[^ \t\n]*[ \t\n]*");
334 strbuf_addstr(®exp
, "\\([^ \t\n]*\\)[ \t\n]*.*$");
335 log
.sort_key
= xmalloc(sizeof(*log
.sort_key
));
336 if (regcomp(log
.sort_key
, regexp
.buf
, 0))
337 die ("Invalid regular expression: '%s'", regexp
.buf
);
338 strbuf_release(®exp
);
341 if (setup_revisions(argc
, argv
, &rev
, NULL
) != 1) {
342 error("unrecognized argument: %s", argv
[1]);
343 usage_with_options(shortlog_usage
, options
);
346 log
.user_format
= rev
.commit_format
== CMIT_FMT_USERFORMAT
;
348 /* assume HEAD if from a tty */
349 if (!nongit
&& !rev
.pending
.nr
&& isatty(0))
350 add_head_to_pending(&rev
);
351 if (rev
.pending
.nr
== 0) {
353 die ("Specifying a sort key is incompatible with "
354 "reading from stdin.");
355 read_from_stdin(&log
);
358 get_from_rev(&rev
, &log
);
360 shortlog_output(&log
);
364 void shortlog_output(struct shortlog
*log
)
367 if (log
->sort_by_number
)
368 qsort(log
->list
.items
, log
->list
.nr
, sizeof(struct string_list_item
),
370 for (i
= 0; i
< log
->list
.nr
; i
++) {
371 struct string_list
*onelines
= log
->list
.items
[i
].util
;
374 printf("%6d\t%s\n", onelines
->nr
, log
->list
.items
[i
].string
);
376 printf("%s (%d):\n", log
->list
.items
[i
].string
, onelines
->nr
);
377 for (j
= onelines
->nr
- 1; j
>= 0; j
--) {
378 const char *msg
= onelines
->items
[j
].string
;
380 if (log
->wrap_lines
) {
381 int col
= print_wrapped_text(msg
, log
->in1
, log
->in2
, log
->wrap
);
382 if (col
!= log
->wrap
)
386 printf(" %s\n", msg
);
391 onelines
->strdup_strings
= 1;
392 string_list_clear(onelines
, 0);
394 log
->list
.items
[i
].util
= NULL
;
397 log
->list
.strdup_strings
= 1;
398 string_list_clear(&log
->list
, 1);
399 clear_mailmap(&log
->mailmap
);