1515#include "strbuf.h"
1616#include "varint.h"
1717#include "split-index.h"
18+ #include "sigchain.h"
1819
1920static struct cache_entry * refresh_cache_entry (struct cache_entry * ce ,
2021 unsigned int options );
@@ -39,7 +40,8 @@ static struct cache_entry *refresh_cache_entry(struct cache_entry *ce,
3940
4041/* changes that can be kept in $GIT_DIR/index (basically all extensions) */
4142#define EXTMASK (RESOLVE_UNDO_CHANGED | CACHE_TREE_CHANGED | \
42- CE_ENTRY_ADDED | CE_ENTRY_REMOVED | CE_ENTRY_CHANGED)
43+ CE_ENTRY_ADDED | CE_ENTRY_REMOVED | CE_ENTRY_CHANGED | \
44+ SPLIT_INDEX_ORDERED)
4345
4446struct index_state the_index ;
4547static const char * alternate_index_output ;
@@ -1860,7 +1862,8 @@ void update_index_if_able(struct index_state *istate, struct lock_file *lockfile
18601862 rollback_lock_file (lockfile );
18611863}
18621864
1863- static int do_write_index (struct index_state * istate , int newfd )
1865+ static int do_write_index (struct index_state * istate , int newfd ,
1866+ int strip_extensions )
18641867{
18651868 git_SHA_CTX c ;
18661869 struct cache_header hdr ;
@@ -1923,7 +1926,7 @@ static int do_write_index(struct index_state *istate, int newfd)
19231926 strbuf_release (& previous_name_buf );
19241927
19251928 /* Write extension data here */
1926- if (istate -> split_index ) {
1929+ if (! strip_extensions && istate -> split_index ) {
19271930 struct strbuf sb = STRBUF_INIT ;
19281931
19291932 err = write_link_extension (& sb , istate ) < 0 ||
@@ -1934,7 +1937,7 @@ static int do_write_index(struct index_state *istate, int newfd)
19341937 if (err )
19351938 return -1 ;
19361939 }
1937- if (istate -> cache_tree ) {
1940+ if (! strip_extensions && istate -> cache_tree ) {
19381941 struct strbuf sb = STRBUF_INIT ;
19391942
19401943 cache_tree_write (& sb , istate -> cache_tree );
@@ -1944,7 +1947,7 @@ static int do_write_index(struct index_state *istate, int newfd)
19441947 if (err )
19451948 return -1 ;
19461949 }
1947- if (istate -> resolve_undo ) {
1950+ if (! strip_extensions && istate -> resolve_undo ) {
19481951 struct strbuf sb = STRBUF_INIT ;
19491952
19501953 resolve_undo_write (& sb , istate -> resolve_undo );
@@ -1985,7 +1988,7 @@ static int commit_locked_index(struct lock_file *lk)
19851988static int do_write_locked_index (struct index_state * istate , struct lock_file * lock ,
19861989 unsigned flags )
19871990{
1988- int ret = do_write_index (istate , lock -> fd );
1991+ int ret = do_write_index (istate , lock -> fd , 0 );
19891992 if (ret )
19901993 return ret ;
19911994 assert ((flags & (COMMIT_LOCK | CLOSE_LOCK )) !=
@@ -2009,6 +2012,52 @@ static int write_split_index(struct index_state *istate,
20092012 return ret ;
20102013}
20112014
2015+ static char * temporary_sharedindex ;
2016+
2017+ static void remove_temporary_sharedindex (void )
2018+ {
2019+ if (temporary_sharedindex ) {
2020+ unlink_or_warn (temporary_sharedindex );
2021+ free (temporary_sharedindex );
2022+ temporary_sharedindex = NULL ;
2023+ }
2024+ }
2025+
2026+ static void remove_temporary_sharedindex_on_signal (int signo )
2027+ {
2028+ remove_temporary_sharedindex ();
2029+ sigchain_pop (signo );
2030+ raise (signo );
2031+ }
2032+
2033+ static int write_shared_index (struct index_state * istate )
2034+ {
2035+ struct split_index * si = istate -> split_index ;
2036+ static int installed_handler ;
2037+ int fd , ret ;
2038+
2039+ temporary_sharedindex = git_pathdup ("sharedindex_XXXXXX" );
2040+ fd = xmkstemp (temporary_sharedindex );
2041+ if (!installed_handler ) {
2042+ atexit (remove_temporary_sharedindex );
2043+ sigchain_push_common (remove_temporary_sharedindex_on_signal );
2044+ }
2045+ move_cache_to_base_index (istate );
2046+ ret = do_write_index (si -> base , fd , 1 );
2047+ close (fd );
2048+ if (ret ) {
2049+ remove_temporary_sharedindex ();
2050+ return ret ;
2051+ }
2052+ ret = rename (temporary_sharedindex ,
2053+ git_path ("sharedindex.%s" , sha1_to_hex (si -> base -> sha1 )));
2054+ free (temporary_sharedindex );
2055+ temporary_sharedindex = NULL ;
2056+ if (!ret )
2057+ hashcpy (si -> base_sha1 , si -> base -> sha1 );
2058+ return ret ;
2059+ }
2060+
20122061int write_locked_index (struct index_state * istate , struct lock_file * lock ,
20132062 unsigned flags )
20142063{
@@ -2020,6 +2069,12 @@ int write_locked_index(struct index_state *istate, struct lock_file *lock,
20202069 return do_write_locked_index (istate , lock , flags );
20212070 }
20222071
2072+ if (istate -> cache_changed & SPLIT_INDEX_ORDERED ) {
2073+ int ret = write_shared_index (istate );
2074+ if (ret )
2075+ return ret ;
2076+ }
2077+
20232078 return write_split_index (istate , lock , flags );
20242079}
20252080
0 commit comments