builtin / shortlog.con commit cache.h: flip NO_THE_INDEX_COMPATIBILITY_MACROS switch (f8adbec)
   1#include "builtin.h"
   2#include "cache.h"
   3#include "config.h"
   4#include "commit.h"
   5#include "diff.h"
   6#include "string-list.h"
   7#include "revision.h"
   8#include "utf8.h"
   9#include "mailmap.h"
  10#include "shortlog.h"
  11#include "parse-options.h"
  12
  13static char const * const shortlog_usage[] = {
  14        N_("git shortlog [<options>] [<revision-range>] [[--] <path>...]"),
  15        N_("git log --pretty=short | git shortlog [<options>]"),
  16        NULL
  17};
  18
  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
  32static 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
  38static int compare_by_list(const void *a1, const void *a2)
  39{
  40        const struct string_list_item *i1 = a1, *i2 = a2;
  41        const struct string_list *l1 = i1->util, *l2 = i2->util;
  42
  43        if (l1->nr < l2->nr)
  44                return 1;
  45        else if (l1->nr == l2->nr)
  46                return 0;
  47        else
  48                return -1;
  49}
  50
  51static void insert_one_record(struct shortlog *log,
  52                              const char *author,
  53                              const char *oneline)
  54{
  55        struct string_list_item *item;
  56
  57        item = string_list_insert(&log->list, author);
  58
  59        if (log->summary)
  60                item->util = (void *)(UTIL_TO_INT(item) + 1);
  61        else {
  62                const char *dot3 = log->common_repo_prefix;
  63                char *buffer, *p;
  64                struct strbuf subject = STRBUF_INIT;
  65                const char *eol;
  66
  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                                }
  91                        }
  92                }
  93
  94                if (item->util == NULL)
  95                        item->util = xcalloc(1, sizeof(struct string_list));
  96                string_list_append(item->util, buffer);
  97        }
  98}
  99
 100static 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
 123static void read_from_stdin(struct shortlog *log)
 124{
 125        struct strbuf author = STRBUF_INIT;
 126        struct strbuf mapped_author = STRBUF_INIT;
 127        struct strbuf oneline = STRBUF_INIT;
 128        static const char *author_match[2] = { "Author: ", "author " };
 129        static const char *committer_match[2] = { "Commit: ", "committer " };
 130        const char **match;
 131
 132        match = log->committer ? committer_match : author_match;
 133        while (strbuf_getline_lf(&author, stdin) != EOF) {
 134                const char *v;
 135                if (!skip_prefix(author.buf, match[0], &v) &&
 136                    !skip_prefix(author.buf, match[1], &v))
 137                        continue;
 138                while (strbuf_getline_lf(&oneline, stdin) != EOF &&
 139                       oneline.len)
 140                        ; /* discard headers */
 141                while (strbuf_getline_lf(&oneline, stdin) != EOF &&
 142                       !oneline.len)
 143                        ; /* discard blanks */
 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);
 150        }
 151        strbuf_release(&author);
 152        strbuf_release(&mapped_author);
 153        strbuf_release(&oneline);
 154}
 155
 156void shortlog_add_commit(struct shortlog *log, struct commit *commit)
 157{
 158        struct strbuf author = STRBUF_INIT;
 159        struct strbuf oneline = STRBUF_INIT;
 160        struct pretty_print_context ctx = {0};
 161        const char *fmt;
 162
 163        ctx.fmt = CMIT_FMT_USERFORMAT;
 164        ctx.abbrev = log->abbrev;
 165        ctx.print_email_subject = 1;
 166        ctx.date_mode.type = DATE_NORMAL;
 167        ctx.output_encoding = get_log_output_encoding();
 168
 169        fmt = log->committer ?
 170                (log->email ? "%cN <%cE>" : "%cN") :
 171                (log->email ? "%aN <%aE>" : "%aN");
 172
 173        format_commit_message(commit, fmt, &author, &ctx);
 174        if (!log->summary) {
 175                if (log->user_format)
 176                        pretty_print_commit(&ctx, commit, &oneline);
 177                else
 178                        format_commit_message(commit, "%s", &oneline, &ctx);
 179        }
 180
 181        insert_one_record(log, author.buf, oneline.len ? oneline.buf : "<none>");
 182
 183        strbuf_release(&author);
 184        strbuf_release(&oneline);
 185}
 186
 187static void get_from_rev(struct rev_info *rev, struct shortlog *log)
 188{
 189        struct commit *commit;
 190
 191        if (prepare_revision_walk(rev))
 192                die(_("revision walk setup failed"));
 193        while ((commit = get_revision(rev)) != NULL)
 194                shortlog_add_commit(log, commit);
 195}
 196
 197static int parse_uint(char const **arg, int comma, int defval)
 198{
 199        unsigned long ul;
 200        int ret;
 201        char *endp;
 202
 203        ul = strtoul(*arg, &endp, 10);
 204        if (*endp && *endp != comma)
 205                return -1;
 206        if (ul > INT_MAX)
 207                return -1;
 208        ret = *arg == endp ? defval : (int)ul;
 209        *arg = *endp ? endp + 1 : endp;
 210        return ret;
 211}
 212
 213static 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
 218static int parse_wrap_args(const struct option *opt, const char *arg, int unset)
 219{
 220        struct shortlog *log = opt->value;
 221
 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        }
 231
 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;
 242}
 243
 244void shortlog_init(struct shortlog *log)
 245{
 246        memset(log, 0, sizeof(*log));
 247
 248        read_mailmap(&log->mailmap, &log->common_repo_prefix);
 249
 250        log->list.strdup_strings = 1;
 251        log->wrap = DEFAULT_WRAPLEN;
 252        log->in1 = DEFAULT_INDENT1;
 253        log->in2 = DEFAULT_INDENT2;
 254}
 255
 256int cmd_shortlog(int argc, const char **argv, const char *prefix)
 257{
 258        struct shortlog log = { STRING_LIST_INIT_NODUP };
 259        struct rev_info rev;
 260        int nongit = !startup_info->have_repository;
 261
 262        const struct option options[] = {
 263                OPT_BOOL('c', "committer", &log.committer,
 264                         N_("Group by committer rather than author")),
 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")),
 271                { OPTION_CALLBACK, 'w', NULL, &log, N_("<w>[,<i1>[,<i2>]]"),
 272                        N_("Linewrap output"), PARSE_OPT_OPTARG,
 273                        &parse_wrap_args },
 274                OPT_END(),
 275        };
 276
 277        struct parse_opt_ctx_t ctx;
 278
 279        git_config(git_default_config, NULL);
 280        shortlog_init(&log);
 281        repo_init_revisions(the_repository, &rev, prefix);
 282        parse_options_start(&ctx, argc, argv, prefix, options,
 283                            PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0);
 284
 285        for (;;) {
 286                switch (parse_options_step(&ctx, options, shortlog_usage)) {
 287                case PARSE_OPT_HELP:
 288                case PARSE_OPT_ERROR:
 289                        exit(129);
 290                case PARSE_OPT_COMPLETE:
 291                        exit(0);
 292                case PARSE_OPT_DONE:
 293                        goto parse_done;
 294                }
 295                parse_revision_opt(&rev, &ctx, options, shortlog_usage);
 296        }
 297parse_done:
 298        argc = parse_options_end(&ctx);
 299
 300        if (nongit && argc > 1) {
 301                error(_("too many arguments given outside repository"));
 302                usage_with_options(shortlog_usage, options);
 303        }
 304
 305        if (setup_revisions(argc, argv, &rev, NULL) != 1) {
 306                error(_("unrecognized argument: %s"), argv[1]);
 307                usage_with_options(shortlog_usage, options);
 308        }
 309
 310        log.user_format = rev.commit_format == CMIT_FMT_USERFORMAT;
 311        log.abbrev = rev.abbrev;
 312        log.file = rev.diffopt.file;
 313
 314        /* assume HEAD if from a tty */
 315        if (!nongit && !rev.pending.nr && isatty(0))
 316                add_head_to_pending(&rev);
 317        if (rev.pending.nr == 0) {
 318                if (isatty(0))
 319                        fprintf(stderr, _("(reading log message from standard input)\n"));
 320                read_from_stdin(&log);
 321        }
 322        else
 323                get_from_rev(&rev, &log);
 324
 325        shortlog_output(&log);
 326        if (log.file != stdout)
 327                fclose(log.file);
 328        return 0;
 329}
 330
 331static void add_wrapped_shortlog_msg(struct strbuf *sb, const char *s,
 332                                     const struct shortlog *log)
 333{
 334        strbuf_add_wrapped_text(sb, s, log->in1, log->in2, log->wrap);
 335        strbuf_addch(sb, '\n');
 336}
 337
 338void shortlog_output(struct shortlog *log)
 339{
 340        int i, j;
 341        struct strbuf sb = STRBUF_INIT;
 342
 343        if (log->sort_by_number)
 344                QSORT(log->list.items, log->list.nr,
 345                      log->summary ? compare_by_counter : compare_by_list);
 346        for (i = 0; i < log->list.nr; i++) {
 347                const struct string_list_item *item = &log->list.items[i];
 348                if (log->summary) {
 349                        fprintf(log->file, "%6d\t%s\n",
 350                                (int)UTIL_TO_INT(item), item->string);
 351                } else {
 352                        struct string_list *onelines = item->util;
 353                        fprintf(log->file, "%s (%d):\n",
 354                                item->string, onelines->nr);
 355                        for (j = onelines->nr - 1; j >= 0; j--) {
 356                                const char *msg = onelines->items[j].string;
 357
 358                                if (log->wrap_lines) {
 359                                        strbuf_reset(&sb);
 360                                        add_wrapped_shortlog_msg(&sb, msg, log);
 361                                        fwrite(sb.buf, sb.len, 1, log->file);
 362                                }
 363                                else
 364                                        fprintf(log->file, "      %s\n", msg);
 365                        }
 366                        putc('\n', log->file);
 367                        onelines->strdup_strings = 1;
 368                        string_list_clear(onelines, 0);
 369                        free(onelines);
 370                }
 371
 372                log->list.items[i].util = NULL;
 373        }
 374
 375        strbuf_release(&sb);
 376        log->list.strdup_strings = 1;
 377        string_list_clear(&log->list, 1);
 378        clear_mailmap(&log->mailmap);
 379}