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
;
45 const char *boemail
, *eoemail
;
46 struct strbuf subject
= STRBUF_INIT
;
48 boemail
= strchr(author
, '<');
51 eoemail
= strchr(boemail
, '>');
54 if (!map_email(&log
->mailmap
, boemail
+1, namebuf
, sizeof(namebuf
))) {
55 while (author
< boemail
&& isspace(*author
))
58 len
< sizeof(namebuf
) - 1 && author
+ len
< boemail
;
60 namebuf
[len
] = author
[len
];
61 while (0 < len
&& isspace(namebuf
[len
-1]))
66 len
= strlen(namebuf
);
69 size_t room
= sizeof(namebuf
) - len
- 1;
70 int maillen
= eoemail
- boemail
+ 1;
71 snprintf(namebuf
+ len
, room
, " %.*s", maillen
, boemail
);
74 item
= string_list_insert(namebuf
, &log
->list
);
75 if (item
->util
== NULL
)
76 item
->util
= xcalloc(1, sizeof(struct string_list
));
78 /* Skip any leading whitespace, including any blank lines. */
79 while (*oneline
&& isspace(*oneline
))
81 eol
= strchr(oneline
, '\n');
83 eol
= oneline
+ strlen(oneline
);
84 if (!prefixcmp(oneline
, "[PATCH")) {
85 char *eob
= strchr(oneline
, ']');
86 if (eob
&& (!eol
|| eob
< eol
))
89 while (*oneline
&& isspace(*oneline
) && *oneline
!= '\n')
92 format_subject(&subject
, oneline
, " ");
93 buffer
= strbuf_detach(&subject
, NULL
);
96 int dot3len
= strlen(dot3
);
98 while ((p
= strstr(buffer
, dot3
)) != NULL
) {
99 int taillen
= strlen(p
) - dot3len
;
100 memcpy(p
, "/.../", 5);
101 memmove(p
+ 5, p
+ dot3len
, taillen
+ 1);
106 string_list_append(buffer
, item
->util
);
109 static void read_from_stdin(struct shortlog
*log
)
111 char author
[1024], oneline
[1024];
113 while (fgets(author
, sizeof(author
), stdin
) != NULL
) {
114 if (!(author
[0] == 'A' || author
[0] == 'a') ||
115 prefixcmp(author
+ 1, "uthor: "))
117 while (fgets(oneline
, sizeof(oneline
), stdin
) &&
119 ; /* discard headers */
120 while (fgets(oneline
, sizeof(oneline
), stdin
) &&
122 ; /* discard blanks */
123 insert_one_record(log
, author
+ 8, oneline
);
127 void shortlog_add_commit(struct shortlog
*log
, struct commit
*commit
)
129 const char *author
= NULL
, *buffer
;
131 buffer
= commit
->buffer
;
132 while (*buffer
&& *buffer
!= '\n') {
133 const char *eol
= strchr(buffer
, '\n');
136 eol
= buffer
+ strlen(buffer
);
140 if (!prefixcmp(buffer
, "author "))
145 die("Missing author: %s",
146 sha1_to_hex(commit
->object
.sha1
));
147 if (log
->user_format
) {
148 struct strbuf buf
= STRBUF_INIT
;
150 pretty_print_commit(CMIT_FMT_USERFORMAT
, commit
, &buf
,
151 DEFAULT_ABBREV
, "", "", DATE_NORMAL
, 0);
152 insert_one_record(log
, author
, buf
.buf
);
153 strbuf_release(&buf
);
158 insert_one_record(log
, author
, !*buffer
? "<none>" : buffer
);
161 static void get_from_rev(struct rev_info
*rev
, struct shortlog
*log
)
163 struct commit
*commit
;
165 if (prepare_revision_walk(rev
))
166 die("revision walk setup failed");
167 while ((commit
= get_revision(rev
)) != NULL
)
168 shortlog_add_commit(log
, commit
);
171 static int parse_uint(char const **arg
, int comma
, int defval
)
177 ul
= strtoul(*arg
, &endp
, 10);
178 if (*endp
&& *endp
!= comma
)
182 ret
= *arg
== endp
? defval
: (int)ul
;
183 *arg
= *endp
? endp
+ 1 : endp
;
187 static const char wrap_arg_usage
[] = "-w[<width>[,<indent1>[,<indent2>]]]";
188 #define DEFAULT_WRAPLEN 76
189 #define DEFAULT_INDENT1 6
190 #define DEFAULT_INDENT2 9
192 static int parse_wrap_args(const struct option
*opt
, const char *arg
, int unset
)
194 struct shortlog
*log
= opt
->value
;
196 log
->wrap_lines
= !unset
;
200 log
->wrap
= DEFAULT_WRAPLEN
;
201 log
->in1
= DEFAULT_INDENT1
;
202 log
->in2
= DEFAULT_INDENT2
;
206 log
->wrap
= parse_uint(&arg
, ',', DEFAULT_WRAPLEN
);
207 log
->in1
= parse_uint(&arg
, ',', DEFAULT_INDENT1
);
208 log
->in2
= parse_uint(&arg
, '\0', DEFAULT_INDENT2
);
209 if (log
->wrap
< 0 || log
->in1
< 0 || log
->in2
< 0)
210 return error(wrap_arg_usage
);
212 ((log
->in1
&& log
->wrap
<= log
->in1
) ||
213 (log
->in2
&& log
->wrap
<= log
->in2
)))
214 return error(wrap_arg_usage
);
218 void shortlog_init(struct shortlog
*log
)
220 memset(log
, 0, sizeof(*log
));
222 read_mailmap(&log
->mailmap
, &log
->common_repo_prefix
);
224 log
->list
.strdup_strings
= 1;
225 log
->wrap
= DEFAULT_WRAPLEN
;
226 log
->in1
= DEFAULT_INDENT1
;
227 log
->in2
= DEFAULT_INDENT2
;
230 int cmd_shortlog(int argc
, const char **argv
, const char *prefix
)
232 static struct shortlog log
;
233 static struct rev_info rev
;
236 static const struct option options
[] = {
237 OPT_BOOLEAN('n', "numbered", &log
.sort_by_number
,
238 "sort output according to the number of commits per author"),
239 OPT_BOOLEAN('s', "summary", &log
.summary
,
240 "Suppress commit descriptions, only provides commit count"),
241 OPT_BOOLEAN('e', "email", &log
.email
,
242 "Show the email address of each author"),
243 { OPTION_CALLBACK
, 'w', NULL
, &log
, "w[,i1[,i2]]",
244 "Linewrap output", PARSE_OPT_OPTARG
, &parse_wrap_args
},
248 struct parse_opt_ctx_t ctx
;
250 prefix
= setup_git_directory_gently(&nongit
);
251 git_config(git_default_config
, NULL
);
253 init_revisions(&rev
, prefix
);
254 parse_options_start(&ctx
, argc
, argv
, PARSE_OPT_KEEP_DASHDASH
|
255 PARSE_OPT_KEEP_ARGV0
);
258 switch (parse_options_step(&ctx
, options
, shortlog_usage
)) {
264 parse_revision_opt(&rev
, &ctx
, options
, shortlog_usage
);
267 argc
= parse_options_end(&ctx
);
269 if (setup_revisions(argc
, argv
, &rev
, NULL
) != 1) {
270 error("unrecognized argument: %s", argv
[1]);
271 usage_with_options(shortlog_usage
, options
);
274 log
.user_format
= rev
.commit_format
== CMIT_FMT_USERFORMAT
;
276 /* assume HEAD if from a tty */
277 if (!nongit
&& !rev
.pending
.nr
&& isatty(0))
278 add_head_to_pending(&rev
);
279 if (rev
.pending
.nr
== 0) {
280 read_from_stdin(&log
);
283 get_from_rev(&rev
, &log
);
285 shortlog_output(&log
);
289 void shortlog_output(struct shortlog
*log
)
292 if (log
->sort_by_number
)
293 qsort(log
->list
.items
, log
->list
.nr
, sizeof(struct string_list_item
),
295 for (i
= 0; i
< log
->list
.nr
; i
++) {
296 struct string_list
*onelines
= log
->list
.items
[i
].util
;
299 printf("%6d\t%s\n", onelines
->nr
, log
->list
.items
[i
].string
);
301 printf("%s (%d):\n", log
->list
.items
[i
].string
, onelines
->nr
);
302 for (j
= onelines
->nr
- 1; j
>= 0; j
--) {
303 const char *msg
= onelines
->items
[j
].string
;
305 if (log
->wrap_lines
) {
306 int col
= print_wrapped_text(msg
, log
->in1
, log
->in2
, log
->wrap
);
307 if (col
!= log
->wrap
)
311 printf(" %s\n", msg
);
316 onelines
->strdup_strings
= 1;
317 string_list_clear(onelines
, 0);
319 log
->list
.items
[i
].util
= NULL
;
322 log
->list
.strdup_strings
= 1;
323 string_list_clear(&log
->list
, 1);
324 log
->mailmap
.strdup_strings
= 1;
325 string_list_clear(&log
->mailmap
, 1);