2 #include "string-list.h"
5 #define DEBUG_MAILMAP 0
7 #define debug_mm(...) fprintf(stderr, __VA_ARGS__)
8 #define debug_str(X) ((X) ? (X) : "(none)")
10 static inline void debug_mm(const char *format
, ...) {}
11 static inline const char *debug_str(const char *s
) { return s
; }
14 const char *git_mailmap_file
;
15 const char *git_mailmap_blob
;
22 struct mailmap_entry
{
23 /* name and email for the simple mail-only case */
27 /* name and email for the complex mail and name matching case */
28 struct string_list namemap
;
31 static void free_mailmap_info(void *p
, const char *s
)
33 struct mailmap_info
*mi
= (struct mailmap_info
*)p
;
34 debug_mm("mailmap: -- complex: '%s' -> '%s' <%s>\n", s
, debug_str(mi
->name
), debug_str(mi
->email
));
39 static void free_mailmap_entry(void *p
, const char *s
)
41 struct mailmap_entry
*me
= (struct mailmap_entry
*)p
;
42 debug_mm("mailmap: removing entries for <%s>, with %d sub-entries\n", s
, me
->namemap
.nr
);
43 debug_mm("mailmap: - simple: '%s' <%s>\n", debug_str(me
->name
), debug_str(me
->email
));
48 me
->namemap
.strdup_strings
= 1;
49 string_list_clear_func(&me
->namemap
, free_mailmap_info
);
52 static void add_mapping(struct string_list
*map
,
53 char *new_name
, char *new_email
, char *old_name
, char *old_email
)
55 struct mailmap_entry
*me
;
58 if (old_email
== NULL
) {
59 old_email
= new_email
;
63 if ((index
= string_list_find_insert_index(map
, old_email
, 1)) < 0) {
64 /* mailmap entry exists, invert index value */
66 me
= (struct mailmap_entry
*)map
->items
[index
].util
;
68 /* create mailmap entry */
69 struct string_list_item
*item
;
71 item
= string_list_insert_at_index(map
, index
, old_email
);
72 me
= xcalloc(1, sizeof(struct mailmap_entry
));
73 me
->namemap
.strdup_strings
= 1;
74 me
->namemap
.cmp
= strcasecmp
;
78 if (old_name
== NULL
) {
79 debug_mm("mailmap: adding (simple) entry for %s at index %d\n", old_email
, index
);
80 /* Replace current name and new email for simple entry */
83 me
->name
= xstrdup(new_name
);
87 me
->email
= xstrdup(new_email
);
90 struct mailmap_info
*mi
= xcalloc(1, sizeof(struct mailmap_info
));
91 debug_mm("mailmap: adding (complex) entry for %s at index %d\n", old_email
, index
);
93 mi
->name
= xstrdup(new_name
);
95 mi
->email
= xstrdup(new_email
);
96 string_list_insert(&me
->namemap
, old_name
)->util
= mi
;
99 debug_mm("mailmap: '%s' <%s> -> '%s' <%s>\n",
100 debug_str(old_name
), old_email
, debug_str(new_name
), debug_str(new_email
));
103 static char *parse_name_and_email(char *buffer
, char **name
,
104 char **email
, int allow_empty_email
)
106 char *left
, *right
, *nstart
, *nend
;
107 *name
= *email
= NULL
;
109 if ((left
= strchr(buffer
, '<')) == NULL
)
111 if ((right
= strchr(left
+1, '>')) == NULL
)
113 if (!allow_empty_email
&& (left
+1 == right
))
116 /* remove whitespace from beginning and end of name */
118 while (isspace(*nstart
) && nstart
< left
)
121 while (nend
> nstart
&& isspace(*nend
))
124 *name
= (nstart
<= nend
? nstart
: NULL
);
129 return (*right
== '\0' ? NULL
: right
);
132 static void read_mailmap_line(struct string_list
*map
, char *buffer
,
135 char *name1
= NULL
, *email1
= NULL
, *name2
= NULL
, *email2
= NULL
;
136 if (buffer
[0] == '#') {
137 static const char abbrev
[] = "# repo-abbrev:";
138 int abblen
= sizeof(abbrev
) - 1;
139 int len
= strlen(buffer
);
144 if (len
&& buffer
[len
- 1] == '\n')
146 if (!strncmp(buffer
, abbrev
, abblen
)) {
151 *repo_abbrev
= xmalloc(len
);
153 for (cp
= buffer
+ abblen
; isspace(*cp
); cp
++)
155 strcpy(*repo_abbrev
, cp
);
159 if ((name2
= parse_name_and_email(buffer
, &name1
, &email1
, 0)) != NULL
)
160 parse_name_and_email(name2
, &name2
, &email2
, 1);
163 add_mapping(map
, name1
, email1
, name2
, email2
);
166 static int read_mailmap_file(struct string_list
*map
, const char *filename
,
175 f
= fopen(filename
, "r");
179 return error("unable to open mailmap at %s: %s",
180 filename
, strerror(errno
));
183 while (fgets(buffer
, sizeof(buffer
), f
) != NULL
)
184 read_mailmap_line(map
, buffer
, repo_abbrev
);
189 static void read_mailmap_buf(struct string_list
*map
,
190 const char *buf
, unsigned long len
,
194 const char *end
= strchrnul(buf
, '\n');
195 unsigned long linelen
= end
- buf
+ 1;
196 char *line
= xmemdupz(buf
, linelen
);
198 read_mailmap_line(map
, line
, repo_abbrev
);
206 static int read_mailmap_blob(struct string_list
*map
,
210 unsigned char sha1
[20];
213 enum object_type type
;
217 if (get_sha1(name
, sha1
) < 0)
220 buf
= read_sha1_file(sha1
, &type
, &size
);
222 return error("unable to read mailmap object at %s", name
);
223 if (type
!= OBJ_BLOB
)
224 return error("mailmap is not a blob: %s", name
);
226 read_mailmap_buf(map
, buf
, size
, repo_abbrev
);
232 int read_mailmap(struct string_list
*map
, char **repo_abbrev
)
236 map
->strdup_strings
= 1;
237 map
->cmp
= strcasecmp
;
239 if (!git_mailmap_blob
&& is_bare_repository())
240 git_mailmap_blob
= "HEAD:.mailmap";
242 err
|= read_mailmap_file(map
, ".mailmap", repo_abbrev
);
243 err
|= read_mailmap_blob(map
, git_mailmap_blob
, repo_abbrev
);
244 err
|= read_mailmap_file(map
, git_mailmap_file
, repo_abbrev
);
248 void clear_mailmap(struct string_list
*map
)
250 debug_mm("mailmap: clearing %d entries...\n", map
->nr
);
251 map
->strdup_strings
= 1;
252 string_list_clear_func(map
, free_mailmap_entry
);
253 debug_mm("mailmap: cleared\n");
257 * Look for an entry in map that match string[0:len]; string[len]
258 * does not have to be NUL (but it could be).
260 static struct string_list_item
*lookup_prefix(struct string_list
*map
,
261 const char *string
, size_t len
)
263 int i
= string_list_find_insert_index(map
, string
, 1);
268 return &map
->items
[i
];
270 * that map entry matches exactly to the string, including
271 * the cruft at the end beyond "len". That is not a match
272 * with string[0:len] that we are looking for.
274 } else if (!string
[len
]) {
276 * asked with the whole string, and got nothing. No
277 * matching entry can exist in the map.
283 * i is at the exact match to an overlong key, or location the
284 * overlong key would be inserted, which must come after the
285 * real location of the key if one exists.
287 while (0 <= --i
&& i
< map
->nr
) {
288 int cmp
= strncasecmp(map
->items
[i
].string
, string
, len
);
291 * "i" points at a key definitely below the prefix;
292 * the map does not have string[0:len] in it.
295 else if (!cmp
&& !map
->items
[i
].string
[len
])
297 return &map
->items
[i
];
299 * otherwise, the string at "i" may be string[0:len]
300 * followed by a string that sorts later than string[len:];
307 int map_user(struct string_list
*map
,
308 const char **email
, size_t *emaillen
,
309 const char **name
, size_t *namelen
)
311 struct string_list_item
*item
;
312 struct mailmap_entry
*me
;
314 debug_mm("map_user: map '%.*s' <%.*s>\n",
315 (int)*namelen
, debug_str(*name
), (int)*emaillen
, debug_str(*email
));
317 item
= lookup_prefix(map
, *email
, *emaillen
);
319 me
= (struct mailmap_entry
*)item
->util
;
320 if (me
->namemap
.nr
) {
321 /* The item has multiple items, so we'll look up on name too */
322 /* If the name is not found, we choose the simple entry */
323 struct string_list_item
*subitem
;
324 subitem
= lookup_prefix(&me
->namemap
, *name
, *namelen
);
330 struct mailmap_info
*mi
= (struct mailmap_info
*)item
->util
;
331 if (mi
->name
== NULL
&& mi
->email
== NULL
) {
332 debug_mm("map_user: -- (no simple mapping)\n");
337 *emaillen
= strlen(*email
);
341 *namelen
= strlen(*name
);
343 debug_mm("map_user: to '%.*s' <%.*s>\n", (int)*namelen
, debug_str(*name
),
344 (int)*emaillen
, debug_str(*email
));
347 debug_mm("map_user: --\n");