mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-11-04 01:34:27 +00:00 
			
		
		
		
	Remove unnecessary syncbranchToDB with tests (#28624)
#28361 introduced `syncBranchToDB` in `CreateNewBranchFromCommit`. This PR will revert the change because it's unnecessary. Every push will already be checked by `syncBranchToDB`. This PR also created a test to ensure it's right.
This commit is contained in:
		@@ -276,16 +276,6 @@ func CreateNewBranchFromCommit(ctx context.Context, doer *user_model.User, repo
 | 
				
			|||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return db.WithTx(ctx, func(ctx context.Context) error {
 | 
					 | 
				
			||||||
		commit, err := gitRepo.GetCommit(commitID)
 | 
					 | 
				
			||||||
		if err != nil {
 | 
					 | 
				
			||||||
			return err
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		// database operation should be done before git operation so that we can rollback if git operation failed
 | 
					 | 
				
			||||||
		if err := syncBranchToDB(ctx, repo.ID, doer.ID, branchName, commit); err != nil {
 | 
					 | 
				
			||||||
			return err
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if err := git.Push(ctx, repo.RepoPath(), git.PushOptions{
 | 
						if err := git.Push(ctx, repo.RepoPath(), git.PushOptions{
 | 
				
			||||||
		Remote: repo.RepoPath(),
 | 
							Remote: repo.RepoPath(),
 | 
				
			||||||
		Branch: fmt.Sprintf("%s:%s%s", commitID, git.BranchPrefix, branchName),
 | 
							Branch: fmt.Sprintf("%s:%s%s", commitID, git.BranchPrefix, branchName),
 | 
				
			||||||
@@ -297,7 +287,6 @@ func CreateNewBranchFromCommit(ctx context.Context, doer *user_model.User, repo
 | 
				
			|||||||
		return fmt.Errorf("push: %w", err)
 | 
							return fmt.Errorf("push: %w", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
	})
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// RenameBranch rename a branch
 | 
					// RenameBranch rename a branch
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -9,6 +9,8 @@ import (
 | 
				
			|||||||
	"testing"
 | 
						"testing"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	auth_model "code.gitea.io/gitea/models/auth"
 | 
						auth_model "code.gitea.io/gitea/models/auth"
 | 
				
			||||||
 | 
						"code.gitea.io/gitea/models/db"
 | 
				
			||||||
 | 
						git_model "code.gitea.io/gitea/models/git"
 | 
				
			||||||
	api "code.gitea.io/gitea/modules/structs"
 | 
						api "code.gitea.io/gitea/modules/structs"
 | 
				
			||||||
	"code.gitea.io/gitea/tests"
 | 
						"code.gitea.io/gitea/tests"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -217,3 +219,37 @@ func TestAPIBranchProtection(t *testing.T) {
 | 
				
			|||||||
	testAPIDeleteBranch(t, "master", http.StatusForbidden)
 | 
						testAPIDeleteBranch(t, "master", http.StatusForbidden)
 | 
				
			||||||
	testAPIDeleteBranch(t, "branch2", http.StatusNoContent)
 | 
						testAPIDeleteBranch(t, "branch2", http.StatusNoContent)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestAPICreateBranchWithSyncBranches(t *testing.T) {
 | 
				
			||||||
 | 
						defer tests.PrepareTestEnv(t)()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						branches, err := db.Find[git_model.Branch](db.DefaultContext, git_model.FindBranchOptions{
 | 
				
			||||||
 | 
							RepoID: 1,
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						assert.NoError(t, err)
 | 
				
			||||||
 | 
						assert.Len(t, branches, 4)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// make a broke repository with no branch on database
 | 
				
			||||||
 | 
						_, err = db.DeleteByBean(db.DefaultContext, git_model.Branch{RepoID: 1})
 | 
				
			||||||
 | 
						assert.NoError(t, err)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
 | 
				
			||||||
 | 
							ctx := NewAPITestContext(t, "user2", "repo1", auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser)
 | 
				
			||||||
 | 
							giteaURL.Path = ctx.GitPath()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							testAPICreateBranch(t, ctx.Session, "user2", "repo1", "", "new_branch", http.StatusCreated)
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						branches, err = db.Find[git_model.Branch](db.DefaultContext, git_model.FindBranchOptions{
 | 
				
			||||||
 | 
							RepoID: 1,
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						assert.NoError(t, err)
 | 
				
			||||||
 | 
						assert.Len(t, branches, 5)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						branches, err = db.Find[git_model.Branch](db.DefaultContext, git_model.FindBranchOptions{
 | 
				
			||||||
 | 
							RepoID:  1,
 | 
				
			||||||
 | 
							Keyword: "new_branch",
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						assert.NoError(t, err)
 | 
				
			||||||
 | 
						assert.Len(t, branches, 1)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user