Raw
1 #define USE_THE_REPOSITORY_VARIABLE
2
3 #include "git-compat-util.h"
4 #include "environment.h"
5 #include "string-list.h"
6 #include "mailmap.h"
7 #include "object-name.h"
8 #include "odb.h"
9 #include "setup.h"
10 #include "config.h"
11
12 struct mailmap_info {
13 char *name;
14 char *email;
15 };
16
17 struct mailmap_entry {
18 /* name and email for the simple mail-only case */
19 char *name;
20 char *email;
21
22 /* name and email for the complex mail and name matching case */
23 struct string_list namemap;
24 };
25
26 static void free_mailmap_info(void *p, const char *s UNUSED)
27 {
28 struct mailmap_info *mi = (struct mailmap_info *)p;
29 free(mi->name);
30 free(mi->email);
31 free(mi);
32 }
33
34 static void free_mailmap_entry(void *p, const char *s UNUSED)
35 {
36 struct mailmap_entry *me = (struct mailmap_entry *)p;
37
38 free(me->name);
39 free(me->email);
40
41 me->namemap.strdup_strings = 1;
42 string_list_clear_func(&me->namemap, free_mailmap_info);
43 free(me);
44 }
45
46 /*
47 * On some systems (e.g. MinGW 4.0), string.h has _only_ inline
48 * definition of strcasecmp and no non-inline implementation is
49 * supplied anywhere, which is, eh, "unusual"; we cannot take an
50 * address of such a function to store it in namemap.cmp. This is
51 * here as a workaround---do not assign strcasecmp directly to
52 * namemap.cmp until we know no systems that matter have such an
53 * "unusual" string.h.
54 */
55 static int namemap_cmp(const char *a, const char *b)
56 {
57 return strcasecmp(a, b);
58 }
59
60 static void add_mapping(struct string_list *map,
61 char *new_name, char *new_email,
62 char *old_name, char *old_email)
63 {
64 struct mailmap_entry *me;
65 struct string_list_item *item;
66
67 if (!old_email) {
68 old_email = new_email;
69 new_email = NULL;
70 }
71
72 item = string_list_insert(map, old_email);
73 if (item->util) {
74 me = (struct mailmap_entry *)item->util;
75 } else {
76 CALLOC_ARRAY(me, 1);
77 me->namemap.strdup_strings = 1;
78 me->namemap.cmp = namemap_cmp;
79 item->util = me;
80 }
81
82 if (!old_name) {
83 /* Replace current name and new email for simple entry */
84 if (new_name) {
85 free(me->name);
86 me->name = xstrdup(new_name);
87 }
88 if (new_email) {
89 free(me->email);
90 me->email = xstrdup(new_email);
91 }
92 } else {
93 struct mailmap_info *mi = xcalloc(1, sizeof(struct mailmap_info));
94 mi->name = xstrdup_or_null(new_name);
95 mi->email = xstrdup_or_null(new_email);
96 string_list_insert(&me->namemap, old_name)->util = mi;
97 }
98 }
99
100 static char *parse_name_and_email(char *buffer, char **name,
101 char **email, int allow_empty_email)
102 {
103 char *left, *right, *nstart, *nend;
104 *name = *email = NULL;
105
106 if (!(left = strchr(buffer, '<')))
107 return NULL;
108 if (!(right = strchr(left + 1, '>')))
109 return NULL;
110 if (!allow_empty_email && (left+1 == right))
111 return NULL;
112
113 /* remove whitespace from beginning and end of name */
114 nstart = buffer;
115 while (isspace(*nstart) && nstart < left)
116 ++nstart;
117 nend = left-1;
118 while (nend > nstart && isspace(*nend))
119 --nend;
120
121 *name = (nstart <= nend ? nstart : NULL);
122 *email = left+1;
123 *(nend+1) = '\0';
124 *right++ = '\0';
125
126 return (*right == '\0' ? NULL : right);
127 }
128
129 static void read_mailmap_line(struct string_list *map, char *buffer)
130 {
131 char *name1 = NULL, *email1 = NULL, *name2 = NULL, *email2 = NULL;
132
133 if (buffer[0] == '#')
134 return;
135
136 if ((name2 = parse_name_and_email(buffer, &name1, &email1, 0)))
137 parse_name_and_email(name2, &name2, &email2, 1);
138
139 if (email1)
140 add_mapping(map, name1, email1, name2, email2);
141 }
142
143 int read_mailmap_file(struct string_list *map, const char *filename,
144 unsigned flags)
145 {
146 char buffer[1024];
147 FILE *f;
148 int fd;
149
150 if (!filename)
151 return 0;
152
153 if (flags & MAILMAP_NOFOLLOW)
154 fd = open_nofollow(filename, O_RDONLY);
155 else
156 fd = open(filename, O_RDONLY);
157
158 if (fd < 0) {
159 if (errno == ENOENT)
160 return 0;
161 return error_errno("unable to open mailmap at %s", filename);
162 }
163 f = xfdopen(fd, "r");
164
165 while (fgets(buffer, sizeof(buffer), f) != NULL)
166 read_mailmap_line(map, buffer);
167 fclose(f);
168 return 0;
169 }
170
171 static void read_mailmap_string(struct string_list *map, char *buf)
172 {
173 while (*buf) {
174 char *end = strchrnul(buf, '\n');
175
176 if (*end)
177 *end++ = '\0';
178
179 read_mailmap_line(map, buf);
180 buf = end;
181 }
182 }
183
184 int read_mailmap_blob(struct repository *repo, struct string_list *map,
185 const char *name)
186 {
187 struct object_id oid;
188 char *buf;
189 unsigned long size;
190 enum object_type type;
191
192 if (!name)
193 return 0;
194 if (repo_get_oid(repo, name, &oid) < 0)
195 return 0;
196
197 buf = odb_read_object(repo->objects, &oid, &type, &size);
198 if (!buf)
199 return error("unable to read mailmap object at %s", name);
200 if (type != OBJ_BLOB) {
201 free(buf);
202 return error("mailmap is not a blob: %s", name);
203 }
204
205 read_mailmap_string(map, buf);
206
207 free(buf);
208 return 0;
209 }
210
211 int read_mailmap(struct repository *repo, struct string_list *map)
212 {
213 int err = 0;
214 char *mailmap_file = NULL, *mailmap_blob = NULL;
215
216 repo_config_get_pathname(repo, "mailmap.file", &mailmap_file);
217 repo_config_get_string(repo, "mailmap.blob", &mailmap_blob);
218
219 map->strdup_strings = 1;
220 map->cmp = namemap_cmp;
221
222 if (!mailmap_blob && is_bare_repository())
223 mailmap_blob = xstrdup("HEAD:.mailmap");
224
225 if (!startup_info->have_repository || !is_bare_repository())
226 err |= read_mailmap_file(map, ".mailmap",
227 startup_info->have_repository ?
228 MAILMAP_NOFOLLOW : 0);
229 if (startup_info->have_repository)
230 err |= read_mailmap_blob(repo, map, mailmap_blob);
231
232 err |= read_mailmap_file(map, mailmap_file, 0);
233
234 free(mailmap_file);
235 free(mailmap_blob);
236
237 return err;
238 }
239
240 void clear_mailmap(struct string_list *map)
241 {
242 map->strdup_strings = 1;
243 string_list_clear_func(map, free_mailmap_entry);
244 }
245
246 /*
247 * Look for an entry in map that match string[0:len]; string[len]
248 * does not have to be NUL (but it could be).
249 */
250 static struct string_list_item *lookup_prefix(struct string_list *map,
251 const char *string, size_t len)
252 {
253 bool exact_match;
254 size_t i = string_list_find_insert_index(map, string, &exact_match);
255 if (exact_match) {
256 if (!string[len])
257 return &map->items[i];
258 /*
259 * that map entry matches exactly to the string, including
260 * the cruft at the end beyond "len". That is not a match
261 * with string[0:len] that we are looking for.
262 */
263 } else if (!string[len]) {
264 /*
265 * asked with the whole string, and got nothing. No
266 * matching entry can exist in the map.
267 */
268 return NULL;
269 }
270
271 /*
272 * i is at the exact match to an overlong key, or location the
273 * overlong key would be inserted, which must come after the
274 * real location of the key if one exists.
275 */
276 while (i-- && i < map->nr) {
277 int cmp = strncasecmp(map->items[i].string, string, len);
278 if (cmp < 0)
279 /*
280 * "i" points at a key definitely below the prefix;
281 * the map does not have string[0:len] in it.
282 */
283 break;
284 else if (!cmp && !map->items[i].string[len])
285 /* found it */
286 return &map->items[i];
287 /*
288 * otherwise, the string at "i" may be string[0:len]
289 * followed by a string that sorts later than string[len:];
290 * keep trying.
291 */
292 }
293 return NULL;
294 }
295
296 int map_user(struct string_list *map,
297 const char **email, size_t *emaillen,
298 const char **name, size_t *namelen)
299 {
300 struct string_list_item *item;
301 struct mailmap_entry *me;
302
303 item = lookup_prefix(map, *email, *emaillen);
304 if (item) {
305 me = (struct mailmap_entry *)item->util;
306 if (me->namemap.nr) {
307 /*
308 * The item has multiple items, so we'll look up on
309 * name too. If the name is not found, we choose the
310 * simple entry.
311 */
312 struct string_list_item *subitem;
313 subitem = lookup_prefix(&me->namemap, *name, *namelen);
314 if (subitem)
315 item = subitem;
316 }
317 }
318 if (item) {
319 struct mailmap_info *mi = (struct mailmap_info *)item->util;
320 if (mi->name == NULL && mi->email == NULL)
321 return 0;
322 if (mi->email) {
323 *email = mi->email;
324 *emaillen = strlen(*email);
325 }
326 if (mi->name) {
327 *name = mi->name;
328 *namelen = strlen(*name);
329 }
330 return 1;
331 }
332 return 0;
333 }