1#include "cache.h"
2#include "path-list.h"
3#include "mailmap.h"
4
5int read_mailmap(struct path_list *map, const char *filename, char **repo_abbrev)
6{
7 char buffer[1024];
8 FILE *f = fopen(filename, "r");
9
10 if (f == NULL)
11 return 1;
12 while (fgets(buffer, sizeof(buffer), f) != NULL) {
13 char *end_of_name, *left_bracket, *right_bracket;
14 char *name, *email;
15 int i;
16 if (buffer[0] == '#') {
17 static const char abbrev[] = "# repo-abbrev:";
18 int abblen = sizeof(abbrev) - 1;
19 int len = strlen(buffer);
20
21 if (!repo_abbrev)
22 continue;
23
24 if (len && buffer[len - 1] == '\n')
25 buffer[--len] = 0;
26 if (!strncmp(buffer, abbrev, abblen)) {
27 char *cp;
28
29 if (repo_abbrev)
30 free(*repo_abbrev);
31 *repo_abbrev = xmalloc(len);
32
33 for (cp = buffer + abblen; isspace(*cp); cp++)
34 ; /* nothing */
35 strcpy(*repo_abbrev, cp);
36 }
37 continue;
38 }
39 if ((left_bracket = strchr(buffer, '<')) == NULL)
40 continue;
41 if ((right_bracket = strchr(left_bracket + 1, '>')) == NULL)
42 continue;
43 if (right_bracket == left_bracket + 1)
44 continue;
45 for (end_of_name = left_bracket; end_of_name != buffer
46 && isspace(end_of_name[-1]); end_of_name--)
47 /* keep on looking */
48 if (end_of_name == buffer)
49 continue;
50 name = xmalloc(end_of_name - buffer + 1);
51 strlcpy(name, buffer, end_of_name - buffer + 1);
52 email = xmalloc(right_bracket - left_bracket);
53 for (i = 0; i < right_bracket - left_bracket - 1; i++)
54 email[i] = tolower(left_bracket[i + 1]);
55 email[right_bracket - left_bracket - 1] = '\0';
56 path_list_insert(email, map)->util = name;
57 }
58 fclose(f);
59 return 0;
60}
61
62int map_email(struct path_list *map, const char *email, char *name, int maxlen)
63{
64 char *p;
65 struct path_list_item *item;
66 char buf[1024], *mailbuf;
67 int i;
68
69 /* autocomplete common developers */
70 p = strchr(email, '>');
71 if (!p)
72 return 0;
73 if (p - email + 1 < sizeof(buf))
74 mailbuf = buf;
75 else
76 mailbuf = xmalloc(p - email + 1);
77
78 /* downcase the email address */
79 for (i = 0; i < p - email; i++)
80 mailbuf[i] = tolower(email[i]);
81 mailbuf[i] = 0;
82 item = path_list_lookup(mailbuf, map);
83 if (mailbuf != buf)
84 free(mailbuf);
85 if (item != NULL) {
86 const char *realname = (const char *)item->util;
87 strlcpy(name, realname, maxlen);
88 return 1;
89 }
90 return 0;
91}
92