read-cache: don't write index twice if we can't write shared index
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>
Wed, 24 Jan 2018 09:38:29 +0000 (16:38 +0700)
committerJunio C Hamano <gitster@pobox.com>
Wed, 24 Jan 2018 18:09:18 +0000 (10:09 -0800)
In a0a967568e ("update-index --split-index: do not split if $GIT_DIR is
read only", 2014-06-13), we tried to make sure we can still write an
index, even if the shared index can not be written.

We did so by just calling 'do_write_locked_index()' just before
'write_shared_index()'. 'do_write_locked_index()' always at least
closes the tempfile nowadays, and used to close or commit the lockfile
if COMMIT_LOCK or CLOSE_LOCK were given at the time this feature was
introduced. COMMIT_LOCK or CLOSE_LOCK is passed in by most callers of
'write_locked_index()'.

After calling 'write_shared_index()', we call 'write_split_index()',
which calls 'do_write_locked_index()' again, which then tries to use the
closed lockfile again, but in fact fails to do so as it's already
closed. This eventually leads to a segfault.

Make sure to write the main index only once.

[nd: most of the commit message and investigation done by Thomas, I only
tweaked the solution a bit]

Helped-by: Thomas Gummerer <t.gummerer@gmail.com>
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
read-cache.c
t/t1700-split-index.sh
index c568643f556d75257b40343d0521eed288351b50..c58c0a978a83529005326e3c2e15216ffd2b1df0 100644 (file)
@@ -2561,8 +2561,9 @@ int write_locked_index(struct index_state *istate, struct lock_file *lock,
                if (!temp) {
                        hashclr(si->base_sha1);
                        ret = do_write_locked_index(istate, lock, flags);
-               } else
-                       ret = write_shared_index(istate, &temp);
+                       goto out;
+               }
+               ret = write_shared_index(istate, &temp);
 
                saved_errno = errno;
                if (is_tempfile_active(temp))
index af9b847761f3c25ddbda8d1e1e978d1a323fa561..cbcefa6e5f84a7911d5de12a5d669b4a85392299 100755 (executable)
@@ -401,4 +401,23 @@ done <<\EOF
 0642 -rw-r---w-
 EOF
 
+test_expect_success POSIXPERM,SANITY 'graceful handling when splitting index is not allowed' '
+       test_create_repo ro &&
+       (
+               cd ro &&
+               test_commit initial &&
+               git update-index --split-index &&
+               test -f .git/sharedindex.*
+       ) &&
+       cp ro/.git/index new-index &&
+       test_when_finished "chmod u+w ro/.git" &&
+       chmod u-w ro/.git &&
+       GIT_INDEX_FILE="$(pwd)/new-index" git -C ro update-index --split-index &&
+       chmod u+w ro/.git &&
+       rm ro/.git/sharedindex.* &&
+       GIT_INDEX_FILE=new-index git ls-files >actual &&
+       echo initial.t >expected &&
+       test_cmp expected actual
+'
+
 test_done