1
+#!/bin/sh
2
+
3
+test_description='Test reference backend URIs'
4
+
5
+. ./test-lib.sh
6
+
7
+# Run a git command with the provided reference storage. Reset the backend
8
+# post running the command.
9
+# Usage: run_with_uri <repo> <backend> <uri> <cmd>
10
+# <repo> is the relative path to the repo to run the command in.
11
+# <backend> is the original ref storage of the repo.
12
+# <uri> is the new URI to be set for the ref storage.
13
+# <cmd> is the git subcommand to be run in the repository.
14
+run_with_uri () {
15
+ repo=$1 &&
16
+ backend=$2 &&
17
+ uri=$3 &&
18
+ cmd=$4 &&
19
+
20
+ git -C "$repo" config set core.repositoryformatversion 1
21
+ git -C "$repo" config set extensions.refStorage "$uri" &&
22
+ git -C "$repo" $cmd &&
23
+ git -C "$repo" config set extensions.refStorage "$backend"
24
+}
25
+
26
+# Test a repository with a given reference storage by running and comparing
27
+# 'git refs list' before and after setting the new reference backend. If
28
+# err_msg is set, expect the command to fail and grep for the provided err_msg.
29
+# Usage: run_with_uri <repo> <backend> <uri> <cmd>
30
+# <repo> is the relative path to the repo to run the command in.
31
+# <backend> is the original ref storage of the repo.
32
+# <uri> is the new URI to be set for the ref storage.
33
+# <err_msg> (optional) if set, check if 'git-refs(1)' failed with the provided msg.
34
+test_refs_backend () {
35
+ repo=$1 &&
36
+ backend=$2 &&
37
+ uri=$3 &&
38
+ err_msg=$4 &&
39
+
40
+ git -C "$repo" config set core.repositoryformatversion 1 &&
41
+ if test -n "$err_msg";
42
+ then
43
+ git -C "$repo" config set extensions.refStorage "$uri" &&
44
+ test_must_fail git -C "$repo" refs list 2>err &&
45
+ test_grep "$err_msg" err
46
+ else
47
+ git -C "$repo" refs list >expect &&
48
+ run_with_uri "$repo" "$backend" "$uri" "refs list" >actual &&
49
+ test_cmp expect actual
50
+ fi
51
+}
52
+
53
+test_expect_success 'URI is invalid' '
54
+ test_when_finished "rm -rf repo" &&
55
+ git init repo &&
56
+ test_refs_backend repo files "reftable@/home/reftable" \
57
+ "invalid value for ${SQ}extensions.refstorage${SQ}"
58
+'
59
+
60
+test_expect_success 'URI ends with colon' '
61
+ test_when_finished "rm -rf repo" &&
62
+ git init repo &&
63
+ test_refs_backend repo files "reftable:" \
64
+ "invalid value for ${SQ}extensions.refstorage${SQ}"
65
+'
66
+
67
+test_expect_success 'unknown reference backend' '
68
+ test_when_finished "rm -rf repo" &&
69
+ git init repo &&
70
+ test_refs_backend repo files "db://.git" \
71
+ "invalid value for ${SQ}extensions.refstorage${SQ}"
72
+'
73
+
74
+ref_formats="files reftable"
75
+for from_format in $ref_formats
76
+do
77
+
78
+for to_format in $ref_formats
79
+do
80
+ if test "$from_format" = "$to_format"
81
+ then
82
+ continue
83
+ fi
84
+
85
+
86
+ for dir in "$(pwd)/repo/.git" "."
87
+ do
88
+
89
+ test_expect_success "read from $to_format backend, $dir dir" '
90
+ test_when_finished "rm -rf repo" &&
91
+ git init --ref-format=$from_format repo &&
92
+ (
93
+ cd repo &&
94
+ test_commit 1 &&
95
+ test_commit 2 &&
96
+ test_commit 3 &&
97
+
98
+ git refs migrate --dry-run --ref-format=$to_format >out &&
99
+ BACKEND_PATH="$dir/$(sed "s/.* ${SQ}.git\/\(.*\)${SQ}/\1/" out)" &&
100
+ test_refs_backend . $from_format "$to_format://$BACKEND_PATH" "$method"
101
+ )
102
+ '
103
+
104
+ test_expect_success "write to $to_format backend, $dir dir" '
105
+ test_when_finished "rm -rf repo" &&
106
+ git init --ref-format=$from_format repo &&
107
+ (
108
+ cd repo &&
109
+ test_commit 1 &&
110
+ test_commit 2 &&
111
+ test_commit 3 &&
112
+
113
+ git refs migrate --dry-run --ref-format=$to_format >out &&
114
+ BACKEND_PATH="$dir/$(sed "s/.* ${SQ}.git\/\(.*\)${SQ}/\1/" out)" &&
115
+
116
+ test_refs_backend . $from_format "$to_format://$BACKEND_PATH" &&
117
+
118
+ git refs list >expect &&
119
+ run_with_uri . "$from_format" "$to_format://$BACKEND_PATH" "tag -d 1" &&
120
+ git refs list >actual &&
121
+ test_cmp expect actual &&
122
+
123
+ git refs list | grep -v "refs/tags/1" >expect &&
124
+ run_with_uri . "$from_format" "$to_format://$BACKEND_PATH" "refs list" >actual &&
125
+ test_cmp expect actual
126
+ )
127
+ '
128
+
129
+ test_expect_success "with worktree and $to_format backend, $dir dir" '
130
+ test_when_finished "rm -rf repo wt" &&
131
+ git init --ref-format=$from_format repo &&
132
+ (
133
+ cd repo &&
134
+ test_commit 1 &&
135
+ test_commit 2 &&
136
+ test_commit 3 &&
137
+
138
+ git refs migrate --dry-run --ref-format=$to_format >out &&
139
+ BACKEND_PATH="$dir/$(sed "s/.* ${SQ}.git\/\(.*\)${SQ}/\1/" out)" &&
140
+
141
+ git config set core.repositoryformatversion 1 &&
142
+ git config set extensions.refStorage "$to_format://$BACKEND_PATH" &&
143
+
144
+ git worktree add ../wt 2
145
+ ) &&
146
+
147
+ git -C repo for-each-ref --include-root-refs >expect &&
148
+ git -C wt for-each-ref --include-root-refs >expect &&
149
+ ! test_cmp expect actual &&
150
+
151
+ git -C wt rev-parse 2 >expect &&
152
+ git -C wt rev-parse HEAD >actual &&
153
+ test_cmp expect actual
154
+ '
155
+ done # closes dir
156
+done # closes to_format
157
+done # closes from_format
158
+
159
+test_done