t8001/t8002 (blame): modernize style
[git] / t / annotate-tests.sh
1 # This file isn't used as a test script directly, instead it is
2 # sourced from t8001-annotate.sh and t8002-blame.sh.
3
4 check_count () {
5         head= &&
6         case "$1" in -h) head="$2"; shift; shift ;; esac &&
7         echo "$PROG file $head" >&4 &&
8         $PROG file $head >actual &&
9         perl -e '
10                 my %expect = (@ARGV);
11                 my %count = map { $_ => 0 } keys %expect;
12                 while (<STDIN>) {
13                         if (/^[0-9a-f]+\t\(([^\t]+)\t/) {
14                                 my $author = $1;
15                                 for ($author) { s/^\s*//; s/\s*$//; }
16                                 $count{$author}++;
17                         }
18                 }
19                 my $bad = 0;
20                 while (my ($author, $count) = each %count) {
21                         my $ok;
22                         my $value = 0;
23                         $value = $expect{$author} if defined $expect{$author};
24                         if ($value != $count) {
25                                 $bad = 1;
26                                 $ok = "bad";
27                         }
28                         else {
29                                 $ok = "good";
30                         }
31                         print STDERR "Author $author (expected $value, attributed $count) $ok\n";
32                 }
33                 exit($bad);
34         ' "$@" <actual
35 }
36
37 test_expect_success 'setup A lines' '
38         echo "1A quick brown fox jumps over the" >file &&
39         echo "lazy dog" >>file &&
40         git add file &&
41         GIT_AUTHOR_NAME="A" GIT_AUTHOR_EMAIL="A@test.git" \
42         git commit -a -m "Initial."
43 '
44
45 test_expect_success 'blame 1 author' '
46         check_count A 2
47 '
48
49 test_expect_success 'setup B lines' '
50         echo "2A quick brown fox jumps over the" >>file &&
51         echo "lazy dog" >>file &&
52         GIT_AUTHOR_NAME="B" GIT_AUTHOR_EMAIL="B@test.git" \
53         git commit -a -m "Second."
54 '
55
56 test_expect_success 'blame 2 authors' '
57         check_count A 2 B 2
58 '
59
60 test_expect_success 'setup B1 lines (branch1)' '
61         git checkout -b branch1 master &&
62         echo "3A slow green fox jumps into the" >>file &&
63         echo "well." >>file &&
64         GIT_AUTHOR_NAME="B1" GIT_AUTHOR_EMAIL="B1@test.git" \
65         git commit -a -m "Branch1-1"
66 '
67
68 test_expect_success 'blame 2 authors + 1 branch1 author' '
69         check_count A 2 B 2 B1 2
70 '
71
72 test_expect_success 'setup B2 lines (branch2)' '
73         git checkout -b branch2 master &&
74         sed -e "s/2A quick brown/4A quick brown lazy dog/" <file >file.new &&
75         mv file.new file &&
76         GIT_AUTHOR_NAME="B2" GIT_AUTHOR_EMAIL="B2@test.git" \
77         git commit -a -m "Branch2-1"
78 '
79
80 test_expect_success 'blame 2 authors + 1 branch2 author' '
81         check_count A 2 B 1 B2 1
82 '
83
84 test_expect_success 'merge branch1 & branch2' '
85         git pull . branch1
86 '
87
88 test_expect_success 'blame 2 authors + 2 merged-in authors' '
89         check_count A 2 B 1 B1 2 B2 1
90 '
91
92 test_expect_success 'blame ancestor' '
93         check_count -h master A 2 B 2
94 '
95
96 test_expect_success 'blame great-ancestor' '
97         check_count -h master^ A 2
98 '
99
100 test_expect_success 'setup evil merge' '
101         echo "evil merge." >>file &&
102         git commit -a --amend
103 '
104
105 test_expect_success 'blame evil merge' '
106         check_count A 2 B 1 B1 2 B2 1 "A U Thor" 1
107 '
108
109 test_expect_success 'setup incomplete line' '
110         echo "incomplete" | tr -d "\\012" >>file &&
111         GIT_AUTHOR_NAME="C" GIT_AUTHOR_EMAIL="C@test.git" \
112         git commit -a -m "Incomplete"
113 '
114
115 test_expect_success 'blame incomplete line' '
116         check_count A 2 B 1 B1 2 B2 1 "A U Thor" 1 C 1
117 '
118
119 test_expect_success 'setup edits' '
120         mv file file.orig &&
121         {
122                 cat file.orig &&
123                 echo
124         } | sed -e "s/^3A/99/" -e "/^1A/d" -e "/^incomplete/d" >file &&
125         echo "incomplete" | tr -d "\\012" >>file &&
126         GIT_AUTHOR_NAME="D" GIT_AUTHOR_EMAIL="D@test.git" \
127         git commit -a -m "edit"
128 '
129
130 test_expect_success 'blame edits' '
131         check_count A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1
132 '
133
134 test_expect_success 'setup obfuscated email' '
135         echo "No robots allowed" >file.new &&
136         cat file >>file.new &&
137         mv file.new file &&
138         GIT_AUTHOR_NAME="E" GIT_AUTHOR_EMAIL="E at test dot git" \
139         git commit -a -m "norobots"
140 '
141
142 test_expect_success 'blame obfuscated email' '
143         check_count A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1 E 1
144 '