]>
Commit | Line | Data |
---|---|---|
7c1c6782 | 1 | #include "cache.h" |
c455c87c | 2 | #include "string-list.h" |
fe5d30b6 | 3 | #include "mailmap.h" |
cbd53a21 | 4 | #include "object-store.h" |
7c1c6782 | 5 | |
0925ce4d MSO |
6 | #define DEBUG_MAILMAP 0 |
7 | #if DEBUG_MAILMAP | |
8 | #define debug_mm(...) fprintf(stderr, __VA_ARGS__) | |
fbfba7ad | 9 | #define debug_str(X) ((X) ? (X) : "(none)") |
0925ce4d | 10 | #else |
48ca53ca | 11 | __attribute__((format (printf, 1, 2))) |
0925ce4d | 12 | static inline void debug_mm(const char *format, ...) {} |
fbfba7ad | 13 | static inline const char *debug_str(const char *s) { return s; } |
0925ce4d MSO |
14 | #endif |
15 | ||
d551a488 | 16 | const char *git_mailmap_file; |
08610900 | 17 | const char *git_mailmap_blob; |
0925ce4d MSO |
18 | |
19 | struct mailmap_info { | |
20 | char *name; | |
21 | char *email; | |
22 | }; | |
23 | ||
24 | struct mailmap_entry { | |
25 | /* name and email for the simple mail-only case */ | |
26 | char *name; | |
27 | char *email; | |
28 | ||
29 | /* name and email for the complex mail and name matching case */ | |
30 | struct string_list namemap; | |
31 | }; | |
32 | ||
33 | static void free_mailmap_info(void *p, const char *s) | |
34 | { | |
35 | struct mailmap_info *mi = (struct mailmap_info *)p; | |
bd237945 JH |
36 | debug_mm("mailmap: -- complex: '%s' -> '%s' <%s>\n", |
37 | s, debug_str(mi->name), debug_str(mi->email)); | |
0925ce4d MSO |
38 | free(mi->name); |
39 | free(mi->email); | |
40 | } | |
41 | ||
42 | static void free_mailmap_entry(void *p, const char *s) | |
43 | { | |
44 | struct mailmap_entry *me = (struct mailmap_entry *)p; | |
bd237945 JH |
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)); | |
fbfba7ad | 49 | |
0925ce4d MSO |
50 | free(me->name); |
51 | free(me->email); | |
52 | ||
53 | me->namemap.strdup_strings = 1; | |
54 | string_list_clear_func(&me->namemap, free_mailmap_info); | |
55 | } | |
56 | ||
de2f95eb JH |
57 | /* |
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. | |
65 | */ | |
66 | static int namemap_cmp(const char *a, const char *b) | |
67 | { | |
68 | return strcasecmp(a, b); | |
69 | } | |
70 | ||
0925ce4d | 71 | static void add_mapping(struct string_list *map, |
bd237945 JH |
72 | char *new_name, char *new_email, |
73 | char *old_name, char *old_email) | |
0925ce4d MSO |
74 | { |
75 | struct mailmap_entry *me; | |
63226218 | 76 | struct string_list_item *item; |
bf637803 | 77 | |
0925ce4d MSO |
78 | if (old_email == NULL) { |
79 | old_email = new_email; | |
80 | new_email = NULL; | |
81 | } | |
82 | ||
63226218 SB |
83 | item = string_list_insert(map, old_email); |
84 | if (item->util) { | |
85 | me = (struct mailmap_entry *)item->util; | |
0925ce4d | 86 | } else { |
ca56dadb | 87 | CALLOC_ARRAY(me, 1); |
97e751be | 88 | me->namemap.strdup_strings = 1; |
de2f95eb | 89 | me->namemap.cmp = namemap_cmp; |
97e751be | 90 | item->util = me; |
0925ce4d | 91 | } |
0925ce4d MSO |
92 | |
93 | if (old_name == NULL) { | |
63226218 SB |
94 | debug_mm("mailmap: adding (simple) entry for '%s'\n", old_email); |
95 | ||
0925ce4d | 96 | /* Replace current name and new email for simple entry */ |
d8d2eb7d JM |
97 | if (new_name) { |
98 | free(me->name); | |
0925ce4d | 99 | me->name = xstrdup(new_name); |
d8d2eb7d JM |
100 | } |
101 | if (new_email) { | |
102 | free(me->email); | |
0925ce4d | 103 | me->email = xstrdup(new_email); |
d8d2eb7d | 104 | } |
0925ce4d | 105 | } else { |
74b531f6 | 106 | struct mailmap_info *mi = xcalloc(1, sizeof(struct mailmap_info)); |
63226218 | 107 | debug_mm("mailmap: adding (complex) entry for '%s'\n", old_email); |
13092a91 JH |
108 | mi->name = xstrdup_or_null(new_name); |
109 | mi->email = xstrdup_or_null(new_email); | |
78a395d3 | 110 | string_list_insert(&me->namemap, old_name)->util = mi; |
0925ce4d MSO |
111 | } |
112 | ||
113 | debug_mm("mailmap: '%s' <%s> -> '%s' <%s>\n", | |
bd237945 JH |
114 | debug_str(old_name), old_email, |
115 | debug_str(new_name), debug_str(new_email)); | |
0925ce4d MSO |
116 | } |
117 | ||
5288dd58 | 118 | static char *parse_name_and_email(char *buffer, char **name, |
bd237945 | 119 | char **email, int allow_empty_email) |
0925ce4d MSO |
120 | { |
121 | char *left, *right, *nstart, *nend; | |
2af202be | 122 | *name = *email = NULL; |
0925ce4d MSO |
123 | |
124 | if ((left = strchr(buffer, '<')) == NULL) | |
125 | return NULL; | |
126 | if ((right = strchr(left+1, '>')) == NULL) | |
127 | return NULL; | |
5288dd58 | 128 | if (!allow_empty_email && (left+1 == right)) |
0925ce4d MSO |
129 | return NULL; |
130 | ||
131 | /* remove whitespace from beginning and end of name */ | |
132 | nstart = buffer; | |
133 | while (isspace(*nstart) && nstart < left) | |
134 | ++nstart; | |
135 | nend = left-1; | |
3174bc5c | 136 | while (nend > nstart && isspace(*nend)) |
0925ce4d MSO |
137 | --nend; |
138 | ||
8c381151 | 139 | *name = (nstart <= nend ? nstart : NULL); |
0925ce4d MSO |
140 | *email = left+1; |
141 | *(nend+1) = '\0'; | |
142 | *right++ = '\0'; | |
143 | ||
144 | return (*right == '\0' ? NULL : right); | |
145 | } | |
146 | ||
4e168333 | 147 | static void read_mailmap_line(struct string_list *map, char *buffer) |
7c8ce308 JK |
148 | { |
149 | char *name1 = NULL, *email1 = NULL, *name2 = NULL, *email2 = NULL; | |
7c8ce308 | 150 | |
4e168333 | 151 | if (buffer[0] == '#') |
7c8ce308 | 152 | return; |
4e168333 | 153 | |
7c8ce308 JK |
154 | if ((name2 = parse_name_and_email(buffer, &name1, &email1, 0)) != NULL) |
155 | parse_name_and_email(name2, &name2, &email2, 1); | |
156 | ||
157 | if (email1) | |
158 | add_mapping(map, name1, email1, name2, email2); | |
159 | } | |
160 | ||
adcd9f54 JK |
161 | /* Flags for read_mailmap_file() */ |
162 | #define MAILMAP_NOFOLLOW (1<<0) | |
163 | ||
164 | static int read_mailmap_file(struct string_list *map, const char *filename, | |
165 | unsigned flags) | |
7c1c6782 JH |
166 | { |
167 | char buffer[1024]; | |
938a60d6 | 168 | FILE *f; |
adcd9f54 | 169 | int fd; |
938a60d6 JK |
170 | |
171 | if (!filename) | |
172 | return 0; | |
173 | ||
adcd9f54 JK |
174 | if (flags & MAILMAP_NOFOLLOW) |
175 | fd = open_nofollow(filename, O_RDONLY); | |
176 | else | |
177 | fd = open(filename, O_RDONLY); | |
178 | ||
179 | if (fd < 0) { | |
938a60d6 JK |
180 | if (errno == ENOENT) |
181 | return 0; | |
60901e4c | 182 | return error_errno("unable to open mailmap at %s", filename); |
938a60d6 | 183 | } |
adcd9f54 | 184 | f = xfdopen(fd, "r"); |
7c1c6782 | 185 | |
7c8ce308 | 186 | while (fgets(buffer, sizeof(buffer), f) != NULL) |
4e168333 | 187 | read_mailmap_line(map, buffer); |
7c1c6782 JH |
188 | fclose(f); |
189 | return 0; | |
190 | } | |
191 | ||
4e168333 | 192 | static void read_mailmap_string(struct string_list *map, char *buf) |
08610900 | 193 | { |
f972a165 JK |
194 | while (*buf) { |
195 | char *end = strchrnul(buf, '\n'); | |
08610900 | 196 | |
f972a165 JK |
197 | if (*end) |
198 | *end++ = '\0'; | |
08610900 | 199 | |
4e168333 | 200 | read_mailmap_line(map, buf); |
f972a165 | 201 | buf = end; |
08610900 JK |
202 | } |
203 | } | |
204 | ||
4e168333 | 205 | static int read_mailmap_blob(struct string_list *map, const char *name) |
08610900 | 206 | { |
15be4a5d | 207 | struct object_id oid; |
08610900 JK |
208 | char *buf; |
209 | unsigned long size; | |
210 | enum object_type type; | |
211 | ||
212 | if (!name) | |
938a60d6 | 213 | return 0; |
15be4a5d | 214 | if (get_oid(name, &oid) < 0) |
938a60d6 | 215 | return 0; |
08610900 | 216 | |
b4f5aca4 | 217 | buf = read_object_file(&oid, &type, &size); |
08610900 | 218 | if (!buf) |
938a60d6 | 219 | return error("unable to read mailmap object at %s", name); |
08610900 | 220 | if (type != OBJ_BLOB) |
938a60d6 | 221 | return error("mailmap is not a blob: %s", name); |
08610900 | 222 | |
4e168333 | 223 | read_mailmap_string(map, buf); |
08610900 JK |
224 | |
225 | free(buf); | |
226 | return 0; | |
227 | } | |
228 | ||
4e168333 | 229 | int read_mailmap(struct string_list *map) |
d551a488 | 230 | { |
938a60d6 | 231 | int err = 0; |
8c473cec | 232 | |
0925ce4d | 233 | map->strdup_strings = 1; |
de2f95eb | 234 | map->cmp = namemap_cmp; |
8c473cec JK |
235 | |
236 | if (!git_mailmap_blob && is_bare_repository()) | |
237 | git_mailmap_blob = "HEAD:.mailmap"; | |
238 | ||
a38cb987 | 239 | if (!startup_info->have_repository || !is_bare_repository()) |
204333b0 JH |
240 | err |= read_mailmap_file(map, ".mailmap", |
241 | startup_info->have_repository ? | |
242 | MAILMAP_NOFOLLOW : 0); | |
5735dc5a | 243 | if (startup_info->have_repository) |
4e168333 | 244 | err |= read_mailmap_blob(map, git_mailmap_blob); |
adcd9f54 | 245 | err |= read_mailmap_file(map, git_mailmap_file, 0); |
938a60d6 | 246 | return err; |
d551a488 MSO |
247 | } |
248 | ||
0925ce4d MSO |
249 | void clear_mailmap(struct string_list *map) |
250 | { | |
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"); | |
255 | } | |
256 | ||
388c7f8a JH |
257 | /* |
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). | |
260 | */ | |
261 | static struct string_list_item *lookup_prefix(struct string_list *map, | |
262 | const char *string, size_t len) | |
263 | { | |
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]; | |
270 | /* | |
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. | |
274 | */ | |
275 | } else if (!string[len]) { | |
276 | /* | |
277 | * asked with the whole string, and got nothing. No | |
278 | * matching entry can exist in the map. | |
279 | */ | |
280 | return NULL; | |
281 | } | |
282 | ||
283 | /* | |
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. | |
287 | */ | |
288 | while (0 <= --i && i < map->nr) { | |
289 | int cmp = strncasecmp(map->items[i].string, string, len); | |
290 | if (cmp < 0) | |
291 | /* | |
292 | * "i" points at a key definitely below the prefix; | |
293 | * the map does not have string[0:len] in it. | |
294 | */ | |
295 | break; | |
296 | else if (!cmp && !map->items[i].string[len]) | |
297 | /* found it */ | |
298 | return &map->items[i]; | |
299 | /* | |
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. | |
303 | */ | |
304 | } | |
305 | return NULL; | |
306 | } | |
307 | ||
0925ce4d | 308 | int map_user(struct string_list *map, |
bd237945 JH |
309 | const char **email, size_t *emaillen, |
310 | const char **name, size_t *namelen) | |
7c1c6782 | 311 | { |
c455c87c | 312 | struct string_list_item *item; |
0925ce4d | 313 | struct mailmap_entry *me; |
388c7f8a | 314 | |
ea02ffa3 | 315 | debug_mm("map_user: map '%.*s' <%.*s>\n", |
bd237945 JH |
316 | (int)*namelen, debug_str(*name), |
317 | (int)*emaillen, debug_str(*email)); | |
388c7f8a | 318 | |
ea02ffa3 | 319 | item = lookup_prefix(map, *email, *emaillen); |
0925ce4d MSO |
320 | if (item != NULL) { |
321 | me = (struct mailmap_entry *)item->util; | |
322 | if (me->namemap.nr) { | |
bd237945 JH |
323 | /* |
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. | |
327 | */ | |
ea02ffa3 AP |
328 | struct string_list_item *subitem; |
329 | subitem = lookup_prefix(&me->namemap, *name, *namelen); | |
0925ce4d MSO |
330 | if (subitem) |
331 | item = subitem; | |
332 | } | |
333 | } | |
7c1c6782 | 334 | if (item != NULL) { |
0925ce4d | 335 | struct mailmap_info *mi = (struct mailmap_info *)item->util; |
ea02ffa3 | 336 | if (mi->name == NULL && mi->email == NULL) { |
0925ce4d MSO |
337 | debug_mm("map_user: -- (no simple mapping)\n"); |
338 | return 0; | |
339 | } | |
ea02ffa3 AP |
340 | if (mi->email) { |
341 | *email = mi->email; | |
342 | *emaillen = strlen(*email); | |
343 | } | |
344 | if (mi->name) { | |
345 | *name = mi->name; | |
346 | *namelen = strlen(*name); | |
347 | } | |
bd237945 JH |
348 | debug_mm("map_user: to '%.*s' <%.*s>\n", |
349 | (int)*namelen, debug_str(*name), | |
350 | (int)*emaillen, debug_str(*email)); | |
7c1c6782 JH |
351 | return 1; |
352 | } | |
0925ce4d | 353 | debug_mm("map_user: --\n"); |
7c1c6782 JH |
354 | return 0; |
355 | } |