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