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