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_record(struct shortlog
*log
,
39 const char *dot3
= log
->common_repo_prefix
;
41 struct string_list_item
*item
;
46 const char *boemail
, *eoemail
;
47 struct strbuf subject
= STRBUF_INIT
;
49 boemail
= strchr(author
, '<');
52 eoemail
= strchr(boemail
, '>');
56 /* copy author name to namebuf, to support matching on both name and email */
57 memcpy(namebuf
, author
, boemail
- author
);
58 len
= boemail
- author
;
59 while(len
> 0 && isspace(namebuf
[len
-1]))
63 /* copy email name to emailbuf, to allow email replacement as well */
64 memcpy(emailbuf
, boemail
+1, eoemail
- boemail
);
65 emailbuf
[eoemail
- boemail
- 1] = 0;
67 if (!map_user(&log
->mailmap
, emailbuf
, sizeof(emailbuf
), namebuf
, sizeof(namebuf
))) {
68 while (author
< boemail
&& isspace(*author
))
71 len
< sizeof(namebuf
) - 1 && author
+ len
< boemail
;
73 namebuf
[len
] = author
[len
];
74 while (0 < len
&& isspace(namebuf
[len
-1]))
79 len
= strlen(namebuf
);
82 size_t room
= sizeof(namebuf
) - len
- 1;
83 int maillen
= strlen(emailbuf
);
84 snprintf(namebuf
+ len
, room
, " <%.*s>", maillen
, emailbuf
);
87 item
= string_list_insert(namebuf
, &log
->list
);
88 if (item
->util
== NULL
)
89 item
->util
= xcalloc(1, sizeof(struct string_list
));
91 /* Skip any leading whitespace, including any blank lines. */
92 while (*oneline
&& isspace(*oneline
))
94 eol
= strchr(oneline
, '\n');
96 eol
= oneline
+ strlen(oneline
);
97 if (!prefixcmp(oneline
, "[PATCH")) {
98 char *eob
= strchr(oneline
, ']');
99 if (eob
&& (!eol
|| eob
< eol
))
102 while (*oneline
&& isspace(*oneline
) && *oneline
!= '\n')
104 format_subject(&subject
, oneline
, " ");
105 buffer
= strbuf_detach(&subject
, NULL
);
108 int dot3len
= strlen(dot3
);
110 while ((p
= strstr(buffer
, dot3
)) != NULL
) {
111 int taillen
= strlen(p
) - dot3len
;
112 memcpy(p
, "/.../", 5);
113 memmove(p
+ 5, p
+ dot3len
, taillen
+ 1);
118 string_list_append(buffer
, item
->util
);
121 static void read_from_stdin(struct shortlog
*log
)
123 char author
[1024], oneline
[1024];
125 while (fgets(author
, sizeof(author
), stdin
) != NULL
) {
126 if (!(author
[0] == 'A' || author
[0] == 'a') ||
127 prefixcmp(author
+ 1, "uthor: "))
129 while (fgets(oneline
, sizeof(oneline
), stdin
) &&
131 ; /* discard headers */
132 while (fgets(oneline
, sizeof(oneline
), stdin
) &&
134 ; /* discard blanks */
135 insert_one_record(log
, author
+ 8, oneline
);
139 void shortlog_add_commit(struct shortlog
*log
, struct commit
*commit
)
141 const char *author
= NULL
, *buffer
;
143 buffer
= commit
->buffer
;
144 while (*buffer
&& *buffer
!= '\n') {
145 const char *eol
= strchr(buffer
, '\n');
148 eol
= buffer
+ strlen(buffer
);
152 if (!prefixcmp(buffer
, "author "))
157 die("Missing author: %s",
158 sha1_to_hex(commit
->object
.sha1
));
159 if (log
->user_format
) {
160 struct strbuf buf
= STRBUF_INIT
;
162 pretty_print_commit(CMIT_FMT_USERFORMAT
, commit
, &buf
,
163 DEFAULT_ABBREV
, "", "", DATE_NORMAL
, 0);
164 insert_one_record(log
, author
, buf
.buf
);
165 strbuf_release(&buf
);
170 insert_one_record(log
, author
, !*buffer
? "<none>" : buffer
);
173 static void get_from_rev(struct rev_info
*rev
, struct shortlog
*log
)
175 struct commit
*commit
;
177 if (prepare_revision_walk(rev
))
178 die("revision walk setup failed");
179 while ((commit
= get_revision(rev
)) != NULL
)
180 shortlog_add_commit(log
, commit
);
183 static int parse_uint(char const **arg
, int comma
, int defval
)
189 ul
= strtoul(*arg
, &endp
, 10);
190 if (*endp
&& *endp
!= comma
)
194 ret
= *arg
== endp
? defval
: (int)ul
;
195 *arg
= *endp
? endp
+ 1 : endp
;
199 static const char wrap_arg_usage
[] = "-w[<width>[,<indent1>[,<indent2>]]]";
200 #define DEFAULT_WRAPLEN 76
201 #define DEFAULT_INDENT1 6
202 #define DEFAULT_INDENT2 9
204 static int parse_wrap_args(const struct option
*opt
, const char *arg
, int unset
)
206 struct shortlog
*log
= opt
->value
;
208 log
->wrap_lines
= !unset
;
212 log
->wrap
= DEFAULT_WRAPLEN
;
213 log
->in1
= DEFAULT_INDENT1
;
214 log
->in2
= DEFAULT_INDENT2
;
218 log
->wrap
= parse_uint(&arg
, ',', DEFAULT_WRAPLEN
);
219 log
->in1
= parse_uint(&arg
, ',', DEFAULT_INDENT1
);
220 log
->in2
= parse_uint(&arg
, '\0', DEFAULT_INDENT2
);
221 if (log
->wrap
< 0 || log
->in1
< 0 || log
->in2
< 0)
222 return error(wrap_arg_usage
);
224 ((log
->in1
&& log
->wrap
<= log
->in1
) ||
225 (log
->in2
&& log
->wrap
<= log
->in2
)))
226 return error(wrap_arg_usage
);
230 void shortlog_init(struct shortlog
*log
)
232 memset(log
, 0, sizeof(*log
));
234 read_mailmap(&log
->mailmap
, &log
->common_repo_prefix
);
236 log
->list
.strdup_strings
= 1;
237 log
->wrap
= DEFAULT_WRAPLEN
;
238 log
->in1
= DEFAULT_INDENT1
;
239 log
->in2
= DEFAULT_INDENT2
;
242 int cmd_shortlog(int argc
, const char **argv
, const char *prefix
)
244 static struct shortlog log
;
245 static struct rev_info rev
;
248 static const struct option options
[] = {
249 OPT_BOOLEAN('n', "numbered", &log
.sort_by_number
,
250 "sort output according to the number of commits per author"),
251 OPT_BOOLEAN('s', "summary", &log
.summary
,
252 "Suppress commit descriptions, only provides commit count"),
253 OPT_BOOLEAN('e', "email", &log
.email
,
254 "Show the email address of each author"),
255 { OPTION_CALLBACK
, 'w', NULL
, &log
, "w[,i1[,i2]]",
256 "Linewrap output", PARSE_OPT_OPTARG
, &parse_wrap_args
},
260 struct parse_opt_ctx_t ctx
;
262 prefix
= setup_git_directory_gently(&nongit
);
263 git_config(git_default_config
, NULL
);
265 init_revisions(&rev
, prefix
);
266 parse_options_start(&ctx
, argc
, argv
, PARSE_OPT_KEEP_DASHDASH
|
267 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
;
288 /* assume HEAD if from a tty */
289 if (!nongit
&& !rev
.pending
.nr
&& isatty(0))
290 add_head_to_pending(&rev
);
291 if (rev
.pending
.nr
== 0) {
292 read_from_stdin(&log
);
295 get_from_rev(&rev
, &log
);
297 shortlog_output(&log
);
301 void shortlog_output(struct shortlog
*log
)
304 if (log
->sort_by_number
)
305 qsort(log
->list
.items
, log
->list
.nr
, sizeof(struct string_list_item
),
307 for (i
= 0; i
< log
->list
.nr
; i
++) {
308 struct string_list
*onelines
= log
->list
.items
[i
].util
;
311 printf("%6d\t%s\n", onelines
->nr
, log
->list
.items
[i
].string
);
313 printf("%s (%d):\n", log
->list
.items
[i
].string
, onelines
->nr
);
314 for (j
= onelines
->nr
- 1; j
>= 0; j
--) {
315 const char *msg
= onelines
->items
[j
].string
;
317 if (log
->wrap_lines
) {
318 int col
= print_wrapped_text(msg
, log
->in1
, log
->in2
, log
->wrap
);
319 if (col
!= log
->wrap
)
323 printf(" %s\n", msg
);
328 onelines
->strdup_strings
= 1;
329 string_list_clear(onelines
, 0);
331 log
->list
.items
[i
].util
= NULL
;
334 log
->list
.strdup_strings
= 1;
335 string_list_clear(&log
->list
, 1);
336 clear_mailmap(&log
->mailmap
);