Skip to content

Commit 1b8cdce

Browse files
lilyballgitster
authored andcommitted
blame: Add option to show author email instead of name
Add a new option -e (or --show-email) to git-blame that will display the author's email instead of name on each line. This option works for both git-blame and git-annotate. Signed-off-by: Kevin Ballard <kevin@sb.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 38a1887 commit 1b8cdce

File tree

4 files changed

+38
-13
lines changed

4 files changed

+38
-13
lines changed

Documentation/git-blame.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ git-blame - Show what revision and author last modified each line of a file
88
SYNOPSIS
99
--------
1010
[verse]
11-
'git blame' [-c] [-b] [-l] [--root] [-t] [-f] [-n] [-s] [-p] [-w] [--incremental] [-L n,m]
11+
'git blame' [-c] [-b] [-l] [--root] [-t] [-f] [-n] [-s] [-e] [-p] [-w] [--incremental] [-L n,m]
1212
[-S <revs-file>] [-M] [-C] [-C] [-C] [--since=<date>]
1313
[<rev> | --contents <file> | --reverse <rev>] [--] <file>
1414

@@ -65,6 +65,10 @@ include::blame-options.txt[]
6565
-s::
6666
Suppress the author name and timestamp from the output.
6767

68+
-e::
69+
--show-email::
70+
Show the author email instead of author name (Default: off).
71+
6872
-w::
6973
Ignore whitespace when comparing the parent's version and
7074
the child's to find where the lines came from.

