update-index: upgrade/downgrade on-disk index version

With the "--index-version <n>" parameter, write the index out in the specified version. With this, an index file that is written in newer format (say v4) can be downgraded to be read by older versions of Git. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Apr 4, 2012 at 09:37 UTC 69dec66b2f6ab5c8138cca37e74bd405c541318a
2 files changed +19 -1
Documentation/git-update-index.txt
+5 -1
@@ -19,7 +19,7 @@ SYNOPSIS
19 [--ignore-submodules]
20 [--really-refresh] [--unresolve] [--again | -g]
21 [--info-only] [--index-info]
22 - [-z] [--stdin]
22 + [-z] [--stdin] [--index-version <n>]
23 [--verbose]
24 [--] [<file>...]
25
@@ -143,6 +143,10 @@ you will need to handle the situation manually.
143 --verbose::
144 Report what is being added and removed from index.
145
146 +--index-version <n>::
147 + Write the resulting index out in the named on-disk format version.
148 + The current default version is 2.
149 +
150 -z::
151 Only meaningful with `--stdin` or `--index-info`; paths are
152 separated with NUL character instead of LF.
builtin/update-index.c
+14
@@ -708,6 +708,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
708 int newfd, entries, has_errors = 0, line_termination = '\n';
709 int read_from_stdin = 0;
710 int prefix_length = prefix ? strlen(prefix) : 0;
711 + int preferred_index_format = 0;
712 char set_executable_bit = 0;
713 struct refresh_params refresh_args = {0, &has_errors};
714 int lock_error = 0;
@@ -791,6 +792,8 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
792 "(for porcelains) forget saved unresolved conflicts",
793 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
794 resolve_undo_clear_callback},
795 + OPT_INTEGER(0, "index-version", &preferred_index_format,
796 + "write index in this format"),
797 OPT_END()
798 };
799
@@ -851,6 +854,17 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
854 }
855 }
856 argc = parse_options_end(&ctx);
857 + if (preferred_index_format) {
858 + if (preferred_index_format < INDEX_FORMAT_LB ||
859 + INDEX_FORMAT_UB < preferred_index_format)
860 + die("index-version %d not in range: %d..%d",
861 + preferred_index_format,
862 + INDEX_FORMAT_LB, INDEX_FORMAT_UB);
863 +
864 + if (the_index.version != preferred_index_format)
865 + active_cache_changed = 1;
866 + the_index.version = preferred_index_format;
867 + }
868
869 if (read_from_stdin) {
870 struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT;