error 500 on "create branch" with pre-receive hook active #4937

Closed
opened 2025-11-02 06:08:13 -06:00 by GiteaMirror · 10 comments
Owner

Originally created by @mbarinc on GitHub (Feb 25, 2020).

  • Gitea version (or commit ref): 1.11.3
  • Git version: 2.23.0
  • Operating system: RH 7.7 (Maipo)
  • Database (use [x]):
    • PostgreSQL
    • MySQL
    • MSSQL
    • SQLite
  • Can you reproduce the bug at https://try.gitea.io:
    • Yes (provide example URL)
    • No
    • Not relevant
  • Log gist:

Description

I've implement the following pre-receive Git hook in order to have a standard prefix naming convention of branches: lab/* (for POC or demo) - develop/* (for any developments) - release/* (for stable feature):

#!/bin/bash

# variable init
read oldrev newrev refname

# check if it's not a tag
if [[ -n $(echo $refname | grep "refs/tags/" ) ]]; then exit 0; fi

export branch=`echo $refname | sed 's/^refs\/heads\///'`
export regex="^master|^develop/|^release/|^lab/"

# check branch prefix name
msgError="\nGT-HOOK-ERR: Il nome branch '$branch' non è conforme agli standard\n"
if [ ! $(echo $branch | egrep $regex) ]; then
        echo -e "$msgError"
        exit 1
fi

This Git hook is working fine, from shell I haven't problem.
But when I create a branch from Gitea, if the prefix is not respected, Gitea retung an error 500.
It's possible to manage the error code and print the hook message instead the HTML error 500 that is bad to see?
For example, I've implement another pre-receive Git hook in order to accept only commit message with specific sintax, in this case, if the git hook is triggered, Gitea print the $msgError not a error 500.

thank in advance

Originally created by @mbarinc on GitHub (Feb 25, 2020). - Gitea version (or commit ref): 1.11.3 - Git version: 2.23.0 - Operating system: RH 7.7 (Maipo) - Database (use `[x]`): - [ ] PostgreSQL - [x] MySQL - [ ] MSSQL - [ ] SQLite - Can you reproduce the bug at https://try.gitea.io: - [ ] Yes (provide example URL) - [x] No - [ ] Not relevant - Log gist: ## Description I've implement the following pre-receive Git hook in order to have a standard prefix naming convention of branches: lab/* (for POC or demo) - develop/* (for any developments) - release/* (for stable feature): ```` shell #!/bin/bash # variable init read oldrev newrev refname # check if it's not a tag if [[ -n $(echo $refname | grep "refs/tags/" ) ]]; then exit 0; fi export branch=`echo $refname | sed 's/^refs\/heads\///'` export regex="^master|^develop/|^release/|^lab/" # check branch prefix name msgError="\nGT-HOOK-ERR: Il nome branch '$branch' non è conforme agli standard\n" if [ ! $(echo $branch | egrep $regex) ]; then echo -e "$msgError" exit 1 fi ```` This Git hook is working fine, from shell I haven't problem. But when I create a branch from Gitea, if the prefix is not respected, Gitea retung an error 500. It's possible to manage the error code and print the hook message instead the HTML error 500 that is bad to see? For example, I've implement another pre-receive Git hook in order to accept only commit message with specific sintax, in this case, if the git hook is triggered, Gitea print the $msgError not a error 500. thank in advance
GiteaMirror added the type/bug label 2025-11-02 06:08:13 -06:00
Author
Owner

@guillep2k commented on GitHub (Feb 27, 2020):

I believe #10495 (#10373) will solve your problem. It will probably be featured in 1.11.2.

@guillep2k commented on GitHub (Feb 27, 2020): I believe #10495 (#10373) will solve your problem. It will probably be featured in 1.11.2.
Author
Owner

@mbarinc commented on GitHub (Mar 16, 2020):

I've tried with 1.11.3 but unfortunately there is still the issue. I've updated the description on top.

@mbarinc commented on GitHub (Mar 16, 2020): I've tried with 1.11.3 but unfortunately there is still the issue. I've updated the description on top.
Author
Owner

@guillep2k commented on GitHub (Mar 17, 2020):

