Re: [BUG] index corruption with git commit -p
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Sep 4, 2018, 15:57 UTC
- Message-ID
- <xmqq36upcl1s.fsf@gitster-ct.c.googlers.com>
- In-Reply-To
- <20180902072408.GA18787@sigill.intra.peff.net>
Jeff King <peff@peff.net> writes:
Show 25 quoted lines
>> diff --git a/builtin/commit.c b/builtin/commit.c
>> index 2be7bdb331..60f30b3780 100644
>> --- a/builtin/commit.c
>> +++ b/builtin/commit.c
>> @@ -432,6 +432,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
>> if (update_main_cache_tree(WRITE_TREE_SILENT) == 0) {
>> if (reopen_lock_file(&index_lock) < 0)
>> die(_("unable to write index file"));
>> + ftruncate(index_lock.tempfile->fd, 0);
>> if (write_locked_index(&the_index, &index_lock, 0))
>> die(_("unable to update temporary index"));
>> } else
>
> Doh, of course. I even thought about this issue and dug all the way into
> reopen_lock_file(), but for some reason temporarily forgot that O_WRONLY
> does not imply O_TRUNC.
>
> Arguably this should be the default for reopen_lockfile(), as getting a
> write pointer into an existing file is not ever going to be useful for
> the way Git uses lockfiles. Opening with O_APPEND could conceivably be
> useful, but it's pretty unlikely (and certainly not helpful here, and
> this is the only caller). Alternatively, the function should just take
> open(2) flags.
>
> At any rate, I think this perfectly explains the behavior we're seeing.Thanks all for digging this down (I am a bit jealous to see that I seem to have missed all this fun over the weekend X-<).