2f9c69157d7fbdd5f2b578fe72c2422712d36800
   1#include "cache.h"
   2#include "string-list.h"
   3#include "mailmap.h"
   4
   5#define DEBUG_MAILMAP 0
   6#if DEBUG_MAILMAP
   7#define debug_mm(...) fprintf(stderr, __VA_ARGS__)
   8#else
   9static inline void debug_mm(const char *format, ...) {}
  10#endif
  11
  12const char *git_mailmap_file;
  13const char *git_mailmap_blob;
  14
  15struct mailmap_info {
  16        char *name;
  17        char *email;
  18};
  19
  20struct mailmap_entry {
  21        /* name and email for the simple mail-only case */
  22        char *name;
  23        char *email;
  24
  25        /* name and email for the complex mail and name matching case */
  26        struct string_list namemap;
  27};
  28
  29static void free_mailmap_info(void *p, const char *s)
  30{
  31        struct mailmap_info *mi = (struct mailmap_info *)p;
  32        debug_mm("mailmap: -- complex: '%s' -> '%s' <%s>\n", s, mi->name, mi->email);
  33        free(mi->name);
  34        free(mi->email);
  35}
  36
  37static void free_mailmap_entry(void *p, const char *s)
  38{
  39        struct mailmap_entry *me = (struct mailmap_entry *)p;
  40        debug_mm("mailmap: removing entries for <%s>, with %d sub-entries\n", s, me->namemap.nr);
  41        debug_mm("mailmap: - simple: '%s' <%s>\n", me->name, me->email);
  42        free(me->name);
  43        free(me->email);
  44
  45        me->namemap.strdup_strings = 1;
  46        string_list_clear_func(&me->namemap, free_mailmap_info);
  47}
  48
  49static void add_mapping(struct string_list *map,
  50                        char *new_name, char *new_email, char *old_name, char *old_email)
  51{
  52        struct mailmap_entry *me;
  53        int index;
  54        char *p;
  55
  56        if (old_email)
  57                for (p = old_email; *p; p++)
  58                        *p = tolower(*p);
  59        if (new_email)
  60                for (p = new_email; *p; p++)
  61                        *p = tolower(*p);
  62
  63        if (old_email == NULL) {
  64                old_email = new_email;
  65                new_email = NULL;
  66        }
  67
  68        if ((index = string_list_find_insert_index(map, old_email, 1)) < 0) {
  69                /* mailmap entry exists, invert index value */
  70                index = -1 - index;
  71        } else {
  72                /* create mailmap entry */
  73                struct string_list_item *item = string_list_insert_at_index(map, index, old_email);
  74                item->util = xcalloc(1, sizeof(struct mailmap_entry));
  75                ((struct mailmap_entry *)item->util)->namemap.strdup_strings = 1;
  76        }
  77        me = (struct mailmap_entry *)map->items[index].util;
  78
  79        if (old_name == NULL) {
  80                debug_mm("mailmap: adding (simple) entry for %s at index %d\n", old_email, index);
  81                /* Replace current name and new email for simple entry */
  82                if (new_name) {
  83                        free(me->name);
  84                        me->name = xstrdup(new_name);
  85                }
  86                if (new_email) {
  87                        free(me->email);
  88                        me->email = xstrdup(new_email);
  89                }
  90        } else {
  91                struct mailmap_info *mi = xcalloc(1, sizeof(struct mailmap_info));
  92                debug_mm("mailmap: adding (complex) entry for %s at index %d\n", old_email, index);
  93                if (new_name)
  94                        mi->name = xstrdup(new_name);
  95                if (new_email)
  96                        mi->email = xstrdup(new_email);
  97                string_list_insert(&me->namemap, old_name)->util = mi;
  98        }
  99
 100        debug_mm("mailmap:  '%s' <%s> -> '%s' <%s>\n",
 101                 old_name, old_email, new_name, new_email);
 102}
 103
 104static char *parse_name_and_email(char *buffer, char **name,
 105                char **email, int allow_empty_email)
 106{
 107        char *left, *right, *nstart, *nend;
 108        *name = *email = NULL;
 109
 110        if ((left = strchr(buffer, '<')) == NULL)
 111                return NULL;
 112        if ((right = strchr(left+1, '>')) == NULL)
 113                return NULL;
 114        if (!allow_empty_email && (left+1 == right))
 115                return NULL;
 116
 117        /* remove whitespace from beginning and end of name */
 118        nstart = buffer;
 119        while (isspace(*nstart) && nstart < left)
 120                ++nstart;
 121        nend = left-1;
 122        while (nend > nstart && isspace(*nend))
 123                --nend;
 124
 125        *name = (nstart < nend ? nstart : NULL);
 126        *email = left+1;
 127        *(nend+1) = '\0';
 128        *right++ = '\0';
 129
 130        return (*right == '\0' ? NULL : right);
 131}
 132
 133static void read_mailmap_line(struct string_list *map, char *buffer,
 134                              char **repo_abbrev)
 135{
 136        char *name1 = NULL, *email1 = NULL, *name2 = NULL, *email2 = NULL;
 137        if (buffer[0] == '#') {
 138                static const char abbrev[] = "# repo-abbrev:";
 139                int abblen = sizeof(abbrev) - 1;
 140                int len = strlen(buffer);
 141
 142                if (!repo_abbrev)
 143                        return;
 144
 145                if (len && buffer[len - 1] == '\n')
 146                        buffer[--len] = 0;
 147                if (!strncmp(buffer, abbrev, abblen)) {
 148                        char *cp;
 149
 150                        if (repo_abbrev)
 151                                free(*repo_abbrev);
 152                        *repo_abbrev = xmalloc(len);
 153
 154                        for (cp = buffer + abblen; isspace(*cp); cp++)
 155                                ; /* nothing */
 156                        strcpy(*repo_abbrev, cp);
 157                }
 158                return;
 159        }
 160        if ((name2 = parse_name_and_email(buffer, &name1, &email1, 0)) != NULL)
 161                parse_name_and_email(name2, &name2, &email2, 1);
 162
 163        if (email1)
 164                add_mapping(map, name1, email1, name2, email2);
 165}
 166
 167static int read_mailmap_file(struct string_list *map, const char *filename,
 168                             char **repo_abbrev)
 169{
 170        char buffer[1024];
 171        FILE *f = (filename == NULL ? NULL : fopen(filename, "r"));
 172
 173        if (f == NULL)
 174                return 1;
 175        while (fgets(buffer, sizeof(buffer), f) != NULL)
 176                read_mailmap_line(map, buffer, repo_abbrev);
 177        fclose(f);
 178        return 0;
 179}
 180
 181static void read_mailmap_buf(struct string_list *map,
 182                             const char *buf, unsigned long len,
 183                             char **repo_abbrev)
 184{
 185        while (len) {
 186                const char *end = strchrnul(buf, '\n');
 187                unsigned long linelen = end - buf + 1;
 188                char *line = xmemdupz(buf, linelen);
 189
 190                read_mailmap_line(map, line, repo_abbrev);
 191
 192                free(line);
 193                buf += linelen;
 194                len -= linelen;
 195        }
 196}
 197
 198static int read_mailmap_blob(struct string_list *map,
 199                             const char *name,
 200                             char **repo_abbrev)
 201{
 202        unsigned char sha1[20];
 203        char *buf;
 204        unsigned long size;
 205        enum object_type type;
 206
 207        if (!name)
 208                return 1;
 209        if (get_sha1(name, sha1) < 0)
 210                return 1;
 211
 212        buf = read_sha1_file(sha1, &type, &size);
 213        if (!buf)
 214                return 1;
 215        if (type != OBJ_BLOB)
 216                return 1;
 217
 218        read_mailmap_buf(map, buf, size, repo_abbrev);
 219
 220        free(buf);
 221        return 0;
 222}
 223
 224int read_mailmap(struct string_list *map, char **repo_abbrev)
 225{
 226        map->strdup_strings = 1;
 227        /* each failure returns 1, so >2 means all calls failed */
 228        return read_mailmap_file(map, ".mailmap", repo_abbrev) +
 229               read_mailmap_blob(map, git_mailmap_blob, repo_abbrev) +
 230               read_mailmap_file(map, git_mailmap_file, repo_abbrev) > 2;
 231}
 232
 233void clear_mailmap(struct string_list *map)
 234{
 235        debug_mm("mailmap: clearing %d entries...\n", map->nr);
 236        map->strdup_strings = 1;
 237        string_list_clear_func(map, free_mailmap_entry);
 238        debug_mm("mailmap: cleared\n");
 239}
 240
 241int map_user(struct string_list *map,
 242             char *email, int maxlen_email, char *name, int maxlen_name)
 243{
 244        char *end_of_email;
 245        struct string_list_item *item;
 246        struct mailmap_entry *me;
 247        char buf[1024], *mailbuf;
 248        int i;
 249
 250        /* figure out space requirement for email */
 251        end_of_email = strchr(email, '>');
 252        if (!end_of_email) {
 253                /* email passed in might not be wrapped in <>, but end with a \0 */
 254                end_of_email = memchr(email, '\0', maxlen_email);
 255                if (!end_of_email)
 256                        return 0;
 257        }
 258        if (end_of_email - email + 1 < sizeof(buf))
 259                mailbuf = buf;
 260        else
 261                mailbuf = xmalloc(end_of_email - email + 1);
 262
 263        /* downcase the email address */
 264        for (i = 0; i < end_of_email - email; i++)
 265                mailbuf[i] = tolower(email[i]);
 266        mailbuf[i] = 0;
 267
 268        debug_mm("map_user: map '%s' <%s>\n", name, mailbuf);
 269        item = string_list_lookup(map, mailbuf);
 270        if (item != NULL) {
 271                me = (struct mailmap_entry *)item->util;
 272                if (me->namemap.nr) {
 273                        /* The item has multiple items, so we'll look up on name too */
 274                        /* If the name is not found, we choose the simple entry      */
 275                        struct string_list_item *subitem = string_list_lookup(&me->namemap, name);
 276                        if (subitem)
 277                                item = subitem;
 278                }
 279        }
 280        if (mailbuf != buf)
 281                free(mailbuf);
 282        if (item != NULL) {
 283                struct mailmap_info *mi = (struct mailmap_info *)item->util;
 284                if (mi->name == NULL && (mi->email == NULL || maxlen_email == 0)) {
 285                        debug_mm("map_user:  -- (no simple mapping)\n");
 286                        return 0;
 287                }
 288                if (maxlen_email && mi->email)
 289                        strlcpy(email, mi->email, maxlen_email);
 290                else
 291                        *end_of_email = '\0';
 292                if (maxlen_name && mi->name)
 293                        strlcpy(name, mi->name, maxlen_name);
 294                debug_mm("map_user:  to '%s' <%s>\n", name, mi->email ? mi->email : "");
 295                return 1;
 296        }
 297        debug_mm("map_user:  --\n");
 298        return 0;
 299}