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