]>
Commit | Line | Data |
---|---|---|
b8ec5923 JS |
1 | #include "builtin.h" |
2 | #include "cache.h" | |
3 | #include "commit.h" | |
4 | #include "diff.h" | |
5 | #include "path-list.h" | |
6 | #include "revision.h" | |
3714e7c8 | 7 | #include "utf8.h" |
7c1c6782 | 8 | #include "mailmap.h" |
b8ec5923 JS |
9 | |
10 | static const char shortlog_usage[] = | |
ac60c94d | 11 | "git-shortlog [-n] [-s] [<commit-id>... ]"; |
b8ec5923 | 12 | |
7595e2ee JH |
13 | static char *common_repo_prefix; |
14 | ||
b8ec5923 JS |
15 | static int compare_by_number(const void *a1, const void *a2) |
16 | { | |
17 | const struct path_list_item *i1 = a1, *i2 = a2; | |
18 | const struct path_list *l1 = i1->util, *l2 = i2->util; | |
19 | ||
20 | if (l1->nr < l2->nr) | |
6d6ab610 | 21 | return 1; |
b8ec5923 JS |
22 | else if (l1->nr == l2->nr) |
23 | return 0; | |
24 | else | |
6d6ab610 | 25 | return -1; |
b8ec5923 JS |
26 | } |
27 | ||
d8e81250 JS |
28 | static struct path_list mailmap = {NULL, 0, 0, 0}; |
29 | ||
b8ec5923 JS |
30 | static void insert_author_oneline(struct path_list *list, |
31 | const char *author, int authorlen, | |
32 | const char *oneline, int onelinelen) | |
33 | { | |
7595e2ee | 34 | const char *dot3 = common_repo_prefix; |
b8ec5923 JS |
35 | char *buffer, *p; |
36 | struct path_list_item *item; | |
37 | struct path_list *onelines; | |
38 | ||
39 | while (authorlen > 0 && isspace(author[authorlen - 1])) | |
40 | authorlen--; | |
41 | ||
42 | buffer = xmalloc(authorlen + 1); | |
43 | memcpy(buffer, author, authorlen); | |
44 | buffer[authorlen] = '\0'; | |
45 | ||
46 | item = path_list_insert(buffer, list); | |
47 | if (item->util == NULL) | |
48 | item->util = xcalloc(1, sizeof(struct path_list)); | |
49 | else | |
50 | free(buffer); | |
51 | ||
cc44c765 | 52 | if (!prefixcmp(oneline, "[PATCH")) { |
72019cde JS |
53 | char *eob = strchr(oneline, ']'); |
54 | ||
55 | if (eob) { | |
56 | while (isspace(eob[1]) && eob[1] != '\n') | |
57 | eob++; | |
58 | if (eob - oneline < onelinelen) { | |
59 | onelinelen -= eob - oneline; | |
60 | oneline = eob; | |
61 | } | |
b8ec5923 JS |
62 | } |
63 | } | |
64 | ||
65 | while (onelinelen > 0 && isspace(oneline[0])) { | |
66 | oneline++; | |
67 | onelinelen--; | |
68 | } | |
69 | ||
70 | while (onelinelen > 0 && isspace(oneline[onelinelen - 1])) | |
71 | onelinelen--; | |
72 | ||
73 | buffer = xmalloc(onelinelen + 1); | |
74 | memcpy(buffer, oneline, onelinelen); | |
75 | buffer[onelinelen] = '\0'; | |
76 | ||
c95044d4 JH |
77 | if (dot3) { |
78 | int dot3len = strlen(dot3); | |
79 | if (dot3len > 5) { | |
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); | |
84 | } | |
85 | } | |
b8ec5923 JS |
86 | } |
87 | ||
b8ec5923 JS |
88 | onelines = item->util; |
89 | if (onelines->nr >= onelines->alloc) { | |
90 | onelines->alloc = alloc_nr(onelines->nr); | |
91 | onelines->items = xrealloc(onelines->items, | |
92 | onelines->alloc | |
93 | * sizeof(struct path_list_item)); | |
94 | } | |
95 | ||
96 | onelines->items[onelines->nr].util = NULL; | |
97 | onelines->items[onelines->nr++].path = buffer; | |
98 | } | |
99 | ||
100 | static void read_from_stdin(struct path_list *list) | |
101 | { | |
102 | char buffer[1024]; | |
103 | ||
104 | while (fgets(buffer, sizeof(buffer), stdin) != NULL) { | |
105 | char *bob; | |
106 | if ((buffer[0] == 'A' || buffer[0] == 'a') && | |
cc44c765 | 107 | !prefixcmp(buffer + 1, "uthor: ") && |
b8ec5923 JS |
108 | (bob = strchr(buffer + 7, '<')) != NULL) { |
109 | char buffer2[1024], offset = 0; | |
110 | ||
7c1c6782 | 111 | if (map_email(&mailmap, bob + 1, buffer, sizeof(buffer))) |
b8ec5923 JS |
112 | bob = buffer + strlen(buffer); |
113 | else { | |
114 | offset = 8; | |
6f987258 JH |
115 | while (buffer + offset < bob && |
116 | isspace(bob[-1])) | |
b8ec5923 JS |
117 | bob--; |
118 | } | |
119 | ||
120 | while (fgets(buffer2, sizeof(buffer2), stdin) && | |
121 | buffer2[0] != '\n') | |
122 | ; /* chomp input */ | |
bca73251 JH |
123 | if (fgets(buffer2, sizeof(buffer2), stdin)) { |
124 | int l2 = strlen(buffer2); | |
125 | int i; | |
126 | for (i = 0; i < l2; i++) | |
127 | if (!isspace(buffer2[i])) | |
128 | break; | |
b8ec5923 JS |
129 | insert_author_oneline(list, |
130 | buffer + offset, | |
131 | bob - buffer - offset, | |
bca73251 JH |
132 | buffer2 + i, l2 - i); |
133 | } | |
b8ec5923 JS |
134 | } |
135 | } | |
136 | } | |
137 | ||
138 | static void get_from_rev(struct rev_info *rev, struct path_list *list) | |
139 | { | |
140 | char scratch[1024]; | |
141 | struct commit *commit; | |
142 | ||
143 | prepare_revision_walk(rev); | |
144 | while ((commit = get_revision(rev)) != NULL) { | |
3a55602e | 145 | const char *author = NULL, *oneline, *buffer; |
b8ec5923 JS |
146 | int authorlen = authorlen, onelinelen; |
147 | ||
148 | /* get author and oneline */ | |
149 | for (buffer = commit->buffer; buffer && *buffer != '\0' && | |
150 | *buffer != '\n'; ) { | |
3a55602e | 151 | const char *eol = strchr(buffer, '\n'); |
b8ec5923 JS |
152 | |
153 | if (eol == NULL) | |
154 | eol = buffer + strlen(buffer); | |
155 | else | |
156 | eol++; | |
157 | ||
cc44c765 | 158 | if (!prefixcmp(buffer, "author ")) { |
b8ec5923 JS |
159 | char *bracket = strchr(buffer, '<'); |
160 | ||
161 | if (bracket == NULL || bracket > eol) | |
162 | die("Invalid commit buffer: %s", | |
163 | sha1_to_hex(commit->object.sha1)); | |
164 | ||
7c1c6782 | 165 | if (map_email(&mailmap, bracket + 1, scratch, |
b8ec5923 JS |
166 | sizeof(scratch))) { |
167 | author = scratch; | |
168 | authorlen = strlen(scratch); | |
169 | } else { | |
90ffefe5 | 170 | if (bracket[-1] == ' ') |
b8ec5923 JS |
171 | bracket--; |
172 | ||
173 | author = buffer + 7; | |
174 | authorlen = bracket - buffer - 7; | |
175 | } | |
176 | } | |
177 | buffer = eol; | |
178 | } | |
179 | ||
180 | if (author == NULL) | |
181 | die ("Missing author: %s", | |
182 | sha1_to_hex(commit->object.sha1)); | |
183 | ||
184 | if (buffer == NULL || *buffer == '\0') { | |
185 | oneline = "<none>"; | |
186 | onelinelen = sizeof(oneline) + 1; | |
187 | } else { | |
188 | char *eol; | |
189 | ||
190 | oneline = buffer + 1; | |
191 | eol = strchr(oneline, '\n'); | |
192 | if (eol == NULL) | |
193 | onelinelen = strlen(oneline); | |
194 | else | |
195 | onelinelen = eol - oneline; | |
196 | } | |
197 | ||
198 | insert_author_oneline(list, | |
199 | author, authorlen, oneline, onelinelen); | |
200 | } | |
201 | ||
202 | } | |
203 | ||
3d711d97 JH |
204 | static int parse_uint(char const **arg, int comma) |
205 | { | |
206 | unsigned long ul; | |
207 | int ret; | |
208 | char *endp; | |
209 | ||
210 | ul = strtoul(*arg, &endp, 10); | |
211 | if (endp != *arg && *endp && *endp != comma) | |
212 | return -1; | |
213 | ret = (int) ul; | |
214 | if (ret != ul) | |
215 | return -1; | |
216 | *arg = endp; | |
217 | if (**arg) | |
218 | (*arg)++; | |
219 | return ret; | |
220 | } | |
221 | ||
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 | |
226 | ||
227 | static void parse_wrap_args(const char *arg, int *in1, int *in2, int *wrap) | |
228 | { | |
229 | arg += 2; /* skip -w */ | |
230 | ||
231 | *wrap = parse_uint(&arg, ','); | |
232 | if (*wrap < 0) | |
233 | die(wrap_arg_usage); | |
234 | *in1 = parse_uint(&arg, ','); | |
235 | if (*in1 < 0) | |
236 | die(wrap_arg_usage); | |
237 | *in2 = parse_uint(&arg, '\0'); | |
238 | if (*in2 < 0) | |
239 | die(wrap_arg_usage); | |
240 | ||
241 | if (!*wrap) | |
242 | *wrap = DEFAULT_WRAPLEN; | |
243 | if (!*in1) | |
244 | *in1 = DEFAULT_INDENT1; | |
245 | if (!*in2) | |
246 | *in2 = DEFAULT_INDENT2; | |
247 | if (*wrap && | |
248 | ((*in1 && *wrap <= *in1) || | |
249 | (*in2 && *wrap <= *in2))) | |
250 | die(wrap_arg_usage); | |
251 | } | |
252 | ||
b8ec5923 JS |
253 | int cmd_shortlog(int argc, const char **argv, const char *prefix) |
254 | { | |
255 | struct rev_info rev; | |
256 | struct path_list list = { NULL, 0, 0, 1 }; | |
257 | int i, j, sort_by_number = 0, summary = 0; | |
3d711d97 JH |
258 | int wrap_lines = 0; |
259 | int wrap = DEFAULT_WRAPLEN; | |
260 | int in1 = DEFAULT_INDENT1; | |
261 | int in2 = DEFAULT_INDENT2; | |
b8ec5923 | 262 | |
6d6ab610 | 263 | /* since -n is a shadowed rev argument, parse our args first */ |
b8ec5923 JS |
264 | while (argc > 1) { |
265 | if (!strcmp(argv[1], "-n") || !strcmp(argv[1], "--numbered")) | |
266 | sort_by_number = 1; | |
267 | else if (!strcmp(argv[1], "-s") || | |
268 | !strcmp(argv[1], "--summary")) | |
269 | summary = 1; | |
3d711d97 JH |
270 | else if (!prefixcmp(argv[1], "-w")) { |
271 | wrap_lines = 1; | |
272 | parse_wrap_args(argv[1], &in1, &in2, &wrap); | |
273 | } | |
b8ec5923 JS |
274 | else if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) |
275 | usage(shortlog_usage); | |
276 | else | |
6d6ab610 | 277 | break; |
b8ec5923 JS |
278 | argv++; |
279 | argc--; | |
280 | } | |
6d6ab610 JS |
281 | init_revisions(&rev, prefix); |
282 | argc = setup_revisions(argc, argv, &rev, NULL); | |
283 | if (argc > 1) | |
284 | die ("unrecognized argument: %s", argv[1]); | |
b8ec5923 | 285 | |
e44b5d10 | 286 | read_mailmap(&mailmap, ".mailmap", &common_repo_prefix); |
d8e81250 | 287 | |
0497c620 JH |
288 | if (rev.pending.nr == 0) { |
289 | if (isatty(0)) | |
290 | fprintf(stderr, "(reading log to summarize from standard input)\n"); | |
b8ec5923 | 291 | read_from_stdin(&list); |
0497c620 | 292 | } |
b8ec5923 JS |
293 | else |
294 | get_from_rev(&rev, &list); | |
295 | ||
296 | if (sort_by_number) | |
6d6ab610 | 297 | qsort(list.items, list.nr, sizeof(struct path_list_item), |
b8ec5923 JS |
298 | compare_by_number); |
299 | ||
300 | for (i = 0; i < list.nr; i++) { | |
301 | struct path_list *onelines = list.items[i].util; | |
302 | ||
ac60c94d NP |
303 | if (summary) { |
304 | printf("%s: %d\n", list.items[i].path, onelines->nr); | |
305 | } else { | |
306 | printf("%s (%d):\n", list.items[i].path, onelines->nr); | |
3714e7c8 | 307 | for (j = onelines->nr - 1; j >= 0; j--) { |
3d711d97 JH |
308 | const char *msg = onelines->items[j].path; |
309 | ||
310 | if (wrap_lines) { | |
311 | int col = print_wrapped_text(msg, in1, in2, wrap); | |
312 | if (col != wrap) | |
313 | putchar('\n'); | |
314 | } | |
315 | else | |
316 | printf(" %s\n", msg); | |
3714e7c8 JS |
317 | } |
318 | putchar('\n'); | |
b8ec5923 JS |
319 | } |
320 | ||
321 | onelines->strdup_paths = 1; | |
322 | path_list_clear(onelines, 1); | |
323 | free(onelines); | |
324 | list.items[i].util = NULL; | |
325 | } | |
326 | ||
327 | list.strdup_paths = 1; | |
328 | path_list_clear(&list, 1); | |
d8e81250 JS |
329 | mailmap.strdup_paths = 1; |
330 | path_list_clear(&mailmap, 1); | |
b8ec5923 JS |
331 | |
332 | return 0; | |
333 | } | |
334 |