@@ -46,15 +46,12 @@ func GetBranch(ctx *context.APIContext) {
46
46
// responses:
47
47
// "200":
48
48
// "$ref": "#/responses/Branch"
49
+ // "404":
50
+ // "$ref": "#/responses/notFound"
49
51
50
- if ctx .Repo .TreePath != "" {
51
- // if TreePath != "", then URL contained extra slashes
52
- // (i.e. "master/subbranch" instead of "master"), so branch does
53
- // not exist
54
- ctx .NotFound ()
55
- return
56
- }
57
- branch , err := repo_module .GetBranch (ctx .Repo .Repository , ctx .Repo .BranchName )
52
+ branchName := ctx .Params ("*" )
53
+
54
+ branch , err := repo_module .GetBranch (ctx .Repo .Repository , branchName )
58
55
if err != nil {
59
56
if git .IsErrBranchNotExist (err ) {
60
57
ctx .NotFound (err )
@@ -70,7 +67,7 @@ func GetBranch(ctx *context.APIContext) {
70
67
return
71
68
}
72
69
73
- branchProtection , err := ctx .Repo .Repository .GetBranchProtection (ctx . Repo . BranchName )
70
+ branchProtection , err := ctx .Repo .Repository .GetBranchProtection (branchName )
74
71
if err != nil {
75
72
ctx .Error (http .StatusInternalServerError , "GetBranchProtection" , err )
76
73
return
@@ -113,21 +110,17 @@ func DeleteBranch(ctx *context.APIContext) {
113
110
// "$ref": "#/responses/empty"
114
111
// "403":
115
112
// "$ref": "#/responses/error"
113
+ // "404":
114
+ // "$ref": "#/responses/notFound"
116
115
117
- if ctx .Repo .TreePath != "" {
118
- // if TreePath != "", then URL contained extra slashes
119
- // (i.e. "master/subbranch" instead of "master"), so branch does
120
- // not exist
121
- ctx .NotFound ()
122
- return
123
- }
116
+ branchName := ctx .Params ("*" )
124
117
125
- if ctx .Repo .Repository .DefaultBranch == ctx . Repo . BranchName {
118
+ if ctx .Repo .Repository .DefaultBranch == branchName {
126
119
ctx .Error (http .StatusForbidden , "DefaultBranch" , fmt .Errorf ("can not delete default branch" ))
127
120
return
128
121
}
129
122
130
- isProtected , err := ctx .Repo .Repository .IsProtectedBranch (ctx . Repo . BranchName , ctx .User )
123
+ isProtected , err := ctx .Repo .Repository .IsProtectedBranch (branchName , ctx .User )
131
124
if err != nil {
132
125
ctx .InternalServerError (err )
133
126
return
@@ -137,7 +130,7 @@ func DeleteBranch(ctx *context.APIContext) {
137
130
return
138
131
}
139
132
140
- branch , err := repo_module .GetBranch (ctx .Repo .Repository , ctx . Repo . BranchName )
133
+ branch , err := repo_module .GetBranch (ctx .Repo .Repository , branchName )
141
134
if err != nil {
142
135
if git .IsErrBranchNotExist (err ) {
143
136
ctx .NotFound (err )
@@ -153,7 +146,17 @@ func DeleteBranch(ctx *context.APIContext) {
153
146
return
154
147
}
155
148
156
- if err := ctx .Repo .GitRepo .DeleteBranch (ctx .Repo .BranchName , git.DeleteBranchOptions {
149
+ if ctx .Repo .GitRepo == nil {
150
+ repoPath := models .RepoPath (ctx .Repo .Owner .Name , ctx .Repo .Repository .Name )
151
+ ctx .Repo .GitRepo , err = git .OpenRepository (repoPath )
152
+ if err != nil {
153
+ ctx .InternalServerError (err )
154
+ return
155
+ }
156
+ defer ctx .Repo .GitRepo .Close ()
157
+ }
158
+
159
+ if err := ctx .Repo .GitRepo .DeleteBranch (branchName , git.DeleteBranchOptions {
157
160
Force : true ,
158
161
}); err != nil {
159
162
ctx .Error (http .StatusInternalServerError , "DeleteBranch" , err )
@@ -163,7 +166,7 @@ func DeleteBranch(ctx *context.APIContext) {
163
166
// Don't return error below this
164
167
if err := repo_service .PushUpdate (
165
168
& repo_module.PushUpdateOptions {
166
- RefFullName : git .BranchPrefix + ctx . Repo . BranchName ,
169
+ RefFullName : git .BranchPrefix + branchName ,
167
170
OldCommitID : c .ID .String (),
168
171
NewCommitID : git .EmptySHA ,
169
172
PusherID : ctx .User .ID ,
@@ -174,7 +177,7 @@ func DeleteBranch(ctx *context.APIContext) {
174
177
log .Error ("Update: %v" , err )
175
178
}
176
179
177
- if err := ctx .Repo .Repository .AddDeletedBranch (ctx . Repo . BranchName , c .ID .String (), ctx .User .ID ); err != nil {
180
+ if err := ctx .Repo .Repository .AddDeletedBranch (branchName , c .ID .String (), ctx .User .ID ); err != nil {
178
181
log .Warn ("AddDeletedBranch: %v" , err )
179
182
}
180
183
0 commit comments