2 #include "string-list.h"
5 #define DEBUG_MAILMAP 0
7 #define debug_mm(...) fprintf(stderr, __VA_ARGS__)
9 static inline void debug_mm(const char *format, ...) {}
12 const char *git_mailmap_file;
13 const char *git_mailmap_blob;
20 struct mailmap_entry {
21 /* name and email for the simple mail-only case */
25 /* name and email for the complex mail and name matching case */
26 struct string_list namemap;
29 static void free_mailmap_info(void *p, const char *s)
31 struct mailmap_info *mi = (struct mailmap_info *)p;
32 debug_mm("mailmap: -- complex: '%s' -> '%s' <%s>\n", s, mi->name, mi->email);
37 static void free_mailmap_entry(void *p, const char *s)
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);
45 me->namemap.strdup_strings = 1;
46 string_list_clear_func(&me->namemap, free_mailmap_info);
49 static void add_mapping(struct string_list *map,
50 char *new_name, char *new_email, char *old_name, char *old_email)
52 struct mailmap_entry *me;
57 for (p = old_email; *p; p++)
60 for (p = new_email; *p; p++)
63 if (old_email == NULL) {
64 old_email = new_email;
68 if ((index = string_list_find_insert_index(map, old_email, 1)) < 0) {
69 /* mailmap entry exists, invert index value */
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;
77 me = (struct mailmap_entry *)map->items[index].util;
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 */
84 me->name = xstrdup(new_name);
88 me->email = xstrdup(new_email);
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);
94 mi->name = xstrdup(new_name);
96 mi->email = xstrdup(new_email);
97 string_list_insert(&me->namemap, old_name)->util = mi;
100 debug_mm("mailmap: '%s' <%s> -> '%s' <%s>\n",
101 old_name, old_email, new_name, new_email);
104 static char *parse_name_and_email(char *buffer, char **name,
105 char **email, int allow_empty_email)
107 char *left, *right, *nstart, *nend;
108 *name = *email = NULL;
110 if ((left = strchr(buffer, '<')) == NULL)
112 if ((right = strchr(left+1, '>')) == NULL)
114 if (!allow_empty_email && (left+1 == right))
117 /* remove whitespace from beginning and end of name */
119 while (isspace(*nstart) && nstart < left)
122 while (nend > nstart && isspace(*nend))
125 *name = (nstart < nend ? nstart : NULL);
130 return (*right == '\0' ? NULL : right);
133 static void read_mailmap_line(struct string_list *map, char *buffer,
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);
145 if (len && buffer[len - 1] == '\n')
147 if (!strncmp(buffer, abbrev, abblen)) {
152 *repo_abbrev = xmalloc(len);
154 for (cp = buffer + abblen; isspace(*cp); cp++)
156 strcpy(*repo_abbrev, cp);
160 if ((name2 = parse_name_and_email(buffer, &name1, &email1, 0)) != NULL)
161 parse_name_and_email(name2, &name2, &email2, 1);
164 add_mapping(map, name1, email1, name2, email2);
167 static int read_mailmap_file(struct string_list *map, const char *filename,
176 f = fopen(filename, "r");
180 return error("unable to open mailmap at %s: %s",
181 filename, strerror(errno));
184 while (fgets(buffer, sizeof(buffer), f) != NULL)
185 read_mailmap_line(map, buffer, repo_abbrev);
190 static void read_mailmap_string(struct string_list *map, char *buf,
194 char *end = strchrnul(buf, '\n');
199 read_mailmap_line(map, buf, repo_abbrev);
204 static int read_mailmap_blob(struct string_list *map,
208 unsigned char sha1[20];
211 enum object_type type;
215 if (get_sha1(name, sha1) < 0)
218 buf = read_sha1_file(sha1, &type, &size);
220 return error("unable to read mailmap object at %s", name);
221 if (type != OBJ_BLOB)
222 return error("mailmap is not a blob: %s", name);
224 read_mailmap_string(map, buf, repo_abbrev);
230 int read_mailmap(struct string_list *map, char **repo_abbrev)
234 map->strdup_strings = 1;
235 map->cmp = strcasecmp;
237 if (!git_mailmap_blob && is_bare_repository())
238 git_mailmap_blob = "HEAD:.mailmap";
240 err |= read_mailmap_file(map, ".mailmap", repo_abbrev);
241 err |= read_mailmap_blob(map, git_mailmap_blob, repo_abbrev);
242 err |= read_mailmap_file(map, git_mailmap_file, repo_abbrev);
246 void clear_mailmap(struct string_list *map)
248 debug_mm("mailmap: clearing %d entries...\n", map->nr);
249 map->strdup_strings = 1;
250 string_list_clear_func(map, free_mailmap_entry);
251 debug_mm("mailmap: cleared\n");
255 * Look for an entry in map that match string[0:len]; string[len]
256 * does not have to be NUL (but it could be).
258 static struct string_list_item *lookup_prefix(struct string_list *map,
259 const char *string, size_t len)
261 int i = string_list_find_insert_index(map, string, 1);
266 return &map->items[i];
268 * that map entry matches exactly to the string, including
269 * the cruft at the end beyond "len". That is not a match
270 * with string[0:len] that we are looking for.
272 } else if (!string[len]) {
274 * asked with the whole string, and got nothing. No
275 * matching entry can exist in the map.
281 * i is at the exact match to an overlong key, or location the
282 * overlong key would be inserted, which must come after the
283 * real location of the key if one exists.
285 while (0 <= --i && i < map->nr) {
286 int cmp = strncasecmp(map->items[i].string, string, len);
289 * "i" points at a key definitely below the prefix;
290 * the map does not have string[0:len] in it.
293 else if (!cmp && !map->items[i].string[len])
295 return &map->items[i];
297 * otherwise, the string at "i" may be string[0:len]
298 * followed by a string that sorts later than string[len:];
305 int map_user(struct string_list *map,
306 const char **email, size_t *emaillen,
307 const char **name, size_t *namelen)
309 struct string_list_item *item;
310 struct mailmap_entry *me;
312 debug_mm("map_user: map '%.*s' <%.*s>\n",
313 *name, *namelen, *emaillen, *email);
315 item = lookup_prefix(map, *email, *emaillen);
317 me = (struct mailmap_entry *)item->util;
318 if (me->namemap.nr) {
319 /* The item has multiple items, so we'll look up on name too */
320 /* If the name is not found, we choose the simple entry */
321 struct string_list_item *subitem;
322 subitem = lookup_prefix(&me->namemap, *name, *namelen);
328 struct mailmap_info *mi = (struct mailmap_info *)item->util;
329 if (mi->name == NULL && mi->email == NULL) {
330 debug_mm("map_user: -- (no simple mapping)\n");
335 *emaillen = strlen(*email);
339 *namelen = strlen(*name);
341 debug_mm("map_user: to '%.*s' <.*%s>\n", *namelen, *name,
345 debug_mm("map_user: --\n");