# bug: `git pull` does not clean up after itself if it fails

3 messages from 2024-07-15 to 2024-07-17. Participants: Nikolai Zaki, Jeff King.
Thread: https://gitlist.dev/t/61780

## Nikolai Zaki, 2024-07-15 11:19

Subject: bug: `git pull` does not clean up after itself if it fails
Message-ID: <0cc13c53-1d8d-4501-89f2-e8329bc95485@charite.de>
URL: https://gitlist.dev/e/0cc13c53-1d8d-4501-89f2-e8329bc95485%40charite.de

```
What did you do before the bug happened? (Steps to reproduce your issue)
In your local repo have a directory that you cannot write to.
Pull from a remote that has changes/new files in that directory.

What did you expect to happen? (Expected behavior)
The pull fails and the local repo in the same state as before.

What happened instead? (Actual behavior)
All changes to writeable files are applied and all new files in 
writeable dirs are added.
The local HEAD however remained the same.

What's different between what you expected and what actually happened?
A failed `git pull` inadvertently creates changes and untracked files.

Anything else you want to add:
I created this bugreport on my local machine, but I first noticed this 
on git version 2.25.1 under Ubuntu (uname: Linux s-c05-mvi-civdb 
5.4.0-174-generic #193-Ubuntu SMP Thu Mar 7 14:29:28 UTC 2024 x86_64 
x86_64 x86_64 GNU/Linux)

[System Info]
git version:
git version 2.45.2
cpu: x86_64
no commit associated with this build
sizeof-long: 8
sizeof-size_t: 8
shell-path: /bin/sh
uname: Linux 6.9.7-arch1-1 #1 SMP PREEMPT_DYNAMIC Fri, 28 Jun 2024 
04:32:50 +0000 x86_64
compiler info: gnuc: 14.1
libc info: glibc: 2.39
$SHELL (typically, interactive shell): /bin/zsh

-- 
Best,
Nikolai

```

## Jeff King, 2024-07-17 05:55

Subject: Re: bug: `git pull` does not clean up after itself if it fails
Message-ID: <20240717055549.GD547635@coredump.intra.peff.net>
URL: https://gitlist.dev/e/20240717055549.GD547635%40coredump.intra.peff.net
In-Reply-To: <0cc13c53-1d8d-4501-89f2-e8329bc95485@charite.de>

```
On Mon, Jul 15, 2024 at 01:19:32PM +0200, Nikolai Zaki wrote:

> What did you do before the bug happened? (Steps to reproduce your issue)
> In your local repo have a directory that you cannot write to.
> Pull from a remote that has changes/new files in that directory.
> 
> What did you expect to happen? (Expected behavior)
> The pull fails and the local repo in the same state as before.
> 
> What happened instead? (Actual behavior)
> All changes to writeable files are applied and all new files in writeable
> dirs are added.
> The local HEAD however remained the same.
> 
> What's different between what you expected and what actually happened?
> A failed `git pull` inadvertently creates changes and untracked files.

A pull is basically fetch followed by merge. So I'd expect us to fail in
the merge step, at which point pull would generally leave the state
as-is, because merge failures tend to be conflicts.

So I guess doing what you expect would require a merge which hits a
non-conflict error (like an unwriteable directory) to clean up after
itself. That's probably not impossible, but it may be rather tricky,
especially if we are concerned about unrelated changes in the working
tree.

-Peff

```

## Nikolai Zaki, 2024-07-17 09:11

Subject: Re: [ext] Re: bug: `git pull` does not clean up after itself if it fails
Message-ID: <0a20d825-3430-4397-962c-a6b5e94d70eb@charite.de>
URL: https://gitlist.dev/e/0a20d825-3430-4397-962c-a6b5e94d70eb%40charite.de
In-Reply-To: <20240717055549.GD547635@coredump.intra.peff.net>

```
Hi Peff,

thanks for your reply. You're right, the problem happens during merge.

Since merge already aborts if the index doesn't match HEAD though, 
couldn't it be as simple as saving the state of the worktree and then 
either restoring it in case of failure or applying the diff in case of 
success?

In terms of git commands I think the following would work:

When `git merge` is run first run `git stash --all` then `git merge` as 
usual.

If there are non-conflict errors: `git checkout .` and `git clean -f;

If there are no errors `git stash pop`.


On 7/17/24 07:55, Jeff King wrote:
> On Mon, Jul 15, 2024 at 01:19:32PM +0200, Nikolai Zaki wrote:
>
>> What did you do before the bug happened? (Steps to reproduce your issue)
>> In your local repo have a directory that you cannot write to.
>> Pull from a remote that has changes/new files in that directory.
>>
>> What did you expect to happen? (Expected behavior)
>> The pull fails and the local repo in the same state as before.
>>
>> What happened instead? (Actual behavior)
>> All changes to writeable files are applied and all new files in writeable
>> dirs are added.
>> The local HEAD however remained the same.
>>
>> What's different between what you expected and what actually happened?
>> A failed `git pull` inadvertently creates changes and untracked files.
> A pull is basically fetch followed by merge. So I'd expect us to fail in
> the merge step, at which point pull would generally leave the state
> as-is, because merge failures tend to be conflicts.
>
> So I guess doing what you expect would require a merge which hits a
> non-conflict error (like an unwriteable directory) to clean up after
> itself. That's probably not impossible, but it may be rather tricky,
> especially if we are concerned about unrelated changes in the working
> tree.
>
> -Peff
-- 
Best,
Nikolai

```
