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