@@ -965,6 +965,22 @@ func MergePullRequest(ctx *context.Context) {
965
965
}
966
966
967
967
log .Trace ("Pull request merged: %d" , pr .ID )
968
+
969
+ if form .DeleteBranchAfterMerge {
970
+ var headRepo * git.Repository
971
+ if ctx .Repo != nil && ctx .Repo .Repository != nil && pr .HeadRepoID == ctx .Repo .Repository .ID && ctx .Repo .GitRepo != nil {
972
+ headRepo = ctx .Repo .GitRepo
973
+ } else {
974
+ headRepo , err = git .OpenRepository (pr .HeadRepo .RepoPath ())
975
+ if err != nil {
976
+ ctx .ServerError (fmt .Sprintf ("OpenRepository[%s]" , pr .HeadRepo .RepoPath ()), err )
977
+ return
978
+ }
979
+ defer headRepo .Close ()
980
+ }
981
+ deleteBranch (ctx , pr , headRepo )
982
+ }
983
+
968
984
ctx .Redirect (ctx .Repo .RepoLink + "/pulls/" + fmt .Sprint (pr .Index ))
969
985
}
970
986
@@ -1170,19 +1186,35 @@ func CleanUpPullRequest(ctx *context.Context) {
1170
1186
1171
1187
fullBranchName := pr .HeadRepo .Owner .Name + "/" + pr .HeadBranch
1172
1188
1173
- gitRepo , err := git .OpenRepository (pr .HeadRepo .RepoPath ())
1174
- if err != nil {
1175
- ctx .ServerError (fmt .Sprintf ("OpenRepository[%s]" , pr .HeadRepo .RepoPath ()), err )
1176
- return
1189
+ var gitBaseRepo * git.Repository
1190
+
1191
+ // Assume that the base repo is the current context (almost certainly)
1192
+ if ctx .Repo != nil && ctx .Repo .Repository != nil && ctx .Repo .Repository .ID == pr .BaseRepoID && ctx .Repo .GitRepo != nil {
1193
+ gitBaseRepo = ctx .Repo .GitRepo
1194
+ } else {
1195
+ // If not just open it
1196
+ gitBaseRepo , err = git .OpenRepository (pr .BaseRepo .RepoPath ())
1197
+ if err != nil {
1198
+ ctx .ServerError (fmt .Sprintf ("OpenRepository[%s]" , pr .BaseRepo .RepoPath ()), err )
1199
+ return
1200
+ }
1201
+ defer gitBaseRepo .Close ()
1177
1202
}
1178
- defer gitRepo .Close ()
1179
1203
1180
- gitBaseRepo , err := git .OpenRepository (pr .BaseRepo .RepoPath ())
1181
- if err != nil {
1182
- ctx .ServerError (fmt .Sprintf ("OpenRepository[%s]" , pr .BaseRepo .RepoPath ()), err )
1183
- return
1204
+ // Now assume that the head repo is the same as the base repo (reasonable chance)
1205
+ gitRepo := gitBaseRepo
1206
+ // But if not: is it the same as the context?
1207
+ if pr .BaseRepoID != pr .HeadRepoID && ctx .Repo != nil && ctx .Repo .Repository != nil && ctx .Repo .Repository .ID == pr .HeadRepoID && ctx .Repo .GitRepo != nil {
1208
+ gitRepo = ctx .Repo .GitRepo
1209
+ } else if pr .BaseRepoID != pr .HeadRepoID {
1210
+ // Otherwise just load it up
1211
+ gitRepo , err = git .OpenRepository (pr .HeadRepo .RepoPath ())
1212
+ if err != nil {
1213
+ ctx .ServerError (fmt .Sprintf ("OpenRepository[%s]" , pr .HeadRepo .RepoPath ()), err )
1214
+ return
1215
+ }
1216
+ defer gitRepo .Close ()
1184
1217
}
1185
- defer gitBaseRepo .Close ()
1186
1218
1187
1219
defer func () {
1188
1220
ctx .JSON (http .StatusOK , map [string ]interface {}{
@@ -1208,6 +1240,11 @@ func CleanUpPullRequest(ctx *context.Context) {
1208
1240
return
1209
1241
}
1210
1242
1243
+ deleteBranch (ctx , pr , gitRepo )
1244
+ }
1245
+
1246
+ func deleteBranch (ctx * context.Context , pr * models.PullRequest , gitRepo * git.Repository ) {
1247
+ fullBranchName := pr .HeadRepo .Owner .Name + "/" + pr .HeadBranch
1211
1248
if err := repo_service .DeleteBranch (ctx .User , pr .HeadRepo , gitRepo , pr .HeadBranch ); err != nil {
1212
1249
switch {
1213
1250
case git .IsErrBranchNotExist (err ):
@@ -1223,7 +1260,7 @@ func CleanUpPullRequest(ctx *context.Context) {
1223
1260
return
1224
1261
}
1225
1262
1226
- if err := models .AddDeletePRBranchComment (ctx .User , pr .BaseRepo , issue . ID , pr .HeadBranch ); err != nil {
1263
+ if err := models .AddDeletePRBranchComment (ctx .User , pr .BaseRepo , pr . IssueID , pr .HeadBranch ); err != nil {
1227
1264
// Do not fail here as branch has already been deleted
1228
1265
log .Error ("DeleteBranch: %v" , err )
1229
1266
}
0 commit comments