]> git.ipfire.org Git - thirdparty/git.git/blob - mailmap.c
l10n: tr: v2.33 (round 2)
[thirdparty/git.git] / mailmap.c
1 #include "cache.h"
2 #include "string-list.h"
3 #include "mailmap.h"
4 #include "object-store.h"
5
6 #define DEBUG_MAILMAP 0
7 #if DEBUG_MAILMAP
8 #define debug_mm(...) fprintf(stderr, __VA_ARGS__)
9 #define debug_str(X) ((X) ? (X) : "(none)")
10 #else
11 __attribute__((format (printf, 1, 2)))
12 static inline void debug_mm(const char *format, ...) {}
13 static inline const char *debug_str(const char *s) { return s; }
14 #endif
15
16 const char *git_mailmap_file;
17 const char *git_mailmap_blob;
18
19 struct mailmap_info {
20 char *name;
21 char *email;
22 };
23
24 struct mailmap_entry {
25 /* name and email for the simple mail-only case */
26 char *name;
27 char *email;
28
29 /* name and email for the complex mail and name matching case */
30 struct string_list namemap;
31 };
32
33 static void free_mailmap_info(void *p, const char *s)
34 {
35 struct mailmap_info *mi = (struct mailmap_info *)p;
36 debug_mm("mailmap: -- complex: '%s' -> '%s' <%s>\n",
37 s, debug_str(mi->name), debug_str(mi->email));
38 free(mi->name);
39 free(mi->email);
40 }
41
42 static void free_mailmap_entry(void *p, const char *s)
43 {
44 struct mailmap_entry *me = (struct mailmap_entry *)p;
45 debug_mm("mailmap: removing entries for <%s>, with %d sub-entries\n",
46 s, me->namemap.nr);
47 debug_mm("mailmap: - simple: '%s' <%s>\n",
48 debug_str(me->name), debug_str(me->email));
49
50 free(me->name);
51 free(me->email);
52
53 me->namemap.strdup_strings = 1;
54 string_list_clear_func(&me->namemap, free_mailmap_info);
55 }
56
57 /*
58 * On some systems (e.g. MinGW 4.0), string.h has _only_ inline
59 * definition of strcasecmp and no non-inline implementation is
60 * supplied anywhere, which is, eh, "unusual"; we cannot take an
61 * address of such a function to store it in namemap.cmp. This is
62 * here as a workaround---do not assign strcasecmp directly to
63 * namemap.cmp until we know no systems that matter have such an
64 * "unusual" string.h.
65 */
66 static int namemap_cmp(const char *a, const char *b)
67 {
68 return strcasecmp(a, b);
69 }
70
71 static void add_mapping(struct string_list *map,
72 char *new_name, char *new_email,
73 char *old_name, char *old_email)
74 {
75 struct mailmap_entry *me;
76 struct string_list_item *item;
77
78 if (old_email == NULL) {
79 old_email = new_email;
80 new_email = NULL;
81 }
82
83 item = string_list_insert(map, old_email);
84 if (item->util) {
85 me = (struct mailmap_entry *)item->util;
86 } else {
87 CALLOC_ARRAY(me, 1);
88 me->namemap.strdup_strings = 1;
89 me->namemap.cmp = namemap_cmp;
90 item->util = me;
91 }
92
93 if (old_name == NULL) {
94 debug_mm("mailmap: adding (simple) entry for '%s'\n", old_email);
95
96 /* Replace current name and new email for simple entry */
97 if (new_name) {
98 free(me->name);
99 me->name = xstrdup(new_name);
100 }
101 if (new_email) {
102 free(me->email);
103 me->email = xstrdup(new_email);
104 }
105 } else {
106 struct mailmap_info *mi = xcalloc(1, sizeof(struct mailmap_info));
107 debug_mm("mailmap: adding (complex) entry for '%s'\n", old_email);
108 mi->name = xstrdup_or_null(new_name);
109 mi->email = xstrdup_or_null(new_email);
110 string_list_insert(&me->namemap, old_name)->util = mi;
111 }
112
113 debug_mm("mailmap: '%s' <%s> -> '%s' <%s>\n",
114 debug_str(old_name), old_email,
115 debug_str(new_name), debug_str(new_email));
116 }
117
118 static char *parse_name_and_email(char *buffer, char **name,
119 char **email, int allow_empty_email)
120 {
121 char *left, *right, *nstart, *nend;
122 *name = *email = NULL;
123
124 if ((left = strchr(buffer, '<')) == NULL)
125 return NULL;
126 if ((right = strchr(left+1, '>')) == NULL)
127 return NULL;
128 if (!allow_empty_email && (left+1 == right))
129 return NULL;
130
131 /* remove whitespace from beginning and end of name */
132 nstart = buffer;
133 while (isspace(*nstart) && nstart < left)
134 ++nstart;
135 nend = left-1;
136 while (nend > nstart && isspace(*nend))
137 --nend;
138
139 *name = (nstart <= nend ? nstart : NULL);
140 *email = left+1;
141 *(nend+1) = '\0';
142 *right++ = '\0';
143
144 return (*right == '\0' ? NULL : right);
145 }
146
147 static void read_mailmap_line(struct string_list *map, char *buffer)
148 {
149 char *name1 = NULL, *email1 = NULL, *name2 = NULL, *email2 = NULL;
150
151 if (buffer[0] == '#')
152 return;
153
154 if ((name2 = parse_name_and_email(buffer, &name1, &email1, 0)) != NULL)
155 parse_name_and_email(name2, &name2, &email2, 1);
156
157 if (email1)
158 add_mapping(map, name1, email1, name2, email2);
159 }
160
161 /* Flags for read_mailmap_file() */
162 #define MAILMAP_NOFOLLOW (1<<0)
163
164 static int read_mailmap_file(struct string_list *map, const char *filename,
165 unsigned flags)
166 {
167 char buffer[1024];
168 FILE *f;
169 int fd;
170
171 if (!filename)
172 return 0;
173
174 if (flags & MAILMAP_NOFOLLOW)
175 fd = open_nofollow(filename, O_RDONLY);
176 else
177 fd = open(filename, O_RDONLY);
178
179 if (fd < 0) {
180 if (errno == ENOENT)
181 return 0;
182 return error_errno("unable to open mailmap at %s", filename);
183 }
184 f = xfdopen(fd, "r");
185
186 while (fgets(buffer, sizeof(buffer), f) != NULL)
187 read_mailmap_line(map, buffer);
188 fclose(f);
189 return 0;
190 }
191
192 static void read_mailmap_string(struct string_list *map, char *buf)
193 {
194 while (*buf) {
195 char *end = strchrnul(buf, '\n');
196
197 if (*end)
198 *end++ = '\0';
199
200 read_mailmap_line(map, buf);
201 buf = end;
202 }
203 }
204
205 static int read_mailmap_blob(struct string_list *map, const char *name)
206 {
207 struct object_id oid;
208 char *buf;
209 unsigned long size;
210 enum object_type type;
211
212 if (!name)
213 return 0;
214 if (get_oid(name, &oid) < 0)
215 return 0;
216
217 buf = read_object_file(&oid, &type, &size);
218 if (!buf)
219 return error("unable to read mailmap object at %s", name);
220 if (type != OBJ_BLOB)
221 return error("mailmap is not a blob: %s", name);
222
223 read_mailmap_string(map, buf);
224
225 free(buf);
226 return 0;
227 }
228
229 int read_mailmap(struct string_list *map)
230 {
231 int err = 0;
232
233 map->strdup_strings = 1;
234 map->cmp = namemap_cmp;
235
236 if (!git_mailmap_blob && is_bare_repository())
237 git_mailmap_blob = "HEAD:.mailmap";
238
239 if (!startup_info->have_repository || !is_bare_repository())
240 err |= read_mailmap_file(map, ".mailmap",
241 startup_info->have_repository ?
242 MAILMAP_NOFOLLOW : 0);
243 if (startup_info->have_repository)
244 err |= read_mailmap_blob(map, git_mailmap_blob);
245 err |= read_mailmap_file(map, git_mailmap_file, 0);
246 return err;
247 }
248
249 void clear_mailmap(struct string_list *map)
250 {
251 debug_mm("mailmap: clearing %d entries...\n", map->nr);
252 map->strdup_strings = 1;
253 string_list_clear_func(map, free_mailmap_entry);
254 debug_mm("mailmap: cleared\n");
255 }
256
257 /*
258 * Look for an entry in map that match string[0:len]; string[len]
259 * does not have to be NUL (but it could be).
260 */
261 static struct string_list_item *lookup_prefix(struct string_list *map,
262 const char *string, size_t len)
263 {
264 int i = string_list_find_insert_index(map, string, 1);
265 if (i < 0) {
266 /* exact match */
267 i = -1 - i;
268 if (!string[len])
269 return &map->items[i];
270 /*
271 * that map entry matches exactly to the string, including
272 * the cruft at the end beyond "len". That is not a match
273 * with string[0:len] that we are looking for.
274 */
275 } else if (!string[len]) {
276 /*
277 * asked with the whole string, and got nothing. No
278 * matching entry can exist in the map.
279 */
280 return NULL;
281 }
282
283 /*
284 * i is at the exact match to an overlong key, or location the
285 * overlong key would be inserted, which must come after the
286 * real location of the key if one exists.
287 */
288 while (0 <= --i && i < map->nr) {
289 int cmp = strncasecmp(map->items[i].string, string, len);
290 if (cmp < 0)
291 /*
292 * "i" points at a key definitely below the prefix;
293 * the map does not have string[0:len] in it.
294 */
295 break;
296 else if (!cmp && !map->items[i].string[len])
297 /* found it */
298 return &map->items[i];
299 /*
300 * otherwise, the string at "i" may be string[0:len]
301 * followed by a string that sorts later than string[len:];
302 * keep trying.
303 */
304 }
305 return NULL;
306 }
307
308 int map_user(struct string_list *map,
309 const char **email, size_t *emaillen,
310 const char **name, size_t *namelen)
311 {
312 struct string_list_item *item;
313 struct mailmap_entry *me;
314
315 debug_mm("map_user: map '%.*s' <%.*s>\n",
316 (int)*namelen, debug_str(*name),
317 (int)*emaillen, debug_str(*email));
318
319 item = lookup_prefix(map, *email, *emaillen);
320 if (item != NULL) {
321 me = (struct mailmap_entry *)item->util;
322 if (me->namemap.nr) {
323 /*
324 * The item has multiple items, so we'll look up on
325 * name too. If the name is not found, we choose the
326 * simple entry.
327 */
328 struct string_list_item *subitem;
329 subitem = lookup_prefix(&me->namemap, *name, *namelen);
330 if (subitem)
331 item = subitem;
332 }
333 }
334 if (item != NULL) {
335 struct mailmap_info *mi = (struct mailmap_info *)item->util;
336 if (mi->name == NULL && mi->email == NULL) {
337 debug_mm("map_user: -- (no simple mapping)\n");
338 return 0;
339 }
340 if (mi->email) {
341 *email = mi->email;
342 *emaillen = strlen(*email);
343 }
344 if (mi->name) {
345 *name = mi->name;
346 *namelen = strlen(*name);
347 }
348 debug_mm("map_user: to '%.*s' <%.*s>\n",
349 (int)*namelen, debug_str(*name),
350 (int)*emaillen, debug_str(*email));
351 return 1;
352 }
353 debug_mm("map_user: --\n");
354 return 0;
355 }