2 #include "environment.h"
3 #include "string-list.h"
5 #include "object-store.h"
8 #define DEBUG_MAILMAP 0
10 #define debug_mm(...) fprintf(stderr, __VA_ARGS__)
11 #define debug_str(X) ((X) ? (X) : "(none)")
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
; }
18 const char *git_mailmap_file
;
19 const char *git_mailmap_blob
;
26 struct mailmap_entry
{
27 /* name and email for the simple mail-only case */
31 /* name and email for the complex mail and name matching case */
32 struct string_list namemap
;
35 static void free_mailmap_info(void *p
, const char *s
)
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
));
45 static void free_mailmap_entry(void *p
, const char *s
)
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
));
56 me
->namemap
.strdup_strings
= 1;
57 string_list_clear_func(&me
->namemap
, free_mailmap_info
);
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
70 static int namemap_cmp(const char *a
, const char *b
)
72 return strcasecmp(a
, b
);
75 static void add_mapping(struct string_list
*map
,
76 char *new_name
, char *new_email
,
77 char *old_name
, char *old_email
)
79 struct mailmap_entry
*me
;
80 struct string_list_item
*item
;
83 old_email
= new_email
;
87 item
= string_list_insert(map
, old_email
);
89 me
= (struct mailmap_entry
*)item
->util
;
92 me
->namemap
.strdup_strings
= 1;
93 me
->namemap
.cmp
= namemap_cmp
;
98 debug_mm("mailmap: adding (simple) entry for '%s'\n", old_email
);
100 /* Replace current name and new email for simple entry */
103 me
->name
= xstrdup(new_name
);
107 me
->email
= xstrdup(new_email
);
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
;
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
));
122 static char *parse_name_and_email(char *buffer
, char **name
,
123 char **email
, int allow_empty_email
)
125 char *left
, *right
, *nstart
, *nend
;
126 *name
= *email
= NULL
;
128 if (!(left
= strchr(buffer
, '<')))
130 if (!(right
= strchr(left
+ 1, '>')))
132 if (!allow_empty_email
&& (left
+1 == right
))
135 /* remove whitespace from beginning and end of name */
137 while (isspace(*nstart
) && nstart
< left
)
140 while (nend
> nstart
&& isspace(*nend
))
143 *name
= (nstart
<= nend
? nstart
: NULL
);
148 return (*right
== '\0' ? NULL
: right
);
151 static void read_mailmap_line(struct string_list
*map
, char *buffer
)
153 char *name1
= NULL
, *email1
= NULL
, *name2
= NULL
, *email2
= NULL
;
155 if (buffer
[0] == '#')
158 if ((name2
= parse_name_and_email(buffer
, &name1
, &email1
, 0)))
159 parse_name_and_email(name2
, &name2
, &email2
, 1);
162 add_mapping(map
, name1
, email1
, name2
, email2
);
165 /* Flags for read_mailmap_file() */
166 #define MAILMAP_NOFOLLOW (1<<0)
168 static int read_mailmap_file(struct string_list
*map
, const char *filename
,
178 if (flags
& MAILMAP_NOFOLLOW
)
179 fd
= open_nofollow(filename
, O_RDONLY
);
181 fd
= open(filename
, O_RDONLY
);
186 return error_errno("unable to open mailmap at %s", filename
);
188 f
= xfdopen(fd
, "r");
190 while (fgets(buffer
, sizeof(buffer
), f
) != NULL
)
191 read_mailmap_line(map
, buffer
);
196 static void read_mailmap_string(struct string_list
*map
, char *buf
)
199 char *end
= strchrnul(buf
, '\n');
204 read_mailmap_line(map
, buf
);
209 static int read_mailmap_blob(struct string_list
*map
, const char *name
)
211 struct object_id oid
;
214 enum object_type type
;
218 if (get_oid(name
, &oid
) < 0)
221 buf
= read_object_file(&oid
, &type
, &size
);
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
);
227 read_mailmap_string(map
, buf
);
233 int read_mailmap(struct string_list
*map
)
237 map
->strdup_strings
= 1;
238 map
->cmp
= namemap_cmp
;
240 if (!git_mailmap_blob
&& is_bare_repository())
241 git_mailmap_blob
= "HEAD:.mailmap";
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);
253 void clear_mailmap(struct string_list
*map
)
255 debug_mm("mailmap: clearing %"PRIuMAX
" entries...\n",
257 map
->strdup_strings
= 1;
258 string_list_clear_func(map
, free_mailmap_entry
);
259 debug_mm("mailmap: cleared\n");
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).
266 static struct string_list_item
*lookup_prefix(struct string_list
*map
,
267 const char *string
, size_t len
)
269 int i
= string_list_find_insert_index(map
, string
, 1);
274 return &map
->items
[i
];
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.
280 } else if (!string
[len
]) {
282 * asked with the whole string, and got nothing. No
283 * matching entry can exist in the map.
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.
293 while (0 <= --i
&& i
< map
->nr
) {
294 int cmp
= strncasecmp(map
->items
[i
].string
, string
, len
);
297 * "i" points at a key definitely below the prefix;
298 * the map does not have string[0:len] in it.
301 else if (!cmp
&& !map
->items
[i
].string
[len
])
303 return &map
->items
[i
];
305 * otherwise, the string at "i" may be string[0:len]
306 * followed by a string that sorts later than string[len:];
313 int map_user(struct string_list
*map
,
314 const char **email
, size_t *emaillen
,
315 const char **name
, size_t *namelen
)
317 struct string_list_item
*item
;
318 struct mailmap_entry
*me
;
320 debug_mm("map_user: map '%.*s' <%.*s>\n",
321 (int)*namelen
, debug_str(*name
),
322 (int)*emaillen
, debug_str(*email
));
324 item
= lookup_prefix(map
, *email
, *emaillen
);
326 me
= (struct mailmap_entry
*)item
->util
;
327 if (me
->namemap
.nr
) {
329 * The item has multiple items, so we'll look up on
330 * name too. If the name is not found, we choose the
333 struct string_list_item
*subitem
;
334 subitem
= lookup_prefix(&me
->namemap
, *name
, *namelen
);
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");
347 *emaillen
= strlen(*email
);
351 *namelen
= strlen(*name
);
353 debug_mm("map_user: to '%.*s' <%.*s>\n",
354 (int)*namelen
, debug_str(*name
),
355 (int)*emaillen
, debug_str(*email
));
358 debug_mm("map_user: --\n");