builtin-shortlog.con commit Merge branch 'master' of git://repo.or.cz/git-gui (82ff9d2)
   1#include "builtin.h"
   2#include "cache.h"
   3#include "commit.h"
   4#include "diff.h"
   5#include "path-list.h"
   6#include "revision.h"
   7#include "utf8.h"
   8#include "mailmap.h"
   9
  10static const char shortlog_usage[] =
  11"git-shortlog [-n] [-s] [<commit-id>... ]";
  12
  13static char *common_repo_prefix;
  14
  15static int compare_by_number(const void *a1, const void *a2)
  16{
  17        const struct path_list_item *i1 = a1, *i2 = a2;
  18        const struct path_list *l1 = i1->util, *l2 = i2->util;
  19
  20        if (l1->nr < l2->nr)
  21                return 1;
  22        else if (l1->nr == l2->nr)
  23                return 0;
  24        else
  25                return -1;
  26}
  27
  28static struct path_list mailmap = {NULL, 0, 0, 0};
  29
  30static void insert_author_oneline(struct path_list *list,
  31                const char *author, int authorlen,
  32                const char *oneline, int onelinelen)
  33{
  34        const char *dot3 = common_repo_prefix;
  35        char *buffer, *p;
  36        struct path_list_item *item;
  37        struct path_list *onelines;
  38
  39        while (authorlen > 0 && isspace(author[authorlen - 1]))
  40                authorlen--;
  41
  42        buffer = xmalloc(authorlen + 1);
  43        memcpy(buffer, author, authorlen);
  44        buffer[authorlen] = '\0';
  45
  46        item = path_list_insert(buffer, list);
  47        if (item->util == NULL)
  48                item->util = xcalloc(1, sizeof(struct path_list));
  49        else
  50                free(buffer);
  51
  52        if (!prefixcmp(oneline, "[PATCH")) {
  53                char *eob = strchr(oneline, ']');
  54
  55                if (eob) {
  56                        while (isspace(eob[1]) && eob[1] != '\n')
  57                                eob++;
  58                        if (eob - oneline < onelinelen) {
  59                                onelinelen -= eob - oneline;
  60                                oneline = eob;
  61                        }
  62                }
  63        }
  64
  65        while (onelinelen > 0 && isspace(oneline[0])) {
  66                oneline++;
  67                onelinelen--;
  68        }
  69
  70        while (onelinelen > 0 && isspace(oneline[onelinelen - 1]))
  71                onelinelen--;
  72
  73        buffer = xmalloc(onelinelen + 1);
  74        memcpy(buffer, oneline, onelinelen);
  75        buffer[onelinelen] = '\0';
  76
  77        if (dot3) {
  78                int dot3len = strlen(dot3);
  79                if (dot3len > 5) {
  80                        while ((p = strstr(buffer, dot3)) != NULL) {
  81                                int taillen = strlen(p) - dot3len;
  82                                memcpy(p, "/.../", 5);
  83                                memmove(p + 5, p + dot3len, taillen + 1);
  84                        }
  85                }
  86        }
  87
  88        onelines = item->util;
  89        if (onelines->nr >= onelines->alloc) {
  90                onelines->alloc = alloc_nr(onelines->nr);
  91                onelines->items = xrealloc(onelines->items,
  92                                onelines->alloc
  93                                * sizeof(struct path_list_item));
  94        }
  95
  96        onelines->items[onelines->nr].util = NULL;
  97        onelines->items[onelines->nr++].path = buffer;
  98}
  99
 100static void read_from_stdin(struct path_list *list)
 101{
 102        char buffer[1024];
 103
 104        while (fgets(buffer, sizeof(buffer), stdin) != NULL) {
 105                char *bob;
 106                if ((buffer[0] == 'A' || buffer[0] == 'a') &&
 107                                !prefixcmp(buffer + 1, "uthor: ") &&
 108                                (bob = strchr(buffer + 7, '<')) != NULL) {
 109                        char buffer2[1024], offset = 0;
 110
 111                        if (map_email(&mailmap, bob + 1, buffer, sizeof(buffer)))
 112                                bob = buffer + strlen(buffer);
 113                        else {
 114                                offset = 8;
 115                                while (buffer + offset < bob &&
 116                                       isspace(bob[-1]))
 117                                        bob--;
 118                        }
 119
 120                        while (fgets(buffer2, sizeof(buffer2), stdin) &&
 121                                        buffer2[0] != '\n')
 122                                ; /* chomp input */
 123                        if (fgets(buffer2, sizeof(buffer2), stdin)) {
 124                                int l2 = strlen(buffer2);
 125                                int i;
 126                                for (i = 0; i < l2; i++)
 127                                        if (!isspace(buffer2[i]))
 128                                                break;
 129                                insert_author_oneline(list,
 130                                                buffer + offset,
 131                                                bob - buffer - offset,
 132                                                buffer2 + i, l2 - i);
 133                        }
 134                }
 135        }
 136}
 137
 138static void get_from_rev(struct rev_info *rev, struct path_list *list)
 139{
 140        char scratch[1024];
 141        struct commit *commit;
 142
 143        prepare_revision_walk(rev);
 144        while ((commit = get_revision(rev)) != NULL) {
 145                const char *author = NULL, *oneline, *buffer;
 146                int authorlen = authorlen, onelinelen;
 147
 148                /* get author and oneline */
 149                for (buffer = commit->buffer; buffer && *buffer != '\0' &&
 150                                *buffer != '\n'; ) {
 151                        const char *eol = strchr(buffer, '\n');
 152
 153                        if (eol == NULL)
 154                                eol = buffer + strlen(buffer);
 155                        else
 156                                eol++;
 157
 158                        if (!prefixcmp(buffer, "author ")) {
 159                                char *bracket = strchr(buffer, '<');
 160
 161                                if (bracket == NULL || bracket > eol)
 162                                        die("Invalid commit buffer: %s",
 163                                            sha1_to_hex(commit->object.sha1));
 164
 165                                if (map_email(&mailmap, bracket + 1, scratch,
 166                                                        sizeof(scratch))) {
 167                                        author = scratch;
 168                                        authorlen = strlen(scratch);
 169                                } else {
 170                                        if (bracket[-1] == ' ')
 171                                                bracket--;
 172
 173                                        author = buffer + 7;
 174                                        authorlen = bracket - buffer - 7;
 175                                }
 176                        }
 177                        buffer = eol;
 178                }
 179
 180                if (author == NULL)
 181                        die ("Missing author: %s",
 182                                        sha1_to_hex(commit->object.sha1));
 183
 184                if (buffer == NULL || *buffer == '\0') {
 185                        oneline = "<none>";
 186                        onelinelen = sizeof(oneline) + 1;
 187                } else {
 188                        char *eol;
 189
 190                        oneline = buffer + 1;
 191                        eol = strchr(oneline, '\n');
 192                        if (eol == NULL)
 193                                onelinelen = strlen(oneline);
 194                        else
 195                                onelinelen = eol - oneline;
 196                }
 197
 198                insert_author_oneline(list,
 199                                author, authorlen, oneline, onelinelen);
 200        }
 201
 202}
 203
 204static int parse_uint(char const **arg, int comma)
 205{
 206        unsigned long ul;
 207        int ret;
 208        char *endp;
 209
 210        ul = strtoul(*arg, &endp, 10);
 211        if (endp != *arg && *endp && *endp != comma)
 212                return -1;
 213        ret = (int) ul;
 214        if (ret != ul)
 215                return -1;
 216        *arg = endp;
 217        if (**arg)
 218                (*arg)++;
 219        return ret;
 220}
 221
 222static const char wrap_arg_usage[] = "-w[<width>[,<indent1>[,<indent2>]]]";
 223#define DEFAULT_WRAPLEN 76
 224#define DEFAULT_INDENT1 6
 225#define DEFAULT_INDENT2 9
 226
 227static void parse_wrap_args(const char *arg, int *in1, int *in2, int *wrap)
 228{
 229        arg += 2; /* skip -w */
 230
 231        *wrap = parse_uint(&arg, ',');
 232        if (*wrap < 0)
 233                die(wrap_arg_usage);
 234        *in1 = parse_uint(&arg, ',');
 235        if (*in1 < 0)
 236                die(wrap_arg_usage);
 237        *in2 = parse_uint(&arg, '\0');
 238        if (*in2 < 0)
 239                die(wrap_arg_usage);
 240
 241        if (!*wrap)
 242                *wrap = DEFAULT_WRAPLEN;
 243        if (!*in1)
 244                *in1 = DEFAULT_INDENT1;
 245        if (!*in2)
 246                *in2 = DEFAULT_INDENT2;
 247        if (*wrap &&
 248            ((*in1 && *wrap <= *in1) ||
 249             (*in2 && *wrap <= *in2)))
 250                die(wrap_arg_usage);
 251}
 252
 253int cmd_shortlog(int argc, const char **argv, const char *prefix)
 254{
 255        struct rev_info rev;
 256        struct path_list list = { NULL, 0, 0, 1 };
 257        int i, j, sort_by_number = 0, summary = 0;
 258        int wrap_lines = 0;
 259        int wrap = DEFAULT_WRAPLEN;
 260        int in1 = DEFAULT_INDENT1;
 261        int in2 = DEFAULT_INDENT2;
 262
 263        /* since -n is a shadowed rev argument, parse our args first */
 264        while (argc > 1) {
 265                if (!strcmp(argv[1], "-n") || !strcmp(argv[1], "--numbered"))
 266                        sort_by_number = 1;
 267                else if (!strcmp(argv[1], "-s") ||
 268                                !strcmp(argv[1], "--summary"))
 269                        summary = 1;
 270                else if (!prefixcmp(argv[1], "-w")) {
 271                        wrap_lines = 1;
 272                        parse_wrap_args(argv[1], &in1, &in2, &wrap);
 273                }
 274                else if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))
 275                        usage(shortlog_usage);
 276                else
 277                        break;
 278                argv++;
 279                argc--;
 280        }
 281        init_revisions(&rev, prefix);
 282        argc = setup_revisions(argc, argv, &rev, NULL);
 283        if (argc > 1)
 284                die ("unrecognized argument: %s", argv[1]);
 285
 286        read_mailmap(&mailmap, ".mailmap", &common_repo_prefix);
 287
 288        if (rev.pending.nr == 0) {
 289                if (isatty(0))
 290                        fprintf(stderr, "(reading log to summarize from standard input)\n");
 291                read_from_stdin(&list);
 292        }
 293        else
 294                get_from_rev(&rev, &list);
 295
 296        if (sort_by_number)
 297                qsort(list.items, list.nr, sizeof(struct path_list_item),
 298                        compare_by_number);
 299
 300        for (i = 0; i < list.nr; i++) {
 301                struct path_list *onelines = list.items[i].util;
 302
 303                if (summary) {
 304                        printf("%s: %d\n", list.items[i].path, onelines->nr);
 305                } else {
 306                        printf("%s (%d):\n", list.items[i].path, onelines->nr);
 307                        for (j = onelines->nr - 1; j >= 0; j--) {
 308                                const char *msg = onelines->items[j].path;
 309
 310                                if (wrap_lines) {
 311                                        int col = print_wrapped_text(msg, in1, in2, wrap);
 312                                        if (col != wrap)
 313                                                putchar('\n');
 314                                }
 315                                else
 316                                        printf("      %s\n", msg);
 317                        }
 318                        putchar('\n');
 319                }
 320
 321                onelines->strdup_paths = 1;
 322                path_list_clear(onelines, 1);
 323                free(onelines);
 324                list.items[i].util = NULL;
 325        }
 326
 327        list.strdup_paths = 1;
 328        path_list_clear(&list, 1);
 329        mailmap.strdup_paths = 1;
 330        path_list_clear(&mailmap, 1);
 331
 332        return 0;
 333}