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