Skip to content

Commit 0925ce4

Browse files
Marius Storm-Olsengitster
authored andcommitted
Add map_user() and clear_mailmap() to mailmap
map_user() allows to lookup and replace both email and name of a user, based on a new style mailmap file. The possible mailmap definitions are now: proper_name <commit_email> # Old style <proper_email> <commit_email> # New style proper_name <proper_email> <commit_email> # New style proper_name <proper_email> commit_name <commit_email> # New style map_email() operates the same as before, with the exception that it also will to try to match on a name passed in through the name return buffer. clear_mailmap() is needed to now clear the more complex mailmap structure. Signed-off-by: Marius Storm-Olsen <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cfa1ee6 commit 0925ce4

File tree

3 files changed

+236
-51
lines changed

3 files changed

+236
-51
lines changed

Documentation/git-shortlog.txt

Lines changed: 64 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,24 +48,38 @@ OPTIONS
4848
FILES
4949
-----
5050

51-
If a file `.mailmap` exists at the toplevel of the repository, or at the
52-
location pointed to by the log.mailmap configuration option,
53-
it is used to map an author email address to a canonical real name. This
54-
can be used to coalesce together commits by the same person where their
55-
name was spelled differently (whether with the same email address or
56-
not).
57-
58-
Each line in the file consists, in this order, of the canonical real name
59-
of an author, whitespace, and an email address (enclosed by '<' and '>')
60-
to map to the name. Use hash '#' for comments, either on their own line,
61-
or after the email address.
62-
63-
A canonical name may appear in more than one line, associated with
64-
different email addresses, but it doesn't make sense for a given address
65-
to appear more than once (if that happens, a later line overrides the
66-
earlier ones).
67-
68-
So, for example, if your history contains commits by two authors, Jane
51+
If the file `.mailmap` exists at the toplevel of the repository, or at
52+
the location pointed to by the mailmap.file configuration option, it
53+
is used to map author and committer names and email addresses to
54+
canonical real names and email addresses.
55+
This mapping can be used to coalesce together commits by the same
56+
person where their name and/or email address was spelled differently.
57+
58+
In the simple form, each line in the file consists of the canonical
59+
real name of an author, whitespace, and an email address used in the
60+
commit (enclosed by '<' and '>') to map to the name. Thus, looks like
61+
this
62+
--
63+
Proper Name <[email protected]>
64+
--
65+
66+
The more complex forms are
67+
--
68+
69+
--
70+
which allows mailmap to replace only the email part of a commit, and
71+
--
72+
73+
--
74+
which allows mailmap to replace both the name and the email of a
75+
commit matching the specified commit email address, and
76+
--
77+
Proper Name <[email protected]> Commit Name <[email protected]>
78+
--
79+
which allows mailmap to replace both the name and the email of a
80+
commit matching both the specified commit name and email address.
81+
82+
Example 1: Your history contains commits by two authors, Jane
6983
and Joe, whose names appear in the repository under several forms:
7084

7185
------------
@@ -76,16 +90,43 @@ Jane Doe <jane@laptop.(none)>
7690
Jane D. <jane@desktop.(none)>
7791
------------
7892

79-
Then, supposing Joe wants his middle name initial used, and Jane prefers
80-
her family name fully spelled out, a proper `.mailmap` file would look like:
93+
Now suppose that Joe wants his middle name initial used, and Jane
94+
prefers her family name fully spelled out. A proper `.mailmap` file
95+
would look like:
8196

8297
------------
83-
# Note how we don't need an entry for <jane@laptop.(none)>, because the
84-
# real name of that author is correct already, and coalesced directly.
85-
Jane Doe <jane@desktop.(none)>
98+
Jane Doe <jane@desktop.(none)>
8699
Joe R. Developer <[email protected]>
87100
------------
88101

102+
Note how we don't need an entry for <jane@laptop.(none)>, because the
103+
real name of that author is correct already, and coalesced directly.
104+
105+
Example 2: Your repository contains commits from the following
106+
authors:
107+
108+
------------
109+
110+
111+
112+
113+
114+
115+
------------
116+
117+
Then, you might want a `.mailmap` file looking like:
118+
------------
119+
120+
121+
Other Author <[email protected]> nick2 <[email protected]>
122+
123+
124+
------------
125+
126+
Use hash '#' for comments that are either on their own line, or after
127+
the email address.
128+
129+
89130
Author
90131
------
91132
Written by Jeff Garzik <[email protected]>

