Ignore non-standard refs in git push (#6758)

When replicating to gitea from a remote system which makes use of
git refs to store extra data (for example, gerrit), pushing a lot
of refs to gitea can cause problems due to the extra processing
that the pre and post receive hooks perform.  But it's still
useful for gitea to be able to serve those refs.  This change
skips unecessary processing of refs other than branches or tags.

We don't need to check any ref that isn't a branch for branch
protection (protection will never be enabled).  So in the
pre-receive hook, we wrap that check in a test for whether the
ref is a branch.

We also don't need to add information to the activity stream about
pushes to non-standard refs, so we skip that step in the
post-receive hook for refs which are not branches or tags.

For some concrete examples, gerrit maintains a ref for every
patchset of every change in the form refs/changes/XX/YYYY/Z.
Many systems use refs/notes to store additonal data about commits.
This change allows these and other schemes to be used without
affecting gitea.
This commit is contained in:
James E. Blair 2019-05-14 07:40:27 -07:00 committed by techknowlogick
parent 24a536d145
commit 488d34691a

View File

@ -89,6 +89,8 @@ func runHookPreReceive(c *cli.Context) error {
newCommitID := string(fields[1]) newCommitID := string(fields[1])
refFullName := string(fields[2]) refFullName := string(fields[2])
// If the ref is a branch, check if it's protected
if strings.HasPrefix(refFullName, git.BranchPrefix) {
branchName := strings.TrimPrefix(refFullName, git.BranchPrefix) branchName := strings.TrimPrefix(refFullName, git.BranchPrefix)
protectBranch, err := private.GetProtectedBranchBy(repoID, branchName) protectBranch, err := private.GetProtectedBranchBy(repoID, branchName)
if err != nil { if err != nil {
@ -120,6 +122,7 @@ func runHookPreReceive(c *cli.Context) error {
} }
} }
} }
}
return nil return nil
} }
@ -169,6 +172,11 @@ func runHookPostReceive(c *cli.Context) error {
newCommitID := string(fields[1]) newCommitID := string(fields[1])
refFullName := string(fields[2]) refFullName := string(fields[2])
// Only trigger activity updates for changes to branches or
// tags. Updates to other refs (eg, refs/notes, refs/changes,
// or other less-standard refs spaces are ignored since there
// may be a very large number of them).
if strings.HasPrefix(refFullName, git.BranchPrefix) || strings.HasPrefix(refFullName, git.TagPrefix) {
if err := private.PushUpdate(models.PushUpdateOptions{ if err := private.PushUpdate(models.PushUpdateOptions{
RefFullName: refFullName, RefFullName: refFullName,
OldCommitID: oldCommitID, OldCommitID: oldCommitID,
@ -180,6 +188,7 @@ func runHookPostReceive(c *cli.Context) error {
}); err != nil { }); err != nil {
log.GitLogger.Error("Update: %v", err) log.GitLogger.Error("Update: %v", err)
} }
}
if newCommitID != git.EmptySHA && strings.HasPrefix(refFullName, git.BranchPrefix) { if newCommitID != git.EmptySHA && strings.HasPrefix(refFullName, git.BranchPrefix) {
branch := strings.TrimPrefix(refFullName, git.BranchPrefix) branch := strings.TrimPrefix(refFullName, git.BranchPrefix)