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