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