Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reverting a merge commit fails #22236

Closed
JensTimmerman opened this issue Dec 25, 2022 · 6 comments · Fixed by #28794
Closed

reverting a merge commit fails #22236

JensTimmerman opened this issue Dec 25, 2022 · 6 comments · Fixed by #28794
Labels
issue/confirmed Issue has been reviewed and confirmed to be present or accepted to be implemented type/bug
Milestone

Comments

@JensTimmerman
Copy link

JensTimmerman commented Dec 25, 2022

Description

Steps to reproduce:

Create a repository
create a commit
create a new branch
make a change to the file from the original commit in the new branch
create a pull request
merge the pull request
click the merge commit id e.g. https://try.gitea.io/JensTimmerman/test/commit/84f4738d2ab5263abe4dc2460b24c4d78aa6c68c
click on operations, revert
select the main branch in the pop up
select create a new branch for this commit and start a pull request on the revert page that shows up (e.g. https://try.gitea.io/JensTimmerman/test/_cherrypick/84f4738d2ab5263abe4dc2460b24c4d78aa6c68c/main?ref=main&refType=branch&cherry-pick-type=revert )
click propose file change

You get an error (on the page https://try.gitea.io/JensTimmerman/test/_cherrypick/84f4738d2ab5263abe4dc2460b24c4d78aa6c68c/main ):
Unable to apply patch 'Error: Stdout: Stderr: error: No valid patches in input (allow with "--allow-empty") Err: exit status 128'

(this error is slighty different on different versions of gitea, or perhaps depending on the exact test scenario)

The 'new patch' textfield shows
revert 84f4738d2ab5263abe4dc2460b24c4d78aa6c68c

instead of the code to be changed

The debug logs state:

2022/12/24 22:36:02 ...dules/git/command.go:179:Run() [D] [63a77ed2] /var/lib/gitea/data/tmp/local-repo/upload.git1909367275: /usr/bin/git -c protocol.version=2 -c uploadpack.allowfilter=true -c uploadpack.allowAnySHA1InWant=true -c credential.helper= read-tree -m a0830bee2fbb72fb55370bc8a9c0c4e05acba853 686d56c5e84498ca372c8ed86b9e743aa57c4955 b269bbeb490033dd65b029dce65b85780cd2a519
2022/12/24 22:36:02 ...rvices/pull/patch.go:238:AttemptThreeWayMerge() [E] [63a77ed2] Unable to run read-tree -m! Error: exit status 128 - fatal: this operation must be run in a work tree
	 - fatal: this operation must be run in a work tree

Gitea Version

1.17.4

Can you reproduce the bug on the Gitea demo site?

Yes

Log Gist

No response

Screenshots

image

Git Version

2.31.1

Operating System

centos8 stream

How are you running Gitea?

I'm using the do1jlr.gitea ansible role to deploy on a container on centos8stream running on proxmox.
But this is probably not relevant since I'm able to reproduce this issue on try.gitea.io.

Database

PostgreSQL

@JensTimmerman
Copy link
Author

I looked a bit better at the logs and found more information

2022/12/26 15:51:08 ...dules/git/command.go:182:Run() [D] [63a9c2eb-2] /var/lib/gitea/jens/test.git: /usr/bin/git -c protocol.version=2 -c credential.helper= rev-parse
2022/12/26 15:51:08 ...dules/git/command.go:182:Run() [D] [63a9c2eb-2] /var/lib/gitea/jens/test.git: /usr/bin/git -c protocol.version=2 -c credential.helper= show-ref --tags
2022/12/26 15:51:08 ...dules/git/command.go:182:Run() [D] [63a9c2eb-2] /var/lib/gitea/jens/test.git: /usr/bin/git -c protocol.version=2 -c credential.helper= cat-file --batch
2022/12/26 15:51:08 ...dules/git/command.go:182:Run() [D] [63a9c2eb-2] /var/lib/gitea/jens/test.git: /usr/bin/git -c protocol.version=2 -c credential.helper= cat-file --batch-check
2022/12/26 15:51:08 ...dules/git/command.go:182:Run() [D] [63a9c2eb-2] /var/lib/gitea/jens/test.git: /usr/bin/git -c protocol.version=2 -c credential.helper= show-ref --heads
2022/12/26 15:51:08 ...dules/git/command.go:182:Run() [D] [63a9c2eb-2] /var/lib/gitea/jens/test.git: /usr/bin/git -c protocol.version=2 -c credential.helper= config --get commit.gpgsign
2022/12/26 15:51:08 ...dules/git/command.go:180:Run() [D] [63a9c2eb-2] /usr/bin/git -c protocol.version=2 -c credential.helper= clone -s --bare -b master /var/lib/gitea/jens/test.git /var/lib/gitea/data/tmp/local-repo/upload.git3912241519
2022/12/26 15:51:08 ...s/context/context.go:220:HTML() [D] [63a9c2ec-8] Template: user/auth/signin
2022/12/26 15:51:09 ...dules/git/command.go:182:Run() [D] [63a9c2eb-2] /var/lib/gitea/data/tmp/local-repo/upload.git3912241519: /usr/bin/git -c protocol.version=2 -c credential.helper= rev-parse
2022/12/26 15:51:09 ...dules/git/command.go:182:Run() [D] [63a9c2eb-2] /var/lib/gitea/data/tmp/local-repo/upload.git3912241519: /usr/bin/git -c protocol.version=2 -c credential.helper= read-tree HEAD
2022/12/26 15:51:09 ...dules/git/command.go:182:Run() [D] [63a9c2eb-2] /var/lib/gitea/data/tmp/local-repo/upload.git3912241519: /usr/bin/git -c protocol.version=2 -c credential.helper= cat-file --batch
2022/12/26 15:51:09 ...dules/git/command.go:182:Run() [D] [63a9c2eb-2] /var/lib/gitea/data/tmp/local-repo/upload.git3912241519: /usr/bin/git -c protocol.version=2 -c credential.helper= cat-file --batch-check
2022/12/26 15:51:09 ...dules/git/command.go:182:Run() [D] [63a9c2eb-2] /var/lib/gitea/data/tmp/local-repo/upload.git3912241519: /usr/bin/git -c protocol.version=2 -c credential.helper= read-tree -m 3644c871176f9638fc01aaec036c80a44982c00f 3644c871176f9638fc01aaec036c80a44982c00f b3d7a10bf0c1ad339c83319688d34bb52c0666f4
2022/12/26 15:51:09 ...rvices/pull/patch.go:240:AttemptThreeWayMerge() [E] [63a9c2eb-2] Unable to run read-tree -m! Error: exit status 128 - fatal: this operation must be run in a work tree
	 - fatal: this operation must be run in a work tree
	
2022/12/26 15:51:09 ...dules/git/command.go:182:Run() [D] [63a9c2eb-2] /var/lib/gitea/jens/test.git: /usr/bin/git -c protocol.version=2 -c credential.helper= show --pretty=format:revert %H%n -R 3644c871176f9638fc01aaec036c80a44982c00f
2022/12/26 15:51:09 ...dules/git/command.go:180:Run() [D] [63a9c2eb-2] /usr/bin/git -c protocol.version=2 -c credential.helper= clone -s --bare -b master /var/lib/gitea/jens/test.git /var/lib/gitea/data/tmp/local-repo/upload.git3308124929
2022/12/26 15:51:10 ...dules/git/command.go:182:Run() [D] [63a9c2eb-2] /var/lib/gitea/data/tmp/local-repo/upload.git3308124929: /usr/bin/git -c protocol.version=2 -c credential.helper= rev-parse
2022/12/26 15:51:10 ...dules/git/command.go:182:Run() [D] [63a9c2eb-2] /var/lib/gitea/data/tmp/local-repo/upload.git3308124929: /usr/bin/git -c protocol.version=2 -c credential.helper= read-tree HEAD
2022/12/26 15:51:10 ...dules/git/command.go:182:Run() [D] [63a9c2eb-2] /var/lib/gitea/data/tmp/local-repo/upload.git3308124929: /usr/bin/git -c protocol.version=2 -c credential.helper= cat-file --batch
2022/12/26 15:51:10 ...dules/git/command.go:182:Run() [D] [63a9c2eb-2] /var/lib/gitea/data/tmp/local-repo/upload.git3308124929: /usr/bin/git -c protocol.version=2 -c credential.helper= cat-file --batch-check
2022/12/26 15:51:10 ...dules/git/command.go:182:Run() [D] [63a9c2eb-2] /var/lib/gitea/data/tmp/local-repo/upload.git3308124929: /usr/bin/git -c protocol.version=2 -c credential.helper= apply --index --recount --cached --ignore-whitespace --whitespace=fix --binary


It looks like the --bare should not be in the initial clone to the temp dir if git expects this read tree to be run in a work tree?

@JensTimmerman
Copy link
Author

JensTimmerman commented Dec 26, 2022

Enabling traces shows

2022/12/26 16:04:43 ...rvices/pull/patch.go:240:AttemptThreeWayMerge() [E] [63a9c61a] Unable to run read-tree -m! Error: exit status 128 - fatal: this operation must be run in a work tree
	 - fatal: this operation must be run in a work tree
	
	/source/services/pull/patch.go:240 (0x1ba540d)
	/source/services/repository/files/cherry_pick.go:81 (0x1fa8e9c)
	/source/routers/web/repo/cherry_pick.go:127 (0x20f8afb)
	/source/modules/web/wrap_convert.go:47 (0x1f57036)
	/source/modules/web/wrap.go:41 (0x1f55269)
	/usr/local/go/src/net/http/server.go:2084 (0x93e22e)
	/go/pkg/mod/github.com/go-chi/chi/v5@v5.0.7/mux.go:442 (0x173ce95)
	/usr/local/go/src/net/http/server.go:2084 (0x93e22e)

and

2022/12/26 16:04:43 ...dules/git/command.go:182:Run() [D] [63a9c61a] /var/lib/gitea/data/tmp/local-repo/upload.git1078981948: /usr/bin/git -c protocol.version=2 -c credential.helper= read-tree -m 3644c871176f9638fc01aaec036c80a44982c00f 3644c871176f9638fc01aaec036c80a44982c00f b3d7a10bf0c1ad339c83319688d34bb52c0666f4
	/source/modules/git/command.go:182 (0xc649d1)
	/source/modules/git/command.go:325 (0xc65b04)
	/source/modules/git/command.go:291 (0xc6589c)
	/source/services/pull/patch.go:239 (0x1ba52c4)
	/source/services/repository/files/cherry_pick.go:81 (0x1fa8e9c)
	/source/routers/web/repo/cherry_pick.go:127 (0x20f8afb)
	/source/modules/web/wrap_convert.go:47 (0x1f57036)
	/source/modules/web/wrap.go:41 (0x1f55269)
	/usr/local/go/src/net/http/server.go:2084 (0x93e22e)

@lunny lunny added this to the 1.17.5 milestone Dec 27, 2022
@lunny lunny removed this from the 1.17.5 milestone Mar 20, 2023
@spielerf
Copy link

Got same problem while reverting

Git Version - 2.31.1, Wire Protocol Version 2 Enabled
Gitea Version - 1.19.3 built with GNU Make 4.1, go1.20.4
RockyLinux 8.7

@lunny lunny added the issue/confirmed Issue has been reviewed and confirmed to be present or accepted to be implemented label Jun 25, 2023
@lunny lunny added this to the 1.20.0 milestone Jul 3, 2023
@6543 6543 modified the milestones: 1.20.0, 1.21.0 Jul 3, 2023
@lunny lunny modified the milestones: 1.21.0, 1.22.0 Sep 21, 2023
@me-heer
Copy link
Contributor

me-heer commented Jan 10, 2024

I'd like to work on this if no one else has picked it.

lunny pushed a commit that referenced this issue Jan 16, 2024
Fixes #22236

---
Error occurring currently while trying to revert commit using read-tree
-m approach:
> 2022/12/26 16:04:43 ...rvices/pull/patch.go:240:AttemptThreeWayMerge()
[E] [63a9c61a] Unable to run read-tree -m! Error: exit status 128 -
fatal: this operation must be run in a work tree
> 	 - fatal: this operation must be run in a work tree

We need to clone a non-bare repository for `git read-tree -m` to work.

bb371ae
adds support to create a non-bare cloned temporary upload repository.

After cloning a non-bare temporary upload repository, we [set default
index](https://github.com/go-gitea/gitea/blob/main/services/repository/files/cherry_pick.go#L37)
(`git read-tree HEAD`).
This operation ends up resetting the git index file (see investigation
details below), due to which, we need to call `git update-index
--refresh` afterward.


Here's the diff of the index file before and after we execute
SetDefaultIndex: https://www.diffchecker.com/hyOP3eJy/

Notice the **ctime**, **mtime** are set to 0 after SetDefaultIndex.

You can reproduce the same behavior using these steps:
```bash
$ git clone https://try.gitea.io/me-heer/test.git -s -b main
$ cd test
$ git read-tree HEAD
$ git read-tree -m 1f085d7ed8 1f085d7ed8 9933caed00
error: Entry '1' not uptodate. Cannot merge.
```

After which, we can fix like this:
```
$ git update-index --refresh
$ git read-tree -m 1f085d7ed8 1f085d7ed8 9933caed00
```
me-heer added a commit to me-heer/gitea that referenced this issue Jan 17, 2024
Fixes go-gitea#22236

---
Error occurring currently while trying to revert commit using read-tree
-m approach:
> 2022/12/26 16:04:43 ...rvices/pull/patch.go:240:AttemptThreeWayMerge()
[E] [63a9c61a] Unable to run read-tree -m! Error: exit status 128 -
fatal: this operation must be run in a work tree
> 	 - fatal: this operation must be run in a work tree

We need to clone a non-bare repository for `git read-tree -m` to work.

go-gitea@bb371ae
adds support to create a non-bare cloned temporary upload repository.

After cloning a non-bare temporary upload repository, we [set default
index](https://github.com/go-gitea/gitea/blob/main/services/repository/files/cherry_pick.go#L37)
(`git read-tree HEAD`).
This operation ends up resetting the git index file (see investigation
details below), due to which, we need to call `git update-index
--refresh` afterward.

Here's the diff of the index file before and after we execute
SetDefaultIndex: https://www.diffchecker.com/hyOP3eJy/

Notice the **ctime**, **mtime** are set to 0 after SetDefaultIndex.

You can reproduce the same behavior using these steps:
```bash
$ git clone https://try.gitea.io/me-heer/test.git -s -b main
$ cd test
$ git read-tree HEAD
$ git read-tree -m 1f085d7ed8 1f085d7ed8 9933caed00
error: Entry '1' not uptodate. Cannot merge.
```

After which, we can fix like this:
```
$ git update-index --refresh
$ git read-tree -m 1f085d7ed8 1f085d7ed8 9933caed00
```
me-heer added a commit to me-heer/gitea that referenced this issue Jan 17, 2024
Fixes go-gitea#22236

---
Error occurring currently while trying to revert commit using read-tree
-m approach:
> 2022/12/26 16:04:43 ...rvices/pull/patch.go:240:AttemptThreeWayMerge()
[E] [63a9c61a] Unable to run read-tree -m! Error: exit status 128 -
fatal: this operation must be run in a work tree
> 	 - fatal: this operation must be run in a work tree

We need to clone a non-bare repository for `git read-tree -m` to work.

go-gitea@bb371ae
adds support to create a non-bare cloned temporary upload repository.

After cloning a non-bare temporary upload repository, we [set default
index](https://github.com/go-gitea/gitea/blob/main/services/repository/files/cherry_pick.go#L37)
(`git read-tree HEAD`).
This operation ends up resetting the git index file (see investigation
details below), due to which, we need to call `git update-index
--refresh` afterward.

Here's the diff of the index file before and after we execute
SetDefaultIndex: https://www.diffchecker.com/hyOP3eJy/

Notice the **ctime**, **mtime** are set to 0 after SetDefaultIndex.

You can reproduce the same behavior using these steps:
```bash
$ git clone https://try.gitea.io/me-heer/test.git -s -b main
$ cd test
$ git read-tree HEAD
$ git read-tree -m 1f085d7ed8 1f085d7ed8 9933caed00
error: Entry '1' not uptodate. Cannot merge.
```

After which, we can fix like this:
```
$ git update-index --refresh
$ git read-tree -m 1f085d7ed8 1f085d7ed8 9933caed00
```
@JensTimmerman
Copy link
Author

thank you for looking into this @me-heer !

fuxiaohei pushed a commit to fuxiaohei/gitea that referenced this issue Jan 17, 2024
Fixes go-gitea#22236

---
Error occurring currently while trying to revert commit using read-tree
-m approach:
> 2022/12/26 16:04:43 ...rvices/pull/patch.go:240:AttemptThreeWayMerge()
[E] [63a9c61a] Unable to run read-tree -m! Error: exit status 128 -
fatal: this operation must be run in a work tree
> 	 - fatal: this operation must be run in a work tree

We need to clone a non-bare repository for `git read-tree -m` to work.

go-gitea@bb371ae
adds support to create a non-bare cloned temporary upload repository.

After cloning a non-bare temporary upload repository, we [set default
index](https://github.com/go-gitea/gitea/blob/main/services/repository/files/cherry_pick.go#L37)
(`git read-tree HEAD`).
This operation ends up resetting the git index file (see investigation
details below), due to which, we need to call `git update-index
--refresh` afterward.


Here's the diff of the index file before and after we execute
SetDefaultIndex: https://www.diffchecker.com/hyOP3eJy/

Notice the **ctime**, **mtime** are set to 0 after SetDefaultIndex.

You can reproduce the same behavior using these steps:
```bash
$ git clone https://try.gitea.io/me-heer/test.git -s -b main
$ cd test
$ git read-tree HEAD
$ git read-tree -m 1f085d7ed8 1f085d7ed8 9933caed00
error: Entry '1' not uptodate. Cannot merge.
```

After which, we can fix like this:
```
$ git update-index --refresh
$ git read-tree -m 1f085d7ed8 1f085d7ed8 9933caed00
```
lunny pushed a commit that referenced this issue Jan 21, 2024
Backport #28794

Fixes #22236

---
Error occurring currently while trying to revert commit using read-tree
-m approach:
> 2022/12/26 16:04:43 ...rvices/pull/patch.go:240:AttemptThreeWayMerge()
[E] [63a9c61a] Unable to run read-tree -m! Error: exit status 128 -
fatal: this operation must be run in a work tree
> 	 - fatal: this operation must be run in a work tree

We need to clone a non-bare repository for `git read-tree -m` to work.


bb371ae
adds support to create a non-bare cloned temporary upload repository.

After cloning a non-bare temporary upload repository, we [set default
index](https://github.com/go-gitea/gitea/blob/main/services/repository/files/cherry_pick.go#L37)
(`git read-tree HEAD`).
This operation ends up resetting the git index file (see investigation
details below), due to which, we need to call `git update-index
--refresh` afterward.

Here's the diff of the index file before and after we execute
SetDefaultIndex: https://www.diffchecker.com/hyOP3eJy/

Notice the **ctime**, **mtime** are set to 0 after SetDefaultIndex.

You can reproduce the same behavior using these steps:
```bash
$ git clone https://try.gitea.io/me-heer/test.git -s -b main
$ cd test
$ git read-tree HEAD
$ git read-tree -m 1f085d7ed8 1f085d7ed8 9933caed00
error: Entry '1' not uptodate. Cannot merge.
```

After which, we can fix like this:
```bash
$ git update-index --refresh
$ git read-tree -m 1f085d7ed8 1f085d7ed8 9933caed00
```
henrygoodman pushed a commit to henrygoodman/gitea that referenced this issue Jan 31, 2024
Fixes go-gitea#22236

---
Error occurring currently while trying to revert commit using read-tree
-m approach:
> 2022/12/26 16:04:43 ...rvices/pull/patch.go:240:AttemptThreeWayMerge()
[E] [63a9c61a] Unable to run read-tree -m! Error: exit status 128 -
fatal: this operation must be run in a work tree
> 	 - fatal: this operation must be run in a work tree

We need to clone a non-bare repository for `git read-tree -m` to work.

go-gitea@bb371ae
adds support to create a non-bare cloned temporary upload repository.

After cloning a non-bare temporary upload repository, we [set default
index](https://github.com/go-gitea/gitea/blob/main/services/repository/files/cherry_pick.go#L37)
(`git read-tree HEAD`).
This operation ends up resetting the git index file (see investigation
details below), due to which, we need to call `git update-index
--refresh` afterward.


Here's the diff of the index file before and after we execute
SetDefaultIndex: https://www.diffchecker.com/hyOP3eJy/

Notice the **ctime**, **mtime** are set to 0 after SetDefaultIndex.

You can reproduce the same behavior using these steps:
```bash
$ git clone https://try.gitea.io/me-heer/test.git -s -b main
$ cd test
$ git read-tree HEAD
$ git read-tree -m 1f085d7ed8 1f085d7ed8 9933caed00
error: Entry '1' not uptodate. Cannot merge.
```

After which, we can fix like this:
```
$ git update-index --refresh
$ git read-tree -m 1f085d7ed8 1f085d7ed8 9933caed00
```
silverwind pushed a commit to silverwind/gitea that referenced this issue Feb 20, 2024
Fixes go-gitea#22236

---
Error occurring currently while trying to revert commit using read-tree
-m approach:
> 2022/12/26 16:04:43 ...rvices/pull/patch.go:240:AttemptThreeWayMerge()
[E] [63a9c61a] Unable to run read-tree -m! Error: exit status 128 -
fatal: this operation must be run in a work tree
> 	 - fatal: this operation must be run in a work tree

We need to clone a non-bare repository for `git read-tree -m` to work.

go-gitea@bb371ae
adds support to create a non-bare cloned temporary upload repository.

After cloning a non-bare temporary upload repository, we [set default
index](https://github.com/go-gitea/gitea/blob/main/services/repository/files/cherry_pick.go#L37)
(`git read-tree HEAD`).
This operation ends up resetting the git index file (see investigation
details below), due to which, we need to call `git update-index
--refresh` afterward.


Here's the diff of the index file before and after we execute
SetDefaultIndex: https://www.diffchecker.com/hyOP3eJy/

Notice the **ctime**, **mtime** are set to 0 after SetDefaultIndex.

You can reproduce the same behavior using these steps:
```bash
$ git clone https://try.gitea.io/me-heer/test.git -s -b main
$ cd test
$ git read-tree HEAD
$ git read-tree -m 1f085d7ed8 1f085d7ed8 9933caed00
error: Entry '1' not uptodate. Cannot merge.
```

After which, we can fix like this:
```
$ git update-index --refresh
$ git read-tree -m 1f085d7ed8 1f085d7ed8 9933caed00
```
Copy link

github-actions bot commented Mar 1, 2024

Automatically locked because of our CONTRIBUTING guidelines

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
issue/confirmed Issue has been reviewed and confirmed to be present or accepted to be implemented type/bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants