Skip to content

Commit 356854f

Browse files
zeripathlunny
authored andcommitted
Move serv hook functionality & drop GitLogger (#6993)
* Move hook functionality internally * Internalise serv logic * Remove old internal paths * finally remove the gitlogger * Disallow push on archived repositories * fix lint error * Update modules/private/key.go * Update routers/private/hook.go * Update routers/private/hook.go * Update routers/private/hook.go * Updated routers/private/serv.go * Fix LFS Locks over SSH * rev-list needs to be run by the hook process * fixup * Improve git test * Ensure that the lfs files are created with a different prefix * Reduce the replication in git_test.go * slight refactor * Remove unnecessary "/" * Restore ensureAnonymousClone * Restore ensureAnonymousClone * Run rev-list on server side * Try passing in the alternative directories instead * Mark test as skipped * Improve git test * Ensure that the lfs files are created with a different prefix * Reduce the replication in git_test.go * Remove unnecessary "/"
1 parent 8a343dd commit 356854f

25 files changed

+801
-977
lines changed

cmd/hook.go

+36-89
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ import (
88
"bufio"
99
"bytes"
1010
"fmt"
11+
"net/http"
1112
"os"
1213
"strconv"
1314
"strings"
1415

1516
"code.gitea.io/gitea/models"
1617
"code.gitea.io/gitea/modules/git"
17-
"code.gitea.io/gitea/modules/log"
1818
"code.gitea.io/gitea/modules/private"
19-
"code.gitea.io/gitea/modules/util"
2019

2120
"github.com/urfave/cli"
2221
)
@@ -62,12 +61,10 @@ func runHookPreReceive(c *cli.Context) error {
6261
setup("hooks/pre-receive.log")
6362

6463
// the environment setted on serv command
65-
repoID, _ := strconv.ParseInt(os.Getenv(models.ProtectedBranchRepoID), 10, 64)
6664
isWiki := (os.Getenv(models.EnvRepoIsWiki) == "true")
6765
username := os.Getenv(models.EnvRepoUsername)
6866
reponame := os.Getenv(models.EnvRepoName)
69-
userIDStr := os.Getenv(models.EnvPusherID)
70-
repoPath := models.RepoPath(username, reponame)
67+
userID, _ := strconv.ParseInt(os.Getenv(models.EnvPusherID), 10, 64)
7168

7269
buf := bytes.NewBuffer(nil)
7370
scanner := bufio.NewScanner(os.Stdin)
@@ -91,35 +88,19 @@ func runHookPreReceive(c *cli.Context) error {
9188

9289
// If the ref is a branch, check if it's protected
9390
if strings.HasPrefix(refFullName, git.BranchPrefix) {
94-
branchName := strings.TrimPrefix(refFullName, git.BranchPrefix)
95-
protectBranch, err := private.GetProtectedBranchBy(repoID, branchName)
96-
if err != nil {
97-
fail("Internal error", fmt.Sprintf("retrieve protected branches information failed: %v", err))
98-
}
99-
100-
if protectBranch != nil && protectBranch.IsProtected() {
101-
// check and deletion
102-
if newCommitID == git.EmptySHA {
103-
fail(fmt.Sprintf("branch %s is protected from deletion", branchName), "")
104-
}
105-
106-
// detect force push
107-
if git.EmptySHA != oldCommitID {
108-
output, err := git.NewCommand("rev-list", "--max-count=1", oldCommitID, "^"+newCommitID).RunInDir(repoPath)
109-
if err != nil {
110-
fail("Internal error", "Fail to detect force push: %v", err)
111-
} else if len(output) > 0 {
112-
fail(fmt.Sprintf("branch %s is protected from force push", branchName), "")
113-
}
114-
}
115-
116-
userID, _ := strconv.ParseInt(userIDStr, 10, 64)
117-
canPush, err := private.CanUserPush(protectBranch.ID, userID)
118-
if err != nil {
119-
fail("Internal error", "Fail to detect user can push: %v", err)
120-
} else if !canPush {
121-
fail(fmt.Sprintf("protected branch %s can not be pushed to", branchName), "")
122-
}
91+
statusCode, msg := private.HookPreReceive(username, reponame, private.HookOptions{
92+
OldCommitID: oldCommitID,
93+
NewCommitID: newCommitID,
94+
RefFullName: refFullName,
95+
UserID: userID,
96+
GitAlternativeObjectDirectories: os.Getenv(private.GitAlternativeObjectDirectories),
97+
GitObjectDirectory: os.Getenv(private.GitObjectDirectory),
98+
})
99+
switch statusCode {
100+
case http.StatusInternalServerError:
101+
fail("Internal Server Error", msg)
102+
case http.StatusForbidden:
103+
fail(msg, "")
123104
}
124105
}
125106
}
@@ -145,7 +126,6 @@ func runHookPostReceive(c *cli.Context) error {
145126
setup("hooks/post-receive.log")
146127

147128
// the environment setted on serv command
148-
repoID, _ := strconv.ParseInt(os.Getenv(models.ProtectedBranchRepoID), 10, 64)
149129
repoUser := os.Getenv(models.EnvRepoUsername)
150130
isWiki := (os.Getenv(models.EnvRepoIsWiki) == "true")
151131
repoName := os.Getenv(models.EnvRepoName)
@@ -172,64 +152,31 @@ func runHookPostReceive(c *cli.Context) error {
172152
newCommitID := string(fields[1])
173153
refFullName := string(fields[2])
174154

175-
// Only trigger activity updates for changes to branches or
176-
// tags. Updates to other refs (eg, refs/notes, refs/changes,
177-
// or other less-standard refs spaces are ignored since there
178-
// may be a very large number of them).
179-
if strings.HasPrefix(refFullName, git.BranchPrefix) || strings.HasPrefix(refFullName, git.TagPrefix) {
180-
if err := private.PushUpdate(models.PushUpdateOptions{
181-
RefFullName: refFullName,
182-
OldCommitID: oldCommitID,
183-
NewCommitID: newCommitID,
184-
PusherID: pusherID,
185-
PusherName: pusherName,
186-
RepoUserName: repoUser,
187-
RepoName: repoName,
188-
}); err != nil {
189-
log.GitLogger.Error("Update: %v", err)
190-
}
191-
}
192-
193-
if newCommitID != git.EmptySHA && strings.HasPrefix(refFullName, git.BranchPrefix) {
194-
branch := strings.TrimPrefix(refFullName, git.BranchPrefix)
195-
repo, pullRequestAllowed, err := private.GetRepository(repoID)
196-
if err != nil {
197-
log.GitLogger.Error("get repo: %v", err)
198-
break
199-
}
200-
if !pullRequestAllowed {
201-
break
202-
}
203-
204-
baseRepo := repo
205-
if repo.IsFork {
206-
baseRepo = repo.BaseRepo
207-
}
208-
209-
if !repo.IsFork && branch == baseRepo.DefaultBranch {
210-
break
211-
}
155+
res, err := private.HookPostReceive(repoUser, repoName, private.HookOptions{
156+
OldCommitID: oldCommitID,
157+
NewCommitID: newCommitID,
158+
RefFullName: refFullName,
159+
UserID: pusherID,
160+
UserName: pusherName,
161+
})
212162

213-
pr, err := private.ActivePullRequest(baseRepo.ID, repo.ID, baseRepo.DefaultBranch, branch)
214-
if err != nil {
215-
log.GitLogger.Error("get active pr: %v", err)
216-
break
217-
}
163+
if res == nil {
164+
fail("Internal Server Error", err)
165+
}
218166

219-
fmt.Fprintln(os.Stderr, "")
220-
if pr == nil {
221-
if repo.IsFork {
222-
branch = fmt.Sprintf("%s:%s", repo.OwnerName, branch)
223-
}
224-
fmt.Fprintf(os.Stderr, "Create a new pull request for '%s':\n", branch)
225-
fmt.Fprintf(os.Stderr, " %s/compare/%s...%s\n", baseRepo.HTMLURL(), util.PathEscapeSegments(baseRepo.DefaultBranch), util.PathEscapeSegments(branch))
226-
} else {
227-
fmt.Fprint(os.Stderr, "Visit the existing pull request:\n")
228-
fmt.Fprintf(os.Stderr, " %s/pulls/%d\n", baseRepo.HTMLURL(), pr.Index)
229-
}
230-
fmt.Fprintln(os.Stderr, "")
167+
if res["message"] == false {
168+
continue
231169
}
232170

171+
fmt.Fprintln(os.Stderr, "")
172+
if res["create"] == true {
173+
fmt.Fprintf(os.Stderr, "Create a new pull request for '%s':\n", res["branch"])
174+
fmt.Fprintf(os.Stderr, " %s\n", res["url"])
175+
} else {
176+
fmt.Fprint(os.Stderr, "Visit the existing pull request:\n")
177+
fmt.Fprintf(os.Stderr, " %s\n", res["url"])
178+
}
179+
fmt.Fprintln(os.Stderr, "")
233180
}
234181

235182
return nil

0 commit comments

Comments
 (0)