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 <t.gummerer@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Thomas Gummerer committed Feb 23, 2014 at 21:49 UTC 3c09d6845d253f9d8a75f3a36278c69e01b073e9
3 files changed +57 -9
Documentation/config.txt
+4
@@ -1601,6 +1601,10 @@ imap::
1601 The configuration variables in the 'imap' section are described
1602 in linkgit:git-imap-send[1].
1603
1604 +index.version::
1605 + Specify the version with which new index files should be
1606 + initialized. This does not affect existing repositories.
1607 +
1608 init.templatedir::
1609 Specify the directory from which templates will be copied.
1610 (See the "TEMPLATE DIRECTORY" section of linkgit:git-init[1].)
read-cache.c
+26 -9
@@ -1219,23 +1219,40 @@ static struct cache_entry *refresh_cache_entry(struct cache_entry *ce, int reall
1219
1220 #define INDEX_FORMAT_DEFAULT 3
1221
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 +
1232 static unsigned int get_index_format_default(void)
1233 {
1234 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;
1237
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"
1242 "Using version %i"), INDEX_FORMAT_DEFAULT);
1235 - version = INDEX_FORMAT_DEFAULT;
1243 + return INDEX_FORMAT_DEFAULT;
1244 }
1245 return version;
1246 }
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;
1256 }
1257
1258 /*
t/t1600-index.sh
+27
@@ -46,4 +46,31 @@ test_expect_success 'no warning with bogus GIT_INDEX_VERSION and existing index'
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