Skip to content

Commit 3c09d68

Browse files
tgummerergitster
authored andcommitted
read-cache: add index.version config variable
Add a config variable that allows setting the default index version when initializing a new index file. Similar to the GIT_INDEX_VERSION environment variable this only affects new index files. Signed-off-by: Thomas Gummerer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5d9fc88 commit 3c09d68

File tree

3 files changed

+57
-9
lines changed

3 files changed

+57
-9
lines changed

Documentation/config.txt

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

1604+
index.version::
1605+
Specify the version with which new index files should be
1606+
initialized. This does not affect existing repositories.
1607+
16041608
init.templatedir::
16051609
Specify the directory from which templates will be copied.
16061610
(See the "TEMPLATE DIRECTORY" section of linkgit:git-init[1].)

read-cache.c

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,23 +1219,40 @@ static struct cache_entry *refresh_cache_entry(struct cache_entry *ce, int reall
12191219

12201220
#define INDEX_FORMAT_DEFAULT 3
12211221

1222+
static int index_format_config(const char *var, const char *value, void *cb)
1223+
{
1224+
unsigned int *version = cb;
1225+
if (!strcmp(var, "index.version")) {
1226+
*version = git_config_int(var, value);
1227+
return 0;
1228+
}
1229+
return 1;
1230+
}
1231+
12221232
static unsigned int get_index_format_default(void)
12231233
{
12241234
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);
1235+
char *endp;
1236+
unsigned int version = INDEX_FORMAT_DEFAULT;
12301237

1231-
if (*endp ||
1232-
version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < version) {
1233-
warning(_("GIT_INDEX_VERSION set, but the value is invalid.\n"
1238+
if (!envversion) {
1239+
git_config(index_format_config, &version);
1240+
if (version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < version) {
1241+
warning(_("index.version set, but the value is invalid.\n"
12341242
"Using version %i"), INDEX_FORMAT_DEFAULT);
1235-
version = INDEX_FORMAT_DEFAULT;
1243+
return INDEX_FORMAT_DEFAULT;
12361244
}
12371245
return version;
12381246
}
1247+
1248+
version = strtoul(envversion, &endp, 10);
1249+
if (*endp ||
1250+
version < INDEX_FORMAT_LB || INDEX_FORMAT_UB < version) {
1251+
warning(_("GIT_INDEX_VERSION set, but the value is invalid.\n"
1252+
"Using version %i"), INDEX_FORMAT_DEFAULT);
1253+
version = INDEX_FORMAT_DEFAULT;
1254+
}
1255+
return version;
12391256
}
12401257

12411258
/*

t/t1600-index.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,31 @@ test_expect_success 'no warning with bogus GIT_INDEX_VERSION and existing index'
4646
)
4747
'
4848

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+
4976
test_done

0 commit comments

Comments
 (0)