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 static void insert_one_record(struct shortlog
*log
,
36 const char *dot3
= log
->common_repo_prefix
;
38 struct string_list_item
*item
;
42 const char *boemail
, *eoemail
;
44 boemail
= strchr(author
, '<');
47 eoemail
= strchr(boemail
, '>');
50 if (!map_email(&log
->mailmap
, boemail
+1, namebuf
, sizeof(namebuf
))) {
51 while (author
< boemail
&& isspace(*author
))
54 len
< sizeof(namebuf
) - 1 && author
+ len
< boemail
;
56 namebuf
[len
] = author
[len
];
57 while (0 < len
&& isspace(namebuf
[len
-1]))
62 len
= strlen(namebuf
);
65 size_t room
= sizeof(namebuf
) - len
- 1;
66 int maillen
= eoemail
- boemail
+ 1;
67 snprintf(namebuf
+ len
, room
, " %.*s", maillen
, boemail
);
70 item
= string_list_insert(namebuf
, &log
->list
);
71 if (item
->util
== NULL
)
72 item
->util
= xcalloc(1, sizeof(struct string_list
));
74 /* Skip any leading whitespace, including any blank lines. */
75 while (*oneline
&& isspace(*oneline
))
77 eol
= strchr(oneline
, '\n');
79 eol
= oneline
+ strlen(oneline
);
80 if (!prefixcmp(oneline
, "[PATCH")) {
81 char *eob
= strchr(oneline
, ']');
82 if (eob
&& (!eol
|| eob
< eol
))
85 while (*oneline
&& isspace(*oneline
) && *oneline
!= '\n')
88 while (len
&& isspace(oneline
[len
-1]))
90 buffer
= xmemdupz(oneline
, len
);
93 int dot3len
= strlen(dot3
);
95 while ((p
= strstr(buffer
, dot3
)) != NULL
) {
96 int taillen
= strlen(p
) - dot3len
;
97 memcpy(p
, "/.../", 5);
98 memmove(p
+ 5, p
+ dot3len
, taillen
+ 1);
103 string_list_append(buffer
, item
->util
);
106 static void read_from_stdin(struct shortlog
*log
)
108 char author
[1024], oneline
[1024];
110 while (fgets(author
, sizeof(author
), stdin
) != NULL
) {
111 if (!(author
[0] == 'A' || author
[0] == 'a') ||
112 prefixcmp(author
+ 1, "uthor: "))
114 while (fgets(oneline
, sizeof(oneline
), stdin
) &&
116 ; /* discard headers */
117 while (fgets(oneline
, sizeof(oneline
), stdin
) &&
119 ; /* discard blanks */
120 insert_one_record(log
, author
+ 8, oneline
);
124 void shortlog_add_commit(struct shortlog
*log
, struct commit
*commit
)
126 const char *author
= NULL
, *buffer
;
128 buffer
= commit
->buffer
;
129 while (*buffer
&& *buffer
!= '\n') {
130 const char *eol
= strchr(buffer
, '\n');
133 eol
= buffer
+ strlen(buffer
);
137 if (!prefixcmp(buffer
, "author "))
142 die("Missing author: %s",
143 sha1_to_hex(commit
->object
.sha1
));
144 if (log
->user_format
) {
145 struct strbuf buf
= STRBUF_INIT
;
147 pretty_print_commit(CMIT_FMT_USERFORMAT
, commit
, &buf
,
148 DEFAULT_ABBREV
, "", "", DATE_NORMAL
, 0);
149 insert_one_record(log
, author
, buf
.buf
);
150 strbuf_release(&buf
);
155 insert_one_record(log
, author
, !*buffer
? "<none>" : buffer
);
158 static void get_from_rev(struct rev_info
*rev
, struct shortlog
*log
)
160 struct commit
*commit
;
162 if (prepare_revision_walk(rev
))
163 die("revision walk setup failed");
164 while ((commit
= get_revision(rev
)) != NULL
)
165 shortlog_add_commit(log
, commit
);
168 static int parse_uint(char const **arg
, int comma
, int defval
)
174 ul
= strtoul(*arg
, &endp
, 10);
175 if (*endp
&& *endp
!= comma
)
179 ret
= *arg
== endp
? defval
: (int)ul
;
180 *arg
= *endp
? endp
+ 1 : endp
;
184 static const char wrap_arg_usage
[] = "-w[<width>[,<indent1>[,<indent2>]]]";
185 #define DEFAULT_WRAPLEN 76
186 #define DEFAULT_INDENT1 6
187 #define DEFAULT_INDENT2 9
189 static int parse_wrap_args(const struct option
*opt
, const char *arg
, int unset
)
191 struct shortlog
*log
= opt
->value
;
193 log
->wrap_lines
= !unset
;
197 log
->wrap
= DEFAULT_WRAPLEN
;
198 log
->in1
= DEFAULT_INDENT1
;
199 log
->in2
= DEFAULT_INDENT2
;
203 log
->wrap
= parse_uint(&arg
, ',', DEFAULT_WRAPLEN
);
204 log
->in1
= parse_uint(&arg
, ',', DEFAULT_INDENT1
);
205 log
->in2
= parse_uint(&arg
, '\0', DEFAULT_INDENT2
);
206 if (log
->wrap
< 0 || log
->in1
< 0 || log
->in2
< 0)
207 return error(wrap_arg_usage
);
209 ((log
->in1
&& log
->wrap
<= log
->in1
) ||
210 (log
->in2
&& log
->wrap
<= log
->in2
)))
211 return error(wrap_arg_usage
);
215 void shortlog_init(struct shortlog
*log
)
217 memset(log
, 0, sizeof(*log
));
219 read_mailmap(&log
->mailmap
, ".mailmap", &log
->common_repo_prefix
);
221 log
->list
.strdup_strings
= 1;
222 log
->wrap
= DEFAULT_WRAPLEN
;
223 log
->in1
= DEFAULT_INDENT1
;
224 log
->in2
= DEFAULT_INDENT2
;
227 int cmd_shortlog(int argc
, const char **argv
, const char *prefix
)
229 static struct shortlog log
;
230 static struct rev_info rev
;
233 static const struct option options
[] = {
234 OPT_BOOLEAN('n', "numbered", &log
.sort_by_number
,
235 "sort output according to the number of commits per author"),
236 OPT_BOOLEAN('s', "summary", &log
.summary
,
237 "Suppress commit descriptions, only provides commit count"),
238 OPT_BOOLEAN('e', "email", &log
.email
,
239 "Show the email address of each author"),
240 { OPTION_CALLBACK
, 'w', NULL
, &log
, "w[,i1[,i2]]",
241 "Linewrap output", PARSE_OPT_OPTARG
, &parse_wrap_args
},
245 struct parse_opt_ctx_t ctx
;
247 prefix
= setup_git_directory_gently(&nongit
);
249 init_revisions(&rev
, prefix
);
250 parse_options_start(&ctx
, argc
, argv
, PARSE_OPT_KEEP_DASHDASH
|
251 PARSE_OPT_KEEP_ARGV0
);
254 switch (parse_options_step(&ctx
, options
, shortlog_usage
)) {
260 parse_revision_opt(&rev
, &ctx
, options
, shortlog_usage
);
263 argc
= parse_options_end(&ctx
);
265 if (setup_revisions(argc
, argv
, &rev
, NULL
) != 1) {
266 error("unrecognized argument: %s", argv
[1]);
267 usage_with_options(shortlog_usage
, options
);
270 log
.user_format
= rev
.commit_format
== CMIT_FMT_USERFORMAT
;
272 /* assume HEAD if from a tty */
273 if (!nongit
&& !rev
.pending
.nr
&& isatty(0))
274 add_head_to_pending(&rev
);
275 if (rev
.pending
.nr
== 0) {
276 read_from_stdin(&log
);
279 get_from_rev(&rev
, &log
);
281 shortlog_output(&log
);
285 void shortlog_output(struct shortlog
*log
)
288 if (log
->sort_by_number
)
289 qsort(log
->list
.items
, log
->list
.nr
, sizeof(struct string_list_item
),
291 for (i
= 0; i
< log
->list
.nr
; i
++) {
292 struct string_list
*onelines
= log
->list
.items
[i
].util
;
295 printf("%6d\t%s\n", onelines
->nr
, log
->list
.items
[i
].string
);
297 printf("%s (%d):\n", log
->list
.items
[i
].string
, onelines
->nr
);
298 for (j
= onelines
->nr
- 1; j
>= 0; j
--) {
299 const char *msg
= onelines
->items
[j
].string
;
301 if (log
->wrap_lines
) {
302 int col
= print_wrapped_text(msg
, log
->in1
, log
->in2
, log
->wrap
);
303 if (col
!= log
->wrap
)
307 printf(" %s\n", msg
);
312 onelines
->strdup_strings
= 1;
313 string_list_clear(onelines
, 0);
315 log
->list
.items
[i
].util
= NULL
;
318 log
->list
.strdup_strings
= 1;
319 string_list_clear(&log
->list
, 1);
320 log
->mailmap
.strdup_strings
= 1;
321 string_list_clear(&log
->mailmap
, 1);