|
| 1 | +// Copyright 2024 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package integration |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "net/url" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "code.gitea.io/gitea/models/db" |
| 12 | + git_model "code.gitea.io/gitea/models/git" |
| 13 | + "code.gitea.io/gitea/models/unittest" |
| 14 | + user_model "code.gitea.io/gitea/models/user" |
| 15 | + "code.gitea.io/gitea/modules/git" |
| 16 | + repo_service "code.gitea.io/gitea/services/repository" |
| 17 | + |
| 18 | + "github.com/stretchr/testify/assert" |
| 19 | + "github.com/stretchr/testify/require" |
| 20 | +) |
| 21 | + |
| 22 | +func TestGitPush(t *testing.T) { |
| 23 | + onGiteaRun(t, testGitPush) |
| 24 | +} |
| 25 | + |
| 26 | +func testGitPush(t *testing.T, u *url.URL) { |
| 27 | + t.Run("Push branches at once", func(t *testing.T) { |
| 28 | + runTestGitPush(t, u, func(t *testing.T, gitPath string) (pushed, deleted []string) { |
| 29 | + for i := 0; i < 100; i++ { |
| 30 | + branchName := fmt.Sprintf("branch-%d", i) |
| 31 | + pushed = append(pushed, branchName) |
| 32 | + doGitCreateBranch(gitPath, branchName)(t) |
| 33 | + } |
| 34 | + pushed = append(pushed, "master") |
| 35 | + doGitPushTestRepository(gitPath, "origin", "--all")(t) |
| 36 | + return pushed, deleted |
| 37 | + }) |
| 38 | + }) |
| 39 | + |
| 40 | + t.Run("Push branches one by one", func(t *testing.T) { |
| 41 | + runTestGitPush(t, u, func(t *testing.T, gitPath string) (pushed, deleted []string) { |
| 42 | + for i := 0; i < 100; i++ { |
| 43 | + branchName := fmt.Sprintf("branch-%d", i) |
| 44 | + doGitCreateBranch(gitPath, branchName)(t) |
| 45 | + doGitPushTestRepository(gitPath, "origin", branchName)(t) |
| 46 | + pushed = append(pushed, branchName) |
| 47 | + } |
| 48 | + return pushed, deleted |
| 49 | + }) |
| 50 | + }) |
| 51 | + |
| 52 | + t.Run("Delete branches", func(t *testing.T) { |
| 53 | + runTestGitPush(t, u, func(t *testing.T, gitPath string) (pushed, deleted []string) { |
| 54 | + doGitPushTestRepository(gitPath, "origin", "master")(t) // make sure master is the default branch instead of a branch we are going to delete |
| 55 | + pushed = append(pushed, "master") |
| 56 | + |
| 57 | + for i := 0; i < 100; i++ { |
| 58 | + branchName := fmt.Sprintf("branch-%d", i) |
| 59 | + pushed = append(pushed, branchName) |
| 60 | + doGitCreateBranch(gitPath, branchName)(t) |
| 61 | + } |
| 62 | + doGitPushTestRepository(gitPath, "origin", "--all")(t) |
| 63 | + |
| 64 | + for i := 0; i < 10; i++ { |
| 65 | + branchName := fmt.Sprintf("branch-%d", i) |
| 66 | + doGitPushTestRepository(gitPath, "origin", "--delete", branchName)(t) |
| 67 | + deleted = append(deleted, branchName) |
| 68 | + } |
| 69 | + return pushed, deleted |
| 70 | + }) |
| 71 | + }) |
| 72 | +} |
| 73 | + |
| 74 | +func runTestGitPush(t *testing.T, u *url.URL, gitOperation func(t *testing.T, gitPath string) (pushed, deleted []string)) { |
| 75 | + user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) |
| 76 | + repo, err := repo_service.CreateRepository(db.DefaultContext, user, user, repo_service.CreateRepoOptions{ |
| 77 | + Name: "repo-to-push", |
| 78 | + Description: "test git push", |
| 79 | + AutoInit: false, |
| 80 | + DefaultBranch: "main", |
| 81 | + IsPrivate: false, |
| 82 | + }) |
| 83 | + require.NoError(t, err) |
| 84 | + require.NotEmpty(t, repo) |
| 85 | + |
| 86 | + gitPath := t.TempDir() |
| 87 | + |
| 88 | + doGitInitTestRepository(gitPath)(t) |
| 89 | + |
| 90 | + oldPath := u.Path |
| 91 | + oldUser := u.User |
| 92 | + defer func() { |
| 93 | + u.Path = oldPath |
| 94 | + u.User = oldUser |
| 95 | + }() |
| 96 | + u.Path = repo.FullName() + ".git" |
| 97 | + u.User = url.UserPassword(user.LowerName, userPassword) |
| 98 | + |
| 99 | + doGitAddRemote(gitPath, "origin", u)(t) |
| 100 | + |
| 101 | + gitRepo, err := git.OpenRepository(git.DefaultContext, gitPath) |
| 102 | + require.NoError(t, err) |
| 103 | + defer gitRepo.Close() |
| 104 | + |
| 105 | + pushedBranches, deletedBranches := gitOperation(t, gitPath) |
| 106 | + |
| 107 | + dbBranches := make([]*git_model.Branch, 0) |
| 108 | + require.NoError(t, db.GetEngine(db.DefaultContext).Where("repo_id=?", repo.ID).Find(&dbBranches)) |
| 109 | + assert.Equalf(t, len(pushedBranches), len(dbBranches), "mismatched number of branches in db") |
| 110 | + dbBranchesMap := make(map[string]*git_model.Branch, len(dbBranches)) |
| 111 | + for _, branch := range dbBranches { |
| 112 | + dbBranchesMap[branch.Name] = branch |
| 113 | + } |
| 114 | + |
| 115 | + deletedBranchesMap := make(map[string]bool, len(deletedBranches)) |
| 116 | + for _, branchName := range deletedBranches { |
| 117 | + deletedBranchesMap[branchName] = true |
| 118 | + } |
| 119 | + |
| 120 | + for _, branchName := range pushedBranches { |
| 121 | + branch, ok := dbBranchesMap[branchName] |
| 122 | + deleted := deletedBranchesMap[branchName] |
| 123 | + assert.True(t, ok, "branch %s not found in database", branchName) |
| 124 | + assert.Equal(t, deleted, branch.IsDeleted, "IsDeleted of %s is %v, but it's expected to be %v", branchName, branch.IsDeleted, deleted) |
| 125 | + commitID, err := gitRepo.GetBranchCommitID(branchName) |
| 126 | + require.NoError(t, err) |
| 127 | + assert.Equal(t, commitID, branch.CommitID) |
| 128 | + } |
| 129 | + |
| 130 | + require.NoError(t, repo_service.DeleteRepositoryDirectly(db.DefaultContext, user, repo.ID)) |
| 131 | +} |
0 commit comments