Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 1 | #include "builtin.h" |
| 2 | #include "cache.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 3 | #include "config.h" |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 4 | #include "commit.h" |
| 5 | #include "diff.h" |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 6 | #include "string-list.h" |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 7 | #include "revision.h" |
Johannes Schindelin | 3714e7c | 2006-12-22 22:15:59 +0100 | [diff] [blame] | 8 | #include "utf8.h" |
Junio C Hamano | 7c1c678 | 2007-04-27 00:41:15 -0700 | [diff] [blame] | 9 | #include "mailmap.h" |
Daniel Barkalow | 552bcac | 2008-02-25 18:24:14 -0500 | [diff] [blame] | 10 | #include "shortlog.h" |
Pierre Habouzit | 14ec9cb | 2008-07-09 23:38:33 +0200 | [diff] [blame] | 11 | #include "parse-options.h" |
Jeff King | 47beb37 | 2020-09-27 04:40:04 -0400 | [diff] [blame] | 12 | #include "trailer.h" |
Elijah Newren | 449a900 | 2020-11-11 20:02:21 +0000 | [diff] [blame] | 13 | #include "strmap.h" |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 14 | |
Pierre Habouzit | 14ec9cb | 2008-07-09 23:38:33 +0200 | [diff] [blame] | 15 | static char const * const shortlog_usage[] = { |
Martin Ågren | cd56d4e | 2018-03-10 12:52:11 +0100 | [diff] [blame] | 16 | N_("git shortlog [<options>] [<revision-range>] [[--] <path>...]"), |
| 17 | N_("git log --pretty=short | git shortlog [<options>]"), |
Pierre Habouzit | 14ec9cb | 2008-07-09 23:38:33 +0200 | [diff] [blame] | 18 | NULL |
| 19 | }; |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 20 | |
Jeff King | 9b21a34 | 2016-01-18 15:02:59 -0500 | [diff] [blame] | 21 | /* |
| 22 | * The util field of our string_list_items will contain one of two things: |
| 23 | * |
| 24 | * - if --summary is not in use, it will point to a string list of the |
| 25 | * oneline subjects assigned to this author |
| 26 | * |
| 27 | * - if --summary is in use, we don't need that list; we only need to know |
| 28 | * its size. So we abuse the pointer slot to store our integer counter. |
| 29 | * |
| 30 | * This macro accesses the latter. |
| 31 | */ |
| 32 | #define UTIL_TO_INT(x) ((intptr_t)(x)->util) |
| 33 | |
| 34 | static int compare_by_counter(const void *a1, const void *a2) |
| 35 | { |
| 36 | const struct string_list_item *i1 = a1, *i2 = a2; |
| 37 | return UTIL_TO_INT(i2) - UTIL_TO_INT(i1); |
| 38 | } |
| 39 | |
| 40 | static int compare_by_list(const void *a1, const void *a2) |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 41 | { |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 42 | const struct string_list_item *i1 = a1, *i2 = a2; |
| 43 | const struct string_list *l1 = i1->util, *l2 = i2->util; |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 44 | |
| 45 | if (l1->nr < l2->nr) |
Johannes Schindelin | 6d6ab61 | 2006-11-21 21:12:06 +0100 | [diff] [blame] | 46 | return 1; |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 47 | else if (l1->nr == l2->nr) |
| 48 | return 0; |
| 49 | else |
Johannes Schindelin | 6d6ab61 | 2006-11-21 21:12:06 +0100 | [diff] [blame] | 50 | return -1; |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 51 | } |
| 52 | |
Daniel Barkalow | 552bcac | 2008-02-25 18:24:14 -0500 | [diff] [blame] | 53 | static void insert_one_record(struct shortlog *log, |
Jeff King | 45d93eb | 2020-09-25 03:01:50 -0400 | [diff] [blame] | 54 | const char *ident, |
Junio C Hamano | 1e931cb | 2007-12-07 17:07:41 -0800 | [diff] [blame] | 55 | const char *oneline) |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 56 | { |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 57 | struct string_list_item *item; |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 58 | |
Jeff King | 45d93eb | 2020-09-25 03:01:50 -0400 | [diff] [blame] | 59 | item = string_list_insert(&log->list, ident); |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 60 | |
Jeff King | ed7eba9 | 2016-01-18 15:02:56 -0500 | [diff] [blame] | 61 | if (log->summary) |
Jeff King | 9b21a34 | 2016-01-18 15:02:59 -0500 | [diff] [blame] | 62 | item->util = (void *)(UTIL_TO_INT(item) + 1); |
Jeff King | ed7eba9 | 2016-01-18 15:02:56 -0500 | [diff] [blame] | 63 | else { |
Ævar Arnfjörð Bjarmason | 4e16833 | 2021-01-12 21:18:06 +0100 | [diff] [blame] | 64 | char *buffer; |
Jeff King | ed7eba9 | 2016-01-18 15:02:56 -0500 | [diff] [blame] | 65 | struct strbuf subject = STRBUF_INIT; |
| 66 | const char *eol; |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 67 | |
Jeff King | ed7eba9 | 2016-01-18 15:02:56 -0500 | [diff] [blame] | 68 | /* Skip any leading whitespace, including any blank lines. */ |
| 69 | while (*oneline && isspace(*oneline)) |
| 70 | oneline++; |
| 71 | eol = strchr(oneline, '\n'); |
| 72 | if (!eol) |
| 73 | eol = oneline + strlen(oneline); |
| 74 | if (starts_with(oneline, "[PATCH")) { |
| 75 | char *eob = strchr(oneline, ']'); |
| 76 | if (eob && (!eol || eob < eol)) |
| 77 | oneline = eob + 1; |
| 78 | } |
| 79 | while (*oneline && isspace(*oneline) && *oneline != '\n') |
| 80 | oneline++; |
| 81 | format_subject(&subject, oneline, " "); |
| 82 | buffer = strbuf_detach(&subject, NULL); |
| 83 | |
Jeff King | 9b21a34 | 2016-01-18 15:02:59 -0500 | [diff] [blame] | 84 | if (item->util == NULL) |
| 85 | item->util = xcalloc(1, sizeof(struct string_list)); |
Jeff King | ed7eba9 | 2016-01-18 15:02:56 -0500 | [diff] [blame] | 86 | string_list_append(item->util, buffer); |
| 87 | } |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 88 | } |
| 89 | |
Jeff King | 87abb96 | 2020-09-27 04:40:09 -0400 | [diff] [blame] | 90 | static int parse_ident(struct shortlog *log, |
| 91 | struct strbuf *out, const char *in) |
Jeff King | 1ab03a5 | 2017-09-08 05:21:27 -0400 | [diff] [blame] | 92 | { |
| 93 | const char *mailbuf, *namebuf; |
| 94 | size_t namelen, maillen; |
| 95 | struct ident_split ident; |
| 96 | |
| 97 | if (split_ident_line(&ident, in, strlen(in))) |
| 98 | return -1; |
| 99 | |
| 100 | namebuf = ident.name_begin; |
| 101 | mailbuf = ident.mail_begin; |
| 102 | namelen = ident.name_end - ident.name_begin; |
| 103 | maillen = ident.mail_end - ident.mail_begin; |
| 104 | |
| 105 | map_user(&log->mailmap, &mailbuf, &maillen, &namebuf, &namelen); |
| 106 | strbuf_add(out, namebuf, namelen); |
| 107 | if (log->email) |
| 108 | strbuf_addf(out, " <%.*s>", (int)maillen, mailbuf); |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
Daniel Barkalow | 552bcac | 2008-02-25 18:24:14 -0500 | [diff] [blame] | 113 | static void read_from_stdin(struct shortlog *log) |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 114 | { |
Jeff King | 45d93eb | 2020-09-25 03:01:50 -0400 | [diff] [blame] | 115 | struct strbuf ident = STRBUF_INIT; |
| 116 | struct strbuf mapped_ident = STRBUF_INIT; |
Jeff King | 5025049 | 2016-01-18 15:02:44 -0500 | [diff] [blame] | 117 | struct strbuf oneline = STRBUF_INIT; |
Linus Torvalds | fbfda15 | 2016-10-11 11:45:58 -0700 | [diff] [blame] | 118 | static const char *author_match[2] = { "Author: ", "author " }; |
| 119 | static const char *committer_match[2] = { "Commit: ", "committer " }; |
| 120 | const char **match; |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 121 | |
Jeff King | 63d24fa | 2020-09-27 04:40:15 -0400 | [diff] [blame] | 122 | if (HAS_MULTI_BITS(log->groups)) |
| 123 | die(_("using multiple --group options with stdin is not supported")); |
| 124 | |
| 125 | switch (log->groups) { |
Jeff King | 92338c4 | 2020-09-27 04:39:59 -0400 | [diff] [blame] | 126 | case SHORTLOG_GROUP_AUTHOR: |
| 127 | match = author_match; |
| 128 | break; |
| 129 | case SHORTLOG_GROUP_COMMITTER: |
| 130 | match = committer_match; |
| 131 | break; |
Jeff King | 47beb37 | 2020-09-27 04:40:04 -0400 | [diff] [blame] | 132 | case SHORTLOG_GROUP_TRAILER: |
| 133 | die(_("using --group=trailer with stdin is not supported")); |
Jeff King | 92338c4 | 2020-09-27 04:39:59 -0400 | [diff] [blame] | 134 | default: |
| 135 | BUG("unhandled shortlog group"); |
| 136 | } |
| 137 | |
Jeff King | 45d93eb | 2020-09-25 03:01:50 -0400 | [diff] [blame] | 138 | while (strbuf_getline_lf(&ident, stdin) != EOF) { |
Jeff King | 5c3894c | 2016-01-18 15:02:40 -0500 | [diff] [blame] | 139 | const char *v; |
Jeff King | 45d93eb | 2020-09-25 03:01:50 -0400 | [diff] [blame] | 140 | if (!skip_prefix(ident.buf, match[0], &v) && |
| 141 | !skip_prefix(ident.buf, match[1], &v)) |
Junio C Hamano | 1e931cb | 2007-12-07 17:07:41 -0800 | [diff] [blame] | 142 | continue; |
Junio C Hamano | a1c5405 | 2016-01-28 16:10:14 -0800 | [diff] [blame] | 143 | while (strbuf_getline_lf(&oneline, stdin) != EOF && |
Jeff King | 5025049 | 2016-01-18 15:02:44 -0500 | [diff] [blame] | 144 | oneline.len) |
Junio C Hamano | 1e931cb | 2007-12-07 17:07:41 -0800 | [diff] [blame] | 145 | ; /* discard headers */ |
Junio C Hamano | a1c5405 | 2016-01-28 16:10:14 -0800 | [diff] [blame] | 146 | while (strbuf_getline_lf(&oneline, stdin) != EOF && |
Jeff King | 5025049 | 2016-01-18 15:02:44 -0500 | [diff] [blame] | 147 | !oneline.len) |
Junio C Hamano | 1e931cb | 2007-12-07 17:07:41 -0800 | [diff] [blame] | 148 | ; /* discard blanks */ |
Jeff King | 1ab03a5 | 2017-09-08 05:21:27 -0400 | [diff] [blame] | 149 | |
Jeff King | 45d93eb | 2020-09-25 03:01:50 -0400 | [diff] [blame] | 150 | strbuf_reset(&mapped_ident); |
Jeff King | 87abb96 | 2020-09-27 04:40:09 -0400 | [diff] [blame] | 151 | if (parse_ident(log, &mapped_ident, v) < 0) |
Jeff King | 1ab03a5 | 2017-09-08 05:21:27 -0400 | [diff] [blame] | 152 | continue; |
| 153 | |
Jeff King | 45d93eb | 2020-09-25 03:01:50 -0400 | [diff] [blame] | 154 | insert_one_record(log, mapped_ident.buf, oneline.buf); |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 155 | } |
Jeff King | 45d93eb | 2020-09-25 03:01:50 -0400 | [diff] [blame] | 156 | strbuf_release(&ident); |
| 157 | strbuf_release(&mapped_ident); |
Jeff King | 5025049 | 2016-01-18 15:02:44 -0500 | [diff] [blame] | 158 | strbuf_release(&oneline); |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 159 | } |
| 160 | |
Jeff King | 47beb37 | 2020-09-27 04:40:04 -0400 | [diff] [blame] | 161 | static void insert_records_from_trailers(struct shortlog *log, |
Jeff King | 63d24fa | 2020-09-27 04:40:15 -0400 | [diff] [blame] | 162 | struct strset *dups, |
Jeff King | 47beb37 | 2020-09-27 04:40:04 -0400 | [diff] [blame] | 163 | struct commit *commit, |
| 164 | struct pretty_print_context *ctx, |
| 165 | const char *oneline) |
| 166 | { |
| 167 | struct trailer_iterator iter; |
| 168 | const char *commit_buffer, *body; |
Jeff King | 56d5dde | 2020-09-27 04:40:11 -0400 | [diff] [blame] | 169 | struct strbuf ident = STRBUF_INIT; |
Jeff King | 47beb37 | 2020-09-27 04:40:04 -0400 | [diff] [blame] | 170 | |
| 171 | /* |
| 172 | * Using format_commit_message("%B") would be simpler here, but |
| 173 | * this saves us copying the message. |
| 174 | */ |
| 175 | commit_buffer = logmsg_reencode(commit, NULL, ctx->output_encoding); |
| 176 | body = strstr(commit_buffer, "\n\n"); |
| 177 | if (!body) |
| 178 | return; |
| 179 | |
| 180 | trailer_iterator_init(&iter, body); |
| 181 | while (trailer_iterator_advance(&iter)) { |
| 182 | const char *value = iter.val.buf; |
| 183 | |
Jeff King | 63d24fa | 2020-09-27 04:40:15 -0400 | [diff] [blame] | 184 | if (!string_list_has_string(&log->trailers, iter.key.buf)) |
Jeff King | 47beb37 | 2020-09-27 04:40:04 -0400 | [diff] [blame] | 185 | continue; |
| 186 | |
Jeff King | 56d5dde | 2020-09-27 04:40:11 -0400 | [diff] [blame] | 187 | strbuf_reset(&ident); |
| 188 | if (!parse_ident(log, &ident, value)) |
| 189 | value = ident.buf; |
| 190 | |
Elijah Newren | 449a900 | 2020-11-11 20:02:21 +0000 | [diff] [blame] | 191 | if (!strset_add(dups, value)) |
Jeff King | f17b0b9 | 2020-09-27 04:40:07 -0400 | [diff] [blame] | 192 | continue; |
Jeff King | 47beb37 | 2020-09-27 04:40:04 -0400 | [diff] [blame] | 193 | insert_one_record(log, value, oneline); |
| 194 | } |
| 195 | trailer_iterator_release(&iter); |
| 196 | |
Jeff King | 56d5dde | 2020-09-27 04:40:11 -0400 | [diff] [blame] | 197 | strbuf_release(&ident); |
Jeff King | 47beb37 | 2020-09-27 04:40:04 -0400 | [diff] [blame] | 198 | unuse_commit_buffer(commit, commit_buffer); |
| 199 | } |
| 200 | |
Daniel Barkalow | 552bcac | 2008-02-25 18:24:14 -0500 | [diff] [blame] | 201 | void shortlog_add_commit(struct shortlog *log, struct commit *commit) |
| 202 | { |
Jeff King | 45d93eb | 2020-09-25 03:01:50 -0400 | [diff] [blame] | 203 | struct strbuf ident = STRBUF_INIT; |
Jeff King | 2db6b83 | 2016-01-18 15:02:48 -0500 | [diff] [blame] | 204 | struct strbuf oneline = STRBUF_INIT; |
Jeff King | 63d24fa | 2020-09-27 04:40:15 -0400 | [diff] [blame] | 205 | struct strset dups = STRSET_INIT; |
Jeff King | 2db6b83 | 2016-01-18 15:02:48 -0500 | [diff] [blame] | 206 | struct pretty_print_context ctx = {0}; |
Jeff King | 92338c4 | 2020-09-27 04:39:59 -0400 | [diff] [blame] | 207 | const char *oneline_str; |
Daniel Barkalow | 552bcac | 2008-02-25 18:24:14 -0500 | [diff] [blame] | 208 | |
Jeff King | 2db6b83 | 2016-01-18 15:02:48 -0500 | [diff] [blame] | 209 | ctx.fmt = CMIT_FMT_USERFORMAT; |
| 210 | ctx.abbrev = log->abbrev; |
René Scharfe | 6d167fd | 2017-03-01 12:37:07 +0100 | [diff] [blame] | 211 | ctx.print_email_subject = 1; |
Jeff King | 2db6b83 | 2016-01-18 15:02:48 -0500 | [diff] [blame] | 212 | ctx.date_mode.type = DATE_NORMAL; |
| 213 | ctx.output_encoding = get_log_output_encoding(); |
Daniel Barkalow | 552bcac | 2008-02-25 18:24:14 -0500 | [diff] [blame] | 214 | |
Jeff King | 4e1d1a2 | 2016-01-18 15:02:52 -0500 | [diff] [blame] | 215 | if (!log->summary) { |
| 216 | if (log->user_format) |
| 217 | pretty_print_commit(&ctx, commit, &oneline); |
Daniel Barkalow | 552bcac | 2008-02-25 18:24:14 -0500 | [diff] [blame] | 218 | else |
Jeff King | 4e1d1a2 | 2016-01-18 15:02:52 -0500 | [diff] [blame] | 219 | format_commit_message(commit, "%s", &oneline, &ctx); |
| 220 | } |
Jeff King | 92338c4 | 2020-09-27 04:39:59 -0400 | [diff] [blame] | 221 | oneline_str = oneline.len ? oneline.buf : "<none>"; |
Daniel Barkalow | 552bcac | 2008-02-25 18:24:14 -0500 | [diff] [blame] | 222 | |
Jeff King | 63d24fa | 2020-09-27 04:40:15 -0400 | [diff] [blame] | 223 | if (log->groups & SHORTLOG_GROUP_AUTHOR) { |
| 224 | strbuf_reset(&ident); |
Jeff King | 92338c4 | 2020-09-27 04:39:59 -0400 | [diff] [blame] | 225 | format_commit_message(commit, |
| 226 | log->email ? "%aN <%aE>" : "%aN", |
| 227 | &ident, &ctx); |
Jeff King | 63d24fa | 2020-09-27 04:40:15 -0400 | [diff] [blame] | 228 | if (!HAS_MULTI_BITS(log->groups) || |
Elijah Newren | 449a900 | 2020-11-11 20:02:21 +0000 | [diff] [blame] | 229 | strset_add(&dups, ident.buf)) |
Jeff King | 63d24fa | 2020-09-27 04:40:15 -0400 | [diff] [blame] | 230 | insert_one_record(log, ident.buf, oneline_str); |
| 231 | } |
| 232 | if (log->groups & SHORTLOG_GROUP_COMMITTER) { |
| 233 | strbuf_reset(&ident); |
Jeff King | 92338c4 | 2020-09-27 04:39:59 -0400 | [diff] [blame] | 234 | format_commit_message(commit, |
| 235 | log->email ? "%cN <%cE>" : "%cN", |
| 236 | &ident, &ctx); |
Jeff King | 63d24fa | 2020-09-27 04:40:15 -0400 | [diff] [blame] | 237 | if (!HAS_MULTI_BITS(log->groups) || |
Elijah Newren | 449a900 | 2020-11-11 20:02:21 +0000 | [diff] [blame] | 238 | strset_add(&dups, ident.buf)) |
Jeff King | 63d24fa | 2020-09-27 04:40:15 -0400 | [diff] [blame] | 239 | insert_one_record(log, ident.buf, oneline_str); |
| 240 | } |
| 241 | if (log->groups & SHORTLOG_GROUP_TRAILER) { |
| 242 | insert_records_from_trailers(log, &dups, commit, &ctx, oneline_str); |
Jeff King | 92338c4 | 2020-09-27 04:39:59 -0400 | [diff] [blame] | 243 | } |
Jeff King | 2db6b83 | 2016-01-18 15:02:48 -0500 | [diff] [blame] | 244 | |
Jeff King | 63d24fa | 2020-09-27 04:40:15 -0400 | [diff] [blame] | 245 | strset_clear(&dups); |
Jeff King | 45d93eb | 2020-09-25 03:01:50 -0400 | [diff] [blame] | 246 | strbuf_release(&ident); |
Jeff King | 2db6b83 | 2016-01-18 15:02:48 -0500 | [diff] [blame] | 247 | strbuf_release(&oneline); |
Daniel Barkalow | 552bcac | 2008-02-25 18:24:14 -0500 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | static void get_from_rev(struct rev_info *rev, struct shortlog *log) |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 251 | { |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 252 | struct commit *commit; |
| 253 | |
Daniel Barkalow | 552bcac | 2008-02-25 18:24:14 -0500 | [diff] [blame] | 254 | if (prepare_revision_walk(rev)) |
Ævar Arnfjörð Bjarmason | ab8b53b | 2011-02-22 23:42:32 +0000 | [diff] [blame] | 255 | die(_("revision walk setup failed")); |
Daniel Barkalow | 552bcac | 2008-02-25 18:24:14 -0500 | [diff] [blame] | 256 | while ((commit = get_revision(rev)) != NULL) |
| 257 | shortlog_add_commit(log, commit); |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 258 | } |
| 259 | |
Pierre Habouzit | 14ec9cb | 2008-07-09 23:38:33 +0200 | [diff] [blame] | 260 | static int parse_uint(char const **arg, int comma, int defval) |
Junio C Hamano | 3d711d9 | 2007-04-08 01:28:00 -0700 | [diff] [blame] | 261 | { |
| 262 | unsigned long ul; |
| 263 | int ret; |
| 264 | char *endp; |
| 265 | |
| 266 | ul = strtoul(*arg, &endp, 10); |
Pierre Habouzit | 14ec9cb | 2008-07-09 23:38:33 +0200 | [diff] [blame] | 267 | if (*endp && *endp != comma) |
Junio C Hamano | 3d711d9 | 2007-04-08 01:28:00 -0700 | [diff] [blame] | 268 | return -1; |
Pierre Habouzit | 14ec9cb | 2008-07-09 23:38:33 +0200 | [diff] [blame] | 269 | if (ul > INT_MAX) |
Junio C Hamano | 3d711d9 | 2007-04-08 01:28:00 -0700 | [diff] [blame] | 270 | return -1; |
Pierre Habouzit | 14ec9cb | 2008-07-09 23:38:33 +0200 | [diff] [blame] | 271 | ret = *arg == endp ? defval : (int)ul; |
| 272 | *arg = *endp ? endp + 1 : endp; |
Junio C Hamano | 3d711d9 | 2007-04-08 01:28:00 -0700 | [diff] [blame] | 273 | return ret; |
| 274 | } |
| 275 | |
| 276 | static const char wrap_arg_usage[] = "-w[<width>[,<indent1>[,<indent2>]]]"; |
| 277 | #define DEFAULT_WRAPLEN 76 |
| 278 | #define DEFAULT_INDENT1 6 |
| 279 | #define DEFAULT_INDENT2 9 |
| 280 | |
Pierre Habouzit | 14ec9cb | 2008-07-09 23:38:33 +0200 | [diff] [blame] | 281 | static int parse_wrap_args(const struct option *opt, const char *arg, int unset) |
Junio C Hamano | 3d711d9 | 2007-04-08 01:28:00 -0700 | [diff] [blame] | 282 | { |
Pierre Habouzit | 14ec9cb | 2008-07-09 23:38:33 +0200 | [diff] [blame] | 283 | struct shortlog *log = opt->value; |
Junio C Hamano | 3d711d9 | 2007-04-08 01:28:00 -0700 | [diff] [blame] | 284 | |
Pierre Habouzit | 14ec9cb | 2008-07-09 23:38:33 +0200 | [diff] [blame] | 285 | log->wrap_lines = !unset; |
| 286 | if (unset) |
| 287 | return 0; |
| 288 | if (!arg) { |
| 289 | log->wrap = DEFAULT_WRAPLEN; |
| 290 | log->in1 = DEFAULT_INDENT1; |
| 291 | log->in2 = DEFAULT_INDENT2; |
| 292 | return 0; |
| 293 | } |
Junio C Hamano | 3d711d9 | 2007-04-08 01:28:00 -0700 | [diff] [blame] | 294 | |
Pierre Habouzit | 14ec9cb | 2008-07-09 23:38:33 +0200 | [diff] [blame] | 295 | log->wrap = parse_uint(&arg, ',', DEFAULT_WRAPLEN); |
| 296 | log->in1 = parse_uint(&arg, ',', DEFAULT_INDENT1); |
| 297 | log->in2 = parse_uint(&arg, '\0', DEFAULT_INDENT2); |
| 298 | if (log->wrap < 0 || log->in1 < 0 || log->in2 < 0) |
| 299 | return error(wrap_arg_usage); |
| 300 | if (log->wrap && |
| 301 | ((log->in1 && log->wrap <= log->in1) || |
| 302 | (log->in2 && log->wrap <= log->in2))) |
| 303 | return error(wrap_arg_usage); |
| 304 | return 0; |
Junio C Hamano | 3d711d9 | 2007-04-08 01:28:00 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Jeff King | 92338c4 | 2020-09-27 04:39:59 -0400 | [diff] [blame] | 307 | static int parse_group_option(const struct option *opt, const char *arg, int unset) |
| 308 | { |
| 309 | struct shortlog *log = opt->value; |
Jeff King | 47beb37 | 2020-09-27 04:40:04 -0400 | [diff] [blame] | 310 | const char *field; |
Jeff King | 92338c4 | 2020-09-27 04:39:59 -0400 | [diff] [blame] | 311 | |
Jeff King | 63d24fa | 2020-09-27 04:40:15 -0400 | [diff] [blame] | 312 | if (unset) { |
| 313 | log->groups = 0; |
| 314 | string_list_clear(&log->trailers, 0); |
| 315 | } else if (!strcasecmp(arg, "author")) |
| 316 | log->groups |= SHORTLOG_GROUP_AUTHOR; |
Jeff King | 92338c4 | 2020-09-27 04:39:59 -0400 | [diff] [blame] | 317 | else if (!strcasecmp(arg, "committer")) |
Jeff King | 63d24fa | 2020-09-27 04:40:15 -0400 | [diff] [blame] | 318 | log->groups |= SHORTLOG_GROUP_COMMITTER; |
Jeff King | 47beb37 | 2020-09-27 04:40:04 -0400 | [diff] [blame] | 319 | else if (skip_prefix(arg, "trailer:", &field)) { |
Jeff King | 63d24fa | 2020-09-27 04:40:15 -0400 | [diff] [blame] | 320 | log->groups |= SHORTLOG_GROUP_TRAILER; |
| 321 | string_list_append(&log->trailers, field); |
Jeff King | 47beb37 | 2020-09-27 04:40:04 -0400 | [diff] [blame] | 322 | } else |
Jeff King | 92338c4 | 2020-09-27 04:39:59 -0400 | [diff] [blame] | 323 | return error(_("unknown group type: %s"), arg); |
| 324 | |
| 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | |
Daniel Barkalow | 552bcac | 2008-02-25 18:24:14 -0500 | [diff] [blame] | 329 | void shortlog_init(struct shortlog *log) |
| 330 | { |
| 331 | memset(log, 0, sizeof(*log)); |
| 332 | |
Ævar Arnfjörð Bjarmason | 4e16833 | 2021-01-12 21:18:06 +0100 | [diff] [blame] | 333 | read_mailmap(&log->mailmap); |
Daniel Barkalow | 552bcac | 2008-02-25 18:24:14 -0500 | [diff] [blame] | 334 | |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 335 | log->list.strdup_strings = 1; |
Daniel Barkalow | 552bcac | 2008-02-25 18:24:14 -0500 | [diff] [blame] | 336 | log->wrap = DEFAULT_WRAPLEN; |
| 337 | log->in1 = DEFAULT_INDENT1; |
| 338 | log->in2 = DEFAULT_INDENT2; |
Jeff King | 63d24fa | 2020-09-27 04:40:15 -0400 | [diff] [blame] | 339 | log->trailers.strdup_strings = 1; |
| 340 | log->trailers.cmp = strcasecmp; |
Daniel Barkalow | 552bcac | 2008-02-25 18:24:14 -0500 | [diff] [blame] | 341 | } |
| 342 | |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 343 | int cmd_shortlog(int argc, const char **argv, const char *prefix) |
| 344 | { |
Jeff King | 64093fc | 2016-06-13 01:39:28 -0400 | [diff] [blame] | 345 | struct shortlog log = { STRING_LIST_INIT_NODUP }; |
| 346 | struct rev_info rev; |
Nguyễn Thái Ngọc Duy | 773b69b | 2010-08-05 22:01:37 -0500 | [diff] [blame] | 347 | int nongit = !startup_info->have_repository; |
Daniel Barkalow | 552bcac | 2008-02-25 18:24:14 -0500 | [diff] [blame] | 348 | |
Jeff King | 64093fc | 2016-06-13 01:39:28 -0400 | [diff] [blame] | 349 | const struct option options[] = { |
Jeff King | 63d24fa | 2020-09-27 04:40:15 -0400 | [diff] [blame] | 350 | OPT_BIT('c', "committer", &log.groups, |
ZheNing Hu | e73fe3d | 2021-01-06 14:44:03 +0000 | [diff] [blame] | 351 | N_("group by committer rather than author"), |
Jeff King | 63d24fa | 2020-09-27 04:40:15 -0400 | [diff] [blame] | 352 | SHORTLOG_GROUP_COMMITTER), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 353 | OPT_BOOL('n', "numbered", &log.sort_by_number, |
| 354 | N_("sort output according to the number of commits per author")), |
| 355 | OPT_BOOL('s', "summary", &log.summary, |
ZheNing Hu | e73fe3d | 2021-01-06 14:44:03 +0000 | [diff] [blame] | 356 | N_("suppress commit descriptions, only provides commit count")), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 357 | OPT_BOOL('e', "email", &log.email, |
ZheNing Hu | e73fe3d | 2021-01-06 14:44:03 +0000 | [diff] [blame] | 358 | N_("show the email address of each author")), |
Denton Liu | 203c853 | 2020-04-28 04:36:28 -0400 | [diff] [blame] | 359 | OPT_CALLBACK_F('w', NULL, &log, N_("<w>[,<i1>[,<i2>]]"), |
ZheNing Hu | e73fe3d | 2021-01-06 14:44:03 +0000 | [diff] [blame] | 360 | N_("linewrap output"), PARSE_OPT_OPTARG, |
Denton Liu | 203c853 | 2020-04-28 04:36:28 -0400 | [diff] [blame] | 361 | &parse_wrap_args), |
Jeff King | 92338c4 | 2020-09-27 04:39:59 -0400 | [diff] [blame] | 362 | OPT_CALLBACK(0, "group", &log, N_("field"), |
ZheNing Hu | e73fe3d | 2021-01-06 14:44:03 +0000 | [diff] [blame] | 363 | N_("group by field"), parse_group_option), |
Pierre Habouzit | 14ec9cb | 2008-07-09 23:38:33 +0200 | [diff] [blame] | 364 | OPT_END(), |
| 365 | }; |
| 366 | |
| 367 | struct parse_opt_ctx_t ctx; |
| 368 | |
Marius Storm-Olsen | d551a48 | 2009-02-08 15:34:27 +0100 | [diff] [blame] | 369 | git_config(git_default_config, NULL); |
Daniel Barkalow | 552bcac | 2008-02-25 18:24:14 -0500 | [diff] [blame] | 370 | shortlog_init(&log); |
Nguyễn Thái Ngọc Duy | 2abf350 | 2018-09-21 17:57:38 +0200 | [diff] [blame] | 371 | repo_init_revisions(the_repository, &rev, prefix); |
Stephen Boyd | 9ca1169 | 2010-12-05 23:57:42 -0800 | [diff] [blame] | 372 | parse_options_start(&ctx, argc, argv, prefix, options, |
| 373 | PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0); |
Pierre Habouzit | 14ec9cb | 2008-07-09 23:38:33 +0200 | [diff] [blame] | 374 | |
| 375 | for (;;) { |
Pierre Habouzit | 14ec9cb | 2008-07-09 23:38:33 +0200 | [diff] [blame] | 376 | switch (parse_options_step(&ctx, options, shortlog_usage)) { |
Ævar Arnfjörð Bjarmason | 352e761 | 2021-10-08 21:07:39 +0200 | [diff] [blame] | 377 | case PARSE_OPT_NON_OPTION: |
| 378 | case PARSE_OPT_UNKNOWN: |
| 379 | break; |
Pierre Habouzit | 14ec9cb | 2008-07-09 23:38:33 +0200 | [diff] [blame] | 380 | case PARSE_OPT_HELP: |
Paul-Sebastian Ungureanu | 3bb0923 | 2018-03-22 20:43:51 +0200 | [diff] [blame] | 381 | case PARSE_OPT_ERROR: |
Pierre Habouzit | 14ec9cb | 2008-07-09 23:38:33 +0200 | [diff] [blame] | 382 | exit(129); |
Nguyễn Thái Ngọc Duy | a92ec7e | 2018-12-11 16:35:01 +0100 | [diff] [blame] | 383 | case PARSE_OPT_COMPLETE: |
| 384 | exit(0); |
Pierre Habouzit | 14ec9cb | 2008-07-09 23:38:33 +0200 | [diff] [blame] | 385 | case PARSE_OPT_DONE: |
| 386 | goto parse_done; |
| 387 | } |
Pierre Habouzit | 6b61ec0 | 2008-07-09 23:38:34 +0200 | [diff] [blame] | 388 | parse_revision_opt(&rev, &ctx, options, shortlog_usage); |
Pierre Habouzit | 14ec9cb | 2008-07-09 23:38:33 +0200 | [diff] [blame] | 389 | } |
| 390 | parse_done: |
| 391 | argc = parse_options_end(&ctx); |
| 392 | |
Martin Ågren | 4aa0161 | 2018-03-14 22:34:19 +0100 | [diff] [blame] | 393 | if (nongit && argc > 1) { |
| 394 | error(_("too many arguments given outside repository")); |
| 395 | usage_with_options(shortlog_usage, options); |
| 396 | } |
| 397 | |
Pierre Habouzit | 14ec9cb | 2008-07-09 23:38:33 +0200 | [diff] [blame] | 398 | if (setup_revisions(argc, argv, &rev, NULL) != 1) { |
Ævar Arnfjörð Bjarmason | ab8b53b | 2011-02-22 23:42:32 +0000 | [diff] [blame] | 399 | error(_("unrecognized argument: %s"), argv[1]); |
Pierre Habouzit | 14ec9cb | 2008-07-09 23:38:33 +0200 | [diff] [blame] | 400 | usage_with_options(shortlog_usage, options); |
| 401 | } |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 402 | |
Johannes Schindelin | b526f8e | 2008-07-14 19:08:52 +0100 | [diff] [blame] | 403 | log.user_format = rev.commit_format == CMIT_FMT_USERFORMAT; |
Will Palmer | c197702 | 2010-05-03 22:18:57 -0500 | [diff] [blame] | 404 | log.abbrev = rev.abbrev; |
Johannes Schindelin | 7f7d712 | 2016-06-22 17:02:07 +0200 | [diff] [blame] | 405 | log.file = rev.diffopt.file; |
Johannes Schindelin | b526f8e | 2008-07-14 19:08:52 +0100 | [diff] [blame] | 406 | |
Jeff King | 63d24fa | 2020-09-27 04:40:15 -0400 | [diff] [blame] | 407 | if (!log.groups) |
| 408 | log.groups = SHORTLOG_GROUP_AUTHOR; |
| 409 | string_list_sort(&log.trailers); |
| 410 | |
Junio C Hamano | 3384a2d | 2007-12-11 10:09:04 -0800 | [diff] [blame] | 411 | /* assume HEAD if from a tty */ |
Jonas Fonseca | abe549e | 2008-03-14 22:35:24 +0100 | [diff] [blame] | 412 | if (!nongit && !rev.pending.nr && isatty(0)) |
Junio C Hamano | 3384a2d | 2007-12-11 10:09:04 -0800 | [diff] [blame] | 413 | add_head_to_pending(&rev); |
Junio C Hamano | 0497c62 | 2007-03-08 02:12:06 -0800 | [diff] [blame] | 414 | if (rev.pending.nr == 0) { |
Michele Ballabio | 3731449 | 2010-02-24 21:49:03 +0100 | [diff] [blame] | 415 | if (isatty(0)) |
Ævar Arnfjörð Bjarmason | ab8b53b | 2011-02-22 23:42:32 +0000 | [diff] [blame] | 416 | fprintf(stderr, _("(reading log message from standard input)\n")); |
Daniel Barkalow | 552bcac | 2008-02-25 18:24:14 -0500 | [diff] [blame] | 417 | read_from_stdin(&log); |
Junio C Hamano | 0497c62 | 2007-03-08 02:12:06 -0800 | [diff] [blame] | 418 | } |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 419 | else |
Daniel Barkalow | 552bcac | 2008-02-25 18:24:14 -0500 | [diff] [blame] | 420 | get_from_rev(&rev, &log); |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 421 | |
Daniel Barkalow | 552bcac | 2008-02-25 18:24:14 -0500 | [diff] [blame] | 422 | shortlog_output(&log); |
Johannes Schindelin | 7f7d712 | 2016-06-22 17:02:07 +0200 | [diff] [blame] | 423 | if (log.file != stdout) |
| 424 | fclose(log.file); |
Daniel Barkalow | 552bcac | 2008-02-25 18:24:14 -0500 | [diff] [blame] | 425 | return 0; |
| 426 | } |
| 427 | |
René Scharfe | bb96a2c | 2010-02-19 23:15:01 +0100 | [diff] [blame] | 428 | static void add_wrapped_shortlog_msg(struct strbuf *sb, const char *s, |
| 429 | const struct shortlog *log) |
| 430 | { |
Steffen Prohaska | 5b59708 | 2012-12-11 06:59:21 +0100 | [diff] [blame] | 431 | strbuf_add_wrapped_text(sb, s, log->in1, log->in2, log->wrap); |
| 432 | strbuf_addch(sb, '\n'); |
René Scharfe | bb96a2c | 2010-02-19 23:15:01 +0100 | [diff] [blame] | 433 | } |
| 434 | |
Daniel Barkalow | 552bcac | 2008-02-25 18:24:14 -0500 | [diff] [blame] | 435 | void shortlog_output(struct shortlog *log) |
| 436 | { |
| 437 | int i, j; |
René Scharfe | bb96a2c | 2010-02-19 23:15:01 +0100 | [diff] [blame] | 438 | struct strbuf sb = STRBUF_INIT; |
| 439 | |
Daniel Barkalow | 552bcac | 2008-02-25 18:24:14 -0500 | [diff] [blame] | 440 | if (log->sort_by_number) |
René Scharfe | 1b5294d | 2016-09-30 01:40:14 +0200 | [diff] [blame] | 441 | QSORT(log->list.items, log->list.nr, |
Jeff King | 9b21a34 | 2016-01-18 15:02:59 -0500 | [diff] [blame] | 442 | log->summary ? compare_by_counter : compare_by_list); |
Daniel Barkalow | 552bcac | 2008-02-25 18:24:14 -0500 | [diff] [blame] | 443 | for (i = 0; i < log->list.nr; i++) { |
Jeff King | 9b21a34 | 2016-01-18 15:02:59 -0500 | [diff] [blame] | 444 | const struct string_list_item *item = &log->list.items[i]; |
Daniel Barkalow | 552bcac | 2008-02-25 18:24:14 -0500 | [diff] [blame] | 445 | if (log->summary) { |
Johannes Schindelin | 0a7b357 | 2016-06-22 17:01:49 +0200 | [diff] [blame] | 446 | fprintf(log->file, "%6d\t%s\n", |
| 447 | (int)UTIL_TO_INT(item), item->string); |
Nicolas Pitre | ac60c94 | 2006-11-21 15:49:45 -0500 | [diff] [blame] | 448 | } else { |
Jeff King | 9b21a34 | 2016-01-18 15:02:59 -0500 | [diff] [blame] | 449 | struct string_list *onelines = item->util; |
Johannes Schindelin | 0a7b357 | 2016-06-22 17:01:49 +0200 | [diff] [blame] | 450 | fprintf(log->file, "%s (%d):\n", |
| 451 | item->string, onelines->nr); |
Johannes Schindelin | 3714e7c | 2006-12-22 22:15:59 +0100 | [diff] [blame] | 452 | for (j = onelines->nr - 1; j >= 0; j--) { |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 453 | const char *msg = onelines->items[j].string; |
Junio C Hamano | 3d711d9 | 2007-04-08 01:28:00 -0700 | [diff] [blame] | 454 | |
Daniel Barkalow | 552bcac | 2008-02-25 18:24:14 -0500 | [diff] [blame] | 455 | if (log->wrap_lines) { |
René Scharfe | bb96a2c | 2010-02-19 23:15:01 +0100 | [diff] [blame] | 456 | strbuf_reset(&sb); |
| 457 | add_wrapped_shortlog_msg(&sb, msg, log); |
Johannes Schindelin | 0a7b357 | 2016-06-22 17:01:49 +0200 | [diff] [blame] | 458 | fwrite(sb.buf, sb.len, 1, log->file); |
Junio C Hamano | 3d711d9 | 2007-04-08 01:28:00 -0700 | [diff] [blame] | 459 | } |
| 460 | else |
Johannes Schindelin | 0a7b357 | 2016-06-22 17:01:49 +0200 | [diff] [blame] | 461 | fprintf(log->file, " %s\n", msg); |
Johannes Schindelin | 3714e7c | 2006-12-22 22:15:59 +0100 | [diff] [blame] | 462 | } |
Johannes Schindelin | 0a7b357 | 2016-06-22 17:01:49 +0200 | [diff] [blame] | 463 | putc('\n', log->file); |
Jeff King | 9b21a34 | 2016-01-18 15:02:59 -0500 | [diff] [blame] | 464 | onelines->strdup_strings = 1; |
| 465 | string_list_clear(onelines, 0); |
| 466 | free(onelines); |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 467 | } |
| 468 | |
Daniel Barkalow | 552bcac | 2008-02-25 18:24:14 -0500 | [diff] [blame] | 469 | log->list.items[i].util = NULL; |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 470 | } |
| 471 | |
René Scharfe | bb96a2c | 2010-02-19 23:15:01 +0100 | [diff] [blame] | 472 | strbuf_release(&sb); |
Johannes Schindelin | c455c87 | 2008-07-21 19:03:49 +0100 | [diff] [blame] | 473 | log->list.strdup_strings = 1; |
| 474 | string_list_clear(&log->list, 1); |
Marius Storm-Olsen | d20d654 | 2009-02-08 15:34:30 +0100 | [diff] [blame] | 475 | clear_mailmap(&log->mailmap); |
Johannes Schindelin | b8ec592 | 2006-10-22 13:23:31 +0200 | [diff] [blame] | 476 | } |