11 static const char shortlog_usage
[] =
12 "git-shortlog [-n] [-s] [-e] [<commit-id>... ]";
14 static int compare_by_number(const void *a1
, const void *a2
)
16 const struct path_list_item
*i1
= a1
, *i2
= a2
;
17 const struct path_list
*l1
= i1
->util
, *l2
= i2
->util
;
21 else if (l1
->nr
== l2
->nr
)
27 static void insert_one_record(struct shortlog
*log
,
31 const char *dot3
= log
->common_repo_prefix
;
33 struct path_list_item
*item
;
34 struct path_list
*onelines
;
38 const char *boemail
, *eoemail
;
40 boemail
= strchr(author
, '<');
43 eoemail
= strchr(boemail
, '>');
46 if (!map_email(&log
->mailmap
, boemail
+1, namebuf
, sizeof(namebuf
))) {
47 while (author
< boemail
&& isspace(*author
))
50 len
< sizeof(namebuf
) - 1 && author
+ len
< boemail
;
52 namebuf
[len
] = author
[len
];
53 while (0 < len
&& isspace(namebuf
[len
-1]))
58 len
= strlen(namebuf
);
61 size_t room
= sizeof(namebuf
) - len
- 1;
62 int maillen
= eoemail
- boemail
+ 1;
63 snprintf(namebuf
+ len
, room
, " %.*s", maillen
, boemail
);
66 buffer
= xstrdup(namebuf
);
67 item
= path_list_insert(buffer
, &log
->list
);
68 if (item
->util
== NULL
)
69 item
->util
= xcalloc(1, sizeof(struct path_list
));
73 /* Skip any leading whitespace, including any blank lines. */
74 while (*oneline
&& isspace(*oneline
))
76 eol
= strchr(oneline
, '\n');
78 eol
= oneline
+ strlen(oneline
);
79 if (!prefixcmp(oneline
, "[PATCH")) {
80 char *eob
= strchr(oneline
, ']');
81 if (eob
&& (!eol
|| eob
< eol
))
84 while (*oneline
&& isspace(*oneline
) && *oneline
!= '\n')
87 while (len
&& isspace(oneline
[len
-1]))
89 buffer
= xmemdupz(oneline
, len
);
92 int dot3len
= strlen(dot3
);
94 while ((p
= strstr(buffer
, dot3
)) != NULL
) {
95 int taillen
= strlen(p
) - dot3len
;
96 memcpy(p
, "/.../", 5);
97 memmove(p
+ 5, p
+ dot3len
, taillen
+ 1);
102 onelines
= item
->util
;
103 if (onelines
->nr
>= onelines
->alloc
) {
104 onelines
->alloc
= alloc_nr(onelines
->nr
);
105 onelines
->items
= xrealloc(onelines
->items
,
107 * sizeof(struct path_list_item
));
110 onelines
->items
[onelines
->nr
].util
= NULL
;
111 onelines
->items
[onelines
->nr
++].path
= buffer
;
114 static void read_from_stdin(struct shortlog
*log
)
116 char author
[1024], oneline
[1024];
118 while (fgets(author
, sizeof(author
), stdin
) != NULL
) {
119 if (!(author
[0] == 'A' || author
[0] == 'a') ||
120 prefixcmp(author
+ 1, "uthor: "))
122 while (fgets(oneline
, sizeof(oneline
), stdin
) &&
124 ; /* discard headers */
125 while (fgets(oneline
, sizeof(oneline
), stdin
) &&
127 ; /* discard blanks */
128 insert_one_record(log
, author
+ 8, oneline
);
132 void shortlog_add_commit(struct shortlog
*log
, struct commit
*commit
)
134 const char *author
= NULL
, *buffer
;
136 buffer
= commit
->buffer
;
137 while (*buffer
&& *buffer
!= '\n') {
138 const char *eol
= strchr(buffer
, '\n');
141 eol
= buffer
+ strlen(buffer
);
145 if (!prefixcmp(buffer
, "author "))
150 die("Missing author: %s",
151 sha1_to_hex(commit
->object
.sha1
));
154 insert_one_record(log
, author
, !*buffer
? "<none>" : buffer
);
157 static void get_from_rev(struct rev_info
*rev
, struct shortlog
*log
)
159 struct commit
*commit
;
161 if (prepare_revision_walk(rev
))
162 die("revision walk setup failed");
163 while ((commit
= get_revision(rev
)) != NULL
)
164 shortlog_add_commit(log
, commit
);
167 static int parse_uint(char const **arg
, int comma
)
173 ul
= strtoul(*arg
, &endp
, 10);
174 if (endp
!= *arg
&& *endp
&& *endp
!= comma
)
185 static const char wrap_arg_usage
[] = "-w[<width>[,<indent1>[,<indent2>]]]";
186 #define DEFAULT_WRAPLEN 76
187 #define DEFAULT_INDENT1 6
188 #define DEFAULT_INDENT2 9
190 static void parse_wrap_args(const char *arg
, int *in1
, int *in2
, int *wrap
)
192 arg
+= 2; /* skip -w */
194 *wrap
= parse_uint(&arg
, ',');
197 *in1
= parse_uint(&arg
, ',');
200 *in2
= parse_uint(&arg
, '\0');
205 *wrap
= DEFAULT_WRAPLEN
;
207 *in1
= DEFAULT_INDENT1
;
209 *in2
= DEFAULT_INDENT2
;
211 ((*in1
&& *wrap
<= *in1
) ||
212 (*in2
&& *wrap
<= *in2
)))
216 void shortlog_init(struct shortlog
*log
)
218 memset(log
, 0, sizeof(*log
));
220 read_mailmap(&log
->mailmap
, ".mailmap", &log
->common_repo_prefix
);
222 log
->list
.strdup_paths
= 1;
223 log
->wrap
= DEFAULT_WRAPLEN
;
224 log
->in1
= DEFAULT_INDENT1
;
225 log
->in2
= DEFAULT_INDENT2
;
228 int cmd_shortlog(int argc
, const char **argv
, const char *prefix
)
234 prefix
= setup_git_directory_gently(&nongit
);
237 /* since -n is a shadowed rev argument, parse our args first */
239 if (!strcmp(argv
[1], "-n") || !strcmp(argv
[1], "--numbered"))
240 log
.sort_by_number
= 1;
241 else if (!strcmp(argv
[1], "-s") ||
242 !strcmp(argv
[1], "--summary"))
244 else if (!strcmp(argv
[1], "-e") ||
245 !strcmp(argv
[1], "--email"))
247 else if (!prefixcmp(argv
[1], "-w")) {
249 parse_wrap_args(argv
[1], &log
.in1
, &log
.in2
, &log
.wrap
);
251 else if (!strcmp(argv
[1], "-h") || !strcmp(argv
[1], "--help"))
252 usage(shortlog_usage
);
258 init_revisions(&rev
, prefix
);
259 argc
= setup_revisions(argc
, argv
, &rev
, NULL
);
261 die ("unrecognized argument: %s", argv
[1]);
263 /* assume HEAD if from a tty */
264 if (!nongit
&& !rev
.pending
.nr
&& isatty(0))
265 add_head_to_pending(&rev
);
266 if (rev
.pending
.nr
== 0) {
267 read_from_stdin(&log
);
270 get_from_rev(&rev
, &log
);
272 shortlog_output(&log
);
276 void shortlog_output(struct shortlog
*log
)
279 if (log
->sort_by_number
)
280 qsort(log
->list
.items
, log
->list
.nr
, sizeof(struct path_list_item
),
282 for (i
= 0; i
< log
->list
.nr
; i
++) {
283 struct path_list
*onelines
= log
->list
.items
[i
].util
;
286 printf("%6d\t%s\n", onelines
->nr
, log
->list
.items
[i
].path
);
288 printf("%s (%d):\n", log
->list
.items
[i
].path
, onelines
->nr
);
289 for (j
= onelines
->nr
- 1; j
>= 0; j
--) {
290 const char *msg
= onelines
->items
[j
].path
;
292 if (log
->wrap_lines
) {
293 int col
= print_wrapped_text(msg
, log
->in1
, log
->in2
, log
->wrap
);
294 if (col
!= log
->wrap
)
298 printf(" %s\n", msg
);
303 onelines
->strdup_paths
= 1;
304 path_list_clear(onelines
, 1);
306 log
->list
.items
[i
].util
= NULL
;
309 log
->list
.strdup_paths
= 1;
310 path_list_clear(&log
->list
, 1);
311 log
->mailmap
.strdup_paths
= 1;
312 path_list_clear(&log
->mailmap
, 1);