mailmap: refactor mailmap parsing for non-file sources
[git/git.git] / mailmap.c
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
9 static inline void debug_mm(const char *format, ...) {}
10 #endif
11
12 const char *git_mailmap_file;
13
14 struct mailmap_info {
15 char *name;
16 char *email;
17 };
18
19 struct 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
28 static 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
36 static 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
48 static 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(map, index, old_email);
73 item->util = xcalloc(1, sizeof(struct mailmap_entry));
74 ((struct mailmap_entry *)item->util)->namemap.strdup_strings = 1;
75 }
76 me = (struct mailmap_entry *)map->items[index].util;
77
78 if (old_name == NULL) {
79 debug_mm("mailmap: adding (simple) entry for %s at index %d\n", old_email, index);
80 /* Replace current name and new email for simple entry */
81 if (new_name) {
82 free(me->name);
83 me->name = xstrdup(new_name);
84 }
85 if (new_email) {
86 free(me->email);
87 me->email = xstrdup(new_email);
88 }
89 } else {
90 struct mailmap_info *mi = xcalloc(1, sizeof(struct mailmap_info));
91 debug_mm("mailmap: adding (complex) entry for %s at index %d\n", old_email, index);
92 if (new_name)
93 mi->name = xstrdup(new_name);
94 if (new_email)
95 mi->email = xstrdup(new_email);
96 string_list_insert(&me->namemap, old_name)->util = mi;
97 }
98
99 debug_mm("mailmap: '%s' <%s> -> '%s' <%s>\n",
100 old_name, old_email, new_name, new_email);
101 }
102
103 static char *parse_name_and_email(char *buffer, char **name,
104 char **email, int allow_empty_email)
105 {
106 char *left, *right, *nstart, *nend;
107 *name = *email = NULL;
108
109 if ((left = strchr(buffer, '<')) == NULL)
110 return NULL;
111 if ((right = strchr(left+1, '>')) == NULL)
112 return NULL;
113 if (!allow_empty_email && (left+1 == right))
114 return NULL;
115
116 /* remove whitespace from beginning and end of name */
117 nstart = buffer;
118 while (isspace(*nstart) && nstart < left)
119 ++nstart;
120 nend = left-1;
121 while (nend > nstart && isspace(*nend))
122 --nend;
123
124 *name = (nstart < nend ? nstart : NULL);
125 *email = left+1;
126 *(nend+1) = '\0';
127 *right++ = '\0';
128
129 return (*right == '\0' ? NULL : right);
130 }
131
132 static void read_mailmap_line(struct string_list *map, char *buffer,
133 char **repo_abbrev)
134 {
135 char *name1 = NULL, *email1 = NULL, *name2 = NULL, *email2 = NULL;
136 if (buffer[0] == '#') {
137 static const char abbrev[] = "# repo-abbrev:";
138 int abblen = sizeof(abbrev) - 1;
139 int len = strlen(buffer);
140
141 if (!repo_abbrev)
142 return;
143
144 if (len && buffer[len - 1] == '\n')
145 buffer[--len] = 0;
146 if (!strncmp(buffer, abbrev, abblen)) {
147 char *cp;
148
149 if (repo_abbrev)
150 free(*repo_abbrev);
151 *repo_abbrev = xmalloc(len);
152
153 for (cp = buffer + abblen; isspace(*cp); cp++)
154 ; /* nothing */
155 strcpy(*repo_abbrev, cp);
156 }
157 return;
158 }
159 if ((name2 = parse_name_and_email(buffer, &name1, &email1, 0)) != NULL)
160 parse_name_and_email(name2, &name2, &email2, 1);
161
162 if (email1)
163 add_mapping(map, name1, email1, name2, email2);
164 }
165
166 static int read_mailmap_file(struct string_list *map, const char *filename,
167 char **repo_abbrev)
168 {
169 char buffer[1024];
170 FILE *f = (filename == NULL ? NULL : fopen(filename, "r"));
171
172 if (f == NULL)
173 return 1;
174 while (fgets(buffer, sizeof(buffer), f) != NULL)
175 read_mailmap_line(map, buffer, repo_abbrev);
176 fclose(f);
177 return 0;
178 }
179
180 int read_mailmap(struct string_list *map, char **repo_abbrev)
181 {
182 map->strdup_strings = 1;
183 /* each failure returns 1, so >1 means both calls failed */
184 return read_mailmap_file(map, ".mailmap", repo_abbrev) +
185 read_mailmap_file(map, git_mailmap_file, repo_abbrev) > 1;
186 }
187
188 void clear_mailmap(struct string_list *map)
189 {
190 debug_mm("mailmap: clearing %d entries...\n", map->nr);
191 map->strdup_strings = 1;
192 string_list_clear_func(map, free_mailmap_entry);
193 debug_mm("mailmap: cleared\n");
194 }
195
196 int map_user(struct string_list *map,
197 char *email, int maxlen_email, char *name, int maxlen_name)
198 {
199 char *end_of_email;
200 struct string_list_item *item;
201 struct mailmap_entry *me;
202 char buf[1024], *mailbuf;
203 int i;
204
205 /* figure out space requirement for email */
206 end_of_email = strchr(email, '>');
207 if (!end_of_email) {
208 /* email passed in might not be wrapped in <>, but end with a \0 */
209 end_of_email = memchr(email, '\0', maxlen_email);
210 if (!end_of_email)
211 return 0;
212 }
213 if (end_of_email - email + 1 < sizeof(buf))
214 mailbuf = buf;
215 else
216 mailbuf = xmalloc(end_of_email - email + 1);
217
218 /* downcase the email address */
219 for (i = 0; i < end_of_email - email; i++)
220 mailbuf[i] = tolower(email[i]);
221 mailbuf[i] = 0;
222
223 debug_mm("map_user: map '%s' <%s>\n", name, mailbuf);
224 item = string_list_lookup(map, mailbuf);
225 if (item != NULL) {
226 me = (struct mailmap_entry *)item->util;
227 if (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 */
230 struct string_list_item *subitem = string_list_lookup(&me->namemap, name);
231 if (subitem)
232 item = subitem;
233 }
234 }
235 if (mailbuf != buf)
236 free(mailbuf);
237 if (item != NULL) {
238 struct mailmap_info *mi = (struct mailmap_info *)item->util;
239 if (mi->name == NULL && (mi->email == NULL || maxlen_email == 0)) {
240 debug_mm("map_user: -- (no simple mapping)\n");
241 return 0;
242 }
243 if (maxlen_email && mi->email)
244 strlcpy(email, mi->email, maxlen_email);
245 else
246 *end_of_email = '\0';
247 if (maxlen_name && mi->name)
248 strlcpy(name, mi->name, maxlen_name);
249 debug_mm("map_user: to '%s' <%s>\n", name, mi->email ? mi->email : "");
250 return 1;
251 }
252 debug_mm("map_user: --\n");
253 return 0;
254 }