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