2 #include "string-list.h"
4 #include "object-store.h"
6 #define DEBUG_MAILMAP 0
8 #define debug_mm(...) fprintf(stderr, __VA_ARGS__)
9 #define debug_str(X) ((X) ? (X) : "(none)")
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
; }
16 const char *git_mailmap_file
;
17 const char *git_mailmap_blob
;
24 struct mailmap_entry
{
25 /* name and email for the simple mail-only case */
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
));
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",
47 debug_mm("mailmap: - simple: '%s' <%s>\n",
48 debug_str(me
->name
), debug_str(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
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
;
83 item
= string_list_insert(map
, old_email
);
85 me
= (struct mailmap_entry
*)item
->util
;
88 me
->namemap
.strdup_strings
= 1;
89 me
->namemap
.cmp
= namemap_cmp
;
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 */
99 me
->name
= xstrdup(new_name
);
103 me
->email
= xstrdup(new_email
);
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
)
126 if ((right
= strchr(left
+1, '>')) == NULL
)
128 if (!allow_empty_email
&& (left
+1 == right
))
131 /* remove whitespace from beginning and end of name */
133 while (isspace(*nstart
) && nstart
< left
)
136 while (nend
> nstart
&& isspace(*nend
))
139 *name
= (nstart
<= nend
? nstart
: NULL
);
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] == '#')
154 if ((name2
= parse_name_and_email(buffer
, &name1
, &email1
, 0)) != NULL
)
155 parse_name_and_email(name2
, &name2
, &email2
, 1);
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
,
174 if (flags
& MAILMAP_NOFOLLOW
)
175 fd
= open_nofollow(filename
, O_RDONLY
);
177 fd
= open(filename
, O_RDONLY
);
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
);
192 static void read_mailmap_string(struct string_list
*map
, char *buf
)
195 char *end
= strchrnul(buf
, '\n');
200 read_mailmap_line(map
, buf
);
205 static int read_mailmap_blob(struct string_list
*map
, const char *name
)
207 struct object_id oid
;
210 enum object_type type
;
214 if (get_oid(name
, &oid
) < 0)
217 buf
= read_object_file(&oid
, &type
, &size
);
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
);
229 int read_mailmap(struct string_list
*map
)
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);
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);
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.
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
);
292 * "i" points at a key definitely below the prefix;
293 * the map does not have string[0:len] in it.
296 else if (!cmp
&& !map
->items
[i
].string
[len
])
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:];
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
);
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
328 struct string_list_item
*subitem
;
329 subitem
= lookup_prefix(&me
->namemap
, *name
, *namelen
);
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");
342 *emaillen
= strlen(*email
);
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
));
353 debug_mm("map_user: --\n");