]> git.ipfire.org Git - thirdparty/git.git/blame - mailmap.c
Merge branch 'rj/add-i-leak-fix'
[thirdparty/git.git] / mailmap.c
CommitLineData
e93fc5d7 1#include "git-compat-util.h"
32a8f510 2#include "environment.h"
c455c87c 3#include "string-list.h"
fe5d30b6 4#include "mailmap.h"
dabab1d6 5#include "object-name.h"
a034e910 6#include "object-store-ll.h"
e38da487 7#include "setup.h"
7c1c6782 8
d551a488 9const char *git_mailmap_file;
08610900 10const char *git_mailmap_blob;
0925ce4d
MSO
11
12struct mailmap_info {
13 char *name;
14 char *email;
15};
16
17struct 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
910d07a8 26static void free_mailmap_info(void *p, const char *s UNUSED)
0925ce4d
MSO
27{
28 struct mailmap_info *mi = (struct mailmap_info *)p;
0925ce4d
MSO
29 free(mi->name);
30 free(mi->email);
ccdd5d1e 31 free(mi);
0925ce4d
MSO
32}
33
910d07a8 34static void free_mailmap_entry(void *p, const char *s UNUSED)
0925ce4d
MSO
35{
36 struct mailmap_entry *me = (struct mailmap_entry *)p;
fbfba7ad 37
0925ce4d
MSO
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);
ccdd5d1e 43 free(me);
0925ce4d
MSO
44}
45
de2f95eb
JH
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 */
55static int namemap_cmp(const char *a, const char *b)
56{
57 return strcasecmp(a, b);
58}
59
0925ce4d 60static void add_mapping(struct string_list *map,
bd237945
JH
61 char *new_name, char *new_email,
62 char *old_name, char *old_email)
0925ce4d
MSO
63{
64 struct mailmap_entry *me;
63226218 65 struct string_list_item *item;
bf637803 66
afe8a907 67 if (!old_email) {
0925ce4d
MSO
68 old_email = new_email;
69 new_email = NULL;
70 }
71
63226218
SB
72 item = string_list_insert(map, old_email);
73 if (item->util) {
74 me = (struct mailmap_entry *)item->util;
0925ce4d 75 } else {
ca56dadb 76 CALLOC_ARRAY(me, 1);
97e751be 77 me->namemap.strdup_strings = 1;
de2f95eb 78 me->namemap.cmp = namemap_cmp;
97e751be 79 item->util = me;
0925ce4d 80 }
0925ce4d 81
afe8a907 82 if (!old_name) {
0925ce4d 83 /* Replace current name and new email for simple entry */
d8d2eb7d
JM
84 if (new_name) {
85 free(me->name);
0925ce4d 86 me->name = xstrdup(new_name);
d8d2eb7d
JM
87 }
88 if (new_email) {
89 free(me->email);
0925ce4d 90 me->email = xstrdup(new_email);
d8d2eb7d 91 }
0925ce4d 92 } else {
74b531f6 93 struct mailmap_info *mi = xcalloc(1, sizeof(struct mailmap_info));
13092a91
JH
94 mi->name = xstrdup_or_null(new_name);
95 mi->email = xstrdup_or_null(new_email);
78a395d3 96 string_list_insert(&me->namemap, old_name)->util = mi;
0925ce4d 97 }
0925ce4d
MSO
98}
99
5288dd58 100static char *parse_name_and_email(char *buffer, char **name,
bd237945 101 char **email, int allow_empty_email)
0925ce4d
MSO
102{
103 char *left, *right, *nstart, *nend;
2af202be 104 *name = *email = NULL;
0925ce4d 105
afe8a907 106 if (!(left = strchr(buffer, '<')))
0925ce4d 107 return NULL;
afe8a907 108 if (!(right = strchr(left + 1, '>')))
0925ce4d 109 return NULL;
5288dd58 110 if (!allow_empty_email && (left+1 == right))
0925ce4d
MSO
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;
3174bc5c 118 while (nend > nstart && isspace(*nend))
0925ce4d
MSO
119 --nend;
120
8c381151 121 *name = (nstart <= nend ? nstart : NULL);
0925ce4d
MSO
122 *email = left+1;
123 *(nend+1) = '\0';
124 *right++ = '\0';
125
126 return (*right == '\0' ? NULL : right);
127}
128
4e168333 129static void read_mailmap_line(struct string_list *map, char *buffer)
7c8ce308
JK
130{
131 char *name1 = NULL, *email1 = NULL, *name2 = NULL, *email2 = NULL;
7c8ce308 132
4e168333 133 if (buffer[0] == '#')
7c8ce308 134 return;
4e168333 135
afe8a907 136 if ((name2 = parse_name_and_email(buffer, &name1, &email1, 0)))
7c8ce308
JK
137 parse_name_and_email(name2, &name2, &email2, 1);
138
139 if (email1)
140 add_mapping(map, name1, email1, name2, email2);
141}
142
adcd9f54
JK
143/* Flags for read_mailmap_file() */
144#define MAILMAP_NOFOLLOW (1<<0)
145
146static int read_mailmap_file(struct string_list *map, const char *filename,
147 unsigned flags)
7c1c6782
JH
148{
149 char buffer[1024];
938a60d6 150 FILE *f;
adcd9f54 151 int fd;
938a60d6
JK
152
153 if (!filename)
154 return 0;
155
adcd9f54
JK
156 if (flags & MAILMAP_NOFOLLOW)
157 fd = open_nofollow(filename, O_RDONLY);
158 else
159 fd = open(filename, O_RDONLY);
160
161 if (fd < 0) {
938a60d6
JK
162 if (errno == ENOENT)
163 return 0;
60901e4c 164 return error_errno("unable to open mailmap at %s", filename);
938a60d6 165 }
adcd9f54 166 f = xfdopen(fd, "r");
7c1c6782 167
7c8ce308 168 while (fgets(buffer, sizeof(buffer), f) != NULL)
4e168333 169 read_mailmap_line(map, buffer);
7c1c6782
JH
170 fclose(f);
171 return 0;
172}
173
4e168333 174static void read_mailmap_string(struct string_list *map, char *buf)
08610900 175{
f972a165
JK
176 while (*buf) {
177 char *end = strchrnul(buf, '\n');
08610900 178
f972a165
JK
179 if (*end)
180 *end++ = '\0';
08610900 181
4e168333 182 read_mailmap_line(map, buf);
f972a165 183 buf = end;
08610900
JK
184 }
185}
186
4e168333 187static int read_mailmap_blob(struct string_list *map, const char *name)
08610900 188{
15be4a5d 189 struct object_id oid;
08610900
JK
190 char *buf;
191 unsigned long size;
192 enum object_type type;
193
194 if (!name)
938a60d6 195 return 0;
d850b7a5 196 if (repo_get_oid(the_repository, name, &oid) < 0)
938a60d6 197 return 0;
08610900 198
bc726bd0 199 buf = repo_read_object_file(the_repository, &oid, &type, &size);
08610900 200 if (!buf)
938a60d6 201 return error("unable to read mailmap object at %s", name);
08610900 202 if (type != OBJ_BLOB)
938a60d6 203 return error("mailmap is not a blob: %s", name);
08610900 204
4e168333 205 read_mailmap_string(map, buf);
08610900
JK
206
207 free(buf);
208 return 0;
209}
210
4e168333 211int read_mailmap(struct string_list *map)
d551a488 212{
938a60d6 213 int err = 0;
8c473cec 214
0925ce4d 215 map->strdup_strings = 1;
de2f95eb 216 map->cmp = namemap_cmp;
8c473cec
JK
217
218 if (!git_mailmap_blob && is_bare_repository())
219 git_mailmap_blob = "HEAD:.mailmap";
220
a38cb987 221 if (!startup_info->have_repository || !is_bare_repository())
204333b0
JH
222 err |= read_mailmap_file(map, ".mailmap",
223 startup_info->have_repository ?
224 MAILMAP_NOFOLLOW : 0);
5735dc5a 225 if (startup_info->have_repository)
4e168333 226 err |= read_mailmap_blob(map, git_mailmap_blob);
adcd9f54 227 err |= read_mailmap_file(map, git_mailmap_file, 0);
938a60d6 228 return err;
d551a488
MSO
229}
230
0925ce4d
MSO
231void clear_mailmap(struct string_list *map)
232{
0925ce4d
MSO
233 map->strdup_strings = 1;
234 string_list_clear_func(map, free_mailmap_entry);
0925ce4d
MSO
235}
236
388c7f8a
JH
237/*
238 * Look for an entry in map that match string[0:len]; string[len]
239 * does not have to be NUL (but it could be).
240 */
241static struct string_list_item *lookup_prefix(struct string_list *map,
242 const char *string, size_t len)
243{
244 int i = string_list_find_insert_index(map, string, 1);
245 if (i < 0) {
246 /* exact match */
247 i = -1 - i;
248 if (!string[len])
249 return &map->items[i];
250 /*
251 * that map entry matches exactly to the string, including
252 * the cruft at the end beyond "len". That is not a match
253 * with string[0:len] that we are looking for.
254 */
255 } else if (!string[len]) {
256 /*
257 * asked with the whole string, and got nothing. No
258 * matching entry can exist in the map.
259 */
260 return NULL;
261 }
262
263 /*
264 * i is at the exact match to an overlong key, or location the
265 * overlong key would be inserted, which must come after the
266 * real location of the key if one exists.
267 */
268 while (0 <= --i && i < map->nr) {
269 int cmp = strncasecmp(map->items[i].string, string, len);
270 if (cmp < 0)
271 /*
272 * "i" points at a key definitely below the prefix;
273 * the map does not have string[0:len] in it.
274 */
275 break;
276 else if (!cmp && !map->items[i].string[len])
277 /* found it */
278 return &map->items[i];
279 /*
280 * otherwise, the string at "i" may be string[0:len]
281 * followed by a string that sorts later than string[len:];
282 * keep trying.
283 */
284 }
285 return NULL;
286}
287
0925ce4d 288int map_user(struct string_list *map,
bd237945
JH
289 const char **email, size_t *emaillen,
290 const char **name, size_t *namelen)
7c1c6782 291{
c455c87c 292 struct string_list_item *item;
0925ce4d 293 struct mailmap_entry *me;
388c7f8a 294
ea02ffa3 295 item = lookup_prefix(map, *email, *emaillen);
afe8a907 296 if (item) {
0925ce4d
MSO
297 me = (struct mailmap_entry *)item->util;
298 if (me->namemap.nr) {
bd237945
JH
299 /*
300 * The item has multiple items, so we'll look up on
301 * name too. If the name is not found, we choose the
302 * simple entry.
303 */
ea02ffa3
AP
304 struct string_list_item *subitem;
305 subitem = lookup_prefix(&me->namemap, *name, *namelen);
0925ce4d
MSO
306 if (subitem)
307 item = subitem;
308 }
309 }
afe8a907 310 if (item) {
0925ce4d 311 struct mailmap_info *mi = (struct mailmap_info *)item->util;
910d07a8 312 if (mi->name == NULL && mi->email == NULL)
0925ce4d 313 return 0;
ea02ffa3
AP
314 if (mi->email) {
315 *email = mi->email;
316 *emaillen = strlen(*email);
317 }
318 if (mi->name) {
319 *name = mi->name;
320 *namelen = strlen(*name);
321 }
7c1c6782
JH
322 return 1;
323 }
324 return 0;
325}