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