Merge branch 'jk/reopen-tempfile-truncate' into maint
authorJunio C Hamano <gitster@pobox.com>
Wed, 21 Nov 2018 13:57:43 +0000 (22:57 +0900)
committerJunio C Hamano <gitster@pobox.com>
Wed, 21 Nov 2018 13:57:43 +0000 (22:57 +0900)
Fix for a long-standing bug that leaves the index file corrupt when
it shrinks during a partial commit.

* jk/reopen-tempfile-truncate:
reopen_tempfile(): truncate opened file

1  2 
t/t0090-cache-tree.sh
tempfile.h
diff --combined t/t0090-cache-tree.sh
index 7de40141ca84dc53657d81c0254870b387339f79,596e269394dccd42e85228c3121f979f5a4bd658..94fcb4a78e661ae793eef54ae98b4821218b1e29
@@@ -156,11 -156,29 +156,29 @@@ test_expect_success PERL 'commit --inte
                return 44;
        }
        EOT
 -      (echo p; echo 1; echo; echo s; echo n; echo y; echo q) |
 +      test_write_lines p 1 "" s n y q |
        git commit --interactive -m foo &&
        test_cache_tree
  '
  
+ test_expect_success PERL 'commit -p with shrinking cache-tree' '
+       mkdir -p deep/subdir &&
+       echo content >deep/subdir/file &&
+       git add deep &&
+       git commit -m add &&
+       git rm -r deep &&
+       before=$(wc -c <.git/index) &&
+       git commit -m delete -p &&
+       after=$(wc -c <.git/index) &&
+       # double check that the index shrank
+       test $before -gt $after &&
+       # and that our index was not corrupted
+       git fsck
+ '
  test_expect_success 'commit in child dir has cache-tree' '
        mkdir dir &&
        >dir/child.t &&
@@@ -239,7 -257,7 +257,7 @@@ test_expect_success 'no phantom error w
        >newdir/one &&
        git add newdir/one &&
        git checkout 2>errors &&
 -      ! test -s errors
 +      test_must_be_empty errors
  '
  
  test_expect_success 'switching trees does not invalidate shared index' '
diff --combined tempfile.h
index 36434eb6fa64721bcafdb40db5f9e7d0400a50b4,b1b085540b1741ee0ca0ee7d265e3e2fd090335b..61d8dc4d1bb2fc446ac7b452661064e615b4b5c9
@@@ -2,7 -2,6 +2,7 @@@
  #define TEMPFILE_H
  
  #include "list.h"
 +#include "strbuf.h"
  
  /*
   * Handle temporary files.
@@@ -236,8 -235,8 +236,8 @@@ extern int close_tempfile_gently(struc
   *   it (and nobody else) to inspect or even modify the file's
   *   contents.
   *
-  * * `reopen_tempfile()` to reopen the temporary file. Make further
-  *   updates to the contents.
+  * * `reopen_tempfile()` to reopen the temporary file, truncating the existing
+  *   contents. Write out the new contents.
   *
   * * `rename_tempfile()` to move the file to its permanent location.
   */