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 <apelisse@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Antoine Pelisse committed
Jan 5, 2013 at 22:26 UTC
d72fbe8111fbd3083d7063078c96eed27098490c
2 files changed
+72
revision.c
+54
@@ -13,6 +13,7 @@
13
#include "decorate.h"
14
#include "log-tree.h"
15
#include "string-list.h"
16
+#include "mailmap.h"
17
18
volatile show_early_output_fn_t show_early_output;
19
@@ -2219,6 +2220,51 @@ static int rewrite_parents(struct rev_info *revs, struct commit *commit)
2220
return 0;
2221
}
2222
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
+
2268
static int commit_match(struct commit *commit, struct rev_info *opt)
2269
{
2270
int retval;
@@ -2237,6 +2283,14 @@ static int commit_match(struct commit *commit, struct rev_info *opt)
2283
if (buf.len)
2284
strbuf_addstr(&buf, commit->buffer);
2285
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
+
2294
/* Append "fake" message parts as needed */
2295
if (opt->show_notes) {
2296
if (!buf.len)
t/t4203-mailmap.sh
+18
@@ -248,11 +248,29 @@ Author: Other Author <other@author.xx>
248
Author: Some Dude <some@dude.xx>
249
Author: A U Thor <author@example.com>
250
EOF
251
+
252
test_expect_success 'Log output with --use-mailmap' '
253
git log --use-mailmap | grep Author >actual &&
254
test_cmp expect actual
255
'
256
257
+cat >expect <<\EOF
258
+Author: Santa Claus <santa.claus@northpole.xx>
259
+Author: Santa Claus <santa.claus@northpole.xx>
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 "<cto@coompany.xx>" >actual &&
271
+ test_cmp expect actual
272
+'
273
+
274
# git blame
275
cat >expect <<\EOF
276
^OBJI (A U Thor DATE 1) one