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 # non-boundary commits get +1 for alignment
117 check_abbrev 31 --abbrev=30 HEAD &&
118 check_abbrev 30 --abbrev=30 ^HEAD
119 '
120
121 test_expect_success 'blame -l aligns regular and boundary commits' '
122 check_abbrev $hexsz -l HEAD &&
123 check_abbrev $((hexsz - 1)) -l ^HEAD
124 '
125
126 test_expect_success 'blame --abbrev with full length behaves like -l' '
127 check_abbrev $hexsz --abbrev=$hexsz HEAD &&
128 check_abbrev $((hexsz - 1)) --abbrev=$hexsz ^HEAD
129 '
130
131 test_expect_success '--no-abbrev works like --abbrev with full length' '
132 check_abbrev $hexsz --no-abbrev
133 '
134
135 test_expect_success 'blame --abbrev gets truncated' '
136 check_abbrev $hexsz --abbrev=9000 HEAD
137 '
138
139 test_expect_success 'blame --abbrev gets truncated with boundary commit' '
140 check_abbrev $hexsz --abbrev=9000 ^HEAD
141 '
142
143 test_expect_success 'blame --abbrev -b truncates the blank boundary' '
144 # Note that `--abbrev=` always gets incremented by 1, which is why we
145 # expect 11 leading spaces and not 10.
146 cat >expect <<-EOF &&
147 $(printf "%11s" "") (<author@example.com> 2005-04-07 15:45:13 -0700 1) abbrev
148 EOF
149 git blame -b --abbrev=10 ^HEAD -- abbrev.t >actual &&
150 test_cmp expect actual
151 '
152
153 test_expect_success 'blame with excessive --abbrev and -b culls to hash length' '
154 cat >expect <<-EOF &&
155 $(printf "%${hexsz}s" "") (<author@example.com> 2005-04-07 15:45:13 -0700 1) abbrev
156 EOF
157 git blame -b --abbrev=9000 ^HEAD -- abbrev.t >actual &&
158 test_cmp expect actual
159 '
160
161 test_expect_success '--exclude-promisor-objects does not BUG-crash' '
162 test_must_fail git blame --exclude-promisor-objects one
163 '
164
165 test_expect_success 'blame with uncommitted edits in partial clone does not crash' '
166 git init server &&
167 echo foo >server/file.txt &&
168 git -C server add file.txt &&
169 git -C server commit -m file &&
170
171 git clone --filter=blob:none "file://$(pwd)/server" client &&
172 echo bar >>client/file.txt &&
173 git -C client blame file.txt
174 '
175
176 test_done