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