1#include "cache.h"
2#include "string-list.h"
3#include "mailmap.h"
45
#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
1112
const char *git_mailmap_file;
1314
struct mailmap_info {
15char *name;
16char *email;
17};
1819
struct mailmap_entry {
20/* name and email for the simple mail-only case */
21char *name;
22char *email;
2324
/* name and email for the complex mail and name matching case */
25struct string_list namemap;
26};
2728
static void free_mailmap_info(void *p, const char *s)
29{
30struct mailmap_info *mi = (struct mailmap_info *)p;
31debug_mm("mailmap: -- complex: '%s' -> '%s' <%s>\n", s, mi->name, mi->email);
32free(mi->name);
33free(mi->email);
34}
3536
static void free_mailmap_entry(void *p, const char *s)
37{
38struct mailmap_entry *me = (struct mailmap_entry *)p;
39debug_mm("mailmap: removing entries for <%s>, with %d sub-entries\n", s, me->namemap.nr);
40debug_mm("mailmap: - simple: '%s' <%s>\n", me->name, me->email);
41free(me->name);
42free(me->email);
4344
me->namemap.strdup_strings = 1;
45string_list_clear_func(&me->namemap, free_mailmap_info);
46}
4748
static void add_mapping(struct string_list *map,
49char *new_name, char *new_email, char *old_name, char *old_email)
50{
51struct mailmap_entry *me;
52int index;
53if (old_email == NULL) {
54old_email = new_email;
55new_email = NULL;
56}
5758
if ((index = string_list_find_insert_index(map, old_email, 1)) < 0) {
59/* mailmap entry exists, invert index value */
60index = -1 - index;
61} else {
62/* create mailmap entry */
63struct string_list_item *item = string_list_insert_at_index(index, old_email, map);
64item->util = xmalloc(sizeof(struct mailmap_entry));
65memset(item->util, 0, sizeof(struct mailmap_entry));
66((struct mailmap_entry *)item->util)->namemap.strdup_strings = 1;
67}
68me = (struct mailmap_entry *)map->items[index].util;
6970
if (old_name == NULL) {
71debug_mm("mailmap: adding (simple) entry for %s at index %d\n", old_email, index);
72/* Replace current name and new email for simple entry */
73free(me->name);
74free(me->email);
75if (new_name)
76me->name = xstrdup(new_name);
77if (new_email)
78me->email = xstrdup(new_email);
79} else {
80struct mailmap_info *mi = xmalloc(sizeof(struct mailmap_info));
81debug_mm("mailmap: adding (complex) entry for %s at index %d\n", old_email, index);
82if (new_name)
83mi->name = xstrdup(new_name);
84if (new_email)
85mi->email = xstrdup(new_email);
86string_list_insert(old_name, &me->namemap)->util = mi;
87}
8889
debug_mm("mailmap: '%s' <%s> -> '%s' <%s>\n",
90old_name, old_email, new_name, new_email);
91}
9293
static char *parse_name_and_email(char *buffer, char **name, char **email)
94{
95char *left, *right, *nstart, *nend;
96*name = *email = 0;
9798
if ((left = strchr(buffer, '<')) == NULL)
99return NULL;
100if ((right = strchr(left+1, '>')) == NULL)
101return NULL;
102if (left+1 == right)
103return NULL;
104105
/* remove whitespace from beginning and end of name */
106nstart = buffer;
107while (isspace(*nstart) && nstart < left)
108++nstart;
109nend = left-1;
110while (isspace(*nend) && nend > nstart)
111--nend;
112113
*name = (nstart < nend ? nstart : NULL);
114*email = left+1;
115*(nend+1) = '\0';
116*right++ = '\0';
117118
return (*right == '\0' ? NULL : right);
119}
120121
static int read_single_mailmap(struct string_list *map, const char *filename, char **repo_abbrev)
122{
123char buffer[1024];
124FILE *f = (filename == NULL ? NULL : fopen(filename, "r"));
125126
if (f == NULL)
127return 1;
128while (fgets(buffer, sizeof(buffer), f) != NULL) {
129char *name1 = 0, *email1 = 0, *name2 = 0, *email2 = 0;
130if (buffer[0] == '#') {
131static const char abbrev[] = "# repo-abbrev:";
132int abblen = sizeof(abbrev) - 1;
133int len = strlen(buffer);
134135
if (!repo_abbrev)
136continue;
137138
if (len && buffer[len - 1] == '\n')
139buffer[--len] = 0;
140if (!strncmp(buffer, abbrev, abblen)) {
141char *cp;
142143
if (repo_abbrev)
144free(*repo_abbrev);
145*repo_abbrev = xmalloc(len);
146147
for (cp = buffer + abblen; isspace(*cp); cp++)
148; /* nothing */
149strcpy(*repo_abbrev, cp);
150}
151continue;
152}
153if ((name2 = parse_name_and_email(buffer, &name1, &email1)) != NULL)
154parse_name_and_email(name2, &name2, &email2);
155156
if (email1)
157add_mapping(map, name1, email1, name2, email2);
158}
159fclose(f);
160return 0;
161}
162163
int read_mailmap(struct string_list *map, char **repo_abbrev)
164{
165map->strdup_strings = 1;
166/* each failure returns 1, so >1 means both calls failed */
167return read_single_mailmap(map, ".mailmap", repo_abbrev) +
168read_single_mailmap(map, git_mailmap_file, repo_abbrev) > 1;
169}
170171
void clear_mailmap(struct string_list *map)
172{
173debug_mm("mailmap: clearing %d entries...\n", map->nr);
174map->strdup_strings = 1;
175string_list_clear_func(map, free_mailmap_entry);
176debug_mm("mailmap: cleared\n");
177}
178179
int map_user(struct string_list *map,
180char *email, int maxlen_email, char *name, int maxlen_name)
181{
182char *p;
183struct string_list_item *item;
184struct mailmap_entry *me;
185char buf[1024], *mailbuf;
186int i;
187188
/* figure out space requirement for email */
189p = strchr(email, '>');
190if (!p) {
191/* email passed in might not be wrapped in <>, but end with a \0 */
192p = memchr(email, '\0', maxlen_email);
193if (p == 0)
194return 0;
195}
196if (p - email + 1 < sizeof(buf))
197mailbuf = buf;
198else
199mailbuf = xmalloc(p - email + 1);
200201
/* downcase the email address */
202for (i = 0; i < p - email; i++)
203mailbuf[i] = tolower(email[i]);
204mailbuf[i] = 0;
205206
debug_mm("map_user: map '%s' <%s>\n", name, mailbuf);
207item = string_list_lookup(mailbuf, map);
208if (item != NULL) {
209me = (struct mailmap_entry *)item->util;
210if (me->namemap.nr) {
211/* The item has multiple items, so we'll look up on name too */
212/* If the name is not found, we choose the simple entry */
213struct string_list_item *subitem = string_list_lookup(name, &me->namemap);
214if (subitem)
215item = subitem;
216}
217}
218if (mailbuf != buf)
219free(mailbuf);
220if (item != NULL) {
221struct mailmap_info *mi = (struct mailmap_info *)item->util;
222if (mi->name == NULL && (mi->email == NULL || maxlen_email == 0)) {
223debug_mm("map_user: -- (no simple mapping)\n");
224return 0;
225}
226if (maxlen_email && mi->email)
227strlcpy(email, mi->email, maxlen_email);
228if (maxlen_name && mi->name)
229strlcpy(name, mi->name, maxlen_name);
230debug_mm("map_user: to '%s' <%s>\n", name, mi->email ? mi->email : "");
231return 1;
232}
233debug_mm("map_user: --\n");
234return 0;
235}
236237
int map_email(struct string_list *map, const char *email, char *name, int maxlen)
238{
239return map_user(map, (char *)email, 0, name, maxlen);
240}