Junio C Hamano | 7c1c678 | 2007-04-27 00:41:15 -0700 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "path-list.h" |
Alex Riesen | fe5d30b | 2007-05-01 00:31:52 +0200 | [diff] [blame] | 3 | #include "mailmap.h" |
Junio C Hamano | 7c1c678 | 2007-04-27 00:41:15 -0700 | [diff] [blame] | 4 | |
| 5 | int 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 | |
Alex Riesen | 8503ee4 | 2007-05-01 00:26:36 +0200 | [diff] [blame] | 21 | if (!repo_abbrev) |
| 22 | continue; |
| 23 | |
Junio C Hamano | 7c1c678 | 2007-04-27 00:41:15 -0700 | [diff] [blame] | 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; |
Junio C Hamano | fd99b36 | 2007-12-07 17:04:11 -0800 | [diff] [blame] | 45 | for (end_of_name = left_bracket; |
| 46 | end_of_name != buffer && isspace(end_of_name[-1]); |
| 47 | end_of_name--) |
| 48 | ; /* keep on looking */ |
Junio C Hamano | 7c1c678 | 2007-04-27 00:41:15 -0700 | [diff] [blame] | 49 | if (end_of_name == buffer) |
| 50 | continue; |
| 51 | name = xmalloc(end_of_name - buffer + 1); |
| 52 | strlcpy(name, buffer, end_of_name - buffer + 1); |
| 53 | email = xmalloc(right_bracket - left_bracket); |
| 54 | for (i = 0; i < right_bracket - left_bracket - 1; i++) |
| 55 | email[i] = tolower(left_bracket[i + 1]); |
| 56 | email[right_bracket - left_bracket - 1] = '\0'; |
| 57 | path_list_insert(email, map)->util = name; |
| 58 | } |
| 59 | fclose(f); |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | int map_email(struct path_list *map, const char *email, char *name, int maxlen) |
| 64 | { |
| 65 | char *p; |
| 66 | struct path_list_item *item; |
| 67 | char buf[1024], *mailbuf; |
| 68 | int i; |
| 69 | |
| 70 | /* autocomplete common developers */ |
| 71 | p = strchr(email, '>'); |
| 72 | if (!p) |
| 73 | return 0; |
| 74 | if (p - email + 1 < sizeof(buf)) |
| 75 | mailbuf = buf; |
| 76 | else |
| 77 | mailbuf = xmalloc(p - email + 1); |
| 78 | |
| 79 | /* downcase the email address */ |
| 80 | for (i = 0; i < p - email; i++) |
| 81 | mailbuf[i] = tolower(email[i]); |
| 82 | mailbuf[i] = 0; |
| 83 | item = path_list_lookup(mailbuf, map); |
| 84 | if (mailbuf != buf) |
| 85 | free(mailbuf); |
| 86 | if (item != NULL) { |
| 87 | const char *realname = (const char *)item->util; |
Alex Riesen | 600682a | 2007-05-01 00:22:53 +0200 | [diff] [blame] | 88 | strlcpy(name, realname, maxlen); |
Junio C Hamano | 7c1c678 | 2007-04-27 00:41:15 -0700 | [diff] [blame] | 89 | return 1; |
| 90 | } |
| 91 | return 0; |
| 92 | } |