]>
Commit | Line | Data |
---|---|---|
b8ec5923 JS |
1 | #include "builtin.h" |
2 | #include "cache.h" | |
3 | #include "commit.h" | |
4 | #include "diff.h" | |
c455c87c | 5 | #include "string-list.h" |
b8ec5923 | 6 | #include "revision.h" |
3714e7c8 | 7 | #include "utf8.h" |
7c1c6782 | 8 | #include "mailmap.h" |
552bcac3 | 9 | #include "shortlog.h" |
14ec9cbd | 10 | #include "parse-options.h" |
b8ec5923 | 11 | |
14ec9cbd | 12 | static char const * const shortlog_usage[] = { |
588c038a | 13 | "git shortlog [-n] [-s] [-e] [-w] [rev-opts] [--] [<commit-id>... ]", |
14ec9cbd PH |
14 | "", |
15 | "[rev-opts] are documented in git-rev-list(1)", | |
16 | NULL | |
17 | }; | |
b8ec5923 JS |
18 | |
19 | static int compare_by_number(const void *a1, const void *a2) | |
20 | { | |
c455c87c JS |
21 | const struct string_list_item *i1 = a1, *i2 = a2; |
22 | const struct string_list *l1 = i1->util, *l2 = i2->util; | |
b8ec5923 JS |
23 | |
24 | if (l1->nr < l2->nr) | |
6d6ab610 | 25 | return 1; |
b8ec5923 JS |
26 | else if (l1->nr == l2->nr) |
27 | return 0; | |
28 | else | |
6d6ab610 | 29 | return -1; |
b8ec5923 JS |
30 | } |
31 | ||
cec08717 RS |
32 | const char *format_subject(struct strbuf *sb, const char *msg, |
33 | const char *line_separator); | |
34 | ||
552bcac3 | 35 | static void insert_one_record(struct shortlog *log, |
1e931cb4 JH |
36 | const char *author, |
37 | const char *oneline) | |
b8ec5923 | 38 | { |
552bcac3 | 39 | const char *dot3 = log->common_repo_prefix; |
b8ec5923 | 40 | char *buffer, *p; |
c455c87c | 41 | struct string_list_item *item; |
1e931cb4 | 42 | char namebuf[1024]; |
d20d654f | 43 | char emailbuf[1024]; |
1e931cb4 JH |
44 | size_t len; |
45 | const char *eol; | |
46 | const char *boemail, *eoemail; | |
cec08717 | 47 | struct strbuf subject = STRBUF_INIT; |
1e931cb4 JH |
48 | |
49 | boemail = strchr(author, '<'); | |
50 | if (!boemail) | |
51 | return; | |
52 | eoemail = strchr(boemail, '>'); | |
53 | if (!eoemail) | |
54 | return; | |
d20d654f MSO |
55 | |
56 | /* copy author name to namebuf, to support matching on both name and email */ | |
57 | memcpy(namebuf, author, boemail - author); | |
58 | len = boemail - author; | |
eeefa7c9 | 59 | while (len > 0 && isspace(namebuf[len-1])) |
d20d654f MSO |
60 | len--; |
61 | namebuf[len] = 0; | |
62 | ||
63 | /* copy email name to emailbuf, to allow email replacement as well */ | |
64 | memcpy(emailbuf, boemail+1, eoemail - boemail); | |
65 | emailbuf[eoemail - boemail - 1] = 0; | |
66 | ||
67 | if (!map_user(&log->mailmap, emailbuf, sizeof(emailbuf), namebuf, sizeof(namebuf))) { | |
1e931cb4 JH |
68 | while (author < boemail && isspace(*author)) |
69 | author++; | |
70 | for (len = 0; | |
71 | len < sizeof(namebuf) - 1 && author + len < boemail; | |
72 | len++) | |
73 | namebuf[len] = author[len]; | |
74 | while (0 < len && isspace(namebuf[len-1])) | |
75 | len--; | |
76 | namebuf[len] = '\0'; | |
77 | } | |
4602c17d JH |
78 | else |
79 | len = strlen(namebuf); | |
80 | ||
552bcac3 | 81 | if (log->email) { |
4602c17d | 82 | size_t room = sizeof(namebuf) - len - 1; |
d20d654f MSO |
83 | int maillen = strlen(emailbuf); |
84 | snprintf(namebuf + len, room, " <%.*s>", maillen, emailbuf); | |
4602c17d | 85 | } |
b8ec5923 | 86 | |
fe73fc1a | 87 | item = string_list_insert(namebuf, &log->list); |
b8ec5923 | 88 | if (item->util == NULL) |
c455c87c | 89 | item->util = xcalloc(1, sizeof(struct string_list)); |
b8ec5923 | 90 | |
c1ce83a5 AW |
91 | /* Skip any leading whitespace, including any blank lines. */ |
92 | while (*oneline && isspace(*oneline)) | |
93 | oneline++; | |
1e931cb4 JH |
94 | eol = strchr(oneline, '\n'); |
95 | if (!eol) | |
96 | eol = oneline + strlen(oneline); | |
cc44c765 | 97 | if (!prefixcmp(oneline, "[PATCH")) { |
72019cde | 98 | char *eob = strchr(oneline, ']'); |
1e931cb4 JH |
99 | if (eob && (!eol || eob < eol)) |
100 | oneline = eob + 1; | |
b8ec5923 | 101 | } |
1e931cb4 | 102 | while (*oneline && isspace(*oneline) && *oneline != '\n') |
b8ec5923 | 103 | oneline++; |
cec08717 RS |
104 | format_subject(&subject, oneline, " "); |
105 | buffer = strbuf_detach(&subject, NULL); | |
b8ec5923 | 106 | |
c95044d4 JH |
107 | if (dot3) { |
108 | int dot3len = strlen(dot3); | |
109 | if (dot3len > 5) { | |
110 | while ((p = strstr(buffer, dot3)) != NULL) { | |
111 | int taillen = strlen(p) - dot3len; | |
112 | memcpy(p, "/.../", 5); | |
113 | memmove(p + 5, p + dot3len, taillen + 1); | |
114 | } | |
115 | } | |
b8ec5923 JS |
116 | } |
117 | ||
fe73fc1a | 118 | string_list_append(buffer, item->util); |
b8ec5923 JS |
119 | } |
120 | ||
552bcac3 | 121 | static void read_from_stdin(struct shortlog *log) |
b8ec5923 | 122 | { |
1e931cb4 JH |
123 | char author[1024], oneline[1024]; |
124 | ||
125 | while (fgets(author, sizeof(author), stdin) != NULL) { | |
126 | if (!(author[0] == 'A' || author[0] == 'a') || | |
127 | prefixcmp(author + 1, "uthor: ")) | |
128 | continue; | |
129 | while (fgets(oneline, sizeof(oneline), stdin) && | |
130 | oneline[0] != '\n') | |
131 | ; /* discard headers */ | |
132 | while (fgets(oneline, sizeof(oneline), stdin) && | |
133 | oneline[0] == '\n') | |
134 | ; /* discard blanks */ | |
552bcac3 | 135 | insert_one_record(log, author + 8, oneline); |
b8ec5923 JS |
136 | } |
137 | } | |
138 | ||
552bcac3 | 139 | void shortlog_add_commit(struct shortlog *log, struct commit *commit) |
b8ec5923 | 140 | { |
552bcac3 | 141 | const char *author = NULL, *buffer; |
79f7ca06 UKK |
142 | struct strbuf buf = STRBUF_INIT; |
143 | struct strbuf ufbuf = STRBUF_INIT; | |
a689faeb | 144 | struct pretty_print_context ctx = {0}; |
b8ec5923 | 145 | |
a689faeb | 146 | pretty_print_commit(CMIT_FMT_RAW, commit, &buf, &ctx); |
79f7ca06 | 147 | buffer = buf.buf; |
552bcac3 DB |
148 | while (*buffer && *buffer != '\n') { |
149 | const char *eol = strchr(buffer, '\n'); | |
b8ec5923 | 150 | |
552bcac3 DB |
151 | if (eol == NULL) |
152 | eol = buffer + strlen(buffer); | |
153 | else | |
154 | eol++; | |
b8ec5923 | 155 | |
552bcac3 DB |
156 | if (!prefixcmp(buffer, "author ")) |
157 | author = buffer + 7; | |
158 | buffer = eol; | |
b8ec5923 | 159 | } |
552bcac3 DB |
160 | if (!author) |
161 | die("Missing author: %s", | |
162 | sha1_to_hex(commit->object.sha1)); | |
b526f8ed | 163 | if (log->user_format) { |
dd2e794a TR |
164 | struct pretty_print_context ctx = {0}; |
165 | ctx.abbrev = DEFAULT_ABBREV; | |
166 | ctx.subject = ""; | |
167 | ctx.after_subject = ""; | |
168 | ctx.date_mode = DATE_NORMAL; | |
a689faeb | 169 | pretty_print_commit(CMIT_FMT_USERFORMAT, commit, &ufbuf, &ctx); |
79f7ca06 UKK |
170 | buffer = ufbuf.buf; |
171 | } else if (*buffer) { | |
552bcac3 | 172 | buffer++; |
79f7ca06 | 173 | } |
552bcac3 | 174 | insert_one_record(log, author, !*buffer ? "<none>" : buffer); |
79f7ca06 UKK |
175 | strbuf_release(&ufbuf); |
176 | strbuf_release(&buf); | |
552bcac3 DB |
177 | } |
178 | ||
179 | static void get_from_rev(struct rev_info *rev, struct shortlog *log) | |
180 | { | |
181 | struct commit *commit; | |
182 | ||
183 | if (prepare_revision_walk(rev)) | |
184 | die("revision walk setup failed"); | |
185 | while ((commit = get_revision(rev)) != NULL) | |
186 | shortlog_add_commit(log, commit); | |
b8ec5923 JS |
187 | } |
188 | ||
14ec9cbd | 189 | static int parse_uint(char const **arg, int comma, int defval) |
3d711d97 JH |
190 | { |
191 | unsigned long ul; | |
192 | int ret; | |
193 | char *endp; | |
194 | ||
195 | ul = strtoul(*arg, &endp, 10); | |
14ec9cbd | 196 | if (*endp && *endp != comma) |
3d711d97 | 197 | return -1; |
14ec9cbd | 198 | if (ul > INT_MAX) |
3d711d97 | 199 | return -1; |
14ec9cbd PH |
200 | ret = *arg == endp ? defval : (int)ul; |
201 | *arg = *endp ? endp + 1 : endp; | |
3d711d97 JH |
202 | return ret; |
203 | } | |
204 | ||
205 | static const char wrap_arg_usage[] = "-w[<width>[,<indent1>[,<indent2>]]]"; | |
206 | #define DEFAULT_WRAPLEN 76 | |
207 | #define DEFAULT_INDENT1 6 | |
208 | #define DEFAULT_INDENT2 9 | |
209 | ||
14ec9cbd | 210 | static int parse_wrap_args(const struct option *opt, const char *arg, int unset) |
3d711d97 | 211 | { |
14ec9cbd PH |
212 | struct shortlog *log = opt->value; |
213 | ||
214 | log->wrap_lines = !unset; | |
215 | if (unset) | |
216 | return 0; | |
217 | if (!arg) { | |
218 | log->wrap = DEFAULT_WRAPLEN; | |
219 | log->in1 = DEFAULT_INDENT1; | |
220 | log->in2 = DEFAULT_INDENT2; | |
221 | return 0; | |
222 | } | |
223 | ||
224 | log->wrap = parse_uint(&arg, ',', DEFAULT_WRAPLEN); | |
225 | log->in1 = parse_uint(&arg, ',', DEFAULT_INDENT1); | |
226 | log->in2 = parse_uint(&arg, '\0', DEFAULT_INDENT2); | |
227 | if (log->wrap < 0 || log->in1 < 0 || log->in2 < 0) | |
228 | return error(wrap_arg_usage); | |
229 | if (log->wrap && | |
230 | ((log->in1 && log->wrap <= log->in1) || | |
231 | (log->in2 && log->wrap <= log->in2))) | |
232 | return error(wrap_arg_usage); | |
233 | return 0; | |
3d711d97 JH |
234 | } |
235 | ||
552bcac3 DB |
236 | void shortlog_init(struct shortlog *log) |
237 | { | |
238 | memset(log, 0, sizeof(*log)); | |
239 | ||
d551a488 | 240 | read_mailmap(&log->mailmap, &log->common_repo_prefix); |
552bcac3 | 241 | |
c455c87c | 242 | log->list.strdup_strings = 1; |
552bcac3 DB |
243 | log->wrap = DEFAULT_WRAPLEN; |
244 | log->in1 = DEFAULT_INDENT1; | |
245 | log->in2 = DEFAULT_INDENT2; | |
246 | } | |
247 | ||
b8ec5923 JS |
248 | int cmd_shortlog(int argc, const char **argv, const char *prefix) |
249 | { | |
14ec9cbd PH |
250 | static struct shortlog log; |
251 | static struct rev_info rev; | |
abe549e1 | 252 | int nongit; |
552bcac3 | 253 | |
14ec9cbd PH |
254 | static const struct option options[] = { |
255 | OPT_BOOLEAN('n', "numbered", &log.sort_by_number, | |
256 | "sort output according to the number of commits per author"), | |
257 | OPT_BOOLEAN('s', "summary", &log.summary, | |
258 | "Suppress commit descriptions, only provides commit count"), | |
259 | OPT_BOOLEAN('e', "email", &log.email, | |
260 | "Show the email address of each author"), | |
261 | { OPTION_CALLBACK, 'w', NULL, &log, "w[,i1[,i2]]", | |
262 | "Linewrap output", PARSE_OPT_OPTARG, &parse_wrap_args }, | |
263 | OPT_END(), | |
264 | }; | |
265 | ||
266 | struct parse_opt_ctx_t ctx; | |
267 | ||
abe549e1 | 268 | prefix = setup_git_directory_gently(&nongit); |
d551a488 | 269 | git_config(git_default_config, NULL); |
552bcac3 | 270 | shortlog_init(&log); |
14ec9cbd | 271 | init_revisions(&rev, prefix); |
37782920 | 272 | parse_options_start(&ctx, argc, argv, prefix, PARSE_OPT_KEEP_DASHDASH | |
14ec9cbd PH |
273 | PARSE_OPT_KEEP_ARGV0); |
274 | ||
275 | for (;;) { | |
14ec9cbd PH |
276 | switch (parse_options_step(&ctx, options, shortlog_usage)) { |
277 | case PARSE_OPT_HELP: | |
278 | exit(129); | |
279 | case PARSE_OPT_DONE: | |
280 | goto parse_done; | |
3d711d97 | 281 | } |
6b61ec05 | 282 | parse_revision_opt(&rev, &ctx, options, shortlog_usage); |
14ec9cbd PH |
283 | } |
284 | parse_done: | |
285 | argc = parse_options_end(&ctx); | |
286 | ||
287 | if (setup_revisions(argc, argv, &rev, NULL) != 1) { | |
288 | error("unrecognized argument: %s", argv[1]); | |
289 | usage_with_options(shortlog_usage, options); | |
b8ec5923 JS |
290 | } |
291 | ||
b526f8ed JS |
292 | log.user_format = rev.commit_format == CMIT_FMT_USERFORMAT; |
293 | ||
3384a2df | 294 | /* assume HEAD if from a tty */ |
abe549e1 | 295 | if (!nongit && !rev.pending.nr && isatty(0)) |
3384a2df | 296 | add_head_to_pending(&rev); |
0497c620 | 297 | if (rev.pending.nr == 0) { |
552bcac3 | 298 | read_from_stdin(&log); |
0497c620 | 299 | } |
b8ec5923 | 300 | else |
552bcac3 | 301 | get_from_rev(&rev, &log); |
b8ec5923 | 302 | |
552bcac3 DB |
303 | shortlog_output(&log); |
304 | return 0; | |
305 | } | |
b8ec5923 | 306 | |
552bcac3 DB |
307 | void shortlog_output(struct shortlog *log) |
308 | { | |
309 | int i, j; | |
310 | if (log->sort_by_number) | |
c455c87c | 311 | qsort(log->list.items, log->list.nr, sizeof(struct string_list_item), |
552bcac3 DB |
312 | compare_by_number); |
313 | for (i = 0; i < log->list.nr; i++) { | |
c455c87c | 314 | struct string_list *onelines = log->list.items[i].util; |
b8ec5923 | 315 | |
552bcac3 | 316 | if (log->summary) { |
c455c87c | 317 | printf("%6d\t%s\n", onelines->nr, log->list.items[i].string); |
ac60c94d | 318 | } else { |
c455c87c | 319 | printf("%s (%d):\n", log->list.items[i].string, onelines->nr); |
3714e7c8 | 320 | for (j = onelines->nr - 1; j >= 0; j--) { |
c455c87c | 321 | const char *msg = onelines->items[j].string; |
3d711d97 | 322 | |
552bcac3 DB |
323 | if (log->wrap_lines) { |
324 | int col = print_wrapped_text(msg, log->in1, log->in2, log->wrap); | |
325 | if (col != log->wrap) | |
3d711d97 JH |
326 | putchar('\n'); |
327 | } | |
328 | else | |
329 | printf(" %s\n", msg); | |
3714e7c8 JS |
330 | } |
331 | putchar('\n'); | |
b8ec5923 JS |
332 | } |
333 | ||
c455c87c | 334 | onelines->strdup_strings = 1; |
fe73fc1a | 335 | string_list_clear(onelines, 0); |
b8ec5923 | 336 | free(onelines); |
552bcac3 | 337 | log->list.items[i].util = NULL; |
b8ec5923 JS |
338 | } |
339 | ||
c455c87c JS |
340 | log->list.strdup_strings = 1; |
341 | string_list_clear(&log->list, 1); | |
d20d654f | 342 | clear_mailmap(&log->mailmap); |
b8ec5923 | 343 | } |