1414#include "resolve-undo.h"
1515#include "strbuf.h"
1616#include "varint.h"
17+ #include "split-index.h"
1718
1819static struct cache_entry * refresh_cache_entry (struct cache_entry * ce ,
1920 unsigned int options );
@@ -34,6 +35,10 @@ static struct cache_entry *refresh_cache_entry(struct cache_entry *ce,
3435#define CACHE_EXT (s ) ( (s[0]<<24)|(s[1]<<16)|(s[2]<<8)|(s[3]) )
3536#define CACHE_EXT_TREE 0x54524545 /* "TREE" */
3637#define CACHE_EXT_RESOLVE_UNDO 0x52455543 /* "REUC" */
38+ #define CACHE_EXT_LINK 0x6c696e6b /* "link" */
39+
40+ /* changes that can be kept in $GIT_DIR/index (basically all extensions) */
41+ #define EXTMASK (RESOLVE_UNDO_CHANGED | CACHE_TREE_CHANGED)
3742
3843struct index_state the_index ;
3944static const char * alternate_index_output ;
@@ -63,6 +68,7 @@ void rename_index_entry_at(struct index_state *istate, int nr, const char *new_n
6368 copy_cache_entry (new , old );
6469 new -> ce_flags &= ~CE_HASHED ;
6570 new -> ce_namelen = namelen ;
71+ new -> index = 0 ;
6672 memcpy (new -> name , new_name , namelen + 1 );
6773
6874 cache_tree_invalidate_path (istate , old -> name );
@@ -1335,6 +1341,10 @@ static int read_index_extension(struct index_state *istate,
13351341 case CACHE_EXT_RESOLVE_UNDO :
13361342 istate -> resolve_undo = resolve_undo_read (data , sz );
13371343 break ;
1344+ case CACHE_EXT_LINK :
1345+ if (read_link_extension (istate , data , sz ))
1346+ return -1 ;
1347+ break ;
13381348 default :
13391349 if (* ext < 'A' || 'Z' < * ext )
13401350 return error ("index uses %.4s extension, which we do not understand" ,
@@ -1369,6 +1379,7 @@ static struct cache_entry *cache_entry_from_ondisk(struct ondisk_cache_entry *on
13691379 ce -> ce_stat_data .sd_size = get_be32 (& ondisk -> size );
13701380 ce -> ce_flags = flags & ~CE_NAMEMASK ;
13711381 ce -> ce_namelen = len ;
1382+ ce -> index = 0 ;
13721383 hashcpy (ce -> sha1 , ondisk -> sha1 );
13731384 memcpy (ce -> name , name , len );
13741385 ce -> name [len ] = '\0' ;
@@ -1443,7 +1454,8 @@ static struct cache_entry *create_from_disk(struct ondisk_cache_entry *ondisk,
14431454}
14441455
14451456/* remember to discard_cache() before reading a different cache! */
1446- int read_index_from (struct index_state * istate , const char * path )
1457+ static int do_read_index (struct index_state * istate , const char * path ,
1458+ int must_exist )
14471459{
14481460 int fd , i ;
14491461 struct stat st ;
@@ -1460,9 +1472,9 @@ int read_index_from(struct index_state *istate, const char *path)
14601472 istate -> timestamp .nsec = 0 ;
14611473 fd = open (path , O_RDONLY );
14621474 if (fd < 0 ) {
1463- if (errno == ENOENT )
1475+ if (! must_exist && errno == ENOENT )
14641476 return 0 ;
1465- die_errno ("index file open failed" );
1477+ die_errno ("%s: index file open failed" , path );
14661478 }
14671479
14681480 if (fstat (fd , & st ))
@@ -1535,6 +1547,42 @@ int read_index_from(struct index_state *istate, const char *path)
15351547 die ("index file corrupt" );
15361548}
15371549
1550+ int read_index_from (struct index_state * istate , const char * path )
1551+ {
1552+ struct split_index * split_index ;
1553+ int ret ;
1554+
1555+ /* istate->initialized covers both .git/index and .git/sharedindex.xxx */
1556+ if (istate -> initialized )
1557+ return istate -> cache_nr ;
1558+
1559+ ret = do_read_index (istate , path , 0 );
1560+ split_index = istate -> split_index ;
1561+ if (!split_index )
1562+ return ret ;
1563+
1564+ if (is_null_sha1 (split_index -> base_sha1 ))
1565+ return ret ;
1566+ if (istate -> cache_nr )
1567+ die ("index in split-index mode must contain no entries" );
1568+
1569+ if (split_index -> base )
1570+ discard_index (split_index -> base );
1571+ else
1572+ split_index -> base = xcalloc (1 , sizeof (* split_index -> base ));
1573+ ret = do_read_index (split_index -> base ,
1574+ git_path ("sharedindex.%s" ,
1575+ sha1_to_hex (split_index -> base_sha1 )), 1 );
1576+ if (hashcmp (split_index -> base_sha1 , split_index -> base -> sha1 ))
1577+ die ("broken index, expect %s in %s, got %s" ,
1578+ sha1_to_hex (split_index -> base_sha1 ),
1579+ git_path ("sharedindex.%s" ,
1580+ sha1_to_hex (split_index -> base_sha1 )),
1581+ sha1_to_hex (split_index -> base -> sha1 ));
1582+ merge_base_index (istate );
1583+ return ret ;
1584+ }
1585+
15381586int is_index_unborn (struct index_state * istate )
15391587{
15401588 return (!istate -> cache_nr && !istate -> timestamp .sec );
@@ -1544,8 +1592,15 @@ int discard_index(struct index_state *istate)
15441592{
15451593 int i ;
15461594
1547- for (i = 0 ; i < istate -> cache_nr ; i ++ )
1595+ for (i = 0 ; i < istate -> cache_nr ; i ++ ) {
1596+ if (istate -> cache [i ]-> index &&
1597+ istate -> split_index &&
1598+ istate -> split_index -> base &&
1599+ istate -> cache [i ]-> index <= istate -> split_index -> base -> cache_nr &&
1600+ istate -> cache [i ] == istate -> split_index -> base -> cache [istate -> cache [i ]-> index - 1 ])
1601+ continue ;
15481602 free (istate -> cache [i ]);
1603+ }
15491604 resolve_undo_clear_index (istate );
15501605 istate -> cache_nr = 0 ;
15511606 istate -> cache_changed = 0 ;
@@ -1557,6 +1612,7 @@ int discard_index(struct index_state *istate)
15571612 free (istate -> cache );
15581613 istate -> cache = NULL ;
15591614 istate -> cache_alloc = 0 ;
1615+ discard_split_index (istate );
15601616 return 0 ;
15611617}
15621618
@@ -1852,6 +1908,17 @@ static int do_write_index(struct index_state *istate, int newfd)
18521908 strbuf_release (& previous_name_buf );
18531909
18541910 /* Write extension data here */
1911+ if (istate -> split_index ) {
1912+ struct strbuf sb = STRBUF_INIT ;
1913+
1914+ err = write_link_extension (& sb , istate ) < 0 ||
1915+ write_index_ext_header (& c , newfd , CACHE_EXT_LINK ,
1916+ sb .len ) < 0 ||
1917+ ce_write (& c , newfd , sb .buf , sb .len ) < 0 ;
1918+ strbuf_release (& sb );
1919+ if (err )
1920+ return -1 ;
1921+ }
18551922 if (istate -> cache_tree ) {
18561923 struct strbuf sb = STRBUF_INIT ;
18571924
@@ -1916,10 +1983,29 @@ static int do_write_locked_index(struct index_state *istate, struct lock_file *l
19161983 return ret ;
19171984}
19181985
1986+ static int write_split_index (struct index_state * istate ,
1987+ struct lock_file * lock ,
1988+ unsigned flags )
1989+ {
1990+ int ret ;
1991+ prepare_to_write_split_index (istate );
1992+ ret = do_write_locked_index (istate , lock , flags );
1993+ finish_writing_split_index (istate );
1994+ return ret ;
1995+ }
1996+
19191997int write_locked_index (struct index_state * istate , struct lock_file * lock ,
19201998 unsigned flags )
19211999{
1922- return do_write_locked_index (istate , lock , flags );
2000+ struct split_index * si = istate -> split_index ;
2001+
2002+ if (!si || (istate -> cache_changed & ~EXTMASK )) {
2003+ if (si )
2004+ hashclr (si -> base_sha1 );
2005+ return do_write_locked_index (istate , lock , flags );
2006+ }
2007+
2008+ return write_split_index (istate , lock , flags );
19232009}
19242010
19252011/*
0 commit comments