|
6 | 6 | package repo
|
7 | 7 |
|
8 | 8 | import (
|
| 9 | + "fmt" |
9 | 10 | "net/http"
|
10 | 11 |
|
11 | 12 | "code.gitea.io/gitea/models"
|
12 | 13 | "code.gitea.io/gitea/modules/context"
|
13 | 14 | "code.gitea.io/gitea/modules/convert"
|
14 | 15 | "code.gitea.io/gitea/modules/git"
|
| 16 | + "code.gitea.io/gitea/modules/log" |
| 17 | + "code.gitea.io/gitea/modules/repofiles" |
15 | 18 | repo_module "code.gitea.io/gitea/modules/repository"
|
16 | 19 | api "code.gitea.io/gitea/modules/structs"
|
17 | 20 | )
|
@@ -81,6 +84,104 @@ func GetBranch(ctx *context.APIContext) {
|
81 | 84 | ctx.JSON(http.StatusOK, br)
|
82 | 85 | }
|
83 | 86 |
|
| 87 | +// DeleteBranch get a branch of a repository |
| 88 | +func DeleteBranch(ctx *context.APIContext) { |
| 89 | + // swagger:operation DELETE /repos/{owner}/{repo}/branches/{branch} repository repoDeleteBranch |
| 90 | + // --- |
| 91 | + // summary: Delete a specific branch from a repository |
| 92 | + // produces: |
| 93 | + // - application/json |
| 94 | + // parameters: |
| 95 | + // - name: owner |
| 96 | + // in: path |
| 97 | + // description: owner of the repo |
| 98 | + // type: string |
| 99 | + // required: true |
| 100 | + // - name: repo |
| 101 | + // in: path |
| 102 | + // description: name of the repo |
| 103 | + // type: string |
| 104 | + // required: true |
| 105 | + // - name: branch |
| 106 | + // in: path |
| 107 | + // description: branch to delete |
| 108 | + // type: string |
| 109 | + // required: true |
| 110 | + // responses: |
| 111 | + // "204": |
| 112 | + // "$ref": "#/responses/empty" |
| 113 | + // "403": |
| 114 | + // "$ref": "#/responses/error" |
| 115 | + |
| 116 | + if ctx.Repo.TreePath != "" { |
| 117 | + // if TreePath != "", then URL contained extra slashes |
| 118 | + // (i.e. "master/subbranch" instead of "master"), so branch does |
| 119 | + // not exist |
| 120 | + ctx.NotFound() |
| 121 | + return |
| 122 | + } |
| 123 | + |
| 124 | + if ctx.Repo.Repository.DefaultBranch == ctx.Repo.BranchName { |
| 125 | + ctx.Error(http.StatusForbidden, "DefaultBranch", fmt.Errorf("can not delete default branch")) |
| 126 | + return |
| 127 | + } |
| 128 | + |
| 129 | + isProtected, err := ctx.Repo.Repository.IsProtectedBranch(ctx.Repo.BranchName, ctx.User) |
| 130 | + if err != nil { |
| 131 | + ctx.InternalServerError(err) |
| 132 | + return |
| 133 | + } |
| 134 | + if isProtected { |
| 135 | + ctx.Error(http.StatusForbidden, "IsProtectedBranch", fmt.Errorf("branch protected")) |
| 136 | + return |
| 137 | + } |
| 138 | + |
| 139 | + branch, err := repo_module.GetBranch(ctx.Repo.Repository, ctx.Repo.BranchName) |
| 140 | + if err != nil { |
| 141 | + if git.IsErrBranchNotExist(err) { |
| 142 | + ctx.NotFound(err) |
| 143 | + } else { |
| 144 | + ctx.Error(http.StatusInternalServerError, "GetBranch", err) |
| 145 | + } |
| 146 | + return |
| 147 | + } |
| 148 | + |
| 149 | + c, err := branch.GetCommit() |
| 150 | + if err != nil { |
| 151 | + ctx.Error(http.StatusInternalServerError, "GetCommit", err) |
| 152 | + return |
| 153 | + } |
| 154 | + |
| 155 | + if err := ctx.Repo.GitRepo.DeleteBranch(ctx.Repo.BranchName, git.DeleteBranchOptions{ |
| 156 | + Force: true, |
| 157 | + }); err != nil { |
| 158 | + ctx.Error(http.StatusInternalServerError, "DeleteBranch", err) |
| 159 | + return |
| 160 | + } |
| 161 | + |
| 162 | + // Don't return error below this |
| 163 | + if err := repofiles.PushUpdate( |
| 164 | + ctx.Repo.Repository, |
| 165 | + ctx.Repo.BranchName, |
| 166 | + repofiles.PushUpdateOptions{ |
| 167 | + RefFullName: git.BranchPrefix + ctx.Repo.BranchName, |
| 168 | + OldCommitID: c.ID.String(), |
| 169 | + NewCommitID: git.EmptySHA, |
| 170 | + PusherID: ctx.User.ID, |
| 171 | + PusherName: ctx.User.Name, |
| 172 | + RepoUserName: ctx.Repo.Owner.Name, |
| 173 | + RepoName: ctx.Repo.Repository.Name, |
| 174 | + }); err != nil { |
| 175 | + log.Error("Update: %v", err) |
| 176 | + } |
| 177 | + |
| 178 | + if err := ctx.Repo.Repository.AddDeletedBranch(ctx.Repo.BranchName, c.ID.String(), ctx.User.ID); err != nil { |
| 179 | + log.Warn("AddDeletedBranch: %v", err) |
| 180 | + } |
| 181 | + |
| 182 | + ctx.Status(http.StatusNoContent) |
| 183 | +} |
| 184 | + |
84 | 185 | // ListBranches list all the branches of a repository
|
85 | 186 | func ListBranches(ctx *context.APIContext) {
|
86 | 187 | // swagger:operation GET /repos/{owner}/{repo}/branches repository repoListBranches
|
|
0 commit comments