Skip to content

Commit 568b284

Browse files
authored
Merge pull request #270 from fluxcd/rename-git-pkgs
2 parents aaee433 + fac1afa commit 568b284

File tree

15 files changed

+157
-155
lines changed

15 files changed

+157
-155
lines changed

api/v1beta1/gitrepository_types.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ import (
2525
const (
2626
// GitRepositoryKind is the string representation of a GitRepository.
2727
GitRepositoryKind = "GitRepository"
28-
// GoGitImplementation represents the go-git git implementation kind.
28+
29+
// GoGitImplementation represents the go-git Git implementation kind.
2930
GoGitImplementation = "go-git"
30-
// LibGit2Implementation represents the gi2go git implementation kind.
31+
// LibGit2Implementation represents the git2go Git implementation kind.
3132
LibGit2Implementation = "libgit2"
3233
)
3334

controllers/gitrepository_controller.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import (
4444

4545
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
4646
"github.com/fluxcd/source-controller/pkg/git"
47-
"github.com/fluxcd/source-controller/pkg/git/common"
47+
"github.com/fluxcd/source-controller/pkg/git/strategy"
4848
)
4949

5050
// +kubebuilder:rbac:groups=source.toolkit.fluxcd.io,resources=gitrepositories,verbs=get;list;watch;create;update;patch;delete
@@ -178,9 +178,9 @@ func (r *GitRepositoryReconciler) reconcile(ctx context.Context, repository sour
178178
defer os.RemoveAll(tmpGit)
179179

180180
// determine auth method
181-
auth := &common.Auth{}
181+
auth := &git.Auth{}
182182
if repository.Spec.SecretRef != nil {
183-
authStrategy, err := git.AuthSecretStrategyForURL(repository.Spec.URL, repository.Spec.GitImplementation)
183+
authStrategy, err := strategy.AuthSecretStrategyForURL(repository.Spec.URL, repository.Spec.GitImplementation)
184184
if err != nil {
185185
return sourcev1.GitRepositoryNotReady(repository, sourcev1.AuthenticationFailedReason, err.Error()), err
186186
}
@@ -204,7 +204,7 @@ func (r *GitRepositoryReconciler) reconcile(ctx context.Context, repository sour
204204
}
205205
}
206206

207-
checkoutStrategy, err := git.CheckoutStrategyForRef(repository.Spec.Reference, repository.Spec.GitImplementation)
207+
checkoutStrategy, err := strategy.CheckoutStrategyForRef(repository.Spec.Reference, repository.Spec.GitImplementation)
208208
if err != nil {
209209
return sourcev1.GitRepositoryNotReady(repository, sourcev1.GitOperationFailedReason, err.Error()), err
210210
}

pkg/git/common/common.go

Lines changed: 0 additions & 51 deletions
This file was deleted.

pkg/git/git.go

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,37 @@ limitations under the License.
1717
package git
1818

1919
import (
20-
"fmt"
20+
"context"
2121

22-
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
23-
"github.com/fluxcd/source-controller/pkg/git/common"
24-
gitv1 "github.com/fluxcd/source-controller/pkg/git/v1"
25-
gitv2 "github.com/fluxcd/source-controller/pkg/git/v2"
22+
"github.com/go-git/go-git/v5/plumbing/transport"
23+
git2go "github.com/libgit2/git2go/v31"
24+
corev1 "k8s.io/api/core/v1"
2625
)
2726

2827
const (
29-
defaultBranch = "master"
28+
DefaultOrigin = "origin"
29+
DefaultBranch = "master"
30+
DefaultPublicKeyAuthUser = "git"
31+
CAFile = "caFile"
3032
)
3133

32-
func CheckoutStrategyForRef(ref *sourcev1.GitRepositoryRef, gitImplementation string) (common.CheckoutStrategy, error) {
33-
switch gitImplementation {
34-
case sourcev1.GoGitImplementation:
35-
return gitv1.CheckoutStrategyForRef(ref), nil
36-
case sourcev1.LibGit2Implementation:
37-
return gitv2.CheckoutStrategyForRef(ref), nil
38-
default:
39-
return nil, fmt.Errorf("invalid git implementation %s", gitImplementation)
40-
}
34+
type Commit interface {
35+
Verify(secret corev1.Secret) error
36+
Hash() string
4137
}
4238

43-
func AuthSecretStrategyForURL(url string, gitImplementation string) (common.AuthSecretStrategy, error) {
44-
switch gitImplementation {
45-
case sourcev1.GoGitImplementation:
46-
return gitv1.AuthSecretStrategyForURL(url)
47-
case sourcev1.LibGit2Implementation:
48-
return gitv2.AuthSecretStrategyForURL(url)
49-
default:
50-
return nil, fmt.Errorf("invalid git implementation %s", gitImplementation)
51-
}
39+
type CheckoutStrategy interface {
40+
Checkout(ctx context.Context, path, url string, auth *Auth) (Commit, string, error)
41+
}
42+
43+
// TODO(hidde): candidate for refactoring, so that we do not directly
44+
// depend on implementation specifics here.
45+
type Auth struct {
46+
AuthMethod transport.AuthMethod
47+
CredCallback git2go.CredentialsCallback
48+
CertCallback git2go.CertificateCheckCallback
49+
}
50+
51+
type AuthSecretStrategy interface {
52+
Method(secret corev1.Secret) (*Auth, error)
5253
}
Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package v1
17+
package gogit
1818

1919
import (
2020
"context"
@@ -23,51 +23,52 @@ import (
2323
"time"
2424

2525
"github.com/Masterminds/semver/v3"
26-
"github.com/go-git/go-git/v5"
26+
extgogit "github.com/go-git/go-git/v5"
2727
"github.com/go-git/go-git/v5/plumbing"
2828

2929
"github.com/fluxcd/pkg/version"
30+
3031
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
31-
"github.com/fluxcd/source-controller/pkg/git/common"
32+
"github.com/fluxcd/source-controller/pkg/git"
3233
)
3334

34-
func CheckoutStrategyForRef(ref *sourcev1.GitRepositoryRef) common.CheckoutStrategy {
35+
func CheckoutStrategyForRef(ref *sourcev1.GitRepositoryRef) git.CheckoutStrategy {
3536
switch {
3637
case ref == nil:
37-
return &CheckoutBranch{branch: common.DefaultBranch}
38+
return &CheckoutBranch{branch: git.DefaultBranch}
3839
case ref.SemVer != "":
3940
return &CheckoutSemVer{semVer: ref.SemVer}
4041
case ref.Tag != "":
4142
return &CheckoutTag{tag: ref.Tag}
4243
case ref.Commit != "":
4344
strategy := &CheckoutCommit{branch: ref.Branch, commit: ref.Commit}
4445
if strategy.branch == "" {
45-
strategy.branch = common.DefaultBranch
46+
strategy.branch = git.DefaultBranch
4647
}
4748
return strategy
4849
case ref.Branch != "":
4950
return &CheckoutBranch{branch: ref.Branch}
5051
default:
51-
return &CheckoutBranch{branch: common.DefaultBranch}
52+
return &CheckoutBranch{branch: git.DefaultBranch}
5253
}
5354
}
5455

5556
type CheckoutBranch struct {
5657
branch string
5758
}
5859

59-
func (c *CheckoutBranch) Checkout(ctx context.Context, path, url string, auth *common.Auth) (common.Commit, string, error) {
60-
repo, err := git.PlainCloneContext(ctx, path, false, &git.CloneOptions{
60+
func (c *CheckoutBranch) Checkout(ctx context.Context, path, url string, auth *git.Auth) (git.Commit, string, error) {
61+
repo, err := extgogit.PlainCloneContext(ctx, path, false, &extgogit.CloneOptions{
6162
URL: url,
6263
Auth: auth.AuthMethod,
63-
RemoteName: common.DefaultOrigin,
64+
RemoteName: git.DefaultOrigin,
6465
ReferenceName: plumbing.NewBranchReferenceName(c.branch),
6566
SingleBranch: true,
6667
NoCheckout: false,
6768
Depth: 1,
6869
RecurseSubmodules: 0,
6970
Progress: nil,
70-
Tags: git.NoTags,
71+
Tags: extgogit.NoTags,
7172
})
7273
if err != nil {
7374
return nil, "", fmt.Errorf("unable to clone '%s', error: %w", url, err)
@@ -87,18 +88,18 @@ type CheckoutTag struct {
8788
tag string
8889
}
8990

90-
func (c *CheckoutTag) Checkout(ctx context.Context, path, url string, auth *common.Auth) (common.Commit, string, error) {
91-
repo, err := git.PlainCloneContext(ctx, path, false, &git.CloneOptions{
91+
func (c *CheckoutTag) Checkout(ctx context.Context, path, url string, auth *git.Auth) (git.Commit, string, error) {
92+
repo, err := extgogit.PlainCloneContext(ctx, path, false, &extgogit.CloneOptions{
9293
URL: url,
9394
Auth: auth.AuthMethod,
94-
RemoteName: common.DefaultOrigin,
95+
RemoteName: git.DefaultOrigin,
9596
ReferenceName: plumbing.NewTagReferenceName(c.tag),
9697
SingleBranch: true,
9798
NoCheckout: false,
9899
Depth: 1,
99100
RecurseSubmodules: 0,
100101
Progress: nil,
101-
Tags: git.NoTags,
102+
Tags: extgogit.NoTags,
102103
})
103104
if err != nil {
104105
return nil, "", fmt.Errorf("unable to clone '%s', error: %w", url, err)
@@ -119,17 +120,17 @@ type CheckoutCommit struct {
119120
commit string
120121
}
121122

122-
func (c *CheckoutCommit) Checkout(ctx context.Context, path, url string, auth *common.Auth) (common.Commit, string, error) {
123-
repo, err := git.PlainCloneContext(ctx, path, false, &git.CloneOptions{
123+
func (c *CheckoutCommit) Checkout(ctx context.Context, path, url string, auth *git.Auth) (git.Commit, string, error) {
124+
repo, err := extgogit.PlainCloneContext(ctx, path, false, &extgogit.CloneOptions{
124125
URL: url,
125126
Auth: auth.AuthMethod,
126-
RemoteName: common.DefaultOrigin,
127+
RemoteName: git.DefaultOrigin,
127128
ReferenceName: plumbing.NewBranchReferenceName(c.branch),
128129
SingleBranch: true,
129130
NoCheckout: false,
130131
RecurseSubmodules: 0,
131132
Progress: nil,
132-
Tags: git.NoTags,
133+
Tags: extgogit.NoTags,
133134
})
134135
if err != nil {
135136
return nil, "", fmt.Errorf("unable to clone '%s', error: %w", url, err)
@@ -142,7 +143,7 @@ func (c *CheckoutCommit) Checkout(ctx context.Context, path, url string, auth *c
142143
if err != nil {
143144
return nil, "", fmt.Errorf("git commit '%s' not found: %w", c.commit, err)
144145
}
145-
err = w.Checkout(&git.CheckoutOptions{
146+
err = w.Checkout(&extgogit.CheckoutOptions{
146147
Hash: commit.Hash,
147148
Force: true,
148149
})
@@ -156,21 +157,21 @@ type CheckoutSemVer struct {
156157
semVer string
157158
}
158159

159-
func (c *CheckoutSemVer) Checkout(ctx context.Context, path, url string, auth *common.Auth) (common.Commit, string, error) {
160+
func (c *CheckoutSemVer) Checkout(ctx context.Context, path, url string, auth *git.Auth) (git.Commit, string, error) {
160161
verConstraint, err := semver.NewConstraint(c.semVer)
161162
if err != nil {
162163
return nil, "", fmt.Errorf("semver parse range error: %w", err)
163164
}
164165

165-
repo, err := git.PlainCloneContext(ctx, path, false, &git.CloneOptions{
166+
repo, err := extgogit.PlainCloneContext(ctx, path, false, &extgogit.CloneOptions{
166167
URL: url,
167168
Auth: auth.AuthMethod,
168-
RemoteName: common.DefaultOrigin,
169+
RemoteName: git.DefaultOrigin,
169170
NoCheckout: false,
170171
Depth: 1,
171172
RecurseSubmodules: 0,
172173
Progress: nil,
173-
Tags: git.AllTags,
174+
Tags: extgogit.AllTags,
174175
})
175176
if err != nil {
176177
return nil, "", fmt.Errorf("unable to clone '%s', error: %w", url, err)
@@ -237,7 +238,7 @@ func (c *CheckoutSemVer) Checkout(ctx context.Context, path, url string, auth *c
237238
return nil, "", fmt.Errorf("git worktree error: %w", err)
238239
}
239240

240-
err = w.Checkout(&git.CheckoutOptions{
241+
err = w.Checkout(&extgogit.CheckoutOptions{
241242
Branch: plumbing.NewTagReferenceName(t),
242243
})
243244
if err != nil {
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package v1
17+
package gogit
1818

1919
import (
2020
"context"
2121
"io/ioutil"
2222
"os"
2323
"testing"
2424

25-
"github.com/fluxcd/source-controller/pkg/git/common"
25+
"github.com/fluxcd/source-controller/pkg/git"
2626
)
2727

2828
func TestCheckoutTagSemVer_Checkout(t *testing.T) {
29-
auth := &common.Auth{}
29+
auth := &git.Auth{}
3030
tag := CheckoutTag{
3131
tag: "v1.7.0",
3232
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package v1
17+
package gogit
1818

1919
import (
2020
"fmt"

0 commit comments

Comments
 (0)