1#!/bin/sh
2
3test_description='Test notes trees that also contain non-notes'
4
5GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8. ./test-lib.sh
9
10number_of_commits=100
11
12start_note_commit () {
13	test_tick &&
14	cat <<INPUT_END
15commit refs/notes/commits
16committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
17data <<COMMIT
18notes
19COMMIT
20
21from refs/notes/commits^0
22deleteall
23INPUT_END
24
25}
26
27verify_notes () {
28	git log | grep "^    " > output &&
29	i=$number_of_commits &&
30	while [ $i -gt 0 ]; do
31		echo "    commit #$i" &&
32		echo "    note for commit #$i" &&
33		i=$(($i-1));
34	done > expect &&
35	test_cmp expect output
36}
37
38test_expect_success "setup: create a couple of commits" '
39
40	test_tick &&
41	cat <<INPUT_END >input &&
42commit refs/heads/main
43committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
44data <<COMMIT
45commit #1
46COMMIT
47
48M 644 inline file
49data <<EOF
50file in commit #1
51EOF
52
53INPUT_END
54
55	test_tick &&
56	cat <<INPUT_END >>input &&
57commit refs/heads/main
58committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
59data <<COMMIT
60commit #2
61COMMIT
62
63M 644 inline file
64data <<EOF
65file in commit #2
66EOF
67
68INPUT_END
69	git fast-import --quiet <input
70'
71
72test_expect_success "create a notes tree with both notes and non-notes" '
73
74	commit1=$(git rev-parse refs/heads/main^) &&
75	commit2=$(git rev-parse refs/heads/main) &&
76	test_tick &&
77	cat <<INPUT_END >input &&
78commit refs/notes/commits
79committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
80data <<COMMIT
81notes commit #1
82COMMIT
83
84N inline $commit1
85data <<EOF
86note for commit #1
87EOF
88
89N inline $commit2
90data <<EOF
91note for commit #2
92EOF
93
94INPUT_END
95	test_tick &&
96	cat <<INPUT_END >>input &&
97commit refs/notes/commits
98committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
99data <<COMMIT
100notes commit #2
101COMMIT
102
103M 644 inline foobar/non-note.txt
104data <<EOF
105A non-note in a notes tree
106EOF
107
108N inline $commit2
109data <<EOF
110edited note for commit #2
111EOF
112
113INPUT_END
114	test_tick &&
115	cat <<INPUT_END >>input &&
116commit refs/notes/commits
117committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
118data <<COMMIT
119notes commit #3
120COMMIT
121
122N inline $commit1
123data <<EOF
124edited note for commit #1
125EOF
126
127M 644 inline deadbeef
128data <<EOF
129non-note with SHA1-like name
130EOF
131
132M 644 inline de/adbeef
133data <<EOF
134another non-note with SHA1-like name
135EOF
136
137M 644 inline de/adbeefdeadbeefdeadbeefdeadbeefdeadbeef
138data <<EOF
139This is actually a valid note, albeit to a non-existing object.
140It is needed in order to trigger the "mishandling" of the dead/beef non-note.
141EOF
142
143M 644 inline dead/beef
144data <<EOF
145yet another non-note with SHA1-like name
146EOF
147
148INPUT_END
149	git fast-import --quiet <input &&
150	git config core.notesRef refs/notes/commits
151'
152
153cat >expect <<EXPECT_END
154    commit #2
155    edited note for commit #2
156    commit #1
157    edited note for commit #1
158EXPECT_END
159
160test_expect_success "verify contents of notes" '
161
162	git log | grep "^    " > actual &&
163	test_cmp expect actual
164'
165
166cat >expect_nn1 <<EXPECT_END
167A non-note in a notes tree
168EXPECT_END
169cat >expect_nn2 <<EXPECT_END
170non-note with SHA1-like name
171EXPECT_END
172cat >expect_nn3 <<EXPECT_END
173another non-note with SHA1-like name
174EXPECT_END
175cat >expect_nn4 <<EXPECT_END
176yet another non-note with SHA1-like name
177EXPECT_END
178
179test_expect_success "verify contents of non-notes" '
180
181	git cat-file -p refs/notes/commits:foobar/non-note.txt > actual_nn1 &&
182	test_cmp expect_nn1 actual_nn1 &&
183	git cat-file -p refs/notes/commits:deadbeef > actual_nn2 &&
184	test_cmp expect_nn2 actual_nn2 &&
185	git cat-file -p refs/notes/commits:de/adbeef > actual_nn3 &&
186	test_cmp expect_nn3 actual_nn3 &&
187	git cat-file -p refs/notes/commits:dead/beef > actual_nn4 &&
188	test_cmp expect_nn4 actual_nn4
189'
190
191test_expect_success "git-notes preserves non-notes" '
192
193	test_tick &&
194	git notes add -f -m "foo bar"
195'
196
197test_expect_success "verify contents of non-notes after git-notes" '
198
199	git cat-file -p refs/notes/commits:foobar/non-note.txt > actual_nn1 &&
200	test_cmp expect_nn1 actual_nn1 &&
201	git cat-file -p refs/notes/commits:deadbeef > actual_nn2 &&
202	test_cmp expect_nn2 actual_nn2 &&
203	git cat-file -p refs/notes/commits:de/adbeef > actual_nn3 &&
204	test_cmp expect_nn3 actual_nn3 &&
205	git cat-file -p refs/notes/commits:dead/beef > actual_nn4 &&
206	test_cmp expect_nn4 actual_nn4
207'
208
209test_done
210