Skip to content

Commit d72fbe8

Browse files
apelissegitster
authored andcommitted
log: grep author/committer using mailmap
Currently you can use mailmap to display log authors and committers but you can't use the mailmap to find commits with mapped values. This commit allows you to run: git log --use-mailmap --author mapped_name_or_email git log --use-mailmap --committer mapped_name_or_email Of course it only works if the --use-mailmap option is used. The new name and email are copied only when necessary. Signed-off-by: Antoine Pelisse <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d207434 commit d72fbe8

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

revision.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "decorate.h"
1414
#include "log-tree.h"
1515
#include "string-list.h"
16+
#include "mailmap.h"
1617

1718
volatile show_early_output_fn_t show_early_output;
1819

@@ -2219,6 +2220,51 @@ static int rewrite_parents(struct rev_info *revs, struct commit *commit)
22192220
return 0;
22202221
}
22212222

2223+
static int commit_rewrite_person(struct strbuf *buf, const char *what, struct string_list *mailmap)
2224+
{
2225+
char *person, *endp;
2226+
size_t len, namelen, maillen;
2227+
const char *name;
2228+
const char *mail;
2229+
struct ident_split ident;
2230+
2231+
person = strstr(buf->buf, what);
2232+
if (!person)
2233+
return 0;
2234+
2235+
person += strlen(what);
2236+
endp = strchr(person, '\n');
2237+
if (!endp)
2238+
return 0;
2239+
2240+
len = endp - person;
2241+
2242+
if (split_ident_line(&ident, person, len))
2243+
return 0;
2244+
2245+
mail = ident.mail_begin;
2246+
maillen = ident.mail_end - ident.mail_begin;
2247+
name = ident.name_begin;
2248+
namelen = ident.name_end - ident.name_begin;
2249+
2250+
if (map_user(mailmap, &mail, &maillen, &name, &namelen)) {
2251+
struct strbuf namemail = STRBUF_INIT;
2252+
2253+
strbuf_addf(&namemail, "%.*s <%.*s>",
2254+
(int)namelen, name, (int)maillen, mail);
2255+
2256+
strbuf_splice(buf, ident.name_begin - buf->buf,
2257+
ident.mail_end - ident.name_begin + 1,
2258+
namemail.buf, namemail.len);
2259+
2260+
strbuf_release(&namemail);
2261+
2262+
return 1;
2263+
}
2264+
2265+
return 0;
2266+
}
2267+
22222268
static int commit_match(struct commit *commit, struct rev_info *opt)
22232269
{
22242270
int retval;
@@ -2237,6 +2283,14 @@ static int commit_match(struct commit *commit, struct rev_info *opt)
22372283
if (buf.len)
22382284
strbuf_addstr(&buf, commit->buffer);
22392285

2286+
if (opt->mailmap) {
2287+
if (!buf.len)
2288+
strbuf_addstr(&buf, commit->buffer);
2289+
2290+
commit_rewrite_person(&buf, "\nauthor ", opt->mailmap);
2291+
commit_rewrite_person(&buf, "\ncommitter ", opt->mailmap);
2292+
}
2293+
22402294
/* Append "fake" message parts as needed */
22412295
if (opt->show_notes) {
22422296
if (!buf.len)

t/t4203-mailmap.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,29 @@ Author: Other Author <[email protected]>
248248
Author: Some Dude <[email protected]>
249249
Author: A U Thor <[email protected]>
250250
EOF
251+
251252
test_expect_success 'Log output with --use-mailmap' '
252253
git log --use-mailmap | grep Author >actual &&
253254
test_cmp expect actual
254255
'
255256

257+
cat >expect <<\EOF
258+
Author: Santa Claus <[email protected]>
259+
Author: Santa Claus <[email protected]>
260+
EOF
261+
262+
test_expect_success 'Grep author with --use-mailmap' '
263+
git log --use-mailmap --author Santa | grep Author >actual &&
264+
test_cmp expect actual
265+
'
266+
267+
>expect
268+
269+
test_expect_success 'Only grep replaced author with --use-mailmap' '
270+
git log --use-mailmap --author "<[email protected]>" >actual &&
271+
test_cmp expect actual
272+
'
273+
256274
# git blame
257275
cat >expect <<\EOF
258276
^OBJI (A U Thor DATE 1) one

0 commit comments

Comments
 (0)