builtin / shortlog.con commit Merge branch 'jn/post-receive-utf8' (09a3730)
   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 (!prefixcmp(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                if (!(author[0] == 'A' || author[0] == 'a') ||
  98                    prefixcmp(author + 1, "uthor: "))
  99                        continue;
 100                while (fgets(oneline, sizeof(oneline), stdin) &&
 101                       oneline[0] != '\n')
 102                        ; /* discard headers */
 103                while (fgets(oneline, sizeof(oneline), stdin) &&
 104                       oneline[0] == '\n')
 105                        ; /* discard blanks */
 106                insert_one_record(log, author + 8, oneline);
 107        }
 108}
 109
 110void shortlog_add_commit(struct shortlog *log, struct commit *commit)
 111{
 112        const char *author = NULL, *buffer;
 113        struct strbuf buf = STRBUF_INIT;
 114        struct strbuf ufbuf = STRBUF_INIT;
 115
 116        pp_commit_easy(CMIT_FMT_RAW, commit, &buf);
 117        buffer = buf.buf;
 118        while (*buffer && *buffer != '\n') {
 119                const char *eol = strchr(buffer, '\n');
 120
 121                if (eol == NULL)
 122                        eol = buffer + strlen(buffer);
 123                else
 124                        eol++;
 125
 126                if (!prefixcmp(buffer, "author "))
 127                        author = buffer + 7;
 128                buffer = eol;
 129        }
 130        if (!author)
 131                die(_("Missing author: %s"),
 132                    sha1_to_hex(commit->object.sha1));
 133        if (log->user_format) {
 134                struct pretty_print_context ctx = {0};
 135                ctx.fmt = CMIT_FMT_USERFORMAT;
 136                ctx.abbrev = log->abbrev;
 137                ctx.subject = "";
 138                ctx.after_subject = "";
 139                ctx.date_mode = DATE_NORMAL;
 140                ctx.output_encoding = get_log_output_encoding();
 141                pretty_print_commit(&ctx, commit, &ufbuf);
 142                buffer = ufbuf.buf;
 143        } else if (*buffer) {
 144                buffer++;
 145        }
 146        insert_one_record(log, author, !*buffer ? "<none>" : buffer);
 147        strbuf_release(&ufbuf);
 148        strbuf_release(&buf);
 149}
 150
 151static void get_from_rev(struct rev_info *rev, struct shortlog *log)
 152{
 153        struct commit *commit;
 154
 155        if (prepare_revision_walk(rev))
 156                die(_("revision walk setup failed"));
 157        while ((commit = get_revision(rev)) != NULL)
 158                shortlog_add_commit(log, commit);
 159}
 160
 161static int parse_uint(char const **arg, int comma, int defval)
 162{
 163        unsigned long ul;
 164        int ret;
 165        char *endp;
 166
 167        ul = strtoul(*arg, &endp, 10);
 168        if (*endp && *endp != comma)
 169                return -1;
 170        if (ul > INT_MAX)
 171                return -1;
 172        ret = *arg == endp ? defval : (int)ul;
 173        *arg = *endp ? endp + 1 : endp;
 174        return ret;
 175}
 176
 177static const char wrap_arg_usage[] = "-w[<width>[,<indent1>[,<indent2>]]]";
 178#define DEFAULT_WRAPLEN 76
 179#define DEFAULT_INDENT1 6
 180#define DEFAULT_INDENT2 9
 181
 182static int parse_wrap_args(const struct option *opt, const char *arg, int unset)
 183{
 184        struct shortlog *log = opt->value;
 185
 186        log->wrap_lines = !unset;
 187        if (unset)
 188                return 0;
 189        if (!arg) {
 190                log->wrap = DEFAULT_WRAPLEN;
 191                log->in1 = DEFAULT_INDENT1;
 192                log->in2 = DEFAULT_INDENT2;
 193                return 0;
 194        }
 195
 196        log->wrap = parse_uint(&arg, ',', DEFAULT_WRAPLEN);
 197        log->in1 = parse_uint(&arg, ',', DEFAULT_INDENT1);
 198        log->in2 = parse_uint(&arg, '\0', DEFAULT_INDENT2);
 199        if (log->wrap < 0 || log->in1 < 0 || log->in2 < 0)
 200                return error(wrap_arg_usage);
 201        if (log->wrap &&
 202            ((log->in1 && log->wrap <= log->in1) ||
 203             (log->in2 && log->wrap <= log->in2)))
 204                return error(wrap_arg_usage);
 205        return 0;
 206}
 207
 208void shortlog_init(struct shortlog *log)
 209{
 210        memset(log, 0, sizeof(*log));
 211
 212        read_mailmap(&log->mailmap, &log->common_repo_prefix);
 213
 214        log->list.strdup_strings = 1;
 215        log->wrap = DEFAULT_WRAPLEN;
 216        log->in1 = DEFAULT_INDENT1;
 217        log->in2 = DEFAULT_INDENT2;
 218}
 219
 220int cmd_shortlog(int argc, const char **argv, const char *prefix)
 221{
 222        static struct shortlog log;
 223        static struct rev_info rev;
 224        int nongit = !startup_info->have_repository;
 225
 226        static const struct option options[] = {
 227                OPT_BOOL('n', "numbered", &log.sort_by_number,
 228                         N_("sort output according to the number of commits per author")),
 229                OPT_BOOL('s', "summary", &log.summary,
 230                         N_("Suppress commit descriptions, only provides commit count")),
 231                OPT_BOOL('e', "email", &log.email,
 232                         N_("Show the email address of each author")),
 233                { OPTION_CALLBACK, 'w', NULL, &log, N_("w[,i1[,i2]]"),
 234                        N_("Linewrap output"), PARSE_OPT_OPTARG, &parse_wrap_args },
 235                OPT_END(),
 236        };
 237
 238        struct parse_opt_ctx_t ctx;
 239
 240        git_config(git_default_config, NULL);
 241        shortlog_init(&log);
 242        init_revisions(&rev, prefix);
 243        parse_options_start(&ctx, argc, argv, prefix, options,
 244                            PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0);
 245
 246        for (;;) {
 247                switch (parse_options_step(&ctx, options, shortlog_usage)) {
 248                case PARSE_OPT_HELP:
 249                        exit(129);
 250                case PARSE_OPT_DONE:
 251                        goto parse_done;
 252                }
 253                parse_revision_opt(&rev, &ctx, options, shortlog_usage);
 254        }
 255parse_done:
 256        argc = parse_options_end(&ctx);
 257
 258        if (setup_revisions(argc, argv, &rev, NULL) != 1) {
 259                error(_("unrecognized argument: %s"), argv[1]);
 260                usage_with_options(shortlog_usage, options);
 261        }
 262
 263        log.user_format = rev.commit_format == CMIT_FMT_USERFORMAT;
 264        log.abbrev = rev.abbrev;
 265
 266        /* assume HEAD if from a tty */
 267        if (!nongit && !rev.pending.nr && isatty(0))
 268                add_head_to_pending(&rev);
 269        if (rev.pending.nr == 0) {
 270                if (isatty(0))
 271                        fprintf(stderr, _("(reading log message from standard input)\n"));
 272                read_from_stdin(&log);
 273        }
 274        else
 275                get_from_rev(&rev, &log);
 276
 277        shortlog_output(&log);
 278        return 0;
 279}
 280
 281static void add_wrapped_shortlog_msg(struct strbuf *sb, const char *s,
 282                                     const struct shortlog *log)
 283{
 284        strbuf_add_wrapped_text(sb, s, log->in1, log->in2, log->wrap);
 285        strbuf_addch(sb, '\n');
 286}
 287
 288void shortlog_output(struct shortlog *log)
 289{
 290        int i, j;
 291        struct strbuf sb = STRBUF_INIT;
 292
 293        if (log->sort_by_number)
 294                qsort(log->list.items, log->list.nr, sizeof(struct string_list_item),
 295                        compare_by_number);
 296        for (i = 0; i < log->list.nr; i++) {
 297                struct string_list *onelines = log->list.items[i].util;
 298
 299                if (log->summary) {
 300                        printf("%6d\t%s\n", onelines->nr, log->list.items[i].string);
 301                } else {
 302                        printf("%s (%d):\n", log->list.items[i].string, onelines->nr);
 303                        for (j = onelines->nr - 1; j >= 0; j--) {
 304                                const char *msg = onelines->items[j].string;
 305
 306                                if (log->wrap_lines) {
 307                                        strbuf_reset(&sb);
 308                                        add_wrapped_shortlog_msg(&sb, msg, log);
 309                                        fwrite(sb.buf, sb.len, 1, stdout);
 310                                }
 311                                else
 312                                        printf("      %s\n", msg);
 313                        }
 314                        putchar('\n');
 315                }
 316
 317                onelines->strdup_strings = 1;
 318                string_list_clear(onelines, 0);
 319                free(onelines);
 320                log->list.items[i].util = NULL;
 321        }
 322
 323        strbuf_release(&sb);
 324        log->list.strdup_strings = 1;
 325        string_list_clear(&log->list, 1);
 326        clear_mailmap(&log->mailmap);
 327}