mirror of
				https://github.com/go-gitea/gitea
				synced 2025-11-04 05:18:25 +00:00 
			
		
		
		
	Fix bug of branches API with tests (#25578)
Fix #25558 Extract from #22743 This PR added a repository's check when creating/deleting branches via API. Mirror repository and archive repository cannot do that.
This commit is contained in:
		@@ -118,6 +118,21 @@ func DeleteBranch(ctx *context.APIContext) {
 | 
			
		||||
	//   "404":
 | 
			
		||||
	//     "$ref": "#/responses/notFound"
 | 
			
		||||
 | 
			
		||||
	if ctx.Repo.Repository.IsEmpty {
 | 
			
		||||
		ctx.Error(http.StatusNotFound, "", "Git Repository is empty.")
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if ctx.Repo.Repository.IsArchived {
 | 
			
		||||
		ctx.Error(http.StatusForbidden, "", "Git Repository is archived.")
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if ctx.Repo.Repository.IsMirror {
 | 
			
		||||
		ctx.Error(http.StatusForbidden, "", "Git Repository is a mirror.")
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	branchName := ctx.Params("*")
 | 
			
		||||
 | 
			
		||||
	if ctx.Repo.Repository.IsEmpty {
 | 
			
		||||
@@ -195,17 +210,30 @@ func CreateBranch(ctx *context.APIContext) {
 | 
			
		||||
	// responses:
 | 
			
		||||
	//   "201":
 | 
			
		||||
	//     "$ref": "#/responses/Branch"
 | 
			
		||||
	//   "403":
 | 
			
		||||
	//     description: The branch is archived or a mirror.
 | 
			
		||||
	//   "404":
 | 
			
		||||
	//     description: The old branch does not exist.
 | 
			
		||||
	//   "409":
 | 
			
		||||
	//     description: The branch with the same name already exists.
 | 
			
		||||
 | 
			
		||||
	opt := web.GetForm(ctx).(*api.CreateBranchRepoOption)
 | 
			
		||||
	if ctx.Repo.Repository.IsEmpty {
 | 
			
		||||
		ctx.Error(http.StatusNotFound, "", "Git Repository is empty.")
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if ctx.Repo.Repository.IsArchived {
 | 
			
		||||
		ctx.Error(http.StatusForbidden, "", "Git Repository is archived.")
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if ctx.Repo.Repository.IsMirror {
 | 
			
		||||
		ctx.Error(http.StatusForbidden, "", "Git Repository is a mirror.")
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	opt := web.GetForm(ctx).(*api.CreateBranchRepoOption)
 | 
			
		||||
 | 
			
		||||
	var oldCommit *git.Commit
 | 
			
		||||
	var err error
 | 
			
		||||
 | 
			
		||||
@@ -313,7 +341,12 @@ func ListBranches(ctx *context.APIContext) {
 | 
			
		||||
 | 
			
		||||
	listOptions := utils.GetListOptions(ctx)
 | 
			
		||||
 | 
			
		||||
	if !ctx.Repo.Repository.IsEmpty && ctx.Repo.GitRepo != nil {
 | 
			
		||||
	if !ctx.Repo.Repository.IsEmpty {
 | 
			
		||||
		if ctx.Repo.GitRepo == nil {
 | 
			
		||||
			ctx.Error(http.StatusInternalServerError, "Load git repository failed", nil)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		branchOpts := git_model.FindBranchOptions{
 | 
			
		||||
			ListOptions:     listOptions,
 | 
			
		||||
			RepoID:          ctx.Repo.Repository.ID,
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user