introduce GIT_INDEX_VERSION environment variable

Respect a GIT_INDEX_VERSION environment variable, when a new index is initialized. Setting the environment variable will not cause existing index files to be converted to another format, but will only affect newly written index files. This can be used to initialize repositories with index-v4. Helped-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Thomas Gummerer committed Feb 23, 2014 at 21:49 UTC 136347d718320c56030e3b7a3437259e99c4c41b
3 files changed +74 -1
Documentation/git.txt
+5
@@ -720,6 +720,11 @@ Git so take care if using Cogito etc.
720 index file. If not specified, the default of `$GIT_DIR/index`
721 is used.
722
723 +'GIT_INDEX_VERSION'::
724 + This environment variable allows the specification of an index
725 + version for new repositories. It won't affect existing index
726 + files. By default index file version [23] is used.
727 +
728 'GIT_OBJECT_DIRECTORY'::
729 If the object storage directory is specified via this
730 environment variable then the sha1 directories are created
read-cache.c
+20 -1
@@ -1219,6 +1219,25 @@ static struct cache_entry *refresh_cache_entry(struct cache_entry *ce, int reall
1219
1220 #define INDEX_FORMAT_DEFAULT 3
1221
1222 +static unsigned int get_index_format_default(void)
1223 +{
1224 + char *envversion = getenv("GIT_INDEX_VERSION");
1225 + if (!envversion) {
1226 + return INDEX_FORMAT_DEFAULT;
1227 + } else {
1228 + char *endp;
1229 + unsigned int version = strtoul(envversion, &endp, 10);
1230 +
1231 + if (*endp ||
1232 + version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < version) {
1233 + warning(_("GIT_INDEX_VERSION set, but the value is invalid.\n"
1234 + "Using version %i"), INDEX_FORMAT_DEFAULT);
1235 + version = INDEX_FORMAT_DEFAULT;
1236 + }
1237 + return version;
1238 + }
1239 +}
1240 +
1241 /*
1242 * dev/ino/uid/gid/size are also just tracked to the low 32 bits
1243 * Again - this is just a (very strong in practice) heuristic that
@@ -1795,7 +1814,7 @@ int write_index(struct index_state *istate, int newfd)
1814 }
1815
1816 if (!istate->version)
1798 - istate->version = INDEX_FORMAT_DEFAULT;
1817 + istate->version = get_index_format_default();
1818
1819 /* demote version 3 to version 2 when the latter suffices */
1820 if (istate->version == 3 || istate->version == 2)
t/t1600-index.sh new
+49
@@ -0,0 +1,49 @@
1 +#!/bin/sh
2 +
3 +test_description='index file specific tests'
4 +
5 +. ./test-lib.sh
6 +
7 +test_expect_success 'setup' '
8 + echo 1 >a
9 +'
10 +
11 +test_expect_success 'bogus GIT_INDEX_VERSION issues warning' '
12 + (
13 + rm -f .git/index &&
14 + GIT_INDEX_VERSION=2bogus &&
15 + export GIT_INDEX_VERSION &&
16 + git add a 2>&1 | sed "s/[0-9]//" >actual.err &&
17 + sed -e "s/ Z$/ /" <<-\EOF >expect.err &&
18 + warning: GIT_INDEX_VERSION set, but the value is invalid.
19 + Using version Z
20 + EOF
21 + test_i18ncmp expect.err actual.err
22 + )
23 +'
24 +
25 +test_expect_success 'out of bounds GIT_INDEX_VERSION issues warning' '
26 + (
27 + rm -f .git/index &&
28 + GIT_INDEX_VERSION=1 &&
29 + export GIT_INDEX_VERSION &&
30 + git add a 2>&1 | sed "s/[0-9]//" >actual.err &&
31 + sed -e "s/ Z$/ /" <<-\EOF >expect.err &&
32 + warning: GIT_INDEX_VERSION set, but the value is invalid.
33 + Using version Z
34 + EOF
35 + test_i18ncmp expect.err actual.err
36 + )
37 +'
38 +
39 +test_expect_success 'no warning with bogus GIT_INDEX_VERSION and existing index' '
40 + (
41 + GIT_INDEX_VERSION=1 &&
42 + export GIT_INDEX_VERSION &&
43 + git add a 2>actual.err &&
44 + >expect.err &&
45 + test_i18ncmp expect.err actual.err
46 + )
47 +'
48 +
49 +test_done