Skip to content

Commit 136347d

Browse files
tgummerergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Thomas Gummerer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5f95c9f commit 136347d

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

Documentation/git.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,11 @@ Git so take care if using Cogito etc.
720720
index file. If not specified, the default of `$GIT_DIR/index`
721721
is used.
722722

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+
723728
'GIT_OBJECT_DIRECTORY'::
724729
If the object storage directory is specified via this
725730
environment variable then the sha1 directories are created

read-cache.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1219,6 +1219,25 @@ static struct cache_entry *refresh_cache_entry(struct cache_entry *ce, int reall
12191219

12201220
#define INDEX_FORMAT_DEFAULT 3
12211221

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+
12221241
/*
12231242
* dev/ino/uid/gid/size are also just tracked to the low 32 bits
12241243
* Again - this is just a (very strong in practice) heuristic that
@@ -1795,7 +1814,7 @@ int write_index(struct index_state *istate, int newfd)
17951814
}
17961815

17971816
if (!istate->version)
1798-
istate->version = INDEX_FORMAT_DEFAULT;
1817+
istate->version = get_index_format_default();
17991818

18001819
/* demote version 3 to version 2 when the latter suffices */
18011820
if (istate->version == 3 || istate->version == 2)

t/t1600-index.sh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

0 commit comments

Comments
 (0)