Skip to content

Commit c81b3ab

Browse files
KarthikNayakgitster
authored andcommitted
refs: add GIT_REFERENCE_BACKEND to specify reference backend
Git allows setting a different object directory via 'GIT_OBJECT_DIRECTORY', but provides no equivalent for references. In the previous commit we extended the 'extensions.refStorage' config to also support an URI input for reference backend with location. Let's also add a new environment variable 'GIT_REFERENCE_BACKEND' that takes in the same input as the config variable. Having an environment variable allows us to modify the reference backend and location on the fly for individual git commands. Helped-by: Jean-Noël Avila <jn.avila@free.fr> Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 825b9b0 commit c81b3ab

4 files changed

Lines changed: 88 additions & 34 deletions

File tree

Documentation/git.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,11 @@ double-quotes and respecting backslash escapes. E.g., the value
584584
repositories will be set to this value. The default is "files".
585585
See `--ref-format` in linkgit:git-init[1].
586586

587+
`GIT_REFERENCE_BACKEND`::
588+
Specify which reference backend to be used along with its URI.
589+
See `extensions.refStorage` option in linkgit:git-config[1] for more
590+
description. Overrides the config variable when used.
591+
587592
Git Commits
588593
~~~~~~~~~~~
589594
`GIT_AUTHOR_NAME`::

environment.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#define GIT_OPTIONAL_LOCKS_ENVIRONMENT "GIT_OPTIONAL_LOCKS"
4343
#define GIT_TEXT_DOMAIN_DIR_ENVIRONMENT "GIT_TEXTDOMAINDIR"
4444
#define GIT_ATTR_SOURCE_ENVIRONMENT "GIT_ATTR_SOURCE"
45+
#define GIT_REFERENCE_BACKEND_ENVIRONMENT "GIT_REFERENCE_BACKEND"
4546

4647
/*
4748
* Environment variable used to propagate the --no-advice global option to the

setup.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1838,6 +1838,7 @@ const char *setup_git_directory_gently(int *nongit_ok)
18381838
static struct strbuf cwd = STRBUF_INIT;
18391839
struct strbuf dir = STRBUF_INIT, gitdir = STRBUF_INIT, report = STRBUF_INIT;
18401840
const char *prefix = NULL;
1841+
const char *ref_backend_uri;
18411842
struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT;
18421843

18431844
/*
@@ -1995,6 +1996,25 @@ const char *setup_git_directory_gently(int *nongit_ok)
19951996
setenv(GIT_PREFIX_ENVIRONMENT, "", 1);
19961997
}
19971998

1999+
/*
2000+
* The env variable should override the repository config
2001+
* for 'extensions.refStorage'.
2002+
*/
2003+
ref_backend_uri = getenv(GIT_REFERENCE_BACKEND_ENVIRONMENT);
2004+
if (ref_backend_uri) {
2005+
char *backend, *location;
2006+
enum ref_storage_format format;
2007+
2008+
parse_reference_uri(ref_backend_uri, &backend, &location);
2009+
format = ref_storage_format_by_name(backend);
2010+
if (format == REF_STORAGE_FORMAT_UNKNOWN)
2011+
die(_("unknown ref storage format: '%s'"), backend);
2012+
repo_set_ref_storage_format(the_repository, format, location);
2013+
2014+
free(backend);
2015+
free(location);
2016+
}
2017+
19982018
setup_original_cwd();
19992019

20002020
strbuf_release(&dir);

t/t1423-ref-backend.sh

Lines changed: 62 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,25 @@ test_description='Test reference backend URIs'
1111
# <backend> is the original ref storage of the repo.
1212
# <uri> is the new URI to be set for the ref storage.
1313
# <cmd> is the git subcommand to be run in the repository.
14+
# <via> if 'config', set the backend via the 'extensions.refStorage' config.
15+
# if 'env', set the backend via the 'GIT_REFERENCE_BACKEND' env.
1416
run_with_uri() {
1517
repo=$1 &&
1618
backend=$2 &&
1719
uri=$3 &&
1820
cmd=$4 &&
21+
via=$5 &&
1922

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"
23+
git -C "$repo" config set core.repositoryformatversion 1 &&
24+
if test "$via" = "env"
25+
then
26+
test_env GIT_REFERENCE_BACKEND="$uri" git -C "$repo" $cmd
27+
elif test "$via" = "config"
28+
then
29+
git -C "$repo" config set extensions.refStorage "$uri" &&
30+
git -C "$repo" $cmd &&
31+
git -C "$repo" config set extensions.refStorage "$backend"
32+
fi
2433
}
2534

