tests: cosmetic improvements to the repo-setup test
[git/jnareb-git/bp-gitweb.git] / mailmap.c
blob02fcfde0b0b786af5229559586cf8ff5758429bc
1 #include "cache.h"
2 #include "string-list.h"
3 #include "mailmap.h"
5 #define DEBUG_MAILMAP 0
6 #if DEBUG_MAILMAP
7 #define debug_mm(...) fprintf(stderr, __VA_ARGS__)
8 #else
9 static inline void debug_mm(const char *format, ...) {}
10 #endif
12 const char *git_mailmap_file;
14 struct mailmap_info {
15 char *name;
16 char *email;
19 struct mailmap_entry {
20 /* name and email for the simple mail-only case */
21 char *name;
22 char *email;
24 /* name and email for the complex mail and name matching case */
25 struct string_list namemap;
28 static void free_mailmap_info(void *p, const char *s)
30 struct mailmap_info *mi = (struct mailmap_info *)p;
31 debug_mm("mailmap: -- complex: '%s' -> '%s' <%s>\n", s, mi->name, mi->email);
32 free(mi->name);
33 free(mi->email);
36 static void free_mailmap_entry(void *p, const char *s)
38 struct mailmap_entry *me = (struct mailmap_entry *)p;
39 debug_mm("mailmap: removing entries for <%s>, with %d sub-entries\n", s, me->namemap.nr);
40 debug_mm("mailmap: - simple: '%s' <%s>\n", me->name, me->email);
41 free(me->name);
42 free(me->email);
44 me->namemap.strdup_strings = 1;
45 string_list_clear_func(&me->namemap, free_mailmap_info);
48 static void add_mapping(struct string_list *map,
49 char *new_name, char *new_email, char *old_name, char *old_email)
51 struct mailmap_entry *me;
52 int index;
53 char *p;
55 if (old_email)
56 for (p = old_email; *p; p++)
57 *p = tolower(*p);
58 if (new_email)
59 for (p = new_email; *p; p++)
60 *p = tolower(*p);
62 if (old_email == NULL) {
63 old_email = new_email;
64 new_email = NULL;
67 if ((index = string_list_find_insert_index(map, old_email, 1)) < 0) {
68 /* mailmap entry exists, invert index value */
69 index = -1 - index;
70 } else {
71 /* create mailmap entry */
72 struct string_list_item *item = string_list_insert_at_index(map, index, old_email);
73 item->util = xmalloc(sizeof(struct mailmap_entry));
74 memset(item->util, 0, sizeof(struct mailmap_entry));
75 ((struct mailmap_entry *)item->util)->namemap.strdup_strings = 1;
77 me = (struct mailmap_entry *)map->items[index].util;
79 if (old_name == NULL) {
80 debug_mm("mailmap: adding (simple) entry for %s at index %d\n", old_email, index);
81 /* Replace current name and new email for simple entry */
82 if (new_name) {
83 free(me->name);
84 me->name = xstrdup(new_name);
86 if (new_email) {
87 free(me->email);
88 me->email = xstrdup(new_email);
90 } else {
91 struct mailmap_info *mi = xmalloc(sizeof(struct mailmap_info));
92 debug_mm("mailmap: adding (complex) entry for %s at index %d\n", old_email, index);
93 if (new_name)
94 mi->name = xstrdup(new_name);
95 if (new_email)
96 mi->email = xstrdup(new_email);
97 string_list_insert(&me->namemap, old_name)->util = mi;
100 debug_mm("mailmap: '%s' <%s> -> '%s' <%s>\n",
101 old_name, old_email, new_name, new_email);
104 static char *parse_name_and_email(char *buffer, char **name,
105 char **email, int allow_empty_email)
107 char *left, *right, *nstart, *nend;
108 *name = *email = NULL;
110 if ((left = strchr(buffer, '<')) == NULL)
111 return NULL;
112 if ((right = strchr(left+1, '>')) == NULL)
113 return NULL;
114 if (!allow_empty_email && (left+1 == right))
115 return NULL;
117 /* remove whitespace from beginning and end of name */
118 nstart = buffer;
119 while (isspace(*nstart) && nstart < left)
120 ++nstart;
121 nend = left-1;
122 while (isspace(*nend) && nend > nstart)
123 --nend;
125 *name = (nstart < nend ? nstart : NULL);
126 *email = left+1;
127 *(nend+1) = '\0';
128 *right++ = '\0';
130 return (*right == '\0' ? NULL : right);
133 static int read_single_mailmap(struct string_list *map, const char *filename, char **repo_abbrev)
135 char buffer[1024];
136 FILE *f = (filename == NULL ? NULL : fopen(filename, "r"));
138 if (f == NULL)
139 return 1;
140 while (fgets(buffer, sizeof(buffer), f) != NULL) {
141 char *name1 = NULL, *email1 = NULL, *name2 = NULL, *email2 = NULL;
142 if (buffer[0] == '#') {
143 static const char abbrev[] = "# repo-abbrev:";
144 int abblen = sizeof(abbrev) - 1;
145 int len = strlen(buffer);
147 if (!repo_abbrev)
148 continue;
150 if (len && buffer[len - 1] == '\n')
151 buffer[--len] = 0;
152 if (!strncmp(buffer, abbrev, abblen)) {
153 char *cp;
155 if (repo_abbrev)
156 free(*repo_abbrev);
157 *repo_abbrev = xmalloc(len);
159 for (cp = buffer + abblen; isspace(*cp); cp++)
160 ; /* nothing */
161 strcpy(*repo_abbrev, cp);
163 continue;
165 if ((name2 = parse_name_and_email(buffer, &name1, &email1, 0)) != NULL)
166 parse_name_and_email(name2, &name2, &email2, 1);
168 if (email1)
169 add_mapping(map, name1, email1, name2, email2);
171 fclose(f);
172 return 0;
175 int read_mailmap(struct string_list *map, char **repo_abbrev)
177 map->strdup_strings = 1;
178 /* each failure returns 1, so >1 means both calls failed */
179 return read_single_mailmap(map, ".mailmap", repo_abbrev) +
180 read_single_mailmap(map, git_mailmap_file, repo_abbrev) > 1;
183 void clear_mailmap(struct string_list *map)
185 debug_mm("mailmap: clearing %d entries...\n", map->nr);
186 map->strdup_strings = 1;
187 string_list_clear_func(map, free_mailmap_entry);
188 debug_mm("mailmap: cleared\n");
191 int map_user(struct string_list *map,
192 char *email, int maxlen_email, char *name, int maxlen_name)
194 char *p;
195 struct string_list_item *item;
196 struct mailmap_entry *me;
197 char buf[1024], *mailbuf;
198 int i;
200 /* figure out space requirement for email */
201 p = strchr(email, '>');
202 if (!p) {
203 /* email passed in might not be wrapped in <>, but end with a \0 */
204 p = memchr(email, '\0', maxlen_email);
205 if (!p)
206 return 0;
208 if (p - email + 1 < sizeof(buf))
209 mailbuf = buf;
210 else
211 mailbuf = xmalloc(p - email + 1);
213 /* downcase the email address */
214 for (i = 0; i < p - email; i++)
215 mailbuf[i] = tolower(email[i]);
216 mailbuf[i] = 0;
218 debug_mm("map_user: map '%s' <%s>\n", name, mailbuf);
219 item = string_list_lookup(map, mailbuf);
220 if (item != NULL) {
221 me = (struct mailmap_entry *)item->util;
222 if (me->namemap.nr) {
223 /* The item has multiple items, so we'll look up on name too */
224 /* If the name is not found, we choose the simple entry */
225 struct string_list_item *subitem = string_list_lookup(&me->namemap, name);
226 if (subitem)
227 item = subitem;
230 if (mailbuf != buf)
231 free(mailbuf);
232 if (item != NULL) {
233 struct mailmap_info *mi = (struct mailmap_info *)item->util;
234 if (mi->name == NULL && (mi->email == NULL || maxlen_email == 0)) {
235 debug_mm("map_user: -- (no simple mapping)\n");
236 return 0;
238 if (maxlen_email && mi->email)
239 strlcpy(email, mi->email, maxlen_email);
240 if (maxlen_name && mi->name)
241 strlcpy(name, mi->name, maxlen_name);
242 debug_mm("map_user: to '%s' <%s>\n", name, mi->email ? mi->email : "");
243 return 1;
245 debug_mm("map_user: --\n");
246 return 0;