Skip to content

Commit 562783f

Browse files
viletyylunny
authored andcommitted
Create a branch directly from commit on the create branch API
1 parent b1cee3f commit 562783f

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

modules/structs/repo.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,10 @@ type CreateBranchRepoOption struct {
249249
// unique: true
250250
BranchName string `json:"new_branch_name" binding:"Required;GitRefName;MaxSize(100)"`
251251

252-
// Name of the old branch to create from
252+
// Name of the commit/branch/tag to create from
253253
//
254254
// unique: true
255-
OldBranchName string `json:"old_branch_name" binding:"GitRefName;MaxSize(100)"`
255+
OldRefName string `json:"old_ref_name" binding:"GitRefName;MaxSize(100)"`
256256
}
257257

258258
// TransferRepoOption options when transfer a repository's ownership

routers/api/v1/repo/branch.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,17 @@ func CreateBranch(ctx *context.APIContext) {
173173
return
174174
}
175175

176-
if len(opt.OldBranchName) == 0 {
177-
opt.OldBranchName = ctx.Repo.Repository.DefaultBranch
176+
if len(opt.OldRefName) == 0 {
177+
opt.OldRefName = ctx.Repo.Repository.DefaultBranch
178178
}
179179

180-
err := repo_service.CreateNewBranch(ctx, ctx.Doer, ctx.Repo.Repository, opt.OldBranchName, opt.BranchName)
180+
oldCommit, err := ctx.Repo.GitRepo.GetCommit(opt.OldRefName)
181+
if err != nil {
182+
ctx.Error(http.StatusInternalServerError, "GetCommit", err)
183+
return
184+
}
185+
186+
err = repo_service.CreateNewBranchFromCommit(ctx, ctx.Doer, ctx.Repo.Repository, oldCommit.ID.String(), opt.BranchName)
181187
if err != nil {
182188
if models.IsErrBranchDoesNotExist(err) {
183189
ctx.Error(http.StatusNotFound, "", "The old branch does not exist")
@@ -189,7 +195,7 @@ func CreateBranch(ctx *context.APIContext) {
189195
} else if models.IsErrBranchNameConflict(err) {
190196
ctx.Error(http.StatusConflict, "", "The branch with the same name already exists.")
191197
} else {
192-
ctx.Error(http.StatusInternalServerError, "CreateRepoBranch", err)
198+
ctx.Error(http.StatusInternalServerError, "CreateNewBranchFromCommit", err)
193199
}
194200
return
195201
}

templates/swagger/v1_json.tmpl

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)