@@ -46,15 +46,12 @@ func GetBranch(ctx *context.APIContext) {
4646 // responses:
4747 // "200":
4848 // "$ref": "#/responses/Branch"
49+ // "404":
50+ // "$ref": "#/responses/notFound"
4951
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 )
5855 if err != nil {
5956 if git .IsErrBranchNotExist (err ) {
6057 ctx .NotFound (err )
@@ -70,7 +67,7 @@ func GetBranch(ctx *context.APIContext) {
7067 return
7168 }
7269
73- branchProtection , err := ctx .Repo .Repository .GetBranchProtection (ctx . Repo . BranchName )
70+ branchProtection , err := ctx .Repo .Repository .GetBranchProtection (branchName )
7471 if err != nil {
7572 ctx .Error (http .StatusInternalServerError , "GetBranchProtection" , err )
7673 return
@@ -113,21 +110,17 @@ func DeleteBranch(ctx *context.APIContext) {
113110 // "$ref": "#/responses/empty"
114111 // "403":
115112 // "$ref": "#/responses/error"
113+ // "404":
114+ // "$ref": "#/responses/notFound"
116115
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 ("*" )
124117
125- if ctx .Repo .Repository .DefaultBranch == ctx . Repo . BranchName {
118+ if ctx .Repo .Repository .DefaultBranch == branchName {
126119 ctx .Error (http .StatusForbidden , "DefaultBranch" , fmt .Errorf ("can not delete default branch" ))
127120 return
128121 }
129122
130- isProtected , err := ctx .Repo .Repository .IsProtectedBranch (ctx . Repo . BranchName , ctx .User )
123+ isProtected , err := ctx .Repo .Repository .IsProtectedBranch (branchName , ctx .User )
131124 if err != nil {
132125 ctx .InternalServerError (err )
133126 return
@@ -137,7 +130,7 @@ func DeleteBranch(ctx *context.APIContext) {
137130 return
138131 }
139132
140- branch , err := repo_module .GetBranch (ctx .Repo .Repository , ctx . Repo . BranchName )
133+ branch , err := repo_module .GetBranch (ctx .Repo .Repository , branchName )
141134 if err != nil {
142135 if git .IsErrBranchNotExist (err ) {
143136 ctx .NotFound (err )
@@ -153,7 +146,17 @@ func DeleteBranch(ctx *context.APIContext) {
153146 return
154147 }
155148
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 {
157160 Force : true ,
158161 }); err != nil {
159162 ctx .Error (http .StatusInternalServerError , "DeleteBranch" , err )
@@ -163,7 +166,7 @@ func DeleteBranch(ctx *context.APIContext) {
163166 // Don't return error below this
164167 if err := repo_service .PushUpdate (
165168 & repo_module.PushUpdateOptions {
166- RefFullName : git .BranchPrefix + ctx . Repo . BranchName ,
169+ RefFullName : git .BranchPrefix + branchName ,
167170 OldCommitID : c .ID .String (),
168171 NewCommitID : git .EmptySHA ,
169172 PusherID : ctx .User .ID ,
@@ -174,7 +177,7 @@ func DeleteBranch(ctx *context.APIContext) {
174177 log .Error ("Update: %v" , err )
175178 }
176179
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 {
178181 log .Warn ("AddDeletedBranch: %v" , err )
179182 }
180183
0 commit comments