mailmap.con commit mailmap: xcalloc mailmap_info (74b531f)
   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;
  13
  14struct mailmap_info {
  15        char *name;
  16        char *email;
  17};
  18
  19struct mailmap_entry {
  20        /* name and email for the simple mail-only case */
  21        char *name;
  22        char *email;
  23
  24        /* name and email for the complex mail and name matching case */
  25        struct string_list namemap;
  26};
  27
  28static void free_mailmap_info(void *p, const char *s)
  29{
  30        struct mailmap_info *mi = (struct mailmap_info *)p;
  31        debug_mm("mailmap: -- complex: '%s' -> '%s' <%s>\n", s, mi->name, mi->email);
  32        free(mi->name);
  33        free(mi->email);
  34}
  35
  36static void free_mailmap_entry(void *p, const char *s)
  37{
  38        struct mailmap_entry *me = (struct mailmap_entry *)p;
  39        debug_mm("mailmap: removing entries for <%s>, with %d sub-entries\n", s, me->namemap.nr);
  40        debug_mm("mailmap: - simple: '%s' <%s>\n", me->name, me->email);
  41        free(me->name);
  42        free(me->email);
  43
  44        me->namemap.strdup_strings = 1;
  45        string_list_clear_func(&me->namemap, free_mailmap_info);
  46}
  47
  48static void add_mapping(struct string_list *map,
  49                        char *new_name, char *new_email, char *old_name, char *old_email)
  50{
  51        struct mailmap_entry *me;
  52        int index;
  53        if (old_email == NULL) {
  54                old_email = new_email;
  55                new_email = NULL;
  56        }
  57
  58        if ((index = string_list_find_insert_index(map, old_email, 1)) < 0) {
  59                /* mailmap entry exists, invert index value */
  60                index = -1 - index;
  61        } else {
  62                /* create mailmap entry */
  63                struct string_list_item *item = string_list_insert_at_index(index, old_email, map);
  64                item->util = xcalloc(1, sizeof(struct mailmap_entry));
  65                ((struct mailmap_entry *)item->util)->namemap.strdup_strings = 1;
  66        }
  67        me = (struct mailmap_entry *)map->items[index].util;
  68
  69        if (old_name == NULL) {
  70                debug_mm("mailmap: adding (simple) entry for %s at index %d\n", old_email, index);
  71                /* Replace current name and new email for simple entry */
  72                free(me->name);
  73                free(me->email);
  74                if (new_name)
  75                        me->name = xstrdup(new_name);
  76                if (new_email)
  77                        me->email = xstrdup(new_email);
  78        } else {
  79                struct mailmap_info *mi = xcalloc(1, sizeof(struct mailmap_info));
  80                debug_mm("mailmap: adding (complex) entry for %s at index %d\n", old_email, index);
  81                if (new_name)
  82                        mi->name = xstrdup(new_name);
  83                if (new_email)
  84                        mi->email = xstrdup(new_email);
  85                string_list_insert(old_name, &me->namemap)->util = mi;
  86        }
  87
  88        debug_mm("mailmap:  '%s' <%s> -> '%s' <%s>\n",
  89                 old_name, old_email, new_name, new_email);
  90}
  91
  92static char *parse_name_and_email(char *buffer, char **name, char **email)
  93{
  94        char *left, *right, *nstart, *nend;
  95        *name = *email = 0;
  96
  97        if ((left = strchr(buffer, '<')) == NULL)
  98                return NULL;
  99        if ((right = strchr(left+1, '>')) == NULL)
 100                return NULL;
 101        if (left+1 == right)
 102                return NULL;
 103
 104        /* remove whitespace from beginning and end of name */
 105        nstart = buffer;
 106        while (isspace(*nstart) && nstart < left)
 107                ++nstart;
 108        nend = left-1;
 109        while (isspace(*nend) && nend > nstart)
 110                --nend;
 111
 112        *name = (nstart < nend ? nstart : NULL);
 113        *email = left+1;
 114        *(nend+1) = '\0';
 115        *right++ = '\0';
 116
 117        return (*right == '\0' ? NULL : right);
 118}
 119
 120static int read_single_mailmap(struct string_list *map, const char *filename, char **repo_abbrev)
 121{
 122        char buffer[1024];
 123        FILE *f = (filename == NULL ? NULL : fopen(filename, "r"));
 124
 125        if (f == NULL)
 126                return 1;
 127        while (fgets(buffer, sizeof(buffer), f) != NULL) {
 128                char *name1 = 0, *email1 = 0, *name2 = 0, *email2 = 0;
 129                if (buffer[0] == '#') {
 130                        static const char abbrev[] = "# repo-abbrev:";
 131                        int abblen = sizeof(abbrev) - 1;
 132                        int len = strlen(buffer);
 133
 134                        if (!repo_abbrev)
 135                                continue;
 136
 137                        if (len && buffer[len - 1] == '\n')
 138                                buffer[--len] = 0;
 139                        if (!strncmp(buffer, abbrev, abblen)) {
 140                                char *cp;
 141
 142                                if (repo_abbrev)
 143                                        free(*repo_abbrev);
 144                                *repo_abbrev = xmalloc(len);
 145
 146                                for (cp = buffer + abblen; isspace(*cp); cp++)
 147                                        ; /* nothing */
 148                                strcpy(*repo_abbrev, cp);
 149                        }
 150                        continue;
 151                }
 152                if ((name2 = parse_name_and_email(buffer, &name1, &email1)) != NULL)
 153                        parse_name_and_email(name2, &name2, &email2);
 154
 155                if (email1)
 156                        add_mapping(map, name1, email1, name2, email2);
 157        }
 158        fclose(f);
 159        return 0;
 160}
 161
 162int read_mailmap(struct string_list *map, char **repo_abbrev)
 163{
 164        map->strdup_strings = 1;
 165        /* each failure returns 1, so >1 means both calls failed */
 166        return read_single_mailmap(map, ".mailmap", repo_abbrev) +
 167               read_single_mailmap(map, git_mailmap_file, repo_abbrev) > 1;
 168}
 169
 170void clear_mailmap(struct string_list *map)
 171{
 172        debug_mm("mailmap: clearing %d entries...\n", map->nr);
 173        map->strdup_strings = 1;
 174        string_list_clear_func(map, free_mailmap_entry);
 175        debug_mm("mailmap: cleared\n");
 176}
 177
 178int map_user(struct string_list *map,
 179             char *email, int maxlen_email, char *name, int maxlen_name)
 180{
 181        char *p;
 182        struct string_list_item *item;
 183        struct mailmap_entry *me;
 184        char buf[1024], *mailbuf;
 185        int i;
 186
 187        /* figure out space requirement for email */
 188        p = strchr(email, '>');
 189        if (!p) {
 190                /* email passed in might not be wrapped in <>, but end with a \0 */
 191                p = memchr(email, '\0', maxlen_email);
 192                if (p == 0)
 193                        return 0;
 194        }
 195        if (p - email + 1 < sizeof(buf))
 196                mailbuf = buf;
 197        else
 198                mailbuf = xmalloc(p - email + 1);
 199
 200        /* downcase the email address */
 201        for (i = 0; i < p - email; i++)
 202                mailbuf[i] = tolower(email[i]);
 203        mailbuf[i] = 0;
 204
 205        debug_mm("map_user: map '%s' <%s>\n", name, mailbuf);
 206        item = string_list_lookup(mailbuf, map);
 207        if (item != NULL) {
 208                me = (struct mailmap_entry *)item->util;
 209                if (me->namemap.nr) {
 210                        /* The item has multiple items, so we'll look up on name too */
 211                        /* If the name is not found, we choose the simple entry      */
 212                        struct string_list_item *subitem = string_list_lookup(name, &me->namemap);
 213                        if (subitem)
 214                                item = subitem;
 215                }
 216        }
 217        if (mailbuf != buf)
 218                free(mailbuf);
 219        if (item != NULL) {
 220                struct mailmap_info *mi = (struct mailmap_info *)item->util;
 221                if (mi->name == NULL && (mi->email == NULL || maxlen_email == 0)) {
 222                        debug_mm("map_user:  -- (no simple mapping)\n");
 223                        return 0;
 224                }
 225                if (maxlen_email && mi->email)
 226                        strlcpy(email, mi->email, maxlen_email);
 227                if (maxlen_name && mi->name)
 228                        strlcpy(name, mi->name, maxlen_name);
 229                debug_mm("map_user:  to '%s' <%s>\n", name, mi->email ? mi->email : "");
 230                return 1;
 231        }
 232        debug_mm("map_user:  --\n");
 233        return 0;
 234}
 235
 236int map_email(struct string_list *map, const char *email, char *name, int maxlen)
 237{
 238        return map_user(map, (char *)email, 0, name, maxlen);
 239}