diff options
author | 来自村里的小螃蟹 <yystopf@163.com> | 2023-05-09 18:22:32 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-09 18:22:32 +0800 |
commit | cd9a13ebb47d32f46b38439a524e3b2e0c619490 (patch) | |
tree | 837477fd835ac987d881689468fef298ded8bbbd /routers | |
parent | 023a048f52b5bf8c4b715285245a129f04e05a8c (diff) | |
download | gitea-cd9a13ebb47d32f46b38439a524e3b2e0c619490.tar.gz gitea-cd9a13ebb47d32f46b38439a524e3b2e0c619490.zip |
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>
Diffstat (limited to 'routers')
-rw-r--r-- | routers/api/v1/repo/branch.go | 32 |
1 files changed, 28 insertions, 4 deletions
diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go index 1ae6193a8a..5336ccb797 100644 --- a/routers/api/v1/repo/branch.go +++ b/routers/api/v1/repo/branch.go @@ -173,11 +173,35 @@ func CreateBranch(ctx *context.APIContext) { return } - if len(opt.OldBranchName) == 0 { - opt.OldBranchName = ctx.Repo.Repository.DefaultBranch + var oldCommit *git.Commit + var err error + + if len(opt.OldRefName) > 0 { + oldCommit, err = ctx.Repo.GitRepo.GetCommit(opt.OldRefName) + if err != nil { + ctx.Error(http.StatusInternalServerError, "GetCommit", err) + return + } + } else if len(opt.OldBranchName) > 0 { //nolint + if ctx.Repo.GitRepo.IsBranchExist(opt.OldBranchName) { //nolint + oldCommit, err = ctx.Repo.GitRepo.GetBranchCommit(opt.OldBranchName) //nolint + if err != nil { + ctx.Error(http.StatusInternalServerError, "GetBranchCommit", err) + return + } + } else { + ctx.Error(http.StatusNotFound, "", "The old branch does not exist") + return + } + } else { + oldCommit, err = ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch) + if err != nil { + ctx.Error(http.StatusInternalServerError, "GetBranchCommit", err) + return + } } - err := repo_service.CreateNewBranch(ctx, ctx.Doer, ctx.Repo.Repository, opt.OldBranchName, opt.BranchName) + err = repo_service.CreateNewBranchFromCommit(ctx, ctx.Doer, ctx.Repo.Repository, oldCommit.ID.String(), opt.BranchName) if err != nil { if models.IsErrBranchDoesNotExist(err) { ctx.Error(http.StatusNotFound, "", "The old branch does not exist") @@ -189,7 +213,7 @@ func CreateBranch(ctx *context.APIContext) { } else if models.IsErrBranchNameConflict(err) { ctx.Error(http.StatusConflict, "", "The branch with the same name already exists.") } else { - ctx.Error(http.StatusInternalServerError, "CreateRepoBranch", err) + ctx.Error(http.StatusInternalServerError, "CreateNewBranchFromCommit", err) } return } |