loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request '[PORT] gitea#30430: Fix rename branch 500 when the target branch is deleted but exist in database' (#3233) from gusted/forgejo-bp-1 into forgejo

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/3233
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>

+123 -6
+25 -6
models/git/branch.go
··· 301 301 302 302 sess := db.GetEngine(ctx) 303 303 304 + // check whether from branch exist 304 305 var branch Branch 305 306 exist, err := db.GetEngine(ctx).Where("repo_id=? AND name=?", repo.ID, from).Get(&branch) 306 307 if err != nil { ··· 309 310 return ErrBranchNotExist{ 310 311 RepoID: repo.ID, 311 312 BranchName: from, 313 + } 314 + } 315 + 316 + // check whether to branch exist or is_deleted 317 + var dstBranch Branch 318 + exist, err = db.GetEngine(ctx).Where("repo_id=? AND name=?", repo.ID, to).Get(&dstBranch) 319 + if err != nil { 320 + return err 321 + } 322 + if exist { 323 + if !dstBranch.IsDeleted { 324 + return ErrBranchAlreadyExists{ 325 + BranchName: to, 326 + } 327 + } 328 + 329 + if _, err := db.GetEngine(ctx).ID(dstBranch.ID).NoAutoCondition().Delete(&dstBranch); err != nil { 330 + return err 312 331 } 313 332 } 314 333 ··· 366 385 return err 367 386 } 368 387 369 - // 5. do git action 370 - if err = gitAction(ctx, isDefault); err != nil { 371 - return err 372 - } 373 - 374 - // 6. insert renamed branch record 388 + // 5. insert renamed branch record 375 389 renamedBranch := &RenamedBranch{ 376 390 RepoID: repo.ID, 377 391 From: from, ··· 379 393 } 380 394 err = db.Insert(ctx, renamedBranch) 381 395 if err != nil { 396 + return err 397 + } 398 + 399 + // 6. do git action 400 + if err = gitAction(ctx, isDefault); err != nil { 382 401 return err 383 402 } 384 403
+3
routers/web/repo/setting/protected_branch.go
··· 321 321 if errors.Is(err, git_model.ErrBranchIsProtected) { 322 322 ctx.Flash.Error(ctx.Tr("repo.settings.rename_branch_failed_protected", form.To)) 323 323 ctx.Redirect(fmt.Sprintf("%s/branches", ctx.Repo.RepoLink)) 324 + } else if git_model.IsErrBranchAlreadyExists(err) { 325 + ctx.Flash.Error(ctx.Tr("repo.branch.branch_already_exists", form.To)) 326 + ctx.Redirect(fmt.Sprintf("%s/branches", ctx.Repo.RepoLink)) 324 327 } else { 325 328 ctx.ServerError("RenameBranch", err) 326 329 }
+95
tests/integration/rename_branch_test.go
··· 5 5 6 6 import ( 7 7 "net/http" 8 + "net/url" 8 9 "testing" 9 10 10 11 git_model "code.gitea.io/gitea/models/git" ··· 17 18 ) 18 19 19 20 func TestRenameBranch(t *testing.T) { 21 + onGiteaRun(t, testRenameBranch) 22 + } 23 + 24 + func testRenameBranch(t *testing.T, u *url.URL) { 20 25 defer tests.PrepareTestEnv(t)() 21 26 22 27 repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) ··· 47 52 // check db 48 53 repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) 49 54 assert.Equal(t, "main", repo1.DefaultBranch) 55 + }) 56 + 57 + t.Run("Database syncronization", func(t *testing.T) { 58 + defer tests.PrintCurrentTest(t)() 59 + 60 + req := NewRequestWithValues(t, "POST", "/user2/repo1/settings/rename_branch", map[string]string{ 61 + "_csrf": GetCSRF(t, session, "/user2/repo1/settings/branches"), 62 + "from": "master", 63 + "to": "main", 64 + }) 65 + session.MakeRequest(t, req, http.StatusSeeOther) 66 + 67 + // check new branch link 68 + req = NewRequestWithValues(t, "GET", "/user2/repo1/src/branch/main/README.md", nil) 69 + session.MakeRequest(t, req, http.StatusOK) 70 + 71 + // check old branch link 72 + req = NewRequestWithValues(t, "GET", "/user2/repo1/src/branch/master/README.md", nil) 73 + resp := session.MakeRequest(t, req, http.StatusSeeOther) 74 + location := resp.Header().Get("Location") 75 + assert.Equal(t, "/user2/repo1/src/branch/main/README.md", location) 76 + 77 + // check db 78 + repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) 79 + assert.Equal(t, "main", repo1.DefaultBranch) 80 + 81 + // create branch1 82 + csrf := GetCSRF(t, session, "/user2/repo1/src/branch/main") 83 + 84 + req = NewRequestWithValues(t, "POST", "/user2/repo1/branches/_new/branch/main", map[string]string{ 85 + "_csrf": csrf, 86 + "new_branch_name": "branch1", 87 + }) 88 + session.MakeRequest(t, req, http.StatusSeeOther) 89 + 90 + branch1 := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: repo1.ID, Name: "branch1"}) 91 + assert.Equal(t, "branch1", branch1.Name) 92 + 93 + // create branch2 94 + req = NewRequestWithValues(t, "POST", "/user2/repo1/branches/_new/branch/main", map[string]string{ 95 + "_csrf": csrf, 96 + "new_branch_name": "branch2", 97 + }) 98 + session.MakeRequest(t, req, http.StatusSeeOther) 99 + 100 + branch2 := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: repo1.ID, Name: "branch2"}) 101 + assert.Equal(t, "branch2", branch2.Name) 102 + 103 + // rename branch2 to branch1 104 + req = NewRequestWithValues(t, "POST", "/user2/repo1/settings/rename_branch", map[string]string{ 105 + "_csrf": GetCSRF(t, session, "/user2/repo1/settings/branches"), 106 + "from": "branch2", 107 + "to": "branch1", 108 + }) 109 + session.MakeRequest(t, req, http.StatusSeeOther) 110 + flashCookie := session.GetCookie(gitea_context.CookieNameFlash) 111 + assert.NotNil(t, flashCookie) 112 + assert.Contains(t, flashCookie.Value, "error") 113 + 114 + branch2 = unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: repo1.ID, Name: "branch2"}) 115 + assert.Equal(t, "branch2", branch2.Name) 116 + branch1 = unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: repo1.ID, Name: "branch1"}) 117 + assert.Equal(t, "branch1", branch1.Name) 118 + 119 + // delete branch1 120 + req = NewRequestWithValues(t, "POST", "/user2/repo1/branches/delete", map[string]string{ 121 + "_csrf": GetCSRF(t, session, "/user2/repo1/settings/branches"), 122 + "name": "branch1", 123 + }) 124 + session.MakeRequest(t, req, http.StatusOK) 125 + branch2 = unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: repo1.ID, Name: "branch2"}) 126 + assert.Equal(t, "branch2", branch2.Name) 127 + branch1 = unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: repo1.ID, Name: "branch1"}) 128 + assert.True(t, branch1.IsDeleted) // virtual deletion 129 + 130 + // rename branch2 to branch1 again 131 + req = NewRequestWithValues(t, "POST", "/user2/repo1/settings/rename_branch", map[string]string{ 132 + "_csrf": GetCSRF(t, session, "/user2/repo1/settings/branches"), 133 + "from": "branch2", 134 + "to": "branch1", 135 + }) 136 + session.MakeRequest(t, req, http.StatusSeeOther) 137 + 138 + flashCookie = session.GetCookie(gitea_context.CookieNameFlash) 139 + assert.NotNil(t, flashCookie) 140 + assert.Contains(t, flashCookie.Value, "success") 141 + 142 + unittest.AssertNotExistsBean(t, &git_model.Branch{RepoID: repo1.ID, Name: "branch2"}) 143 + branch1 = unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: repo1.ID, Name: "branch1"}) 144 + assert.Equal(t, "branch1", branch1.Name) 50 145 }) 51 146 52 147 t.Run("Protected branch", func(t *testing.T) {