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;
53char *p;
5455
if (old_email)
56for (p = old_email; *p; p++)
57*p = tolower(*p);
58if (new_email)
59for (p = new_email; *p; p++)
60*p = tolower(*p);
6162
if (old_email == NULL) {
63old_email = new_email;
64new_email = NULL;
65}
6667
if ((index = string_list_find_insert_index(map, old_email, 1)) < 0) {
68/* mailmap entry exists, invert index value */
69index = -1 - index;
70} else {
71/* create mailmap entry */
72struct string_list_item *item = string_list_insert_at_index(map, index, old_email);
73item->util = xcalloc(1, sizeof(struct mailmap_entry));
74((struct mailmap_entry *)item->util)->namemap.strdup_strings = 1;
75}
76me = (struct mailmap_entry *)map->items[index].util;
7778
if (old_name == NULL) {
79debug_mm("mailmap: adding (simple) entry for %s at index %d\n", old_email, index);
80/* Replace current name and new email for simple entry */
81if (new_name) {
82free(me->name);
83me->name = xstrdup(new_name);
84}
85if (new_email) {
86free(me->email);
87me->email = xstrdup(new_email);
88}
89} else {
90struct mailmap_info *mi = xcalloc(1, sizeof(struct mailmap_info));
91debug_mm("mailmap: adding (complex) entry for %s at index %d\n", old_email, index);
92if (new_name)
93mi->name = xstrdup(new_name);
94if (new_email)
95mi->email = xstrdup(new_email);
96string_list_insert(&me->namemap, old_name)->util = mi;
97}
9899
debug_mm("mailmap: '%s' <%s> -> '%s' <%s>\n",
100old_name, old_email, new_name, new_email);
101}
102103
static char *parse_name_and_email(char *buffer, char **name,
104char **email, int allow_empty_email)
105{
106char *left, *right, *nstart, *nend;
107*name = *email = NULL;
108109
if ((left = strchr(buffer, '<')) == NULL)
110return NULL;
111if ((right = strchr(left+1, '>')) == NULL)
112return NULL;
113if (!allow_empty_email && (left+1 == right))
114return NULL;
115116
/* remove whitespace from beginning and end of name */
117nstart = buffer;
118while (isspace(*nstart) && nstart < left)
119++nstart;
120nend = left-1;
121while (nend > nstart && isspace(*nend))
122--nend;
123124
*name = (nstart < nend ? nstart : NULL);
125*email = left+1;
126*(nend+1) = '\0';
127*right++ = '\0';
128129
return (*right == '\0' ? NULL : right);
130}
131132
static void read_mailmap_line(struct string_list *map, char *buffer,
133char **repo_abbrev)
134{
135char *name1 = NULL, *email1 = NULL, *name2 = NULL, *email2 = NULL;
136if (buffer[0] == '#') {
137static const char abbrev[] = "# repo-abbrev:";
138int abblen = sizeof(abbrev) - 1;
139int len = strlen(buffer);
140141
if (!repo_abbrev)
142return;
143144
if (len && buffer[len - 1] == '\n')
145buffer[--len] = 0;
146if (!strncmp(buffer, abbrev, abblen)) {
147char *cp;
148149
if (repo_abbrev)
150free(*repo_abbrev);
151*repo_abbrev = xmalloc(len);
152153
for (cp = buffer + abblen; isspace(*cp); cp++)
154; /* nothing */
155strcpy(*repo_abbrev, cp);
156}
157return;
158}
159if ((name2 = parse_name_and_email(buffer, &name1, &email1, 0)) != NULL)
160parse_name_and_email(name2, &name2, &email2, 1);
161162
if (email1)
163add_mapping(map, name1, email1, name2, email2);
164}
165166
static int read_mailmap_file(struct string_list *map, const char *filename,
167char **repo_abbrev)
168{
169char buffer[1024];
170FILE *f = (filename == NULL ? NULL : fopen(filename, "r"));
171172
if (f == NULL)
173return 1;
174while (fgets(buffer, sizeof(buffer), f) != NULL)
175read_mailmap_line(map, buffer, repo_abbrev);
176fclose(f);
177return 0;
178}
179180
int read_mailmap(struct string_list *map, char **repo_abbrev)
181{
182map->strdup_strings = 1;
183/* each failure returns 1, so >1 means both calls failed */
184return read_mailmap_file(map, ".mailmap", repo_abbrev) +
185read_mailmap_file(map, git_mailmap_file, repo_abbrev) > 1;
186}
187188
void clear_mailmap(struct string_list *map)
189{
190debug_mm("mailmap: clearing %d entries...\n", map->nr);
191map->strdup_strings = 1;
192string_list_clear_func(map, free_mailmap_entry);
193debug_mm("mailmap: cleared\n");
194}
195196
int map_user(struct string_list *map,
197char *email, int maxlen_email, char *name, int maxlen_name)
198{
199char *end_of_email;
200struct string_list_item *item;
201struct mailmap_entry *me;
202char buf[1024], *mailbuf;
203int i;
204205
/* figure out space requirement for email */
206end_of_email = strchr(email, '>');
207if (!end_of_email) {
208/* email passed in might not be wrapped in <>, but end with a \0 */
209end_of_email = memchr(email, '\0', maxlen_email);
210if (!end_of_email)
211return 0;
212}
213if (end_of_email - email + 1 < sizeof(buf))
214mailbuf = buf;
215else
216mailbuf = xmalloc(end_of_email - email + 1);
217218
/* downcase the email address */
219for (i = 0; i < end_of_email - email; i++)
220mailbuf[i] = tolower(email[i]);
221mailbuf[i] = 0;
222223
debug_mm("map_user: map '%s' <%s>\n", name, mailbuf);
224item = string_list_lookup(map, mailbuf);
225if (item != NULL) {
226me = (struct mailmap_entry *)item->util;
227if (me->namemap.nr) {
228/* The item has multiple items, so we'll look up on name too */
229/* If the name is not found, we choose the simple entry */
230struct string_list_item *subitem = string_list_lookup(&me->namemap, name);
231if (subitem)
232item = subitem;
233}
234}
235if (mailbuf != buf)
236free(mailbuf);
237if (item != NULL) {
238struct mailmap_info *mi = (struct mailmap_info *)item->util;
239if (mi->name == NULL && (mi->email == NULL || maxlen_email == 0)) {
240debug_mm("map_user: -- (no simple mapping)\n");
241return 0;
242}
243if (maxlen_email && mi->email)
244strlcpy(email, mi->email, maxlen_email);
245else
246*end_of_email = '\0';
247if (maxlen_name && mi->name)
248strlcpy(name, mi->name, maxlen_name);
249debug_mm("map_user: to '%s' <%s>\n", name, mi->email ? mi->email : "");
250return 1;
251}
252debug_mm("map_user: --\n");
253return 0;
254}