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

Internal Server Error on push (GetAffectedFiles()) #31738

Closed
f403 opened this issue Jul 30, 2024 · 9 comments · Fixed by #31778
Closed

Internal Server Error on push (GetAffectedFiles()) #31738

f403 opened this issue Jul 30, 2024 · 9 comments · Fixed by #31778
Labels
Milestone

Comments

@f403
Copy link

f403 commented Jul 30, 2024

Description

I was testing how branch protection works and if it is possible to protect modification of .gitea/actions from being modified in a PR. As far as I understand, Gitea is executing actions code from a PR, and not from main, but this must be another issue.

Here is the following bug.
If branch protection is set for branch * all pushes to new branches will fail with "Gitea: Internal Server Error (no message for end users)"

How to reproduce:

  1. Create a branch protection for branch *, set protected files to .gitea/**;mkdocs.yml;mkdocs.yaml, push - Enable Push
  2. clone repo via ssh, and do:
$ git branch pr-test
$ git checkout pr-test
Switched to branch 'pr-test'
$ touch text.txt
$ git add text.txt 
$ git commit -m 'test add file'
[pr-test 665f1b8] test add file
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 text.txt
$ git push origin pr-test
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 16 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 257 bytes | 257.00 KiB/s, done.
Total 2 (delta 0), reused 0 (delta 0), pack-reused 0
remote: 
remote: Gitea: Internal Server Error (no message for end users)
To demo.gitea.com:f403/GetAffectedFiles-test.git
 ! [remote rejected] pr-test -> pr-test (pre-receive hook declined)
error: failed to push some refs to 'demo.gitea.com:f403/GetAffectedFiles-test.git'

(Without protected files it works - this is how a new branch was created on demo.gitea)

gitea.log has following entries (hashes are differ from example above):

2024/07/30 07:41:54 ...ules/context/repo.go:643:RepoAssignment() [E] RepoAssignment Invalid repo /data/git/repositories/someorg/doc-test.git: signal: killed
2024/07/30 07:52:36 modules/git/diff.go:313:GetAffectedFiles() [E] Unable to get affected files for commits from 0000000000000000000000000000000000000000 to f28e63b56d8d5dfedca6e59167f1d32d3f15186c in /data/git/repositories/someorg/doc-test.git: exit status 128
2024/07/30 07:52:36 .../hook_pre_receive.go:235:preReceiveBranch() [E] Unable to check file protection for commits from 0000000000000000000000000000000000000000 to f28e63b56d8d5dfedca6e59167f1d32d3f15186c in <Repository 331:someorg/doc-test>: exit status 128
2024/07/30 07:52:36 ...s/private/ssh_log.go:26:SSHLog() [E] ssh: Internal Server Error (no message for end users) HookPreReceive(last) failed: internal API error response, status=500, err=Unable to check file protection for commits from 0000000000000000000000000000000000000000 to f28e63b56d8d5dfedca6e59167f1d32d3f15186c: exit status 128

Gitea Version

1.21.11

Can you reproduce the bug on the Gitea demo site?

Yes

Log Gist

No response

Screenshots

No response

Git Version

No response

Operating System

No response

How are you running Gitea?

Docker, but it can be reproduced on demo.gitea.com

Database

MySQL/MariaDB

@f403 f403 added the type/bug label Jul 30, 2024
@drecao
Copy link

drecao commented Aug 1, 2024

I have got the same trouble when I tried to push by ssh.

Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 205 bytes | 205.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
remote: fatal: not a git repository: '.'
error: remote unpack failed: unpack-objects abnormal exit
 ! [remote rejected] main -> main (unpacker error)
error: failed to push some refs to 'suzhe.cloud:suzhe/test.git'

The version of gitea I used is 1.22.1
Database is sqlite3

@drecao
Copy link

drecao commented Aug 1, 2024

OMG, I have solved my trouble.
The reason is that I once run these code below to use port 22

chmod root:root gitea
chmod u+s gitea
./gitea web

Maybe just because the user git has no rights to write in the data directory.
When I run the gitea as git, the trouble got missed.

Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 255 bytes | 255.00 KiB/s, done.
Total 4 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
remote: . Processing 1 references
remote: Processed 1 references in total
To ssh://192.168.1.23:2222/suzhe/test.git
 * [new branch]      main -> main

But I have to use port 2222 instead of 22 now

@drecao
Copy link

drecao commented Aug 1, 2024

Good news!
I found the suggestion that gitea gave.Now,I could run the application as gitea with port 22 being listened.

sudo setcap 'cap_net_bind_service=+ep' /PATH_TO_GITEA/gitea
./gitea web

With having run these as git, the trouble I met has got gone.

@f403
Copy link
Author

f403 commented Aug 2, 2024

Glad for you 👍
Yes, setting only required capabilities is better than running completely as root.

Unfortunately this has nothing in common with branch protection and GetAffectedFiles()

@lunny
Copy link
Member

lunny commented Aug 2, 2024

I cannot reproduce this on main branch according to your steps.

remote:
remote: Gitea: branch main is protected from changing file .gitea/workflows/test.yml
To http://localhost:3000/teabot/tddd.git
 ! [remote rejected] main -> main (pre-receive hook declined)
error: failed to push some refs to 'http://localhost:3000/teabot/tddd.git'

Take a look at source 2024/07/30 07:41:54 ...ules/context/repo.go:643:RepoAssignment() [E] RepoAssignment Invalid repo /data/git/repositories/someorg/doc-test.git: signal: killed
When opening a repository, some git command will run and the sub command have been killed or there is a disk permission problem. It should not be related with Gitea itself.

@f403
Copy link
Author

f403 commented Aug 2, 2024

I was pushing a new branch, maybe this is the key difference.
I could reproduce it on the demo site, so it is not only my setup. I'll check again with different combinations.

@lunny
Copy link
Member

lunny commented Aug 2, 2024

I was pushing a new branch, maybe this is the key difference. I could reproduce it on the demo site, so it is not only my setup. I'll check again with different combinations.

Could you paste demo site's repository URL here?

@f403
Copy link
Author

f403 commented Aug 2, 2024

Repo: https://demo.gitea.com/f403/GetAffectedFiles-test2
Settings: https://demo.gitea.com/f403/GetAffectedFiles-test2/settings/branches/edit?rule_name=%2a

Error:

git push origin pr-test
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 16 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 285 bytes | 285.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: 
remote: Gitea: Internal Server Error (no message for end users)
To demo.gitea.com:f403/GetAffectedFiles-test2.git
 ! [remote rejected] pr-test -> pr-test (pre-receive hook declined)
error: failed to push some refs to 'demo.gitea.com:f403/GetAffectedFiles-test2.git'

@lunny lunny added this to the 1.22.2 milestone Aug 2, 2024
@lunny
Copy link
Member

lunny commented Aug 2, 2024

Pushing to a new branch can reproduce it but not pushing to main branch.

GiteaBot pushed a commit to GiteaBot/gitea that referenced this issue Aug 7, 2024
)

Fix go-gitea#31738

When pushing a new branch, the old commit is zero. Most git commands
cannot recognize the zero commit id. To get the changed files in the
push, we need to get the first diverge commit of this branch. In most
situations, we could check commits one by one until one commit is
contained by another branch. Then we will think that commit is the
diverge point.

And in a pre-receive hook, this will be more difficult because all
commits haven't been merged and they actually stored in a temporary
place by git. So we need to bring some envs to let git know the commit
exist.
silverwind pushed a commit that referenced this issue Aug 8, 2024
…1796)

Backport #31778 by @lunny

Fix #31738

When pushing a new branch, the old commit is zero. Most git commands
cannot recognize the zero commit id. To get the changed files in the
push, we need to get the first diverge commit of this branch. In most
situations, we could check commits one by one until one commit is
contained by another branch. Then we will think that commit is the
diverge point.

And in a pre-receive hook, this will be more difficult because all
commits haven't been merged and they actually stored in a temporary
place by git. So we need to bring some envs to let git know the commit
exist.

Co-authored-by: Lunny Xiao <[email protected]>
@go-gitea go-gitea locked as resolved and limited conversation to collaborators Nov 5, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants