From 562783f22a850025620c1a03255e80023fd3f14b Mon Sep 17 00:00:00 2001 From: viletyy Date: Fri, 17 Feb 2023 18:39:41 +0800 Subject: [PATCH 1/7] Create a branch directly from commit on the create branch API --- modules/structs/repo.go | 4 ++-- routers/api/v1/repo/branch.go | 14 ++++++++++---- templates/swagger/v1_json.tmpl | 8 ++++---- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/modules/structs/repo.go b/modules/structs/repo.go index 259c230571573..b4c80d03d1911 100644 --- a/modules/structs/repo.go +++ b/modules/structs/repo.go @@ -249,10 +249,10 @@ type CreateBranchRepoOption struct { // unique: true BranchName string `json:"new_branch_name" binding:"Required;GitRefName;MaxSize(100)"` - // Name of the old branch to create from + // Name of the commit/branch/tag to create from // // unique: true - OldBranchName string `json:"old_branch_name" binding:"GitRefName;MaxSize(100)"` + OldRefName string `json:"old_ref_name" binding:"GitRefName;MaxSize(100)"` } // TransferRepoOption options when transfer a repository's ownership diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go index 1ae6193a8a8dd..71452994dd33e 100644 --- a/routers/api/v1/repo/branch.go +++ b/routers/api/v1/repo/branch.go @@ -173,11 +173,17 @@ func CreateBranch(ctx *context.APIContext) { return } - if len(opt.OldBranchName) == 0 { - opt.OldBranchName = ctx.Repo.Repository.DefaultBranch + if len(opt.OldRefName) == 0 { + opt.OldRefName = ctx.Repo.Repository.DefaultBranch } - err := repo_service.CreateNewBranch(ctx, ctx.Doer, ctx.Repo.Repository, opt.OldBranchName, opt.BranchName) + oldCommit, err := ctx.Repo.GitRepo.GetCommit(opt.OldRefName) + if err != nil { + ctx.Error(http.StatusInternalServerError, "GetCommit", err) + return + } + + 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 +195,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 } diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 34a4101ed33fe..1a7a20f00b509 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -16131,11 +16131,11 @@ "uniqueItems": true, "x-go-name": "BranchName" }, - "old_branch_name": { - "description": "Name of the old branch to create from", + "old_ref_name": { + "description": "Name of the commit/branch/tag to create from", "type": "string", "uniqueItems": true, - "x-go-name": "OldBranchName" + "x-go-name": "OldRefName" } }, "x-go-package": "code.gitea.io/gitea/modules/structs" @@ -22354,4 +22354,4 @@ "TOTPHeader": [] } ] -} +} \ No newline at end of file From 78c1d562bf3caac4b41e89fb95b0024fe105b5f5 Mon Sep 17 00:00:00 2001 From: viletyy Date: Tue, 21 Feb 2023 16:15:59 +0800 Subject: [PATCH 2/7] change parameter rule --- modules/structs/repo.go | 8 +++++- routers/api/v1/repo/branch.go | 47 +++++++++++++++++++++++++++++----- templates/swagger/v1_json.tmpl | 8 +++++- 3 files changed, 55 insertions(+), 8 deletions(-) diff --git a/modules/structs/repo.go b/modules/structs/repo.go index b4c80d03d1911..01239188c2cd5 100644 --- a/modules/structs/repo.go +++ b/modules/structs/repo.go @@ -249,7 +249,13 @@ type CreateBranchRepoOption struct { // unique: true BranchName string `json:"new_branch_name" binding:"Required;GitRefName;MaxSize(100)"` - // Name of the commit/branch/tag to create from + // Deprecated: true + // Name of the old branch to create from + // + // unique: true + OldBranchName string `json:"old_branch_name" binding:"GitRefName;MaxSize(100)"` + + // Name of the old branch/tag/commit to create from // // unique: true OldRefName string `json:"old_ref_name" binding:"GitRefName;MaxSize(100)"` diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go index 71452994dd33e..938d5a7997c93 100644 --- a/routers/api/v1/repo/branch.go +++ b/routers/api/v1/repo/branch.go @@ -173,14 +173,49 @@ func CreateBranch(ctx *context.APIContext) { return } - if len(opt.OldRefName) == 0 { - opt.OldRefName = ctx.Repo.Repository.DefaultBranch - } + var oldCommit *git.Commit + var err error - oldCommit, err := ctx.Repo.GitRepo.GetCommit(opt.OldRefName) - if err != nil { - ctx.Error(http.StatusInternalServerError, "GetCommit", err) + if len(opt.OldRefName) > 0 && len(opt.OldBranchName) > 0 { + ctx.Error(http.StatusInternalServerError, "", "OldRefName And OldBranchName can not be both exist.") return + } else if len(opt.OldRefName) > 0 { + if ctx.Repo.GitRepo.IsBranchExist(opt.OldRefName) { + oldCommit, err = ctx.Repo.GitRepo.GetBranchCommit(opt.OldRefName) + if err != nil { + ctx.Error(http.StatusInternalServerError, "GetBranchCommit", err) + return + } + } else if ctx.Repo.GitRepo.IsTagExist(opt.OldRefName) { + oldCommit, err = ctx.Repo.GitRepo.GetTagCommit(opt.OldRefName) + if err != nil { + ctx.Error(http.StatusInternalServerError, "GetTagCommit", err) + return + } + } else if len(opt.OldRefName) == git.SHAFullLength { + oldCommit, err = ctx.Repo.GitRepo.GetCommit(opt.OldRefName) + if err != nil { + ctx.Error(http.StatusInternalServerError, "GetCommit", err) + return + } + } else { + ctx.Error(http.StatusNotFound, "", "OldRefName is not exits.") + return + } + } else if len(opt.OldBranchName) > 0 { + if ctx.Repo.GitRepo.IsBranchExist(opt.OldBranchName) { + oldCommit, err = ctx.Repo.GitRepo.GetBranchCommit(opt.OldBranchName) + if err != nil { + ctx.Error(http.StatusInternalServerError, "GetBranchCommit", err) + 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.CreateNewBranchFromCommit(ctx, ctx.Doer, ctx.Repo.Repository, oldCommit.ID.String(), opt.BranchName) diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 1a7a20f00b509..7f5f3f8be4f3e 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -16131,8 +16131,14 @@ "uniqueItems": true, "x-go-name": "BranchName" }, + "old_branch_name": { + "description": "Deprecated: true\nName of the old branch to create from", + "type": "string", + "uniqueItems": true, + "x-go-name": "OldBranchName" + }, "old_ref_name": { - "description": "Name of the commit/branch/tag to create from", + "description": "Name of the old branch/tag/commit to create from", "type": "string", "uniqueItems": true, "x-go-name": "OldRefName" From a2925c965cef2dbf0f2f4d8eddc29271e044fc4f Mon Sep 17 00:00:00 2001 From: viletyy Date: Tue, 21 Feb 2023 17:34:51 +0800 Subject: [PATCH 3/7] remove unnecessary condition --- routers/api/v1/repo/branch.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go index 938d5a7997c93..243b5195f0b10 100644 --- a/routers/api/v1/repo/branch.go +++ b/routers/api/v1/repo/branch.go @@ -176,10 +176,7 @@ func CreateBranch(ctx *context.APIContext) { var oldCommit *git.Commit var err error - if len(opt.OldRefName) > 0 && len(opt.OldBranchName) > 0 { - ctx.Error(http.StatusInternalServerError, "", "OldRefName And OldBranchName can not be both exist.") - return - } else if len(opt.OldRefName) > 0 { + if len(opt.OldRefName) > 0 { if ctx.Repo.GitRepo.IsBranchExist(opt.OldRefName) { oldCommit, err = ctx.Repo.GitRepo.GetBranchCommit(opt.OldRefName) if err != nil { From 2dbe25e4b2ea263df29abc862a397fdc593532b6 Mon Sep 17 00:00:00 2001 From: viletyy Date: Mon, 20 Mar 2023 15:06:30 +0800 Subject: [PATCH 4/7] use GetCommit matching all tag/branch/commit sha --- routers/api/v1/repo/branch.go | 23 +++-------------------- 1 file changed, 3 insertions(+), 20 deletions(-) diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go index 243b5195f0b10..4e4ff90136bd9 100644 --- a/routers/api/v1/repo/branch.go +++ b/routers/api/v1/repo/branch.go @@ -177,26 +177,9 @@ func CreateBranch(ctx *context.APIContext) { var err error if len(opt.OldRefName) > 0 { - if ctx.Repo.GitRepo.IsBranchExist(opt.OldRefName) { - oldCommit, err = ctx.Repo.GitRepo.GetBranchCommit(opt.OldRefName) - if err != nil { - ctx.Error(http.StatusInternalServerError, "GetBranchCommit", err) - return - } - } else if ctx.Repo.GitRepo.IsTagExist(opt.OldRefName) { - oldCommit, err = ctx.Repo.GitRepo.GetTagCommit(opt.OldRefName) - if err != nil { - ctx.Error(http.StatusInternalServerError, "GetTagCommit", err) - return - } - } else if len(opt.OldRefName) == git.SHAFullLength { - oldCommit, err = ctx.Repo.GitRepo.GetCommit(opt.OldRefName) - if err != nil { - ctx.Error(http.StatusInternalServerError, "GetCommit", err) - return - } - } else { - ctx.Error(http.StatusNotFound, "", "OldRefName is not exits.") + oldCommit, err = ctx.Repo.GitRepo.GetCommit(opt.OldRefName) + if err != nil { + ctx.Error(http.StatusInternalServerError, "GetCommit", err) return } } else if len(opt.OldBranchName) > 0 { From 89495a5fa775568a3a1073cf76bf5dcb6fef1c34 Mon Sep 17 00:00:00 2001 From: viletyy Date: Mon, 20 Mar 2023 18:10:09 +0800 Subject: [PATCH 5/7] fix old commit nil panic --- routers/api/v1/repo/branch.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go index 4e4ff90136bd9..df0a8da32a5eb 100644 --- a/routers/api/v1/repo/branch.go +++ b/routers/api/v1/repo/branch.go @@ -189,6 +189,9 @@ func CreateBranch(ctx *context.APIContext) { 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) From 1fc95f2bc9a84a94044a8a7353010284072c1800 Mon Sep 17 00:00:00 2001 From: silverwind Date: Mon, 1 May 2023 09:38:42 +0200 Subject: [PATCH 6/7] run generate-swagger to fix newline issue --- templates/swagger/v1_json.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 7f5f3f8be4f3e..cbe02ee23bf11 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -22360,4 +22360,4 @@ "TOTPHeader": [] } ] -} \ No newline at end of file +} From 1c4fc9e00e955b1144f36019211f39a26d4f14b5 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Thu, 4 May 2023 18:22:49 +0800 Subject: [PATCH 7/7] Fix lint --- routers/api/v1/repo/branch.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go index df0a8da32a5eb..5336ccb79743d 100644 --- a/routers/api/v1/repo/branch.go +++ b/routers/api/v1/repo/branch.go @@ -182,9 +182,9 @@ func CreateBranch(ctx *context.APIContext) { ctx.Error(http.StatusInternalServerError, "GetCommit", err) return } - } else if len(opt.OldBranchName) > 0 { - if ctx.Repo.GitRepo.IsBranchExist(opt.OldBranchName) { - oldCommit, err = ctx.Repo.GitRepo.GetBranchCommit(opt.OldBranchName) + } 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