sparse-index: add 'sdir' index extension

The index format does not currently allow for sparse directory entries. This violates some expectations that older versions of Git or third-party tools might not understand. We need an indicator inside the index file to warn these tools to not interact with a sparse index unless they are aware of sparse directory entries. Add a new _required_ index extension, 'sdir', that indicates that the index may contain sparse directory entries. This allows us to continue to use the differences in index formats 2, 3, and 4 before we create a new index version 5 in a later change. Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Mar 30, 2021 at 13:10 UTC cd42415fb4c9680f308ebf8f9f85ba90ae6014be
2 files changed +21
Documentation/technical/index-format.txt
+12
@@ -392,3 +392,15 @@ The remaining data of each directory block is grouped by type:
392 in this block of entries.
393
394 - 32-bit count of cache entries in this block
395 +
396 +== Sparse Directory Entries
397 +
398 + When using sparse-checkout in cone mode, some entire directories within
399 + the index can be summarized by pointing to a tree object instead of the
400 + entire expanded list of paths within that tree. An index containing such
401 + entries is a "sparse index". Index format versions 4 and less were not
402 + implemented with such entries in mind. Thus, for these versions, an
403 + index containing sparse directory entries will include this extension
404 + with signature { 's', 'd', 'i', 'r' }. Like the split-index extension,
405 + tools should avoid interacting with a sparse index unless they understand
406 + this extension.
read-cache.c
+9
@@ -47,6 +47,7 @@
47 #define CACHE_EXT_FSMONITOR 0x46534D4E /* "FSMN" */
48 #define CACHE_EXT_ENDOFINDEXENTRIES 0x454F4945 /* "EOIE" */
49 #define CACHE_EXT_INDEXENTRYOFFSETTABLE 0x49454F54 /* "IEOT" */
50 +#define CACHE_EXT_SPARSE_DIRECTORIES 0x73646972 /* "sdir" */
51
52 /* changes that can be kept in $GIT_DIR/index (basically all extensions) */
53 #define EXTMASK (RESOLVE_UNDO_CHANGED | CACHE_TREE_CHANGED | \
@@ -1763,6 +1764,10 @@ static int read_index_extension(struct index_state *istate,
1764 case CACHE_EXT_INDEXENTRYOFFSETTABLE:
1765 /* already handled in do_read_index() */
1766 break;
1767 + case CACHE_EXT_SPARSE_DIRECTORIES:
1768 + /* no content, only an indicator */
1769 + istate->sparse_index = 1;
1770 + break;
1771 default:
1772 if (*ext < 'A' || 'Z' < *ext)
1773 return error(_("index uses %.4s extension, which we do not understand"),
@@ -3020,6 +3025,10 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
3025 if (err)
3026 return -1;
3027 }
3028 + if (istate->sparse_index) {
3029 + if (write_index_ext_header(&c, &eoie_c, newfd, CACHE_EXT_SPARSE_DIRECTORIES, 0) < 0)
3030 + return -1;
3031 + }
3032
3033 /*
3034 * CACHE_EXT_ENDOFINDEXENTRIES must be written as the last entry before the SHA1