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