-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgit_atomic_push_test.go
More file actions
145 lines (118 loc) · 5.5 KB
/
Copy pathgit_atomic_push_test.go
File metadata and controls
145 lines (118 loc) · 5.5 KB
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
// SPDX-License-Identifier: Apache-2.0
package git
import (
"context"
"path/filepath"
"testing"
"github.com/go-git/go-git/v6/plumbing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestAtomicPush_PushOnEmpty tries to push a new commit on an totally empty repo.
func TestAtomicPush_PushOnEmpty(t *testing.T) {
ctx := context.Background()
tempDir := t.TempDir()
serverPath := filepath.Join(tempDir, "server")
remoteURL := "file://" + serverPath
// Create bare remote repository with HEAD set to main
localPath := filepath.Join(tempDir, "local")
remoteRepo := createBareRepo(t, serverPath)
// Clone that repo and do a first commit on main
localRepo, worktree := initLocalRepo(t, localPath, remoteURL, "main")
createdHash := commitFileChange(t, worktree, localPath, "README.md", "This is cool")
// New branch in empty repo, so rootHash is plumbing.ZeroHash (let's also include the assumption that we merge to main, so that we can detect if it 'updated')
branch := plumbing.NewBranchReferenceName("main")
err := PushAtomic(ctx, localRepo, plumbing.ZeroHash, branch, nil)
require.NoError(t, err)
// Verify branch exists on server
ref, err := remoteRepo.Reference(branch, true)
require.NoError(t, err)
assert.NotNil(t, ref)
require.Equal(t, createdHash, ref.Hash())
}
func TestAtomicPush_PushToMain(t *testing.T) {
ctx := context.Background()
tempDir := t.TempDir()
serverPath := filepath.Join(tempDir, "server")
remoteURL := "file://" + serverPath
// Create a remote with one commit
localPath := filepath.Join(tempDir, "local")
remoteRepo := createBareRepo(t, serverPath)
firstCommit := simulateClientCommitOnDisk(t, remoteURL, "main", "README.md", "This is an initialized remote repo")
// Check it out and create a commit based on that
localRepo, worktree := initLocalRepo(t, localPath, remoteURL, "main")
createdCommit := commitFileChange(t, worktree, localPath, "README.md", "This is cool")
branch := plumbing.NewBranchReferenceName("main")
err := PushAtomic(ctx, localRepo, firstCommit, branch, nil)
require.NoError(t, err)
// Verify branch exists on server
ref, err := remoteRepo.Reference(branch, true)
require.NoError(t, err)
assert.NotNil(t, ref)
require.Equal(t, createdCommit, ref.Hash())
}
func TestAtomicPush_PushToOther(t *testing.T) {
ctx := context.Background()
tempDir := t.TempDir()
serverPath := filepath.Join(tempDir, "server")
remoteURL := "file://" + serverPath
// Create a remote with two commits
localPath := filepath.Join(tempDir, "local")
remoteRepo := createBareRepo(t, serverPath)
firstCommit := simulateClientCommitOnDisk(t, remoteURL, "main", "README.md", "This is an initialized remote repo")
// Check it out and create a commit based on that
localRepo, worktree := initLocalRepo(t, localPath, remoteURL, "feature")
createdCommit := commitFileChange(t, worktree, localPath, "README.md", "This is cool")
// Now we expect this to error out
rootBranch := plumbing.NewBranchReferenceName("main")
featureBranch := plumbing.NewBranchReferenceName("feature")
err := PushAtomic(ctx, localRepo, firstCommit, rootBranch, nil)
require.NoError(t, err)
ref, err := remoteRepo.Reference(featureBranch, true)
require.NoError(t, err)
assert.NotNil(t, ref)
require.Equal(t, createdCommit, ref.Hash())
}
// TestAtomicPush_DetectsMissingBranch tests that push detects when remote branch doesn't exist anymore.
func TestAtomicPush_DetectsMissingBranch(t *testing.T) {
ctx := context.Background()
tempDir := t.TempDir()
serverPath := filepath.Join(tempDir, "server")
remoteURL := "file://" + serverPath
// Create a remote with two commits
localPath := filepath.Join(tempDir, "local")
remoteRepo := createBareRepo(t, serverPath)
simulateClientCommitOnDisk(t, remoteURL, "main", "README.md", "This is an initialized remote repo")
featureCommit := simulateClientCommitOnDisk(t, remoteURL, "feature", "README.md", "Some change")
// Check it out and create a commit based on that
localRepo, worktree := initLocalRepo(t, localPath, remoteURL, "feature")
createdCommit := commitFileChange(t, worktree, localPath, "README.md", "This is cool")
require.Equal(t, 3, countDepth(t, localRepo, createdCommit))
// Remove the branch on the remote (simulating that a merge has been done)
featureBranch := plumbing.NewBranchReferenceName("feature")
err := remoteRepo.Storer.RemoveReference(featureBranch)
require.NoError(t, err)
// Now we epxect this to error out
err = PushAtomic(ctx, localRepo, featureCommit, featureBranch, nil)
require.Error(t, err)
assert.Contains(t, err.Error(), "remote went missing")
}
// TestAtomicPush_DetectsUpdatedRemote should check that a change is deteceted.
func TestAtomicPush_DetectsUpdatedRemote(t *testing.T) {
ctx := context.Background()
tempDir := t.TempDir()
serverPath := filepath.Join(tempDir, "server")
remoteURL := "file://" + serverPath
// Create a remote with two commits
localPath := filepath.Join(tempDir, "local")
createBareRepo(t, serverPath)
firstCommit := simulateClientCommitOnDisk(t, remoteURL, "main", "README.md", "This is an initialized remote repo")
// Check it out and create a commit based on that
localRepo, worktree := initLocalRepo(t, localPath, remoteURL, "main")
commitFileChange(t, worktree, localPath, "README.md", "This is cool")
simulateClientCommitOnDisk(t, remoteURL, "main", "README.md", "Another change on remote!")
// Now we expect this to error out
err := PushAtomic(ctx, localRepo, firstCommit, "refs/heads/main", nil)
require.Error(t, err)
assert.Contains(t, err.Error(), "remote received unknown updates")
}