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