summaryrefslogtreecommitdiffstats
path: root/tests/integration/git_push_test.go
blob: b37fb02444e7265f16e35ddc5fbcca87c5f55400 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package integration

import (
	"fmt"
	"net/url"
	"testing"

	"code.gitea.io/gitea/models/db"
	git_model "code.gitea.io/gitea/models/git"
	"code.gitea.io/gitea/models/unittest"
	user_model "code.gitea.io/gitea/models/user"
	"code.gitea.io/gitea/modules/git"
	repo_service "code.gitea.io/gitea/services/repository"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestGitPush(t *testing.T) {
	onGiteaRun(t, testGitPush)
}

func testGitPush(t *testing.T, u *url.URL) {
	t.Run("Push branches at once", func(t *testing.T) {
		runTestGitPush(t, u, func(t *testing.T, gitPath string) (pushed, deleted []string) {
			for i := 0; i < 100; i++ {
				branchName := fmt.Sprintf("branch-%d", i)
				pushed = append(pushed, branchName)
				doGitCreateBranch(gitPath, branchName)(t)
			}
			pushed = append(pushed, "master")
			doGitPushTestRepository(gitPath, "origin", "--all")(t)
			return pushed, deleted
		})
	})

	t.Run("Push branches one by one", func(t *testing.T) {
		runTestGitPush(t, u, func(t *testing.T, gitPath string) (pushed, deleted []string) {
			for i := 0; i < 100; i++ {
				branchName := fmt.Sprintf("branch-%d", i)
				doGitCreateBranch(gitPath, branchName)(t)
				doGitPushTestRepository(gitPath, "origin", branchName)(t)
				pushed = append(pushed, branchName)
			}
			return pushed, deleted
		})
	})

	t.Run("Push branch with options", func(t *testing.T) {
		runTestGitPush(t, u, func(t *testing.T, gitPath string) (pushed, deleted []string) {
			branchName := "branch-with-options"
			doGitCreateBranch(gitPath, branchName)(t)
			doGitPushTestRepository(gitPath, "origin", branchName, "-o", "repo.private=true", "-o", "repo.template=true")(t)
			pushed = append(pushed, branchName)

			return pushed, deleted
		})
	})

	t.Run("Delete branches", func(t *testing.T) {
		runTestGitPush(t, u, func(t *testing.T, gitPath string) (pushed, deleted []string) {
			doGitPushTestRepository(gitPath, "origin", "master")(t) // make sure master is the default branch instead of a branch we are going to delete
			pushed = append(pushed, "master")

			for i := 0; i < 100; i++ {
				branchName := fmt.Sprintf("branch-%d", i)
				pushed = append(pushed, branchName)
				doGitCreateBranch(gitPath, branchName)(t)
			}
			doGitPushTestRepository(gitPath, "origin", "--all")(t)

			for i := 0; i < 10; i++ {
				branchName := fmt.Sprintf("branch-%d", i)
				doGitPushTestRepository(gitPath, "origin", "--delete", branchName)(t)
				deleted = append(deleted, branchName)
			}
			return pushed, deleted
		})
	})

	t.Run("Push to deleted branch", func(t *testing.T) {
		runTestGitPush(t, u, func(t *testing.T, gitPath string) (pushed, deleted []string) {
			doGitPushTestRepository(gitPath, "origin", "master")(t) // make sure master is the default branch instead of a branch we are going to delete
			pushed = append(pushed, "master")

			doGitCreateBranch(gitPath, "branch-1")(t)
			doGitPushTestRepository(gitPath, "origin", "branch-1")(t)
			pushed = append(pushed, "branch-1")

			// delete and restore
			doGitPushTestRepository(gitPath, "origin", "--delete", "branch-1")(t)
			doGitPushTestRepository(gitPath, "origin", "branch-1")(t)

			return pushed, deleted
		})
	})
}

func runTestGitPush(t *testing.T, u *url.URL, gitOperation func(t *testing.T, gitPath string) (pushed, deleted []string)) {
	user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
	repo, err := repo_service.CreateRepository(db.DefaultContext, user, user, repo_service.CreateRepoOptions{
		Name:          "repo-to-push",
		Description:   "test git push",
		AutoInit:      false,
		DefaultBranch: "main",
		IsPrivate:     false,
	})
	require.NoError(t, err)
	require.NotEmpty(t, repo)

	gitPath := t.TempDir()

	doGitInitTestRepository(gitPath)(t)

	oldPath := u.Path
	oldUser := u.User
	defer func() {
		u.Path = oldPath
		u.User = oldUser
	}()
	u.Path = repo.FullName() + ".git"
	u.User = url.UserPassword(user.LowerName, userPassword)

	doGitAddRemote(gitPath, "origin", u)(t)

	gitRepo, err := git.OpenRepository(git.DefaultContext, gitPath)
	require.NoError(t, err)
	defer gitRepo.Close()

	pushedBranches, deletedBranches := gitOperation(t, gitPath)

	dbBranches := make([]*git_model.Branch, 0)
	require.NoError(t, db.GetEngine(db.DefaultContext).Where("repo_id=?", repo.ID).Find(&dbBranches))
	assert.Equalf(t, len(pushedBranches), len(dbBranches), "mismatched number of branches in db")
	dbBranchesMap := make(map[string]*git_model.Branch, len(dbBranches))
	for _, branch := range dbBranches {
		dbBranchesMap[branch.Name] = branch
	}

	deletedBranchesMap := make(map[string]bool, len(deletedBranches))
	for _, branchName := range deletedBranches {
		deletedBranchesMap[branchName] = true
	}

	for _, branchName := range pushedBranches {
		branch, ok := dbBranchesMap[branchName]
		deleted := deletedBranchesMap[branchName]
		assert.True(t, ok, "branch %s not found in database", branchName)
		assert.Equal(t, deleted, branch.IsDeleted, "IsDeleted of %s is %v, but it's expected to be %v", branchName, branch.IsDeleted, deleted)
		commitID, err := gitRepo.GetBranchCommitID(branchName)
		require.NoError(t, err)
		assert.Equal(t, commitID, branch.CommitID)
	}

	require.NoError(t, repo_service.DeleteRepositoryDirectly(db.DefaultContext, user, repo.ID))
}