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
|
package repository
import (
"fmt"
"log"
"os"
"path"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewGoGitRepo(t *testing.T) {
// Plain
plainRepo := CreateGoGitTestRepo(t, false)
plainRoot := goGitRepoDir(t, plainRepo)
require.NoError(t, plainRepo.Close())
plainGitDir := filepath.Join(plainRoot, ".git")
// Bare
bareRepo := CreateGoGitTestRepo(t, true)
bareRoot := goGitRepoDir(t, bareRepo)
require.NoError(t, bareRepo.Close())
bareGitDir := bareRoot
tests := []struct {
inPath string
outPath string
err bool
}{
// errors
{"/", "", true},
// parent dir of a repo
{filepath.Dir(plainRoot), "", true},
// Plain repo
{plainRoot, plainGitDir, false},
{plainGitDir, plainGitDir, false},
{path.Join(plainGitDir, "objects"), plainGitDir, false},
// Bare repo
{bareRoot, bareGitDir, false},
{bareGitDir, bareGitDir, false},
{path.Join(bareGitDir, "objects"), bareGitDir, false},
}
for i, tc := range tests {
r, err := OpenGoGitRepo(tc.inPath, namespace, nil)
if tc.err {
require.Error(t, err, i)
} else {
require.NoError(t, err, i)
assert.Equal(t, filepath.ToSlash(tc.outPath), filepath.ToSlash(r.path), i)
require.NoError(t, r.Close())
}
}
}
func TestGoGitRepo(t *testing.T) {
RepoTest(t, CreateGoGitTestRepo)
}
func TestGoGitRepo_Indexes(t *testing.T) {
repo := CreateGoGitTestRepo(t, false)
plainRoot := goGitRepoDir(t, repo)
// Can create indices
indexA, err := repo.GetIndex("a")
require.NoError(t, err)
require.NotZero(t, indexA)
require.FileExists(t, filepath.Join(plainRoot, ".git", namespace, "indexes", "a", "index_meta.json"))
require.FileExists(t, filepath.Join(plainRoot, ".git", namespace, "indexes", "a", "store"))
indexB, err := repo.GetIndex("b")
require.NoError(t, err)
require.NotZero(t, indexB)
require.DirExists(t, filepath.Join(plainRoot, ".git", namespace, "indexes", "b"))
// Can get an existing index
indexA, err = repo.GetIndex("a")
require.NoError(t, err)
require.NotZero(t, indexA)
}
func TestGoGit_DetectsSubmodules(t *testing.T) {
repo := CreateGoGitTestRepo(t, false)
expected := filepath.Join(goGitRepoDir(t, repo), "/.git")
d := t.TempDir()
err := os.WriteFile(filepath.Join(d, ".git"), []byte(fmt.Sprintf("gitdir: %s", expected)), 0600)
require.NoError(t, err)
result, err := detectGitPath(d, 0)
assert.Empty(t, err)
assert.Equal(t, expected, result)
}
func TestGoGitRepoSSH(t *testing.T) {
repo := CreateGoGitTestRepo(t, false)
err := repo.AddRemote("ssh", "ssh://git@github.com:MichaelMure/git-bug.git")
if err != nil {
log.Fatal(err)
}
keys, err := repo.SSHAuth("ssh")
require.NotNil(t, keys)
require.Empty(t, err)
err = repo.AddRemote("http", "http://github.com/MichaelMure/git-bug.git")
if err != nil {
log.Fatal(err)
}
keys, err = repo.SSHAuth("http")
require.Nil(t, keys)
require.Empty(t, err)
err = repo.AddRemote("https", "https://github.com/MichaelMure/git-bug.git")
if err != nil {
log.Fatal(err)
}
keys, err = repo.SSHAuth("https")
require.Nil(t, keys)
require.Empty(t, err)
err = repo.AddRemote("git", "git://github.com/MichaelMure/git-bug.git")
if err != nil {
log.Fatal(err)
}
keys, err = repo.SSHAuth("git")
require.Nil(t, keys)
require.Empty(t, err)
err = repo.AddRemote("scp-like", "git@github.com:MichaelMure/git-bug.git")
if err != nil {
log.Fatal(err)
}
keys, err = repo.SSHAuth("scp-like")
require.NotNil(t, keys)
require.Empty(t, err)
}
|