2635
# Test a repository with a given reference storage by running and comparing
@@ -30,44 +39,57 @@ run_with_uri() {
3039
# <repo> is the relative path to the repo to run the command in.
3140
# <backend> is the original ref storage of the repo.
3241
# <uri> is the new URI to be set for the ref storage.
42+
# <via> if 'config', set the backend via the 'extensions.refStorage' config.
43+
# if 'env', set the backend via the 'GIT_REFERENCE_BACKEND' env.
3344
# <err_msg> (optional) if set, check if 'git-refs(1)' failed with the provided msg.
3445
test_refs_backend() {
3546
repo=$1 &&
3647
backend=$2 &&
3748
uri=$3 &&
38-
err_msg=$4 &&
49+
via=$4 &&
50+
err_msg=$5 &&
51+
3952

40-
git -C "$repo" config set core.repositoryformatversion 1 &&
4153
if test -n "$err_msg";
4254
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
55+
if test "$via" = "env"
56+
then
57+
test_env GIT_REFERENCE_BACKEND="$uri" test_must_fail git -C "$repo" refs list 2>err
58+
elif test "$via" = "config"
59+
then
60+
git -C "$repo" config set extensions.refStorage "$uri" &&
61+
test_must_fail git -C "$repo" refs list 2>err &&
62+
test_grep "$err_msg" err
63+
fi
4664
else
4765
git -C "$repo" refs list >expect &&
48-
run_with_uri "$repo" "$backend" "$uri" "refs list" >actual &&
66+
run_with_uri "$repo" "$backend" "$uri" "refs list" "$via">actual &&
4967
test_cmp expect actual
5068
fi
5169
}
5270

53-
test_expect_success 'URI is invalid' '
71+
methods="config"
72+
for method in $methods
73+
do
74+
75+
test_expect_success "$method: URI is invalid" '
5476
test_when_finished "rm -rf repo" &&
5577
git init repo &&
56-
test_refs_backend repo files "reftable@/home/reftable" \
78+
test_refs_backend repo files "reftable@/home/reftable" "$method" \
5779
"invalid value for ${SQ}extensions.refstorage${SQ}"
5880
'
5981

60-
test_expect_success 'URI ends with colon' '
82+
test_expect_success "$method: URI ends with colon" '
6183
test_when_finished "rm -rf repo" &&
6284
git init repo &&
63-
test_refs_backend repo files "reftable:" \
85+
test_refs_backend repo files "reftable:" "$method" \
6486
"invalid value for ${SQ}extensions.refstorage${SQ}"
6587
'
6688

67-
test_expect_success 'unknown reference backend' '
89+
test_expect_success "$method: unknown reference backend" '
6890
test_when_finished "rm -rf repo" &&
6991
git init repo &&
70-
test_refs_backend repo files "db://.git" \
92+
test_refs_backend repo files "db://.git" "$method" \
7193
"invalid value for ${SQ}extensions.refstorage${SQ}"
7294
'
7395

@@ -86,7 +108,7 @@ do
86108
for dir in "$(pwd)/repo/.git" "./"
87109
do
88110

89-
test_expect_success "$read from $to_format backend, $dir dir" '
111+
test_expect_success "$method: $read from $to_format backend, $dir dir" '
90112
test_when_finished "rm -rf repo" &&
91113
git init --ref-format=$from_format repo &&
92114
(
@@ -101,7 +123,7 @@ do
101123
)
102124
'
103125

104-
test_expect_success "$write to $to_format backend, $dir dir" '
126+
test_expect_success "$method: $write to $to_format backend, $dir dir" '
105127
test_when_finished "rm -rf repo" &&
106128
git init --ref-format=$from_format repo &&
107129
(
@@ -113,20 +135,22 @@ do
113135
git refs migrate --dry-run --ref-format=$to_format >out &&
114136
BACKEND_PATH="$dir/$(sed "s/.* ${SQ}.git\/\(.*\)${SQ}/\1/" out)" &&
115137
116-
test_refs_backend . $from_format "$to_format://$BACKEND_PATH" &&
138+
test_refs_backend . $from_format "$to_format://$BACKEND_PATH" "$method" &&
117139
118140
git refs list >expect &&
119-
run_with_uri . "$from_format" "$to_format://$BACKEND_PATH" "tag -d 1" &&
141+
run_with_uri . "$from_format" "$to_format://$BACKEND_PATH" \
142+
"tag -d 1" "$method" &&
120143
git refs list >actual &&
121144
test_cmp expect actual &&
122145
123146
git refs list | grep -v "refs/tags/1" >expect &&
124-
run_with_uri . "$from_format" "$to_format://$BACKEND_PATH" "refs list" >actual &&
147+
run_with_uri . "$from_format" "$to_format://$BACKEND_PATH" \
148+
"refs list" "$method" >actual &&
125149
test_cmp expect actual
126150
)
127151
'
128152

129-
test_expect_success "with worktree and $to_format backend, $dir dir" '
153+
test_expect_success "$method: with worktree and $to_format backend, $dir dir" '
130154
test_when_finished "rm -rf repo wt" &&
131155
git init --ref-format=$from_format repo &&
132156
(
@@ -138,22 +162,26 @@ do
138162
git refs migrate --dry-run --ref-format=$to_format >out &&
139163
BACKEND_PATH="$dir/$(sed "s/.* ${SQ}.git\/\(.*\)${SQ}/\1/" out)" &&
140164
141-
git config set core.repositoryformatversion 1 &&
142-
git config set extensions.refStorage "$to_format://$BACKEND_PATH" &&
165+
run_with_uri . "$from_format" "$to_format://$BACKEND_PATH" \
166+
"worktree add ../wt 2" "$method" &&
143167
144-
git worktree add ../wt 2
145-
) &&
168+
run_with_uri . "$from_format" "$to_format://$BACKEND_PATH" \
169+
"for-each-ref --include-root-refs" "$method" >actual &&
170+
run_with_uri ../wt "$from_format" "$to_format://$BACKEND_PATH" \
171+
"for-each-ref --include-root-refs" "$method" >expect &&
172+
! test_cmp expect actual &&
146173
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
174+
run_with_uri . "$from_format" "$to_format://$BACKEND_PATH" \
175+
"rev-parse 2" "$method" >actual &&
176+
run_with_uri ../wt "$from_format" "$to_format://$BACKEND_PATH" \
177+
"rev-parse HEAD" "$method" >expect &&
178+
test_cmp expect actual
179+
)
154180
'
155181
done # closes dir
156182
done # closes to_format
157-
done # closes from_format
183+
done # closes to_format
184+
185+
done # closes method
158186

159187
test_done

0 commit comments

Comments
 (0)