Skip to content

Commit 13b49f1

Browse files
committed
Merge branch 'tg/index-v4-format'
* tg/index-v4-format: read-cache: add index.version config variable test-lib: allow setting the index format version introduce GIT_INDEX_VERSION environment variable
2 parents 0963008 + 3c09d68 commit 13b49f1

File tree

8 files changed

+142
-1
lines changed

8 files changed

+142
-1
lines changed

Documentation/config.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,6 +1607,10 @@ imap::
16071607
The configuration variables in the 'imap' section are described
16081608
in linkgit:git-imap-send[1].
16091609

1610+
index.version::
1611+
Specify the version with which new index files should be
1612+
initialized. This does not affect existing repositories.
1613+
16101614
init.templatedir::
16111615
Specify the directory from which templates will be copied.
16121616
(See the "TEMPLATE DIRECTORY" section of linkgit:git-init[1].)

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

Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,10 @@ all::
334334
# Define DEFAULT_HELP_FORMAT to "man", "info" or "html"
335335
# (defaults to "man") if you want to have a different default when
336336
# "git help" is called without a parameter specifying the format.
337+
#
338+
# Define TEST_GIT_INDEX_VERSION to 2, 3 or 4 to run the test suite
339+
# with a different indexfile format version. If it isn't set the index
340+
# file format used is index-v[23].
337341

338342
GIT-VERSION-FILE: FORCE
339343
@$(SHELL_PATH) ./GIT-VERSION-GEN
@@ -2212,6 +2216,9 @@ endif
22122216
ifdef GIT_PERF_MAKE_OPTS
22132217
@echo GIT_PERF_MAKE_OPTS=\''$(subst ','\'',$(subst ','\'',$(GIT_PERF_MAKE_OPTS)))'\' >>$@
22142218
endif
2219+
ifdef TEST_GIT_INDEX_VERSION
2220+
@echo TEST_GIT_INDEX_VERSION=\''$(subst ','\'',$(subst ','\'',$(TEST_GIT_INDEX_VERSION)))'\' >>$@
2221+
endif
22152222

22162223
### Detect Python interpreter path changes
22172224
ifndef NO_PYTHON

read-cache.c

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1220,6 +1220,42 @@ static struct cache_entry *refresh_cache_entry(struct cache_entry *ce,
12201220

12211221
#define INDEX_FORMAT_DEFAULT 3
12221222

1223+
static int index_format_config(const char *var, const char *value, void *cb)
1224+
{
1225+
unsigned int *version = cb;
1226+
if (!strcmp(var, "index.version")) {
1227+
*version = git_config_int(var, value);
1228+
return 0;
1229+
}
1230+
return 1;
1231+
}
1232+
1233+
static unsigned int get_index_format_default(void)
1234+
{
1235+
char *envversion = getenv("GIT_INDEX_VERSION");
1236+
char *endp;
1237+
unsigned int version = INDEX_FORMAT_DEFAULT;
1238+
1239+
if (!envversion) {
1240+
git_config(index_format_config, &version);
1241+
if (version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < version) {
1242+
warning(_("index.version set, but the value is invalid.\n"
1243+
"Using version %i"), INDEX_FORMAT_DEFAULT);
1244+
return INDEX_FORMAT_DEFAULT;
1245+
}
1246+
return version;
1247+
}
1248+
1249+
version = strtoul(envversion, &endp, 10);
1250+
if (*endp ||
1251+
version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < version) {
1252+
warning(_("GIT_INDEX_VERSION set, but the value is invalid.\n"
1253+
"Using version %i"), INDEX_FORMAT_DEFAULT);
1254+
version = INDEX_FORMAT_DEFAULT;
1255+
}
1256+
return version;
1257+
}
1258+
12231259
/*
12241260
* dev/ino/uid/gid/size are also just tracked to the low 32 bits
12251261
* Again - this is just a (very strong in practice) heuristic that
@@ -1776,7 +1812,7 @@ int write_index(struct index_state *istate, int newfd)
17761812
}
17771813

17781814
if (!istate->version)
1779-
istate->version = INDEX_FORMAT_DEFAULT;
1815+
istate->version = get_index_format_default();
17801816

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

t/t1600-index.sh

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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_expect_success 'out of bounds index.version issues warning' '
50+
(
51+
sane_unset GIT_INDEX_VERSION &&
52+
rm -f .git/index &&
53+
git config --add index.version 1 &&
54+
git add a 2>&1 | sed "s/[0-9]//" >actual.err &&
55+
sed -e "s/ Z$/ /" <<-\EOF >expect.err &&
56+
warning: index.version set, but the value is invalid.
57+
Using version Z
58+
EOF
59+
test_i18ncmp expect.err actual.err
60+
)
61+
'
62+
63+
test_expect_success 'GIT_INDEX_VERSION takes precedence over config' '
64+
(
65+
rm -f .git/index &&
66+
GIT_INDEX_VERSION=4 &&
67+
export GIT_INDEX_VERSION &&
68+
git config --add index.version 2 &&
69+
git add a 2>&1 &&
70+
echo 4 >expect &&
71+
test-index-version <.git/index >actual &&
72+
test_cmp expect actual
73+
)
74+
'
75+
76+
test_done

t/t2104-update-index-skip-worktree.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ test_description='skip-worktree bit test'
77

88
. ./test-lib.sh
99

10+
test_set_index_version 3
11+
1012
cat >expect.full <<EOF
1113
H 1
1214
H 2

t/test-lib-functions.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ test_set_editor () {
3232
export EDITOR
3333
}
3434

35+
test_set_index_version () {
36+
GIT_INDEX_VERSION="$1"
37+
export GIT_INDEX_VERSION
38+
}
39+
3540
test_decode_color () {
3641
awk '
3742
function name(n) {

t/test-lib.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ export GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME
108108
export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME
109109
export EDITOR
110110

111+
if test -n "${TEST_GIT_INDEX_VERSION:+isset}"
112+
then
113+
GIT_INDEX_VERSION="$TEST_GIT_INDEX_VERSION"
114+
export GIT_INDEX_VERSION
115+
fi
116+
111117
# Add libc MALLOC and MALLOC_PERTURB test
112118
# only if we are not executing the test with valgrind
113119
if expr " $GIT_TEST_OPTS " : ".* --valgrind " >/dev/null ||

0 commit comments

Comments
 (0)