Could you please check your error log? There should be some error entries ([E]) related to that action that could be useful.

@guillep2k commented on GitHub (Mar 17, 2020): Could you please check your error log? There should be some error entries ([E]) related to that action that could be useful.
Author
Owner

@mbarinc commented on GitHub (Mar 26, 2020):

Sure @guillep2k.
This is what I found in the log:

2020/03/26 19:33:19 .../xorm/session_raw.go:78:queryRows() [I] [SQL] SELECT `id`, `repo_id`, `branch_name`, `can_push`, `enable_whitelist`, `whitelist_user_i_ds`, `whitelist_team_i_ds`, `enable_merge_whitelist`, `whitelist_deploy_keys`, `merge_whitelist_user_i_ds`, `merge_whitelist_team_i_ds`, `enable_status_check`, `status_check_contexts`, `enable_approvals_whitelist`, `approvals_whitelist_user_i_ds`, `approvals_whitelist_team_i_ds`, `required_approvals`, `block_on_rejected_reviews`, `created_unix`, `updated_unix` FROM `protected_branch` WHERE `repo_id`=? AND `branch_name`=? LIMIT 1 []interface {}{8, "branch-hello"} - took: 149.987µs
2020/03/26 19:33:19 ...uters/repo/branch.go:351:CreateBranch() [E] CreateNewBranch: Push: exit status 1 - remote:
        remote: GT-HOOK-ERR: Il nome branch 'branch-hello' non è conforme agli standard\03303d[K
        remote:
        To /app/appsup/gitea/repositories/test/test.git
         ! [remote rejected] branch-hello -> branch-hello (pre-receive hook declined)
        error: failed to push some refs to '/app/appsup/gitea/repositories/test/test.git'

2020/03/26 19:33:19 ...s/context/context.go:139:HTML() [D] Template: status/500

@mbarinc commented on GitHub (Mar 26, 2020): Sure @guillep2k. This is what I found in the log: ``` 2020/03/26 19:33:19 .../xorm/session_raw.go:78:queryRows() [I] [SQL] SELECT `id`, `repo_id`, `branch_name`, `can_push`, `enable_whitelist`, `whitelist_user_i_ds`, `whitelist_team_i_ds`, `enable_merge_whitelist`, `whitelist_deploy_keys`, `merge_whitelist_user_i_ds`, `merge_whitelist_team_i_ds`, `enable_status_check`, `status_check_contexts`, `enable_approvals_whitelist`, `approvals_whitelist_user_i_ds`, `approvals_whitelist_team_i_ds`, `required_approvals`, `block_on_rejected_reviews`, `created_unix`, `updated_unix` FROM `protected_branch` WHERE `repo_id`=? AND `branch_name`=? LIMIT 1 []interface {}{8, "branch-hello"} - took: 149.987µs 2020/03/26 19:33:19 ...uters/repo/branch.go:351:CreateBranch() [E] CreateNewBranch: Push: exit status 1 - remote: remote: GT-HOOK-ERR: Il nome branch 'branch-hello' non è conforme agli standard\03303d[K remote: To /app/appsup/gitea/repositories/test/test.git ! [remote rejected] branch-hello -> branch-hello (pre-receive hook declined) error: failed to push some refs to '/app/appsup/gitea/repositories/test/test.git' 2020/03/26 19:33:19 ...s/context/context.go:139:HTML() [D] Template: status/500 ```
Author
Owner

@guillep2k commented on GitHub (Mar 27, 2020):

Sanitizing the error message to avoid exposing system internals is difficult and something we're still struggling to manage properly. 🙁

Maybe we could enable some kind of regexp white list for these cases?

@guillep2k commented on GitHub (Mar 27, 2020): Sanitizing the error message to avoid exposing system internals is difficult and something we're still struggling to manage properly. 🙁 Maybe we could enable some kind of regexp white list for these cases?
Author
Owner

@zeripath commented on GitHub (Mar 27, 2020):

Ah! We're not handling the case of a remote rejected err in create branch!

@zeripath commented on GitHub (Mar 27, 2020): Ah! We're not handling the case of a remote rejected err in create branch!
Author
Owner

@mbarinc commented on GitHub (Apr 15, 2020):

I've tried with 1.12 dev-152, that merged this issue and it's OK.
The hook is triggered correcly and I receive the message that I wrote in the hook, without any error 500 from Gitea.
But there is a side effect, because when I trying to open a Pull Request I receving an error 500.
In the log i found this:

2020/04/15 12:54:43 ...m.io/xorm/core/tx.go:130:ExecContext() [I] [SQL] INSERT INTO `pull_request` (`type`,`status`,`conflicted_files`,`commits_ahead`,`commits_behind`,`issue_id`,`index`,`head_repo_id`,`base_repo_id`,`head_branch`,`base_branch`,`merge_base`,`has_merged`,`merged_commit_id`,`merger_id`,`merged_unix`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [0 0 ["README.md","prova"] 10 31 62 19 8 8 lab/prova master 05e79d2b77eeef042710a92cdd1db2f876036f94 false  0 1586948083] - 428.723µs
2020/04/15 12:54:43 ...o/xorm/session_tx.go:75:Commit() [I] [SQL] COMMIT [] - 5.765333ms
2020/04/15 12:54:43 ...ervices/pull/pull.go:337:PushToBaseRepo() [T] PushToBaseRepo[8]: pushing commits to base repo 'refs/pull/19/head'
2020/04/15 12:54:44 routers/repo/pull.go:949:CompareAndPullRequestPost() [E] NewPullRequest: Push: TEST/test:lab/prova TEST/test:refs/pull/19/head PushRejected Error: exit status 1: remote:
        remote: GT-HOOK-ERR: Il nome branch 'refs/pull/19/head' non è conforme agli standard\03303d[K
        remote:
        To /app/appsup/gitea/repositories/test/test.git
         ! [remote rejected] lab/prova -> refs/pull/19/head (pre-receive hook declined)
        error: failed to push some refs to '/app/appsup/gitea/repositories/test/test.git'


2020/04/15 12:54:44 ...s/context/context.go:139:HTML() [D] Template: status/500

Even if it in error, in the Pull Request page I found the PR number 19 opened but, is not possible to go to it because Gitea go in error 500:

2020/04/15 12:58:03 ...s/repo_permission.go:154:func1() [T] Permission Loaded for 824775322288:confman in 824775322368:TEST/test:
        Permissions: AccessMode: 824775322928:owner, 5 Units, 0 UnitsMode(s): [
        Units[0]: ID: 824775322464 RepoID: 824775322496 Type: 824775322960:UnitTypeCode Config: {}
        Units[1]: ID: 824775322560 RepoID: 824775322592 Type: 824775322992:UnitTypeIssues Config: {"EnableTimetracker":true,"AllowOnlyContributorsToTrackTime":true,"EnableDependencies":true}
        Units[2]: ID: 824775322656 RepoID: 824775322688 Type: 824775323024:UnitTypePullRequests Config: {"IgnoreWhitespaceConflicts":false,"AllowMerge":true,"AllowRebase":true,"AllowRebaseMerge":true,"AllowSquash":true}
        Units[3]: ID: 824775322752 RepoID: 824775322784 Type: 824775323056:UnitTypeReleases Config: {}
        Units[4]: ID: 824775322848 RepoID: 824775322880 Type: 824775323088:UnitTypeWiki Config: {} ]
2020/04/15 12:58:03 ...m.io/xorm/core/db.go:154:QueryContext() [I] [SQL] SELECT `id`, `owner_id`, `owner_name`, `lower_name`, `name`, `description`, `website`, `original_service_type`, `original_url`, `default_branch`, `num_watches`, `num_stars`, `num_forks`, `num_issues`, `num_closed_issues`, `num_pulls`, `num_closed_pulls`, `num_milestones`, `num_closed_milestones`, `is_private`, `is_empty`, `is_archived`, `is_mirror`, `status`, `is_fork`, `fork_id`, `is_template`, `template_id`, `size`, `is_fsck_enabled`, `close_issues_via_commit_in_any_branch`, `topics`, `avatar`, `created_unix`, `updated_unix` FROM `repository` WHERE `id`=? LIMIT 1 [8] - 149.099µs
2020/04/15 12:58:03 ...m.io/xorm/core/db.go:154:QueryContext() [I] [SQL] SELECT `id`, `repo_id`, `branch_name`, `can_push`, `enable_whitelist`, `whitelist_user_i_ds`, `whitelist_team_i_ds`, `enable_merge_whitelist`, `whitelist_deploy_keys`, `merge_whitelist_user_i_ds`, `merge_whitelist_team_i_ds`, `enable_status_check`, `status_check_contexts`, `enable_approvals_whitelist`, `approvals_whitelist_user_i_ds`, `approvals_whitelist_team_i_ds`, `required_approvals`, `block_on_rejected_reviews`, `dismiss_stale_approvals`, `require_signed_commits`, `protected_file_patterns`, `created_unix`, `updated_unix` FROM `protected_branch` WHERE `repo_id`=? AND `branch_name`=? LIMIT 1 [8 lab/prova] - 136.908µs
2020/04/15 12:58:03 routers/repo/pull.go:431:PrepareViewPullInfo() [E] GetRefCommitID(refs/pull/19/head): object does not exist [id: refs/pull/19/head, rel_path: ]
2020/04/15 12:58:03 ...s/context/context.go:139:HTML() [D] Template: status/500

It's possible to fix this collateral problem?

Thanks in advance

@mbarinc commented on GitHub (Apr 15, 2020): I've tried with 1.12 dev-152, that merged this issue and it's OK. The hook is triggered correcly and I receive the message that I wrote in the hook, without any error 500 from Gitea. But there is a side effect, because when I trying to open a Pull Request I receving an error 500. In the log i found this: ```` 2020/04/15 12:54:43 ...m.io/xorm/core/tx.go:130:ExecContext() [I] [SQL] INSERT INTO `pull_request` (`type`,`status`,`conflicted_files`,`commits_ahead`,`commits_behind`,`issue_id`,`index`,`head_repo_id`,`base_repo_id`,`head_branch`,`base_branch`,`merge_base`,`has_merged`,`merged_commit_id`,`merger_id`,`merged_unix`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [0 0 ["README.md","prova"] 10 31 62 19 8 8 lab/prova master 05e79d2b77eeef042710a92cdd1db2f876036f94 false 0 1586948083] - 428.723µs 2020/04/15 12:54:43 ...o/xorm/session_tx.go:75:Commit() [I] [SQL] COMMIT [] - 5.765333ms 2020/04/15 12:54:43 ...ervices/pull/pull.go:337:PushToBaseRepo() [T] PushToBaseRepo[8]: pushing commits to base repo 'refs/pull/19/head' 2020/04/15 12:54:44 routers/repo/pull.go:949:CompareAndPullRequestPost() [E] NewPullRequest: Push: TEST/test:lab/prova TEST/test:refs/pull/19/head PushRejected Error: exit status 1: remote: remote: GT-HOOK-ERR: Il nome branch 'refs/pull/19/head' non è conforme agli standard\03303d[K remote: To /app/appsup/gitea/repositories/test/test.git ! [remote rejected] lab/prova -> refs/pull/19/head (pre-receive hook declined) error: failed to push some refs to '/app/appsup/gitea/repositories/test/test.git' 2020/04/15 12:54:44 ...s/context/context.go:139:HTML() [D] Template: status/500 ```` Even if it in error, in the Pull Request page I found the PR number 19 opened but, is not possible to go to it because Gitea go in error 500: ```` 2020/04/15 12:58:03 ...s/repo_permission.go:154:func1() [T] Permission Loaded for 824775322288:confman in 824775322368:TEST/test: Permissions: AccessMode: 824775322928:owner, 5 Units, 0 UnitsMode(s): [ Units[0]: ID: 824775322464 RepoID: 824775322496 Type: 824775322960:UnitTypeCode Config: {} Units[1]: ID: 824775322560 RepoID: 824775322592 Type: 824775322992:UnitTypeIssues Config: {"EnableTimetracker":true,"AllowOnlyContributorsToTrackTime":true,"EnableDependencies":true} Units[2]: ID: 824775322656 RepoID: 824775322688 Type: 824775323024:UnitTypePullRequests Config: {"IgnoreWhitespaceConflicts":false,"AllowMerge":true,"AllowRebase":true,"AllowRebaseMerge":true,"AllowSquash":true} Units[3]: ID: 824775322752 RepoID: 824775322784 Type: 824775323056:UnitTypeReleases Config: {} Units[4]: ID: 824775322848 RepoID: 824775322880 Type: 824775323088:UnitTypeWiki Config: {} ] 2020/04/15 12:58:03 ...m.io/xorm/core/db.go:154:QueryContext() [I] [SQL] SELECT `id`, `owner_id`, `owner_name`, `lower_name`, `name`, `description`, `website`, `original_service_type`, `original_url`, `default_branch`, `num_watches`, `num_stars`, `num_forks`, `num_issues`, `num_closed_issues`, `num_pulls`, `num_closed_pulls`, `num_milestones`, `num_closed_milestones`, `is_private`, `is_empty`, `is_archived`, `is_mirror`, `status`, `is_fork`, `fork_id`, `is_template`, `template_id`, `size`, `is_fsck_enabled`, `close_issues_via_commit_in_any_branch`, `topics`, `avatar`, `created_unix`, `updated_unix` FROM `repository` WHERE `id`=? LIMIT 1 [8] - 149.099µs 2020/04/15 12:58:03 ...m.io/xorm/core/db.go:154:QueryContext() [I] [SQL] SELECT `id`, `repo_id`, `branch_name`, `can_push`, `enable_whitelist`, `whitelist_user_i_ds`, `whitelist_team_i_ds`, `enable_merge_whitelist`, `whitelist_deploy_keys`, `merge_whitelist_user_i_ds`, `merge_whitelist_team_i_ds`, `enable_status_check`, `status_check_contexts`, `enable_approvals_whitelist`, `approvals_whitelist_user_i_ds`, `approvals_whitelist_team_i_ds`, `required_approvals`, `block_on_rejected_reviews`, `dismiss_stale_approvals`, `require_signed_commits`, `protected_file_patterns`, `created_unix`, `updated_unix` FROM `protected_branch` WHERE `repo_id`=? AND `branch_name`=? LIMIT 1 [8 lab/prova] - 136.908µs 2020/04/15 12:58:03 routers/repo/pull.go:431:PrepareViewPullInfo() [E] GetRefCommitID(refs/pull/19/head): object does not exist [id: refs/pull/19/head, rel_path: ] 2020/04/15 12:58:03 ...s/context/context.go:139:HTML() [D] Template: status/500 ```` It's possible to fix this collateral problem? Thanks in advance
Author
Owner

@guillep2k commented on GitHub (Apr 16, 2020):

@mbarinc That kind of reference exists since long ago and is central to our PR system:

https://github.com/go-gitea/gitea/blame/master/models/pull.go#L317

I'm afraid you'll have to update your hook to accept this kind of reference. If I'm not mistaken, users won't be able to push such references themselves anyway.

@guillep2k commented on GitHub (Apr 16, 2020): @mbarinc That kind of reference exists since long ago and is central to our PR system: https://github.com/go-gitea/gitea/blame/master/models/pull.go#L317 I'm afraid you'll have to update your hook to accept this kind of reference. If I'm not mistaken, users won't be able to push such references themselves anyway.
Author
Owner

@mbarinc commented on GitHub (Apr 16, 2020):

@guillep2k thanks for the clarification, it was useful to me.
I changed the logic in the hook by checking only if reference is refs/heads.
It works and I have no problems with PR.
Thanks again!

@mbarinc commented on GitHub (Apr 16, 2020): @guillep2k thanks for the clarification, it was useful to me. I changed the logic in the hook by checking only if reference is refs/heads. It works and I have no problems with PR. Thanks again!
Author
Owner

@guillep2k commented on GitHub (Apr 17, 2020):

@mbarinc You may also want to check refs/tags too, depending on your goals.

@guillep2k commented on GitHub (Apr 17, 2020): @mbarinc You may also want to check `refs/tags` too, depending on your goals.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/gitea#4937