10 static const char shortlog_usage
[] =
11 "git-shortlog [-n] [-s] [<commit-id>... ]";
13 static char *common_repo_prefix
;
15 static int compare_by_number(const void *a1
, const void *a2
)
17 const struct path_list_item
*i1
= a1
, *i2
= a2
;
18 const struct path_list
*l1
= i1
->util
, *l2
= i2
->util
;
22 else if (l1
->nr
== l2
->nr
)
28 static struct path_list mailmap
= {NULL
, 0, 0, 0};
30 static void insert_author_oneline(struct path_list
*list
,
31 const char *author
, int authorlen
,
32 const char *oneline
, int onelinelen
)
34 const char *dot3
= common_repo_prefix
;
36 struct path_list_item
*item
;
37 struct path_list
*onelines
;
39 while (authorlen
> 0 && isspace(author
[authorlen
- 1]))
42 buffer
= xmalloc(authorlen
+ 1);
43 memcpy(buffer
, author
, authorlen
);
44 buffer
[authorlen
] = '\0';
46 item
= path_list_insert(buffer
, list
);
47 if (item
->util
== NULL
)
48 item
->util
= xcalloc(1, sizeof(struct path_list
));
52 if (!prefixcmp(oneline
, "[PATCH")) {
53 char *eob
= strchr(oneline
, ']');
56 while (isspace(eob
[1]) && eob
[1] != '\n')
58 if (eob
- oneline
< onelinelen
) {
59 onelinelen
-= eob
- oneline
;
65 while (onelinelen
> 0 && isspace(oneline
[0])) {
70 while (onelinelen
> 0 && isspace(oneline
[onelinelen
- 1]))
73 buffer
= xmalloc(onelinelen
+ 1);
74 memcpy(buffer
, oneline
, onelinelen
);
75 buffer
[onelinelen
] = '\0';
78 int dot3len
= strlen(dot3
);
80 while ((p
= strstr(buffer
, dot3
)) != NULL
) {
81 int taillen
= strlen(p
) - dot3len
;
82 memcpy(p
, "/.../", 5);
83 memmove(p
+ 5, p
+ dot3len
, taillen
+ 1);
88 onelines
= item
->util
;
89 if (onelines
->nr
>= onelines
->alloc
) {
90 onelines
->alloc
= alloc_nr(onelines
->nr
);
91 onelines
->items
= xrealloc(onelines
->items
,
93 * sizeof(struct path_list_item
));
96 onelines
->items
[onelines
->nr
].util
= NULL
;
97 onelines
->items
[onelines
->nr
++].path
= buffer
;
100 static void read_from_stdin(struct path_list
*list
)
104 while (fgets(buffer
, sizeof(buffer
), stdin
) != NULL
) {
106 if ((buffer
[0] == 'A' || buffer
[0] == 'a') &&
107 !prefixcmp(buffer
+ 1, "uthor: ") &&
108 (bob
= strchr(buffer
+ 7, '<')) != NULL
) {
109 char buffer2
[1024], offset
= 0;
111 if (map_email(&mailmap
, bob
+ 1, buffer
, sizeof(buffer
)))
112 bob
= buffer
+ strlen(buffer
);
115 while (buffer
+ offset
< bob
&&
120 while (fgets(buffer2
, sizeof(buffer2
), stdin
) &&
123 if (fgets(buffer2
, sizeof(buffer2
), stdin
)) {
124 int l2
= strlen(buffer2
);
126 for (i
= 0; i
< l2
; i
++)
127 if (!isspace(buffer2
[i
]))
129 insert_author_oneline(list
,
131 bob
- buffer
- offset
,
132 buffer2
+ i
, l2
- i
);
138 static void get_from_rev(struct rev_info
*rev
, struct path_list
*list
)
141 struct commit
*commit
;
143 prepare_revision_walk(rev
);
144 while ((commit
= get_revision(rev
)) != NULL
) {
145 const char *author
= NULL
, *oneline
, *buffer
;
146 int authorlen
= authorlen
, onelinelen
;
148 /* get author and oneline */
149 for (buffer
= commit
->buffer
; buffer
&& *buffer
!= '\0' &&
151 const char *eol
= strchr(buffer
, '\n');
154 eol
= buffer
+ strlen(buffer
);
158 if (!prefixcmp(buffer
, "author ")) {
159 char *bracket
= strchr(buffer
, '<');
161 if (bracket
== NULL
|| bracket
> eol
)
162 die("Invalid commit buffer: %s",
163 sha1_to_hex(commit
->object
.sha1
));
165 if (map_email(&mailmap
, bracket
+ 1, scratch
,
168 authorlen
= strlen(scratch
);
170 if (bracket
[-1] == ' ')
174 authorlen
= bracket
- buffer
- 7;
181 die ("Missing author: %s",
182 sha1_to_hex(commit
->object
.sha1
));
184 if (buffer
== NULL
|| *buffer
== '\0') {
186 onelinelen
= sizeof(oneline
) + 1;
190 oneline
= buffer
+ 1;
191 eol
= strchr(oneline
, '\n');
193 onelinelen
= strlen(oneline
);
195 onelinelen
= eol
- oneline
;
198 insert_author_oneline(list
,
199 author
, authorlen
, oneline
, onelinelen
);
204 static int parse_uint(char const **arg
, int comma
)
210 ul
= strtoul(*arg
, &endp
, 10);
211 if (endp
!= *arg
&& *endp
&& *endp
!= comma
)
222 static const char wrap_arg_usage
[] = "-w[<width>[,<indent1>[,<indent2>]]]";
223 #define DEFAULT_WRAPLEN 76
224 #define DEFAULT_INDENT1 6
225 #define DEFAULT_INDENT2 9
227 static void parse_wrap_args(const char *arg
, int *in1
, int *in2
, int *wrap
)
229 arg
+= 2; /* skip -w */
231 *wrap
= parse_uint(&arg
, ',');
234 *in1
= parse_uint(&arg
, ',');
237 *in2
= parse_uint(&arg
, '\0');
242 *wrap
= DEFAULT_WRAPLEN
;
244 *in1
= DEFAULT_INDENT1
;
246 *in2
= DEFAULT_INDENT2
;
248 ((*in1
&& *wrap
<= *in1
) ||
249 (*in2
&& *wrap
<= *in2
)))
253 int cmd_shortlog(int argc
, const char **argv
, const char *prefix
)
256 struct path_list list
= { NULL
, 0, 0, 1 };
257 int i
, j
, sort_by_number
= 0, summary
= 0;
259 int wrap
= DEFAULT_WRAPLEN
;
260 int in1
= DEFAULT_INDENT1
;
261 int in2
= DEFAULT_INDENT2
;
263 /* since -n is a shadowed rev argument, parse our args first */
265 if (!strcmp(argv
[1], "-n") || !strcmp(argv
[1], "--numbered"))
267 else if (!strcmp(argv
[1], "-s") ||
268 !strcmp(argv
[1], "--summary"))
270 else if (!prefixcmp(argv
[1], "-w")) {
272 parse_wrap_args(argv
[1], &in1
, &in2
, &wrap
);
274 else if (!strcmp(argv
[1], "-h") || !strcmp(argv
[1], "--help"))
275 usage(shortlog_usage
);
281 init_revisions(&rev
, prefix
);
282 argc
= setup_revisions(argc
, argv
, &rev
, NULL
);
284 die ("unrecognized argument: %s", argv
[1]);
286 if (!access(".mailmap", R_OK
))
287 read_mailmap(&mailmap
, ".mailmap", &common_repo_prefix
);
289 if (rev
.pending
.nr
== 0) {
291 fprintf(stderr
, "(reading log to summarize from standard input)\n");
292 read_from_stdin(&list
);
295 get_from_rev(&rev
, &list
);
298 qsort(list
.items
, list
.nr
, sizeof(struct path_list_item
),
301 for (i
= 0; i
< list
.nr
; i
++) {
302 struct path_list
*onelines
= list
.items
[i
].util
;
305 printf("%s: %d\n", list
.items
[i
].path
, onelines
->nr
);
307 printf("%s (%d):\n", list
.items
[i
].path
, onelines
->nr
);
308 for (j
= onelines
->nr
- 1; j
>= 0; j
--) {
309 const char *msg
= onelines
->items
[j
].path
;
312 int col
= print_wrapped_text(msg
, in1
, in2
, wrap
);
317 printf(" %s\n", msg
);
322 onelines
->strdup_paths
= 1;
323 path_list_clear(onelines
, 1);
325 list
.items
[i
].util
= NULL
;
328 list
.strdup_paths
= 1;
329 path_list_clear(&list
, 1);
330 mailmap
.strdup_paths
= 1;
331 path_list_clear(&mailmap
, 1);