2 #include "string-list.h"
5 #define DEBUG_MAILMAP 0
7 #define debug_mm(...) fprintf(stderr, __VA_ARGS__)
9 static inline void debug_mm(const char *format
, ...) {}
12 const char *git_mailmap_file
;
19 struct mailmap_entry
{
20 /* name and email for the simple mail-only case */
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
);
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
);
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
;
53 if (old_email
== NULL
) {
54 old_email
= new_email
;
58 if ((index
= string_list_find_insert_index(map
, old_email
, 1)) < 0) {
59 /* mailmap entry exists, invert index value */
62 /* create mailmap entry */
63 struct string_list_item
*item
= string_list_insert_at_index(index
, old_email
, map
);
64 item
->util
= xmalloc(sizeof(struct mailmap_entry
));
65 memset(item
->util
, 0, sizeof(struct mailmap_entry
));
66 ((struct mailmap_entry
*)item
->util
)->namemap
.strdup_strings
= 1;
68 me
= (struct mailmap_entry
*)map
->items
[index
].util
;
70 if (old_name
== NULL
) {
71 debug_mm("mailmap: adding (simple) entry for %s at index %d\n", old_email
, index
);
72 /* Replace current name and new email for simple entry */
76 me
->name
= xstrdup(new_name
);
78 me
->email
= xstrdup(new_email
);
80 struct mailmap_info
*mi
= xmalloc(sizeof(struct mailmap_info
));
81 debug_mm("mailmap: adding (complex) entry for %s at index %d\n", old_email
, index
);
83 mi
->name
= xstrdup(new_name
);
85 mi
->email
= xstrdup(new_email
);
86 string_list_insert(old_name
, &me
->namemap
)->util
= mi
;
89 debug_mm("mailmap: '%s' <%s> -> '%s' <%s>\n",
90 old_name
, old_email
, new_name
, new_email
);
93 static char *parse_name_and_email(char *buffer
, char **name
, char **email
)
95 char *left
, *right
, *nstart
, *nend
;
98 if ((left
= strchr(buffer
, '<')) == NULL
)
100 if ((right
= strchr(left
+1, '>')) == NULL
)
105 /* remove whitespace from beginning and end of name */
107 while (isspace(*nstart
) && nstart
< left
)
110 while (isspace(*nend
) && nend
> nstart
)
113 *name
= (nstart
< nend
? nstart
: NULL
);
118 return (*right
== '\0' ? NULL
: right
);
121 static int read_single_mailmap(struct string_list
*map
, const char *filename
, char **repo_abbrev
)
124 FILE *f
= (filename
== NULL
? NULL
: fopen(filename
, "r"));
128 while (fgets(buffer
, sizeof(buffer
), f
) != NULL
) {
129 char *name1
= 0, *email1
= 0, *name2
= 0, *email2
= 0;
130 if (buffer
[0] == '#') {
131 static const char abbrev
[] = "# repo-abbrev:";
132 int abblen
= sizeof(abbrev
) - 1;
133 int len
= strlen(buffer
);
138 if (len
&& buffer
[len
- 1] == '\n')
140 if (!strncmp(buffer
, abbrev
, abblen
)) {
145 *repo_abbrev
= xmalloc(len
);
147 for (cp
= buffer
+ abblen
; isspace(*cp
); cp
++)
149 strcpy(*repo_abbrev
, cp
);
153 if ((name2
= parse_name_and_email(buffer
, &name1
, &email1
)) != NULL
)
154 parse_name_and_email(name2
, &name2
, &email2
);
157 add_mapping(map
, name1
, email1
, name2
, email2
);
163 int read_mailmap(struct string_list
*map
, char **repo_abbrev
)
165 map
->strdup_strings
= 1;
166 /* each failure returns 1, so >1 means both calls failed */
167 return read_single_mailmap(map
, ".mailmap", repo_abbrev
) +
168 read_single_mailmap(map
, git_mailmap_file
, repo_abbrev
) > 1;
171 void clear_mailmap(struct string_list
*map
)
173 debug_mm("mailmap: clearing %d entries...\n", map
->nr
);
174 map
->strdup_strings
= 1;
175 string_list_clear_func(map
, free_mailmap_entry
);
176 debug_mm("mailmap: cleared\n");
179 int map_user(struct string_list
*map
,
180 char *email
, int maxlen_email
, char *name
, int maxlen_name
)
183 struct string_list_item
*item
;
184 struct mailmap_entry
*me
;
185 char buf
[1024], *mailbuf
;
188 /* figure out space requirement for email */
189 p
= strchr(email
, '>');
191 /* email passed in might not be wrapped in <>, but end with a \0 */
192 p
= memchr(email
, '\0', maxlen_email
);
196 if (p
- email
+ 1 < sizeof(buf
))
199 mailbuf
= xmalloc(p
- email
+ 1);
201 /* downcase the email address */
202 for (i
= 0; i
< p
- email
; i
++)
203 mailbuf
[i
] = tolower(email
[i
]);
206 debug_mm("map_user: map '%s' <%s>\n", name
, mailbuf
);
207 item
= string_list_lookup(mailbuf
, map
);
209 me
= (struct mailmap_entry
*)item
->util
;
210 if (me
->namemap
.nr
) {
211 /* The item has multiple items, so we'll look up on name too */
212 /* If the name is not found, we choose the simple entry */
213 struct string_list_item
*subitem
= string_list_lookup(name
, &me
->namemap
);
221 struct mailmap_info
*mi
= (struct mailmap_info
*)item
->util
;
222 if (mi
->name
== NULL
&& (mi
->email
== NULL
|| maxlen_email
== 0)) {
223 debug_mm("map_user: -- (no simple mapping)\n");
226 if (maxlen_email
&& mi
->email
)
227 strlcpy(email
, mi
->email
, maxlen_email
);
228 if (maxlen_name
&& mi
->name
)
229 strlcpy(name
, mi
->name
, maxlen_name
);
230 debug_mm("map_user: to '%s' <%s>\n", name
, mi
->email
? mi
->email
: "");
233 debug_mm("map_user: --\n");
237 int map_email(struct string_list
*map
, const char *email
, char *name
, int maxlen
)
239 return map_user(map
, (char *)email
, 0, name
, maxlen
);