]>
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 | ||
c1ce83a5 AW |
77 | /* Skip any leading whitespace, including any blank lines. */ |
78 | while (*oneline && isspace(*oneline)) | |
79 | oneline++; | |
1e931cb4 JH |
80 | eol = strchr(oneline, '\n'); |
81 | if (!eol) | |
82 | eol = oneline + strlen(oneline); | |
cc44c765 | 83 | if (!prefixcmp(oneline, "[PATCH")) { |
72019cde | 84 | char *eob = strchr(oneline, ']'); |
1e931cb4 JH |
85 | if (eob && (!eol || eob < eol)) |
86 | oneline = eob + 1; | |
b8ec5923 | 87 | } |
1e931cb4 | 88 | while (*oneline && isspace(*oneline) && *oneline != '\n') |
b8ec5923 | 89 | oneline++; |
1e931cb4 JH |
90 | len = eol - oneline; |
91 | while (len && isspace(oneline[len-1])) | |
92 | len--; | |
93 | buffer = xmemdupz(oneline, len); | |
b8ec5923 | 94 | |
c95044d4 JH |
95 | if (dot3) { |
96 | int dot3len = strlen(dot3); | |
97 | if (dot3len > 5) { | |
98 | while ((p = strstr(buffer, dot3)) != NULL) { | |
99 | int taillen = strlen(p) - dot3len; | |
100 | memcpy(p, "/.../", 5); | |
101 | memmove(p + 5, p + dot3len, taillen + 1); | |
102 | } | |
103 | } | |
b8ec5923 JS |
104 | } |
105 | ||
b8ec5923 JS |
106 | onelines = item->util; |
107 | if (onelines->nr >= onelines->alloc) { | |
108 | onelines->alloc = alloc_nr(onelines->nr); | |
109 | onelines->items = xrealloc(onelines->items, | |
110 | onelines->alloc | |
111 | * sizeof(struct path_list_item)); | |
112 | } | |
113 | ||
114 | onelines->items[onelines->nr].util = NULL; | |
115 | onelines->items[onelines->nr++].path = buffer; | |
116 | } | |
117 | ||
118 | static void read_from_stdin(struct path_list *list) | |
119 | { | |
1e931cb4 JH |
120 | char author[1024], oneline[1024]; |
121 | ||
122 | while (fgets(author, sizeof(author), stdin) != NULL) { | |
123 | if (!(author[0] == 'A' || author[0] == 'a') || | |
124 | prefixcmp(author + 1, "uthor: ")) | |
125 | continue; | |
126 | while (fgets(oneline, sizeof(oneline), stdin) && | |
127 | oneline[0] != '\n') | |
128 | ; /* discard headers */ | |
129 | while (fgets(oneline, sizeof(oneline), stdin) && | |
130 | oneline[0] == '\n') | |
131 | ; /* discard blanks */ | |
132 | insert_one_record(list, author + 8, oneline); | |
b8ec5923 JS |
133 | } |
134 | } | |
135 | ||
136 | static void get_from_rev(struct rev_info *rev, struct path_list *list) | |
137 | { | |
b8ec5923 JS |
138 | struct commit *commit; |
139 | ||
140 | prepare_revision_walk(rev); | |
141 | while ((commit = get_revision(rev)) != NULL) { | |
1e931cb4 | 142 | const char *author = NULL, *buffer; |
b8ec5923 | 143 | |
1e931cb4 JH |
144 | buffer = commit->buffer; |
145 | while (*buffer && *buffer != '\n') { | |
3a55602e | 146 | const char *eol = strchr(buffer, '\n'); |
b8ec5923 JS |
147 | |
148 | if (eol == NULL) | |
149 | eol = buffer + strlen(buffer); | |
150 | else | |
151 | eol++; | |
152 | ||
1e931cb4 JH |
153 | if (!prefixcmp(buffer, "author ")) |
154 | author = buffer + 7; | |
b8ec5923 JS |
155 | buffer = eol; |
156 | } | |
1e931cb4 JH |
157 | if (!author) |
158 | die("Missing author: %s", | |
159 | sha1_to_hex(commit->object.sha1)); | |
160 | if (*buffer) | |
161 | buffer++; | |
162 | insert_one_record(list, author, !*buffer ? "<none>" : buffer); | |
b8ec5923 | 163 | } |
b8ec5923 JS |
164 | } |
165 | ||
3d711d97 JH |
166 | static int parse_uint(char const **arg, int comma) |
167 | { | |
168 | unsigned long ul; | |
169 | int ret; | |
170 | char *endp; | |
171 | ||
172 | ul = strtoul(*arg, &endp, 10); | |
173 | if (endp != *arg && *endp && *endp != comma) | |
174 | return -1; | |
175 | ret = (int) ul; | |
176 | if (ret != ul) | |
177 | return -1; | |
178 | *arg = endp; | |
179 | if (**arg) | |
180 | (*arg)++; | |
181 | return ret; | |
182 | } | |
183 | ||
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 | |
188 | ||
189 | static void parse_wrap_args(const char *arg, int *in1, int *in2, int *wrap) | |
190 | { | |
191 | arg += 2; /* skip -w */ | |
192 | ||
193 | *wrap = parse_uint(&arg, ','); | |
194 | if (*wrap < 0) | |
195 | die(wrap_arg_usage); | |
196 | *in1 = parse_uint(&arg, ','); | |
197 | if (*in1 < 0) | |
198 | die(wrap_arg_usage); | |
199 | *in2 = parse_uint(&arg, '\0'); | |
200 | if (*in2 < 0) | |
201 | die(wrap_arg_usage); | |
202 | ||
203 | if (!*wrap) | |
204 | *wrap = DEFAULT_WRAPLEN; | |
205 | if (!*in1) | |
206 | *in1 = DEFAULT_INDENT1; | |
207 | if (!*in2) | |
208 | *in2 = DEFAULT_INDENT2; | |
209 | if (*wrap && | |
210 | ((*in1 && *wrap <= *in1) || | |
211 | (*in2 && *wrap <= *in2))) | |
212 | die(wrap_arg_usage); | |
213 | } | |
214 | ||
b8ec5923 JS |
215 | int cmd_shortlog(int argc, const char **argv, const char *prefix) |
216 | { | |
217 | struct rev_info rev; | |
218 | struct path_list list = { NULL, 0, 0, 1 }; | |
219 | int i, j, sort_by_number = 0, summary = 0; | |
3d711d97 JH |
220 | int wrap_lines = 0; |
221 | int wrap = DEFAULT_WRAPLEN; | |
222 | int in1 = DEFAULT_INDENT1; | |
223 | int in2 = DEFAULT_INDENT2; | |
b8ec5923 | 224 | |
6d6ab610 | 225 | /* since -n is a shadowed rev argument, parse our args first */ |
b8ec5923 JS |
226 | while (argc > 1) { |
227 | if (!strcmp(argv[1], "-n") || !strcmp(argv[1], "--numbered")) | |
228 | sort_by_number = 1; | |
229 | else if (!strcmp(argv[1], "-s") || | |
230 | !strcmp(argv[1], "--summary")) | |
231 | summary = 1; | |
4602c17d JH |
232 | else if (!strcmp(argv[1], "-e") || |
233 | !strcmp(argv[1], "--email")) | |
234 | email = 1; | |
3d711d97 JH |
235 | else if (!prefixcmp(argv[1], "-w")) { |
236 | wrap_lines = 1; | |
237 | parse_wrap_args(argv[1], &in1, &in2, &wrap); | |
238 | } | |
b8ec5923 JS |
239 | else if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) |
240 | usage(shortlog_usage); | |
241 | else | |
6d6ab610 | 242 | break; |
b8ec5923 JS |
243 | argv++; |
244 | argc--; | |
245 | } | |
6d6ab610 JS |
246 | init_revisions(&rev, prefix); |
247 | argc = setup_revisions(argc, argv, &rev, NULL); | |
248 | if (argc > 1) | |
249 | die ("unrecognized argument: %s", argv[1]); | |
b8ec5923 | 250 | |
e44b5d10 | 251 | read_mailmap(&mailmap, ".mailmap", &common_repo_prefix); |
d8e81250 | 252 | |
3384a2df JH |
253 | /* assume HEAD if from a tty */ |
254 | if (!rev.pending.nr && isatty(0)) | |
255 | add_head_to_pending(&rev); | |
0497c620 | 256 | if (rev.pending.nr == 0) { |
b8ec5923 | 257 | read_from_stdin(&list); |
0497c620 | 258 | } |
b8ec5923 JS |
259 | else |
260 | get_from_rev(&rev, &list); | |
261 | ||
262 | if (sort_by_number) | |
6d6ab610 | 263 | qsort(list.items, list.nr, sizeof(struct path_list_item), |
b8ec5923 JS |
264 | compare_by_number); |
265 | ||
266 | for (i = 0; i < list.nr; i++) { | |
267 | struct path_list *onelines = list.items[i].util; | |
268 | ||
ac60c94d | 269 | if (summary) { |
97566ea7 | 270 | printf("%6d\t%s\n", onelines->nr, list.items[i].path); |
ac60c94d NP |
271 | } else { |
272 | printf("%s (%d):\n", list.items[i].path, onelines->nr); | |
3714e7c8 | 273 | for (j = onelines->nr - 1; j >= 0; j--) { |
3d711d97 JH |
274 | const char *msg = onelines->items[j].path; |
275 | ||
276 | if (wrap_lines) { | |
277 | int col = print_wrapped_text(msg, in1, in2, wrap); | |
278 | if (col != wrap) | |
279 | putchar('\n'); | |
280 | } | |
281 | else | |
282 | printf(" %s\n", msg); | |
3714e7c8 JS |
283 | } |
284 | putchar('\n'); | |
b8ec5923 JS |
285 | } |
286 | ||
287 | onelines->strdup_paths = 1; | |
288 | path_list_clear(onelines, 1); | |
289 | free(onelines); | |
290 | list.items[i].util = NULL; | |
291 | } | |
292 | ||
293 | list.strdup_paths = 1; | |
294 | path_list_clear(&list, 1); | |
d8e81250 JS |
295 | mailmap.strdup_paths = 1; |
296 | path_list_clear(&mailmap, 1); | |
b8ec5923 JS |
297 | |
298 | return 0; | |
299 | } |