builtin/blame.c

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,6 +1606,7 @@ static const char *format_time(unsigned long time, const char *tz_str,
16061606
#define OUTPUT_SHOW_NUMBER 040
16071607
#define OUTPUT_SHOW_SCORE 0100
16081608
#define OUTPUT_NO_AUTHOR 0200
1609+
#define OUTPUT_SHOW_EMAIL 0400
16091610

16101611
static void emit_porcelain(struct scoreboard *sb, struct blame_entry *ent)
16111612
{
@@ -1671,12 +1672,17 @@ static void emit_other(struct scoreboard *sb, struct blame_entry *ent, int opt)
16711672
}
16721673

16731674
printf("%.*s", length, hex);
1674-
if (opt & OUTPUT_ANNOTATE_COMPAT)
1675-
printf("\t(%10s\t%10s\t%d)", ci.author,
1675+
if (opt & OUTPUT_ANNOTATE_COMPAT) {
1676+
const char *name;
1677+
if (opt & OUTPUT_SHOW_EMAIL)
1678+
name = ci.author_mail;
1679+
else
1680+
name = ci.author;
1681+
printf("\t(%10s\t%10s\t%d)", name,
16761682
format_time(ci.author_time, ci.author_tz,
16771683
show_raw_time),
16781684
ent->lno + 1 + cnt);
1679-
else {
1685+
} else {
16801686
if (opt & OUTPUT_SHOW_SCORE)
16811687
printf(" %*d %02d",
16821688
max_score_digits, ent->score,
@@ -1689,9 +1695,15 @@ static void emit_other(struct scoreboard *sb, struct blame_entry *ent, int opt)
16891695
ent->s_lno + 1 + cnt);
16901696

16911697
if (!(opt & OUTPUT_NO_AUTHOR)) {
1692-
int pad = longest_author - utf8_strwidth(ci.author);
1698+
const char *name;
1699+
int pad;
1700+
if (opt & OUTPUT_SHOW_EMAIL)
1701+
name = ci.author_mail;
1702+
else
1703+
name = ci.author;
1704+
pad = longest_author - utf8_strwidth(name);
16931705
printf(" (%s%*s %10s",
1694-
ci.author, pad, "",
1706+
name, pad, "",
16951707
format_time(ci.author_time,
16961708
ci.author_tz,
16971709
show_raw_time));
@@ -1829,7 +1841,10 @@ static void find_alignment(struct scoreboard *sb, int *option)
18291841
if (!(suspect->commit->object.flags & METAINFO_SHOWN)) {
18301842
suspect->commit->object.flags |= METAINFO_SHOWN;
18311843
get_commit_info(suspect->commit, &ci, 1);
1832-
num = utf8_strwidth(ci.author);
1844+
if (*option & OUTPUT_SHOW_EMAIL)
1845+
num = utf8_strwidth(ci.author_mail);
1846+
else
1847+
num = utf8_strwidth(ci.author);
18331848
if (longest_author < num)
18341849
longest_author = num;
18351850
}
@@ -2278,6 +2293,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
22782293
OPT_BIT('t', NULL, &output_option, "Show raw timestamp (Default: off)", OUTPUT_RAW_TIMESTAMP),
22792294
OPT_BIT('l', NULL, &output_option, "Show long commit SHA1 (Default: off)", OUTPUT_LONG_OBJECT_NAME),
22802295
OPT_BIT('s', NULL, &output_option, "Suppress author name and timestamp (Default: off)", OUTPUT_NO_AUTHOR),
2296+
OPT_BIT('e', "show-email", &output_option, "Show author email instead of name (Default: off)", OUTPUT_SHOW_EMAIL),
22812297
OPT_BIT('w', NULL, &xdl_opts, "Ignore whitespace differences", XDF_IGNORE_WHITESPACE),
22822298
OPT_STRING('S', NULL, &revs_file, "file", "Use revisions from <file> instead of calling git-rev-list"),
22832299
OPT_STRING(0, "contents", &contents_from, "file", "Use <file>'s contents as the final image"),

t/annotate-tests.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ test_expect_success \
3939
'echo "1A quick brown fox jumps over the" >file &&
4040
echo "lazy dog" >>file &&
4141
git add file
42-
GIT_AUTHOR_NAME="A" git commit -a -m "Initial."'
42+
GIT_AUTHOR_NAME="A" GIT_AUTHOR_EMAIL="A@test.git" git commit -a -m "Initial."'
4343

4444
test_expect_success \
4545
'check all lines blamed on A' \
@@ -49,7 +49,7 @@ test_expect_success \
4949
'Setup new lines blamed on B' \
5050
'echo "2A quick brown fox jumps over the" >>file &&
5151
echo "lazy dog" >> file &&
52-
GIT_AUTHOR_NAME="B" git commit -a -m "Second."'
52+
GIT_AUTHOR_NAME="B" GIT_AUTHOR_EMAIL="B@test.git" git commit -a -m "Second."'
5353

5454
test_expect_success \
5555
'Two lines blamed on A, two on B' \
@@ -60,7 +60,7 @@ test_expect_success \
6060
'git checkout -b branch1 master &&
6161
echo "3A slow green fox jumps into the" >> file &&
6262
echo "well." >> file &&
63-
GIT_AUTHOR_NAME="B1" git commit -a -m "Branch1-1"'
63+
GIT_AUTHOR_NAME="B1" GIT_AUTHOR_EMAIL="B1@test.git" git commit -a -m "Branch1-1"'
6464

6565
test_expect_success \
6666
'Two lines blamed on A, two on B, two on B1' \
@@ -71,7 +71,7 @@ test_expect_success \
7171
'git checkout -b branch2 master &&
7272
sed -e "s/2A quick brown/4A quick brown lazy dog/" < file > file.new &&
7373
mv file.new file &&
74-
GIT_AUTHOR_NAME="B2" git commit -a -m "Branch2-1"'
74+
GIT_AUTHOR_NAME="B2" GIT_AUTHOR_EMAIL="B2@test.git" git commit -a -m "Branch2-1"'
7575

7676
test_expect_success \
7777
'Two lines blamed on A, one on B, one on B2' \
@@ -105,7 +105,7 @@ test_expect_success \
105105
test_expect_success \
106106
'an incomplete line added' \
107107
'echo "incomplete" | tr -d "\\012" >>file &&
108-
GIT_AUTHOR_NAME="C" git commit -a -m "Incomplete"'
108+
GIT_AUTHOR_NAME="C" GIT_AUTHOR_EMAIL="C@test.git" git commit -a -m "Incomplete"'
109109

110110
test_expect_success \
111111
'With incomplete lines.' \
@@ -119,7 +119,7 @@ test_expect_success \
119119
echo
120120
} | sed -e "s/^3A/99/" -e "/^1A/d" -e "/^incomplete/d" > file &&
121121
echo "incomplete" | tr -d "\\012" >>file &&
122-
GIT_AUTHOR_NAME="D" git commit -a -m "edit"'
122+
GIT_AUTHOR_NAME="D" GIT_AUTHOR_EMAIL="D@test.git" git commit -a -m "edit"'
123123

124124
test_expect_success \
125125
'some edit' \

t/t8002-blame.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,9 @@ test_description='git blame'
66
PROG='git blame -c'
77
. "$TEST_DIRECTORY"/annotate-tests.sh
88

9+
PROG='git blame -c -e'
10+
test_expect_success 'Blame --show-email works' '
11+
check_count "<A@test.git>" 1 "<B@test.git>" 1 "<B1@test.git>" 1 "<B2@test.git>" 1 "<author@example.com>" 1 "<C@test.git>" 1 "<D@test.git>" 1
12+
'
13+
914
test_done

0 commit comments

Comments
 (0)