Skip to content

Commit cd9a13e

Browse files
viletyysilverwindlunny
authored
Create a branch directly from commit on the create branch API (#22956)
#### Added - API: Create a branch directly from commit on the create branch API - Added `old_ref_name` parameter to allow creating a new branch from a specific commit, tag, or branch. - Deprecated `old_branch_name` parameter in favor of the new `old_ref_name` parameter. --------- Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
1 parent 023a048 commit cd9a13e

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

modules/structs/repo.go

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

252+
// Deprecated: true
252253
// Name of the old branch to create from
253254
//
254255
// unique: true
255256
OldBranchName string `json:"old_branch_name" binding:"GitRefName;MaxSize(100)"`
257+
258+
// Name of the old branch/tag/commit to create from
259+
//
260+
// unique: true
261+
OldRefName string `json:"old_ref_name" binding:"GitRefName;MaxSize(100)"`
256262
}
257263

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

routers/api/v1/repo/branch.go

+28-4
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,35 @@ func CreateBranch(ctx *context.APIContext) {
173173
return
174174
}
175175

176-
if len(opt.OldBranchName) == 0 {
177-
opt.OldBranchName = ctx.Repo.Repository.DefaultBranch
176+
var oldCommit *git.Commit
177+
var err error
178+
179+
if len(opt.OldRefName) > 0 {
180+
oldCommit, err = ctx.Repo.GitRepo.GetCommit(opt.OldRefName)
181+
if err != nil {
182+
ctx.Error(http.StatusInternalServerError, "GetCommit", err)
183+
return
184+
}
185+
} else if len(opt.OldBranchName) > 0 { //nolint
186+
if ctx.Repo.GitRepo.IsBranchExist(opt.OldBranchName) { //nolint
187+
oldCommit, err = ctx.Repo.GitRepo.GetBranchCommit(opt.OldBranchName) //nolint
188+
if err != nil {
189+
ctx.Error(http.StatusInternalServerError, "GetBranchCommit", err)
190+
return
191+
}
192+
} else {
193+
ctx.Error(http.StatusNotFound, "", "The old branch does not exist")
194+
return
195+
}
196+
} else {
197+
oldCommit, err = ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch)
198+
if err != nil {
199+
ctx.Error(http.StatusInternalServerError, "GetBranchCommit", err)
200+
return
201+
}
178202
}
179203

180-
err := repo_service.CreateNewBranch(ctx, ctx.Doer, ctx.Repo.Repository, opt.OldBranchName, opt.BranchName)
204+
err = repo_service.CreateNewBranchFromCommit(ctx, ctx.Doer, ctx.Repo.Repository, oldCommit.ID.String(), opt.BranchName)
181205
if err != nil {
182206
if models.IsErrBranchDoesNotExist(err) {
183207
ctx.Error(http.StatusNotFound, "", "The old branch does not exist")
@@ -189,7 +213,7 @@ func CreateBranch(ctx *context.APIContext) {
189213
} else if models.IsErrBranchNameConflict(err) {
190214
ctx.Error(http.StatusConflict, "", "The branch with the same name already exists.")
191215
} else {
192-
ctx.Error(http.StatusInternalServerError, "CreateRepoBranch", err)
216+
ctx.Error(http.StatusInternalServerError, "CreateNewBranchFromCommit", err)
193217
}
194218
return
195219
}

templates/swagger/v1_json.tmpl

+7-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)