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 path_list_item
*i1
= a1
, *i2
= a2
;
22 const struct path_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 path_list_item
*item
;
39 struct path_list
*onelines
;
43 const char *boemail
, *eoemail
;
45 boemail
= strchr(author
, '<');
48 eoemail
= strchr(boemail
, '>');
51 if (!map_email(&log
->mailmap
, boemail
+1, namebuf
, sizeof(namebuf
))) {
52 while (author
< boemail
&& isspace(*author
))
55 len
< sizeof(namebuf
) - 1 && author
+ len
< boemail
;
57 namebuf
[len
] = author
[len
];
58 while (0 < len
&& isspace(namebuf
[len
-1]))
63 len
= strlen(namebuf
);
66 size_t room
= sizeof(namebuf
) - len
- 1;
67 int maillen
= eoemail
- boemail
+ 1;
68 snprintf(namebuf
+ len
, room
, " %.*s", maillen
, boemail
);
71 buffer
= xstrdup(namebuf
);
72 item
= path_list_insert(buffer
, &log
->list
);
73 if (item
->util
== NULL
)
74 item
->util
= xcalloc(1, sizeof(struct path_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 while (len
&& isspace(oneline
[len
-1]))
94 buffer
= xmemdupz(oneline
, len
);
97 int dot3len
= strlen(dot3
);
99 while ((p
= strstr(buffer
, dot3
)) != NULL
) {
100 int taillen
= strlen(p
) - dot3len
;
101 memcpy(p
, "/.../", 5);
102 memmove(p
+ 5, p
+ dot3len
, taillen
+ 1);
107 onelines
= item
->util
;
108 if (onelines
->nr
>= onelines
->alloc
) {
109 onelines
->alloc
= alloc_nr(onelines
->nr
);
110 onelines
->items
= xrealloc(onelines
->items
,
112 * sizeof(struct path_list_item
));
115 onelines
->items
[onelines
->nr
].util
= NULL
;
116 onelines
->items
[onelines
->nr
++].path
= buffer
;
119 static void read_from_stdin(struct shortlog
*log
)
121 char author
[1024], oneline
[1024];
123 while (fgets(author
, sizeof(author
), stdin
) != NULL
) {
124 if (!(author
[0] == 'A' || author
[0] == 'a') ||
125 prefixcmp(author
+ 1, "uthor: "))
127 while (fgets(oneline
, sizeof(oneline
), stdin
) &&
129 ; /* discard headers */
130 while (fgets(oneline
, sizeof(oneline
), stdin
) &&
132 ; /* discard blanks */
133 insert_one_record(log
, author
+ 8, oneline
);
137 void shortlog_add_commit(struct shortlog
*log
, struct commit
*commit
)
139 const char *author
= NULL
, *buffer
;
141 buffer
= commit
->buffer
;
142 while (*buffer
&& *buffer
!= '\n') {
143 const char *eol
= strchr(buffer
, '\n');
146 eol
= buffer
+ strlen(buffer
);
150 if (!prefixcmp(buffer
, "author "))
155 die("Missing author: %s",
156 sha1_to_hex(commit
->object
.sha1
));
159 insert_one_record(log
, author
, !*buffer
? "<none>" : buffer
);
162 static void get_from_rev(struct rev_info
*rev
, struct shortlog
*log
)
164 struct commit
*commit
;
166 if (prepare_revision_walk(rev
))
167 die("revision walk setup failed");
168 while ((commit
= get_revision(rev
)) != NULL
)
169 shortlog_add_commit(log
, commit
);
172 static int parse_uint(char const **arg
, int comma
, int defval
)
178 ul
= strtoul(*arg
, &endp
, 10);
179 if (*endp
&& *endp
!= comma
)
183 ret
= *arg
== endp
? defval
: (int)ul
;
184 *arg
= *endp
? endp
+ 1 : endp
;
188 static const char wrap_arg_usage
[] = "-w[<width>[,<indent1>[,<indent2>]]]";
189 #define DEFAULT_WRAPLEN 76
190 #define DEFAULT_INDENT1 6
191 #define DEFAULT_INDENT2 9
193 static int parse_wrap_args(const struct option
*opt
, const char *arg
, int unset
)
195 struct shortlog
*log
= opt
->value
;
197 log
->wrap_lines
= !unset
;
201 log
->wrap
= DEFAULT_WRAPLEN
;
202 log
->in1
= DEFAULT_INDENT1
;
203 log
->in2
= DEFAULT_INDENT2
;
207 log
->wrap
= parse_uint(&arg
, ',', DEFAULT_WRAPLEN
);
208 log
->in1
= parse_uint(&arg
, ',', DEFAULT_INDENT1
);
209 log
->in2
= parse_uint(&arg
, '\0', DEFAULT_INDENT2
);
210 if (log
->wrap
< 0 || log
->in1
< 0 || log
->in2
< 0)
211 return error(wrap_arg_usage
);
213 ((log
->in1
&& log
->wrap
<= log
->in1
) ||
214 (log
->in2
&& log
->wrap
<= log
->in2
)))
215 return error(wrap_arg_usage
);
219 void shortlog_init(struct shortlog
*log
)
221 memset(log
, 0, sizeof(*log
));
223 read_mailmap(&log
->mailmap
, ".mailmap", &log
->common_repo_prefix
);
225 log
->list
.strdup_paths
= 1;
226 log
->wrap
= DEFAULT_WRAPLEN
;
227 log
->in1
= DEFAULT_INDENT1
;
228 log
->in2
= DEFAULT_INDENT2
;
231 int cmd_shortlog(int argc
, const char **argv
, const char *prefix
)
233 static struct shortlog log
;
234 static struct rev_info rev
;
237 static const struct option options
[] = {
238 OPT_BOOLEAN('n', "numbered", &log
.sort_by_number
,
239 "sort output according to the number of commits per author"),
240 OPT_BOOLEAN('s', "summary", &log
.summary
,
241 "Suppress commit descriptions, only provides commit count"),
242 OPT_BOOLEAN('e', "email", &log
.email
,
243 "Show the email address of each author"),
244 { OPTION_CALLBACK
, 'w', NULL
, &log
, "w[,i1[,i2]]",
245 "Linewrap output", PARSE_OPT_OPTARG
, &parse_wrap_args
},
249 struct parse_opt_ctx_t ctx
;
251 prefix
= setup_git_directory_gently(&nongit
);
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 /* assume HEAD if from a tty */
275 if (!nongit
&& !rev
.pending
.nr
&& isatty(0))
276 add_head_to_pending(&rev
);
277 if (rev
.pending
.nr
== 0) {
278 read_from_stdin(&log
);
281 get_from_rev(&rev
, &log
);
283 shortlog_output(&log
);
287 void shortlog_output(struct shortlog
*log
)
290 if (log
->sort_by_number
)
291 qsort(log
->list
.items
, log
->list
.nr
, sizeof(struct path_list_item
),
293 for (i
= 0; i
< log
->list
.nr
; i
++) {
294 struct path_list
*onelines
= log
->list
.items
[i
].util
;
297 printf("%6d\t%s\n", onelines
->nr
, log
->list
.items
[i
].path
);
299 printf("%s (%d):\n", log
->list
.items
[i
].path
, onelines
->nr
);
300 for (j
= onelines
->nr
- 1; j
>= 0; j
--) {
301 const char *msg
= onelines
->items
[j
].path
;
303 if (log
->wrap_lines
) {
304 int col
= print_wrapped_text(msg
, log
->in1
, log
->in2
, log
->wrap
);
305 if (col
!= log
->wrap
)
309 printf(" %s\n", msg
);
314 onelines
->strdup_paths
= 1;
315 path_list_clear(onelines
, 1);
317 log
->list
.items
[i
].util
= NULL
;
320 log
->list
.strdup_paths
= 1;
321 path_list_clear(&log
->list
, 1);
322 log
->mailmap
.strdup_paths
= 1;
323 path_list_clear(&log
->mailmap
, 1);