1 #include "git-compat-util.h"
2 #include "environment.h"
3 #include "string-list.h"
5 #include "object-name.h"
6 #include "object-store-ll.h"
9 char *git_mailmap_file
;
10 char *git_mailmap_blob
;
17 struct mailmap_entry
{
18 /* name and email for the simple mail-only case */
22 /* name and email for the complex mail and name matching case */
23 struct string_list namemap
;
26 static void free_mailmap_info(void *p
, const char *s UNUSED
)
28 struct mailmap_info
*mi
= (struct mailmap_info
*)p
;
34 static void free_mailmap_entry(void *p
, const char *s UNUSED
)
36 struct mailmap_entry
*me
= (struct mailmap_entry
*)p
;
41 me
->namemap
.strdup_strings
= 1;
42 string_list_clear_func(&me
->namemap
, free_mailmap_info
);
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
55 static int namemap_cmp(const char *a
, const char *b
)
57 return strcasecmp(a
, b
);
60 static void add_mapping(struct string_list
*map
,
61 char *new_name
, char *new_email
,
62 char *old_name
, char *old_email
)
64 struct mailmap_entry
*me
;
65 struct string_list_item
*item
;
68 old_email
= new_email
;
72 item
= string_list_insert(map
, old_email
);
74 me
= (struct mailmap_entry
*)item
->util
;
77 me
->namemap
.strdup_strings
= 1;
78 me
->namemap
.cmp
= namemap_cmp
;
83 /* Replace current name and new email for simple entry */
86 me
->name
= xstrdup(new_name
);
90 me
->email
= xstrdup(new_email
);
93 struct mailmap_info
*mi
= xcalloc(1, sizeof(struct mailmap_info
));
94 mi
->name
= xstrdup_or_null(new_name
);
95 mi
->email
= xstrdup_or_null(new_email
);
96 string_list_insert(&me
->namemap
, old_name
)->util
= mi
;
100 static char *parse_name_and_email(char *buffer
, char **name
,
101 char **email
, int allow_empty_email
)
103 char *left
, *right
, *nstart
, *nend
;
104 *name
= *email
= NULL
;
106 if (!(left
= strchr(buffer
, '<')))
108 if (!(right
= strchr(left
+ 1, '>')))
110 if (!allow_empty_email
&& (left
+1 == right
))
113 /* remove whitespace from beginning and end of name */
115 while (isspace(*nstart
) && nstart
< left
)
118 while (nend
> nstart
&& isspace(*nend
))
121 *name
= (nstart
<= nend
? nstart
: NULL
);
126 return (*right
== '\0' ? NULL
: right
);
129 static void read_mailmap_line(struct string_list
*map
, char *buffer
)
131 char *name1
= NULL
, *email1
= NULL
, *name2
= NULL
, *email2
= NULL
;
133 if (buffer
[0] == '#')
136 if ((name2
= parse_name_and_email(buffer
, &name1
, &email1
, 0)))
137 parse_name_and_email(name2
, &name2
, &email2
, 1);
140 add_mapping(map
, name1
, email1
, name2
, email2
);
143 /* Flags for read_mailmap_file() */
144 #define MAILMAP_NOFOLLOW (1<<0)
146 static int read_mailmap_file(struct string_list
*map
, const char *filename
,
156 if (flags
& MAILMAP_NOFOLLOW
)
157 fd
= open_nofollow(filename
, O_RDONLY
);
159 fd
= open(filename
, O_RDONLY
);
164 return error_errno("unable to open mailmap at %s", filename
);
166 f
= xfdopen(fd
, "r");
168 while (fgets(buffer
, sizeof(buffer
), f
) != NULL
)
169 read_mailmap_line(map
, buffer
);
174 static void read_mailmap_string(struct string_list
*map
, char *buf
)
177 char *end
= strchrnul(buf
, '\n');
182 read_mailmap_line(map
, buf
);
187 static int read_mailmap_blob(struct string_list
*map
, const char *name
)
189 struct object_id oid
;
192 enum object_type type
;
196 if (repo_get_oid(the_repository
, name
, &oid
) < 0)
199 buf
= repo_read_object_file(the_repository
, &oid
, &type
, &size
);
201 return error("unable to read mailmap object at %s", name
);
202 if (type
!= OBJ_BLOB
)
203 return error("mailmap is not a blob: %s", name
);
205 read_mailmap_string(map
, buf
);
211 int read_mailmap(struct string_list
*map
)
215 map
->strdup_strings
= 1;
216 map
->cmp
= namemap_cmp
;
218 if (!git_mailmap_blob
&& is_bare_repository())
219 git_mailmap_blob
= xstrdup("HEAD:.mailmap");
221 if (!startup_info
->have_repository
|| !is_bare_repository())
222 err
|= read_mailmap_file(map
, ".mailmap",
223 startup_info
->have_repository
?
224 MAILMAP_NOFOLLOW
: 0);
225 if (startup_info
->have_repository
)
226 err
|= read_mailmap_blob(map
, git_mailmap_blob
);
227 err
|= read_mailmap_file(map
, git_mailmap_file
, 0);
231 void clear_mailmap(struct string_list
*map
)
233 map
->strdup_strings
= 1;
234 string_list_clear_func(map
, free_mailmap_entry
);
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).
241 static struct string_list_item
*lookup_prefix(struct string_list
*map
,
242 const char *string
, size_t len
)
244 int i
= string_list_find_insert_index(map
, string
, 1);
249 return &map
->items
[i
];
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.
255 } else if (!string
[len
]) {
257 * asked with the whole string, and got nothing. No
258 * matching entry can exist in the map.
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.
268 while (0 <= --i
&& i
< map
->nr
) {
269 int cmp
= strncasecmp(map
->items
[i
].string
, string
, len
);
272 * "i" points at a key definitely below the prefix;
273 * the map does not have string[0:len] in it.
276 else if (!cmp
&& !map
->items
[i
].string
[len
])
278 return &map
->items
[i
];
280 * otherwise, the string at "i" may be string[0:len]
281 * followed by a string that sorts later than string[len:];
288 int map_user(struct string_list
*map
,
289 const char **email
, size_t *emaillen
,
290 const char **name
, size_t *namelen
)
292 struct string_list_item
*item
;
293 struct mailmap_entry
*me
;
295 item
= lookup_prefix(map
, *email
, *emaillen
);
297 me
= (struct mailmap_entry
*)item
->util
;
298 if (me
->namemap
.nr
) {
300 * The item has multiple items, so we'll look up on
301 * name too. If the name is not found, we choose the
304 struct string_list_item
*subitem
;
305 subitem
= lookup_prefix(&me
->namemap
, *name
, *namelen
);
311 struct mailmap_info
*mi
= (struct mailmap_info
*)item
->util
;
312 if (mi
->name
== NULL
&& mi
->email
== NULL
)
316 *emaillen
= strlen(*email
);
320 *namelen
= strlen(*name
);