@@ -11,6 +11,7 @@ import (
11
11
issues_model "code.gitea.io/gitea/models/issues"
12
12
repo_model "code.gitea.io/gitea/models/repo"
13
13
"code.gitea.io/gitea/modules/git"
14
+ "code.gitea.io/gitea/modules/gitrepo"
14
15
"code.gitea.io/gitea/modules/log"
15
16
"code.gitea.io/gitea/modules/private"
16
17
repo_module "code.gitea.io/gitea/modules/repository"
@@ -27,14 +28,19 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
27
28
28
29
// We don't rely on RepoAssignment here because:
29
30
// a) we don't need the git repo in this function
31
+ // OUT OF DATE: we do need the git repo to sync the branch to the db now.
30
32
// b) our update function will likely change the repository in the db so we will need to refresh it
31
33
// c) we don't always need the repo
32
34
33
35
ownerName := ctx .Params (":owner" )
34
36
repoName := ctx .Params (":repo" )
35
37
36
38
// defer getting the repository at this point - as we should only retrieve it if we're going to call update
37
- var repo * repo_model.Repository
39
+ var (
40
+ repo * repo_model.Repository
41
+ gitRepo * git.Repository
42
+ )
43
+ defer gitRepo .Close () // it's safe to call Close on a nil pointer
38
44
39
45
updates := make ([]* repo_module.PushUpdateOptions , 0 , len (opts .OldCommitIDs ))
40
46
wasEmpty := false
@@ -87,6 +93,43 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
87
93
})
88
94
return
89
95
}
96
+
97
+ for _ , update := range updates {
98
+ if ! update .RefFullName .IsBranch () {
99
+ continue
100
+ }
101
+ if repo == nil {
102
+ repo = loadRepository (ctx , ownerName , repoName )
103
+ if ctx .Written () {
104
+ return
105
+ }
106
+ wasEmpty = repo .IsEmpty
107
+ }
108
+ if gitRepo == nil {
109
+ var err error
110
+ gitRepo , err = gitrepo .OpenRepository (ctx , repo )
111
+ if err != nil {
112
+ log .Error ("Failed to open repository: %s/%s Error: %v" , ownerName , repoName , err )
113
+ ctx .JSON (http .StatusInternalServerError , private.HookPostReceiveResult {
114
+ Err : fmt .Sprintf ("Failed to open repository: %s/%s Error: %v" , ownerName , repoName , err ),
115
+ })
116
+ return
117
+ }
118
+ }
119
+ commit , err := gitRepo .GetCommit (update .NewCommitID )
120
+ if err != nil {
121
+ ctx .JSON (http .StatusInternalServerError , private.HookPostReceiveResult {
122
+ Err : fmt .Sprintf ("Failed to get commit %s in repository: %s/%s Error: %v" , update .NewCommitID , ownerName , repoName , err ),
123
+ })
124
+ return
125
+ }
126
+ if err := repo_service .SyncBranchToDB (ctx , repo .ID , update .PusherID , update .RefFullName .BranchName (), commit ); err != nil {
127
+ ctx .JSON (http .StatusInternalServerError , private.HookPostReceiveResult {
128
+ Err : fmt .Sprintf ("Failed to sync branch to DB in repository: %s/%s Error: %v" , ownerName , repoName , err ),
129
+ })
130
+ return
131
+ }
132
+ }
90
133
}
91
134
92
135
// Handle Push Options
0 commit comments