mailmap.c

Lines changed: 168 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,122 @@
22
#include "string-list.h"
33
#include "mailmap.h"
44

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
11+
512
const char *git_mailmap_file;
13+
14+
struct mailmap_info {
15+
char *name;
16+
char *email;
17+
};
18+
19+
struct mailmap_entry {
20+
/* name and email for the simple mail-only case */
21+
char *name;
22+
char *email;
23+
24+
/* name and email for the complex mail and name matching case */
25+
struct string_list namemap;
26+
};
27+
28+
static void free_mailmap_info(void *p, const char *s)
29+
{
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);
34+
}
35+
36+
static void free_mailmap_entry(void *p, const char *s)
37+
{
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);
43+
44+
me->namemap.strdup_strings = 1;
45+
string_list_clear_func(&me->namemap, free_mailmap_info);
46+
}
47+
48+
static void add_mapping(struct string_list *map,
49+
char *new_name, char *new_email, char *old_name, char *old_email)
50+
{
51+
struct mailmap_entry *me;
52+
int index;
53+
if (old_email == NULL) {
54+
old_email = new_email;
55+
new_email = NULL;
56+
}
57+
58+
if ((index = string_list_find_insert_index(map, old_email, 1)) < 0) {
59+
/* mailmap entry exists, invert index value */
60+
index = -1 - index;
61+
} else {
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;
67+
}
68+
me = (struct mailmap_entry *)map->items[index].util;
69+
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 */
73+
free(me->name);
74+
free(me->email);
75+
if (new_name)
76+
me->name = xstrdup(new_name);
77+
if (new_email)
78+
me->email = xstrdup(new_email);
79+
} else {
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);
82+
if (new_name)
83+
mi->name = xstrdup(new_name);
84+
if (new_email)
85+
mi->email = xstrdup(new_email);
86+
string_list_insert(old_name, &me->namemap)->util = mi;
87+
}
88+
89+
debug_mm("mailmap: '%s' <%s> -> '%s' <%s>\n",
90+
old_name, old_email, new_name, new_email);
91+
}
92+
93+
static char *parse_name_and_email(char *buffer, char **name, char **email)
94+
{
95+
char *left, *right, *nstart, *nend;
96+
*name = *email = 0;
97+
98+
if ((left = strchr(buffer, '<')) == NULL)
99+
return NULL;
100+
if ((right = strchr(left+1, '>')) == NULL)
101+
return NULL;
102+
if (left+1 == right)
103+
return NULL;
104+
105+
/* remove whitespace from beginning and end of name */
106+
nstart = buffer;
107+
while (isspace(*nstart) && nstart < left)
108+
++nstart;
109+
nend = left-1;
110+
while (isspace(*nend) && nend > nstart)
111+
--nend;
112+
113+
*name = (nstart < nend ? nstart : NULL);
114+
*email = left+1;
115+
*(nend+1) = '\0';
116+
*right++ = '\0';
117+
118+
return (*right == '\0' ? NULL : right);
119+
}
120+
6121
static int read_single_mailmap(struct string_list *map, const char *filename, char **repo_abbrev)
7122
{
8123
char buffer[1024];
@@ -11,9 +126,7 @@ static int read_single_mailmap(struct string_list *map, const char *filename, ch
11126
if (f == NULL)
12127
return 1;
13128
while (fgets(buffer, sizeof(buffer), f) != NULL) {
14-
char *end_of_name, *left_bracket, *right_bracket;
15-
char *name, *email;
16-
int i;
129+
char *name1 = 0, *email1 = 0, *name2 = 0, *email2 = 0;
17130
if (buffer[0] == '#') {
18131
static const char abbrev[] = "# repo-abbrev:";
19132
int abblen = sizeof(abbrev) - 1;
@@ -37,48 +150,49 @@ static int read_single_mailmap(struct string_list *map, const char *filename, ch
37150
}
38151
continue;
39152
}
40-
if ((left_bracket = strchr(buffer, '<')) == NULL)
41-
continue;
42-
if ((right_bracket = strchr(left_bracket + 1, '>')) == NULL)
43-
continue;
44-
if (right_bracket == left_bracket + 1)
45-
continue;
46-
for (end_of_name = left_bracket;
47-
end_of_name != buffer && isspace(end_of_name[-1]);
48-
end_of_name--)
49-
; /* keep on looking */
50-
if (end_of_name == buffer)
51-
continue;
52-
name = xmalloc(end_of_name - buffer + 1);
53-
strlcpy(name, buffer, end_of_name - buffer + 1);
54-
email = xmalloc(right_bracket - left_bracket);
55-
for (i = 0; i < right_bracket - left_bracket - 1; i++)
56-
email[i] = tolower(left_bracket[i + 1]);
57-
email[right_bracket - left_bracket - 1] = '\0';
58-
string_list_insert(email, map)->util = name;
153+
if ((name2 = parse_name_and_email(buffer, &name1, &email1)) != NULL)
154+
parse_name_and_email(name2, &name2, &email2);
155+
156+
if (email1)
157+
add_mapping(map, name1, email1, name2, email2);
59158
}
60159
fclose(f);
61160
return 0;
62161
}
63162

64163
int read_mailmap(struct string_list *map, char **repo_abbrev)
65164
{
165+
map->strdup_strings = 1;
66166
/* each failure returns 1, so >1 means both calls failed */
67167
return read_single_mailmap(map, ".mailmap", repo_abbrev) +
68168
read_single_mailmap(map, git_mailmap_file, repo_abbrev) > 1;
69169
}
70170

71-
int map_email(struct string_list *map, const char *email, char *name, int maxlen)
171+
void clear_mailmap(struct string_list *map)
172+
{
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");
177+
}
178+
179+
int map_user(struct string_list *map,
180+
char *email, int maxlen_email, char *name, int maxlen_name)
72181
{
73182
char *p;
74183
struct string_list_item *item;
184+
struct mailmap_entry *me;
75185
char buf[1024], *mailbuf;
76186
int i;
77187

78-
/* autocomplete common developers */
188+
/* figure out space requirement for email */
79189
p = strchr(email, '>');
80-
if (!p)
81-
return 0;
190+
if (!p) {
191+
/* email passed in might not be wrapped in <>, but end with a \0 */
192+
p = memchr(email, '\0', maxlen_email);
193+
if (p == 0)
194+
return 0;
195+
}
82196
if (p - email + 1 < sizeof(buf))
83197
mailbuf = buf;
84198
else
@@ -88,13 +202,39 @@ int map_email(struct string_list *map, const char *email, char *name, int maxlen
88202
for (i = 0; i < p - email; i++)
89203
mailbuf[i] = tolower(email[i]);
90204
mailbuf[i] = 0;
205+
206+
debug_mm("map_user: map '%s' <%s>\n", name, mailbuf);
91207
item = string_list_lookup(mailbuf, map);
208+
if (item != NULL) {
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);
214+
if (subitem)
215+
item = subitem;
216+
}
217+
}
92218
if (mailbuf != buf)
93219
free(mailbuf);
94220
if (item != NULL) {
95-
const char *realname = (const char *)item->util;
96-
strlcpy(name, realname, maxlen);
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");
224+
return 0;
225+
}
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 : "");
97231
return 1;
98232
}
233+
debug_mm("map_user: --\n");
99234
return 0;
100235
}
236+
237+
int map_email(struct string_list *map, const char *email, char *name, int maxlen)
238+
{
239+
return map_user(map, (char *)email, 0, name, maxlen);
240+
}

mailmap.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
#define MAILMAP_H
33

44
int read_mailmap(struct string_list *map, char **repo_abbrev);
5+
void clear_mailmap(struct string_list *map);
6+
57
int map_email(struct string_list *mailmap, const char *email, char *name, int maxlen);
8+
int map_user(struct string_list *mailmap,
9+
char *email, int maxlen_email, char *name, int maxlen_name);
610

711
#endif

0 commit comments

Comments
 (0)