mirror of
				https://github.com/go-gitea/gitea
				synced 2025-11-03 21:08:25 +00:00 
			
		
		
		
	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>
This commit is contained in:
		@@ -249,10 +249,16 @@ type CreateBranchRepoOption struct {
 | 
			
		||||
	// unique: true
 | 
			
		||||
	BranchName string `json:"new_branch_name" binding:"Required;GitRefName;MaxSize(100)"`
 | 
			
		||||
 | 
			
		||||
	// 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)"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// TransferRepoOption options when transfer a repository's ownership
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										8
									
								
								templates/swagger/v1_json.tmpl
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										8
									
								
								templates/swagger/v1_json.tmpl
									
									
									
										generated
									
									
									
								
							@@ -16145,10 +16145,16 @@
 | 
			
		||||
          "x-go-name": "BranchName"
 | 
			
		||||
        },
 | 
			
		||||
        "old_branch_name": {
 | 
			
		||||
          "description": "Name of the old branch to create from",
 | 
			
		||||
          "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 old branch/tag/commit to create from",
 | 
			
		||||
          "type": "string",
 | 
			
		||||
          "uniqueItems": true,
 | 
			
		||||
          "x-go-name": "OldRefName"
 | 
			
		||||
        }
 | 
			
		||||
      },
 | 
			
		||||
      "x-go-package": "code.gitea.io/gitea/modules/structs"
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user