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 eol
= strchr(oneline
, '\n');
75 eol
= oneline
+ strlen(oneline
);
76 while (*oneline
&& isspace(*oneline
) && *oneline
!= '\n')
78 if (!prefixcmp(oneline
, "[PATCH")) {
79 char *eob
= strchr(oneline
, ']');
80 if (eob
&& (!eol
|| eob
< eol
))
83 while (*oneline
&& isspace(*oneline
) && *oneline
!= '\n')
86 while (len
&& isspace(oneline
[len
-1]))
88 buffer
= xmemdupz(oneline
, len
);
91 int dot3len
= strlen(dot3
);
93 while ((p
= strstr(buffer
, dot3
)) != NULL
) {
94 int taillen
= strlen(p
) - dot3len
;
95 memcpy(p
, "/.../", 5);
96 memmove(p
+ 5, p
+ dot3len
, taillen
+ 1);
101 onelines
= item
->util
;
102 if (onelines
->nr
>= onelines
->alloc
) {
103 onelines
->alloc
= alloc_nr(onelines
->nr
);
104 onelines
->items
= xrealloc(onelines
->items
,
106 * sizeof(struct path_list_item
));
109 onelines
->items
[onelines
->nr
].util
= NULL
;
110 onelines
->items
[onelines
->nr
++].path
= buffer
;
113 static void read_from_stdin(struct shortlog
*log
)
115 char author
[1024], oneline
[1024];
117 while (fgets(author
, sizeof(author
), stdin
) != NULL
) {
118 if (!(author
[0] == 'A' || author
[0] == 'a') ||
119 prefixcmp(author
+ 1, "uthor: "))
121 while (fgets(oneline
, sizeof(oneline
), stdin
) &&
123 ; /* discard headers */
124 while (fgets(oneline
, sizeof(oneline
), stdin
) &&
126 ; /* discard blanks */
127 insert_one_record(log
, author
+ 8, oneline
);
131 void shortlog_add_commit(struct shortlog
*log
, struct commit
*commit
)
133 const char *author
= NULL
, *buffer
;
135 buffer
= commit
->buffer
;
136 while (*buffer
&& *buffer
!= '\n') {
137 const char *eol
= strchr(buffer
, '\n');
140 eol
= buffer
+ strlen(buffer
);
144 if (!prefixcmp(buffer
, "author "))
149 die("Missing author: %s",
150 sha1_to_hex(commit
->object
.sha1
));
153 insert_one_record(log
, author
, !*buffer
? "<none>" : buffer
);
156 static void get_from_rev(struct rev_info
*rev
, struct shortlog
*log
)
158 struct commit
*commit
;
160 if (prepare_revision_walk(rev
))
161 die("revision walk setup failed");
162 while ((commit
= get_revision(rev
)) != NULL
)
163 shortlog_add_commit(log
, commit
);
166 static int parse_uint(char const **arg
, int comma
)
172 ul
= strtoul(*arg
, &endp
, 10);
173 if (endp
!= *arg
&& *endp
&& *endp
!= comma
)
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 void parse_wrap_args(const char *arg
, int *in1
, int *in2
, int *wrap
)
191 arg
+= 2; /* skip -w */
193 *wrap
= parse_uint(&arg
, ',');
196 *in1
= parse_uint(&arg
, ',');
199 *in2
= parse_uint(&arg
, '\0');
204 *wrap
= DEFAULT_WRAPLEN
;
206 *in1
= DEFAULT_INDENT1
;
208 *in2
= DEFAULT_INDENT2
;
210 ((*in1
&& *wrap
<= *in1
) ||
211 (*in2
&& *wrap
<= *in2
)))
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_paths
= 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
)
234 /* since -n is a shadowed rev argument, parse our args first */
236 if (!strcmp(argv
[1], "-n") || !strcmp(argv
[1], "--numbered"))
237 log
.sort_by_number
= 1;
238 else if (!strcmp(argv
[1], "-s") ||
239 !strcmp(argv
[1], "--summary"))
241 else if (!strcmp(argv
[1], "-e") ||
242 !strcmp(argv
[1], "--email"))
244 else if (!prefixcmp(argv
[1], "-w")) {
246 parse_wrap_args(argv
[1], &log
.in1
, &log
.in2
, &log
.wrap
);
248 else if (!strcmp(argv
[1], "-h") || !strcmp(argv
[1], "--help"))
249 usage(shortlog_usage
);
255 init_revisions(&rev
, prefix
);
256 argc
= setup_revisions(argc
, argv
, &rev
, NULL
);
258 die ("unrecognized argument: %s", argv
[1]);
260 /* assume HEAD if from a tty */
261 if (!rev
.pending
.nr
&& isatty(0))
262 add_head_to_pending(&rev
);
263 if (rev
.pending
.nr
== 0) {
264 read_from_stdin(&log
);
267 get_from_rev(&rev
, &log
);
269 shortlog_output(&log
);
273 void shortlog_output(struct shortlog
*log
)
276 if (log
->sort_by_number
)
277 qsort(log
->list
.items
, log
->list
.nr
, sizeof(struct path_list_item
),
279 for (i
= 0; i
< log
->list
.nr
; i
++) {
280 struct path_list
*onelines
= log
->list
.items
[i
].util
;
283 printf("%6d\t%s\n", onelines
->nr
, log
->list
.items
[i
].path
);
285 printf("%s (%d):\n", log
->list
.items
[i
].path
, onelines
->nr
);
286 for (j
= onelines
->nr
- 1; j
>= 0; j
--) {
287 const char *msg
= onelines
->items
[j
].path
;
289 if (log
->wrap_lines
) {
290 int col
= print_wrapped_text(msg
, log
->in1
, log
->in2
, log
->wrap
);
291 if (col
!= log
->wrap
)
295 printf(" %s\n", msg
);
300 onelines
->strdup_paths
= 1;
301 path_list_clear(onelines
, 1);
303 log
->list
.items
[i
].util
= NULL
;
306 log
->list
.strdup_paths
= 1;
307 path_list_clear(&log
->list
, 1);
308 log
->mailmap
.strdup_paths
= 1;
309 path_list_clear(&log
->mailmap
, 1);