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