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