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