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