Skip to content

Commit 78c1d56

Browse files
viletyylunny
authored andcommitted
change parameter rule
1 parent 562783f commit 78c1d56

File tree

3 files changed

+55
-8
lines changed

3 files changed

+55
-8
lines changed

modules/structs/repo.go

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

252-
// Name of the commit/branch/tag to create from
252+
// Deprecated: true
253+
// Name of the old branch to create from
254+
//
255+
// unique: true
256+
OldBranchName string `json:"old_branch_name" binding:"GitRefName;MaxSize(100)"`
257+
258+
// Name of the old branch/tag/commit to create from
253259
//
254260
// unique: true
255261
OldRefName string `json:"old_ref_name" binding:"GitRefName;MaxSize(100)"`

routers/api/v1/repo/branch.go

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,49 @@ func CreateBranch(ctx *context.APIContext) {
173173
return
174174
}
175175

176-
if len(opt.OldRefName) == 0 {
177-
opt.OldRefName = ctx.Repo.Repository.DefaultBranch
178-
}
176+
var oldCommit *git.Commit
177+
var err error
179178

180-
oldCommit, err := ctx.Repo.GitRepo.GetCommit(opt.OldRefName)
181-
if err != nil {
182-
ctx.Error(http.StatusInternalServerError, "GetCommit", err)
179+
if len(opt.OldRefName) > 0 && len(opt.OldBranchName) > 0 {
180+
ctx.Error(http.StatusInternalServerError, "", "OldRefName And OldBranchName can not be both exist.")
183181
return
182+
} else if len(opt.OldRefName) > 0 {
183+
if ctx.Repo.GitRepo.IsBranchExist(opt.OldRefName) {
184+
oldCommit, err = ctx.Repo.GitRepo.GetBranchCommit(opt.OldRefName)
185+
if err != nil {
186+
ctx.Error(http.StatusInternalServerError, "GetBranchCommit", err)
187+
return
188+
}
189+
} else if ctx.Repo.GitRepo.IsTagExist(opt.OldRefName) {
190+
oldCommit, err = ctx.Repo.GitRepo.GetTagCommit(opt.OldRefName)
191+
if err != nil {
192+
ctx.Error(http.StatusInternalServerError, "GetTagCommit", err)
193+
return
194+
}
195+
} else if len(opt.OldRefName) == git.SHAFullLength {
196+
oldCommit, err = ctx.Repo.GitRepo.GetCommit(opt.OldRefName)
197+
if err != nil {
198+
ctx.Error(http.StatusInternalServerError, "GetCommit", err)
199+
return
200+
}
201+
} else {
202+
ctx.Error(http.StatusNotFound, "", "OldRefName is not exits.")
203+
return
204+
}
205+
} else if len(opt.OldBranchName) > 0 {
206+
if ctx.Repo.GitRepo.IsBranchExist(opt.OldBranchName) {
207+
oldCommit, err = ctx.Repo.GitRepo.GetBranchCommit(opt.OldBranchName)
208+
if err != nil {
209+
ctx.Error(http.StatusInternalServerError, "GetBranchCommit", err)
210+
return
211+
}
212+
}
213+
} else {
214+
oldCommit, err = ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch)
215+
if err != nil {
216+
ctx.Error(http.StatusInternalServerError, "GetBranchCommit", err)
217+
return
218+
}
184219
}
185220

186221
err = repo_service.CreateNewBranchFromCommit(ctx, ctx.Doer, ctx.Repo.Repository, oldCommit.ID.String(), opt.BranchName)

templates/swagger/v1_json.tmpl

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

0 commit comments

Comments
 (0)