Merge branch 'jc/doc-diff-name-only' into maint-2.45
[git.git] / mailmap.c
blob3d6a5e9400f4c8195d0f58ab58f3f9a4dfa0acba
1 #include "git-compat-util.h"
2 #include "environment.h"
3 #include "string-list.h"
4 #include "mailmap.h"
5 #include "object-name.h"
6 #include "object-store-ll.h"
7 #include "setup.h"
9 const char *git_mailmap_file;
10 const char *git_mailmap_blob;
12 struct mailmap_info {
13 char *name;
14 char *email;
17 struct mailmap_entry {
18 /* name and email for the simple mail-only case */
19 char *name;
20 char *email;
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;
29 free(mi->name);
30 free(mi->email);
31 free(mi);
34 static void free_mailmap_entry(void *p, const char *s UNUSED)
36 struct mailmap_entry *me = (struct mailmap_entry *)p;
38 free(me->name);
39 free(me->email);
41 me->namemap.strdup_strings = 1;
42 string_list_clear_func(&me->namemap, free_mailmap_info);
43 free(me);
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
53 * "unusual" string.h.
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;
67 if (!old_email) {
68 old_email = new_email;
69 new_email = NULL;
72 item = string_list_insert(map, old_email);
73 if (item->util) {
74 me = (struct mailmap_entry *)item->util;
75 } else {
76 CALLOC_ARRAY(me, 1);
77 me->namemap.strdup_strings = 1;
78 me->namemap.cmp = namemap_cmp;
79 item->util = me;
82 if (!old_name) {
83 /* Replace current name and new email for simple entry */
84 if (new_name) {
85 free(me->name);
86 me->name = xstrdup(new_name);
88 if (new_email) {
89 free(me->email);
90 me->email = xstrdup(new_email);
92 } else {
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, '<')))
107 return NULL;
108 if (!(right = strchr(left + 1, '>')))
109 return NULL;
110 if (!allow_empty_email && (left+1 == right))
111 return NULL;
113 /* remove whitespace from beginning and end of name */
114 nstart = buffer;
115 while (isspace(*nstart) && nstart < left)
116 ++nstart;
117 nend = left-1;
118 while (nend > nstart && isspace(*nend))
119 --nend;
121 *name = (nstart <= nend ? nstart : NULL);
122 *email = left+1;
123 *(nend+1) = '\0';
124 *right++ = '\0';
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] == '#')
134 return;
136 if ((name2 = parse_name_and_email(buffer, &name1, &email1, 0)))
137 parse_name_and_email(name2, &name2, &email2, 1);
139 if (email1)
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,
147 unsigned flags)
149 char buffer[1024];
150 FILE *f;
151 int fd;
153 if (!filename)
154 return 0;
156 if (flags & MAILMAP_NOFOLLOW)
157 fd = open_nofollow(filename, O_RDONLY);
158 else
159 fd = open(filename, O_RDONLY);
161 if (fd < 0) {
162 if (errno == ENOENT)
163 return 0;
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);
170 fclose(f);
171 return 0;
174 static void read_mailmap_string(struct string_list *map, char *buf)
176 while (*buf) {
177 char *end = strchrnul(buf, '\n');
179 if (*end)
180 *end++ = '\0';
182 read_mailmap_line(map, buf);
183 buf = end;
187 static int read_mailmap_blob(struct string_list *map, const char *name)
189 struct object_id oid;
190 char *buf;
191 unsigned long size;
192 enum object_type type;
194 if (!name)
195 return 0;
196 if (repo_get_oid(the_repository, name, &oid) < 0)
197 return 0;
199 buf = repo_read_object_file(the_repository, &oid, &type, &size);
200 if (!buf)
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);
207 free(buf);
208 return 0;
211 int read_mailmap(struct string_list *map)
213 int err = 0;
215 map->strdup_strings = 1;
216 map->cmp = namemap_cmp;
218 if (!git_mailmap_blob && is_bare_repository())
219 git_mailmap_blob = "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);
228 return err;
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);
245 if (i < 0) {
246 /* exact match */
247 i = -1 - i;
248 if (!string[len])
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.
260 return NULL;
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);
270 if (cmp < 0)
272 * "i" points at a key definitely below the prefix;
273 * the map does not have string[0:len] in it.
275 break;
276 else if (!cmp && !map->items[i].string[len])
277 /* found it */
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:];
282 * keep trying.
285 return NULL;
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);
296 if (item) {
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
302 * simple entry.
304 struct string_list_item *subitem;
305 subitem = lookup_prefix(&me->namemap, *name, *namelen);
306 if (subitem)
307 item = subitem;
310 if (item) {
311 struct mailmap_info *mi = (struct mailmap_info *)item->util;
312 if (mi->name == NULL && mi->email == NULL)
313 return 0;
314 if (mi->email) {
315 *email = mi->email;
316 *emaillen = strlen(*email);
318 if (mi->name) {
319 *name = mi->name;
320 *namelen = strlen(*name);
322 return 1;
324 return 0;