Raw
1 #!/bin/sh
2
3 test_description='git blame'
4 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
5 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
6
7 TEST_CREATE_REPO_NO_TEMPLATE=1
8 . ./test-lib.sh
9
10 if ! test_have_prereq PERL_TEST_HELPERS
11 then
12 skip_all='skipping blame colors tests; Perl not available'
13 test_done
14 fi
15
16 PROG='git blame -c'
17 . "$TEST_DIRECTORY"/annotate-tests.sh
18
19 test_expect_success 'setup' '
20 hexsz=$(test_oid hexsz)
21 '
22
23 test_expect_success 'blame untracked file in empty repo' '
24 >untracked &&
25 test_must_fail git blame untracked
26 '
27
28 PROG='git blame -c -e'
29 test_expect_success 'blame --show-email' '
30 check_count \
31 "<A@test.git>" 1 \
32 "<B@test.git>" 1 \
33 "<B1@test.git>" 1 \
34 "<B2@test.git>" 1 \
35 "<author@example.com>" 1 \
36 "<C@test.git>" 1 \
37 "<D@test.git>" 1 \
38 "<E at test dot git>" 1
39 '
40
41 test_expect_success 'setup showEmail tests' '
42 echo "bin: test number 1" >one &&
43 git add one &&
44 GIT_AUTHOR_NAME=name1 \
45 GIT_AUTHOR_EMAIL=email1@test.git \
46 git commit -m First --date="2010-01-01 01:00:00" &&
47 cat >expected_n <<-\EOF &&
48 (name1 2010-01-01 01:00:00 +0000 1) bin: test number 1
49 EOF
50 cat >expected_e <<-\EOF
51 (<email1@test.git> 2010-01-01 01:00:00 +0000 1) bin: test number 1
52 EOF
53 '
54
55 find_blame () {
56 sed -e 's/^[^(]*//'
57 }
58
59 test_expect_success 'blame with no options and no config' '
60 git blame one >blame &&
61 find_blame <blame >result &&
62 test_cmp expected_n result
63 '
64
65 test_expect_success 'blame with showemail options' '
66 git blame --show-email one >blame1 &&
67 find_blame <blame1 >result &&
68 test_cmp expected_e result &&
69 git blame -e one >blame2 &&
70 find_blame <blame2 >result &&
71 test_cmp expected_e result &&
72 git blame --no-show-email one >blame3 &&
73 find_blame <blame3 >result &&
74 test_cmp expected_n result
75 '
76
77 test_expect_success 'blame with showEmail config false' '
78 git config blame.showEmail false &&
79 git blame one >blame1 &&
80 find_blame <blame1 >result &&
81 test_cmp expected_n result &&
82 git blame --show-email one >blame2 &&
83 find_blame <blame2 >result &&
84 test_cmp expected_e result &&
85 git blame -e one >blame3 &&
86 find_blame <blame3 >result &&
87 test_cmp expected_e result &&
88 git blame --no-show-email one >blame4 &&
89 find_blame <blame4 >result &&
90 test_cmp expected_n result
91 '
92
93 test_expect_success 'blame with showEmail config true' '
94 git config blame.showEmail true &&
95 git blame one >blame1 &&
96 find_blame <blame1 >result &&
97 test_cmp expected_e result &&
98 git blame --no-show-email one >blame2 &&
99 find_blame <blame2 >result &&
100 test_cmp expected_n result
101 '
102
103 test_expect_success 'set up abbrev tests' '
104 test_commit abbrev &&
105 sha1=$(git rev-parse --verify HEAD) &&
106 check_abbrev () {
107 expect=$1 && shift &&
108 echo $sha1 | cut -c 1-$expect >expect &&
109 git blame "$@" abbrev.t >actual &&
110 sed -n "s/^[\^]\{0,1\}\([0-9a-f][0-9a-f]*\).*/\1/p" actual >actual.sha &&
111 test_cmp expect actual.sha
112 }
113 '
114
115 test_expect_success 'blame --abbrev=<n> works' '
116 check_abbrev 30 --abbrev=30 HEAD &&
117 check_abbrev 30 --abbrev=30 ^HEAD
118 '
119
120 test_expect_success 'blame -l aligns regular and boundary commits' '
121 check_abbrev $hexsz -l HEAD &&
122 check_abbrev $((hexsz - 1)) -l ^HEAD
123 '
124
125 test_expect_success 'blame --abbrev with full length behaves like -l' '
126 check_abbrev $hexsz --abbrev=$hexsz HEAD &&
127 check_abbrev $((hexsz - 1)) --abbrev=$hexsz ^HEAD
128 '
129
130 test_expect_success '--no-abbrev works like --abbrev with full length' '
131 check_abbrev $hexsz --no-abbrev
132 '
133
134 test_expect_success 'blame --abbrev gets truncated' '
135 check_abbrev $hexsz --abbrev=9000 HEAD
136 '
137
138 test_expect_success 'blame --abbrev gets truncated with boundary commit' '
139 check_abbrev $hexsz --abbrev=9000 ^HEAD
140 '
141
142 test_expect_success 'blame --abbrev -b truncates the blank boundary' '
143 cat >expect <<-EOF &&
144 $(printf "%10s" "") (<author@example.com> 2005-04-07 15:45:13 -0700 1) abbrev
145 EOF
146 git blame -b --abbrev=10 ^HEAD -- abbrev.t >actual &&
147 test_cmp expect actual
148 '
149
150 test_expect_success 'blame with excessive --abbrev and -b culls to hash length' '
151 cat >expect <<-EOF &&
152 $(printf "%${hexsz}s" "") (<author@example.com> 2005-04-07 15:45:13 -0700 1) abbrev
153 EOF
154 git blame -b --abbrev=9000 ^HEAD -- abbrev.t >actual &&
155 test_cmp expect actual
156 '
157
158 test_expect_success '--exclude-promisor-objects does not BUG-crash' '
159 test_must_fail git blame --exclude-promisor-objects one
160 '
161
162 test_expect_success 'blame with uncommitted edits in partial clone does not crash' '
163 git init server &&
164 echo foo >server/file.txt &&
165 git -C server add file.txt &&
166 git -C server commit -m file &&
167
168 git clone --filter=blob:none "file://$(pwd)/server" client &&
169 echo bar >>client/file.txt &&
170 git -C client blame file.txt
171 '
172
173 test_done