Skip to content

Commit 606e088

Browse files
committed
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 764b233 commit 606e088

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

Documentation/git-update-index.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ you will need to handle the situation manually.
162162
Write the resulting index out in the named on-disk format version.
163163
Supported versions are 2, 3 and 4. The current default version is 2
164164
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.
166167
+
167168
Version 4 performs a simple pathname compression that reduces index
168169
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.
171172
Older versions of this manual page called it "relatively young", but
172173
it should be considered mature technology these days.
173174

175+
--show-index-version::
176+
Report the index format version used by the on-disk index file.
177+
See `--index-version` above.
174178

175179
-z::
176180
Only meaningful with `--stdin` or `--index-info`; paths are

builtin/update-index.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,8 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
10891089
resolve_undo_clear_callback),
10901090
OPT_INTEGER(0, "index-version", &preferred_index_format,
10911091
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),
10921094
OPT_BOOL(0, "split-index", &split_index,
10931095
N_("enable or disable split index")),
10941096
OPT_BOOL(0, "untracked-cache", &untracked_cache,
@@ -1181,15 +1183,20 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
11811183

11821184
getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
11831185
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) {
11861190
die("index-version %d not in range: %d..%d",
11871191
preferred_index_format,
11881192
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+
}
11931200
}
11941201

11951202
if (read_from_stdin) {

t/t2107-update-index-basic.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,35 @@ test_expect_success '--chmod=+x and chmod=-x in the same argument list' '
111111
test_cmp expect actual
112112
'
113113

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

0 commit comments

Comments
 (0)