update-index: add --show-index-version

"git update-index --index-version N" is used to set the index format version to a specific version, but there was no way to query the current version used in the on-disk index file. Teach the command a new "--show-index-version" option, and also teach the "--index-version N" option to report what the version was when run with the "--verbose" option. Helped-by: Linus Arver <linusa@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Sep 12, 2023 at 12:32 UTC 606e088d5d9af2393b4b05a8a180162c129cd734
3 files changed +49 -7
Documentation/git-update-index.txt
+5 -1
@@ -162,7 +162,8 @@ you will need to handle the situation manually.
162 Write the resulting index out in the named on-disk format version.
163 Supported versions are 2, 3 and 4. The current default version is 2
164 or 3, depending on whether extra features are used, such as
165 - `git add -N`.
165 + `git add -N`. With `--verbose`, also report the version the index
166 + file uses before and after this command.
167 +
168 Version 4 performs a simple pathname compression that reduces index
169 size by 30%-50% on large repositories, which results in faster load
@@ -171,6 +172,9 @@ and support for it was added to libgit2 in 2016 and to JGit in 2020.
172 Older versions of this manual page called it "relatively young", but
173 it should be considered mature technology these days.
174
175 +--show-index-version::
176 + Report the index format version used by the on-disk index file.
177 + See `--index-version` above.
178
179 -z::
180 Only meaningful with `--stdin` or `--index-info`; paths are
builtin/update-index.c
+13 -6
@@ -1089,6 +1089,8 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
1089 resolve_undo_clear_callback),
1090 OPT_INTEGER(0, "index-version", &preferred_index_format,
1091 N_("write index in this format")),
1092 + OPT_SET_INT(0, "show-index-version", &preferred_index_format,
1093 + N_("report on-disk index format version"), -1),
1094 OPT_BOOL(0, "split-index", &split_index,
1095 N_("enable or disable split index")),
1096 OPT_BOOL(0, "untracked-cache", &untracked_cache,
@@ -1181,15 +1183,20 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
1183
1184 getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
1185 if (preferred_index_format) {
1184 - if (preferred_index_format < INDEX_FORMAT_LB ||
1185 - INDEX_FORMAT_UB < preferred_index_format)
1186 + if (preferred_index_format < 0) {
1187 + printf(_("%d\n"), the_index.version);
1188 + } else if (preferred_index_format < INDEX_FORMAT_LB ||
1189 + INDEX_FORMAT_UB < preferred_index_format) {
1190 die("index-version %d not in range: %d..%d",
1191 preferred_index_format,
1192 INDEX_FORMAT_LB, INDEX_FORMAT_UB);
1189 -
1190 - if (the_index.version != preferred_index_format)
1191 - the_index.cache_changed |= SOMETHING_CHANGED;
1192 - the_index.version = preferred_index_format;
1193 + } else {
1194 + if (the_index.version != preferred_index_format)
1195 + the_index.cache_changed |= SOMETHING_CHANGED;
1196 + report(_("index-version: was %d, set to %d"),
1197 + the_index.version, preferred_index_format);
1198 + the_index.version = preferred_index_format;
1199 + }
1200 }
1201
1202 if (read_from_stdin) {
t/t2107-update-index-basic.sh
+31
@@ -111,4 +111,35 @@ test_expect_success '--chmod=+x and chmod=-x in the same argument list' '
111 test_cmp expect actual
112 '
113
114 +test_expect_success '--index-version' '
115 + git commit --allow-empty -m snap &&
116 + git reset --hard &&
117 + git rm -f -r --cached . &&
118 +
119 + # The default index version is 2 --- update this test
120 + # when you change it in the code
121 + git update-index --show-index-version >actual &&
122 + echo 2 >expect &&
123 + test_cmp expect actual &&
124 +
125 + # The next test wants us to be using version 2
126 + git update-index --index-version 2 &&
127 +
128 + git update-index --index-version 4 --verbose >actual &&
129 + echo "index-version: was 2, set to 4" >expect &&
130 + test_cmp expect actual &&
131 +
132 + git update-index --index-version 4 --verbose >actual &&
133 + echo "index-version: was 4, set to 4" >expect &&
134 + test_cmp expect actual &&
135 +
136 + git update-index --index-version 2 --verbose >actual &&
137 + echo "index-version: was 4, set to 2" >expect &&
138 + test_cmp expect actual &&
139 +
140 + # non-verbose should be silent
141 + git update-index --index-version 4 >actual &&
142 + test_must_be_empty actual
143 +'
144 +
145 test_done