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