Skip to content

Commit

Permalink
feat: git repo support
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobmoellerdev committed Sep 9, 2024
1 parent 441ffd1 commit 23271da
Show file tree
Hide file tree
Showing 15 changed files with 142 additions and 178 deletions.
1 change: 1 addition & 0 deletions api/credentials/builtin/git/identity/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strings"

giturls "github.com/whilp/git-urls"

"ocm.software/ocm/api/credentials/cpi"
"ocm.software/ocm/api/credentials/identity/hostpath"
"ocm.software/ocm/api/utils/listformat"
Expand Down
17 changes: 9 additions & 8 deletions api/oci/extensions/repositories/git/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@

# Repository `GitRepository` - git based repository

## Synopsis

### Synopsis

```
```yaml
type: GitRepository/v1
```
Expand All @@ -21,11 +20,13 @@ Supported specification version is `v1`.
The type specific specification fields are:

- **`url`** *string*

URL of the git repository in the form of <url>@<ref>#<path> ^([^@#]+)(@[^#\n]+)?(#[^@\n]+)?
- url is the URL of the git repository
- ref is the git reference to checkout, if not specified, defaults to "HEAD"
- path is the path to the file or directory to use as the source, if not specified,defaults to the root of the repository.

URL of the git repository in any standard git URL format.
The schemes `http`, `https`, `git`, `ssh` and `file` are supported.

- **`ref`** *string*

The git reference to use. This can be a branch, tag, or commit hash. The default is `HEAD`, pointing to the default branch of a repository in most implementations.

### Go Bindings

Expand Down
2 changes: 1 addition & 1 deletion api/oci/extensions/repositories/git/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func Open(ctx cpi.ContextProvider, acc accessobj.AccessMode, url string, opts Op
if err != nil {
return nil, err
}
return New(cpi.FromProvider(ctx), spec)
return New(cpi.FromProvider(ctx), spec, nil)
}

func Create(ctx cpi.ContextProvider, acc accessobj.AccessMode, url string, opts Options) (Repository, error) {
Expand Down
8 changes: 4 additions & 4 deletions api/oci/extensions/repositories/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package git_test
import (
"fmt"

. "github.com/mandelsoft/goutils/testutils"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/mandelsoft/filepath/pkg/filepath"
Expand All @@ -12,10 +16,6 @@ import (
"github.com/mandelsoft/vfs/pkg/vfs"
"github.com/opencontainers/go-digest"

. "github.com/mandelsoft/goutils/testutils"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"ocm.software/ocm/api/datacontext/attrs/tmpcache"
"ocm.software/ocm/api/datacontext/attrs/vfsattr"
"ocm.software/ocm/api/oci"
Expand Down
19 changes: 15 additions & 4 deletions api/oci/extensions/repositories/git/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package git

import (
"context"
"fmt"

"github.com/mandelsoft/logging"
"github.com/mandelsoft/vfs/pkg/vfs"
Expand Down Expand Up @@ -32,25 +33,35 @@ var (
_ credentials.ConsumerIdentityProvider = &RepositoryImpl{}
)

func New(ctx cpi.Context, spec *RepositorySpec) (Repository, error) {
func New(ctx cpi.Context, spec *RepositorySpec, creds credentials.Credentials) (Repository, error) {
urs := spec.UniformRepositorySpec()
i := &RepositoryImpl{
RepositoryImplBase: cpi.NewRepositoryImplBase(ctx),
logger: logging.DynamicLogger(ctx, logging.NewAttribute(ocmlog.ATTR_HOST, urs.Host)),
spec: spec,
}

opts := spec.ToClientOptions()

if creds != nil {
auth, err := git.AuthFromCredentials(creds)
if err != nil {
return nil, fmt.Errorf("failed to create git authentication from given credentials: %w", err)
}
opts.AuthMethod = auth
}

var err error
if i.client, err = git.NewClient(spec.ToClientOptions()); err != nil {
return nil, err
if i.client, err = git.NewClient(opts); err != nil {
return nil, fmt.Errorf("failed to create new git client for interacting with the repository: %w", err)
}

repo, err := ctf.New(ctx, &ctf.RepositorySpec{
StandardOptions: spec.StandardOptions,
AccessMode: spec.AccessMode,
}, i.client, i.client, vfs.FileMode(0o770))
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to create new ctf repository within the git repository: %w", err)
}
i.ctf = repo

Expand Down
30 changes: 28 additions & 2 deletions api/oci/extensions/repositories/git/type.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package git

import (
"fmt"

"ocm.software/ocm/api/credentials"
"ocm.software/ocm/api/oci/cpi"
"ocm.software/ocm/api/oci/internal"
"ocm.software/ocm/api/tech/git"
"ocm.software/ocm/api/utils/accessio"
"ocm.software/ocm/api/utils/accessobj"
Expand Down Expand Up @@ -38,9 +41,18 @@ type RepositorySpec struct {
runtime.ObjectVersionedType `json:",inline"`
accessio.StandardOptions `json:",inline"`

// URL is the url of the RepositoryImpl to resolve artifacts.
// URL is the git url of the RepositoryImpl to resolve artifacts.
URL string `json:"url"`

// Ref is the git ref of the RepositoryImpl to resolve artifacts.
// Examples include
// - heads/master
// - tags/v1.0.0
// - pull/123/head
// - remotes/origin/feature
// If empty, the default is set to HEAD.
Ref string `json:"ref,omitempty"`

// Author is the author of commits generated by the repository. If not set, is defaulted from environment and git
// configuration of the host system.
Author *Author `json:"author,omitempty"`
Expand Down Expand Up @@ -110,7 +122,7 @@ func (s *RepositorySpec) UniformRepositorySpec() *cpi.UniformRepositorySpec {
}

func (s *RepositorySpec) Repository(ctx cpi.Context, creds credentials.Credentials) (cpi.Repository, error) {
return New(ctx, s)
return New(ctx, s, creds)
}

func (s *RepositorySpec) ToClientOptions() git.ClientOptions {
Expand All @@ -124,6 +136,20 @@ func (s *RepositorySpec) ToClientOptions() git.ClientOptions {
}

opts.URL = s.URL
opts.Ref = s.Ref

return opts
}

func (s *RepositorySpec) Validate(ctx internal.Context, c credentials.Credentials, context ...credentials.UsageContext) error {
repo, err := New(ctx, s, c)
if err != nil {
return fmt.Errorf("failed to initialize repository for validation: %w", err)
}
defer repo.Close()

if _, err := repo.NamespaceLister().NumNamespaces(""); err != nil {
return fmt.Errorf("failed to list namespaces to validate repository: %w", err)
}
return nil
}
6 changes: 2 additions & 4 deletions api/ocm/extensions/accessmethods/git/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@

# Access Method `git` - Git Commit Access

## Synopsis

### Synopsis

```
```yaml
type: git/v1
```
Expand Down Expand Up @@ -37,7 +36,6 @@ The type specific specification fields are:

The sha/id of the git commit


### Go Bindings

The go binding can be found [here](method.go)
4 changes: 2 additions & 2 deletions api/ocm/extensions/accessmethods/git/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
"github.com/go-git/go-git/v5/plumbing"
"github.com/mandelsoft/goutils/errors"
giturls "github.com/whilp/git-urls"

"ocm.software/ocm/api/credentials"
"ocm.software/ocm/api/credentials/builtin/git/identity"
techgit "ocm.software/ocm/api/tech/git"

"ocm.software/ocm/api/ocm/cpi/accspeccpi"
"ocm.software/ocm/api/ocm/internal"
techgit "ocm.software/ocm/api/tech/git"
"ocm.software/ocm/api/utils/accessio"
"ocm.software/ocm/api/utils/accessio/downloader/git"
"ocm.software/ocm/api/utils/accessobj"
Expand Down
21 changes: 15 additions & 6 deletions api/ocm/extensions/accessmethods/git/method_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@ package git_test

import (
"embed"
_ "embed"
"fmt"
"io"
"os"
"time"

_ "embed"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/mandelsoft/filepath/pkg/filepath"
"github.com/mandelsoft/vfs/pkg/cwdfs"
"github.com/mandelsoft/vfs/pkg/osfs"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"ocm.software/ocm/api/datacontext/attrs/tmpcache"
"ocm.software/ocm/api/datacontext/attrs/vfsattr"
Expand Down Expand Up @@ -61,7 +65,7 @@ var _ = Describe("Method", func() {
fileInRepo, err := os.OpenFile(
repoPath,
os.O_CREATE|os.O_RDWR|os.O_TRUNC,
0600,
0o600,
)
Expect(err).ToNot(HaveOccurred())

Expand All @@ -75,7 +79,13 @@ var _ = Describe("Method", func() {
wt, err := repo.Worktree()
Expect(err).ToNot(HaveOccurred())
Expect(wt.AddGlob("*")).To(Succeed())
_, err = wt.Commit("OCM Test Commit", &git.CommitOptions{})
_, err = wt.Commit("OCM Test Commit", &git.CommitOptions{
Author: &object.Signature{
Name: "OCM Test",
Email: "[email protected]",
When: time.Now(),
},
})
Expect(err).ToNot(HaveOccurred())

accessSpec = me.New(
Expand All @@ -98,5 +108,4 @@ var _ = Describe("Method", func() {
Expect(err).ToNot(HaveOccurred())
Expect(content).To(Equal(expectedBlobContent))
})

})
3 changes: 1 addition & 2 deletions api/ocm/extensions/repositories/git/format.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package git

import (
"ocm.software/ocm/api/ocm/extensions/repositories/ctf"

"ocm.software/ocm/api/oci/extensions/repositories/git"
"ocm.software/ocm/api/ocm/cpi"
"ocm.software/ocm/api/ocm/extensions/repositories/ctf"
"ocm.software/ocm/api/ocm/extensions/repositories/genericocireg"
"ocm.software/ocm/api/utils/accessobj"
)
Expand Down
15 changes: 8 additions & 7 deletions api/ocm/extensions/repositories/git/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,26 @@ import (
"path/filepath"
"regexp"

. "github.com/mandelsoft/goutils/finalizer"
. "github.com/mandelsoft/goutils/testutils"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "ocm.software/ocm/api/ocm/testhelper"

"github.com/go-git/go-billy/v5"
gitgo "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/cache"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/transport/client"
"github.com/go-git/go-git/v5/plumbing/transport/server"
"github.com/go-git/go-git/v5/storage/filesystem"
. "github.com/mandelsoft/goutils/finalizer"
. "github.com/mandelsoft/goutils/testutils"
"github.com/mandelsoft/logging"
"github.com/mandelsoft/vfs/pkg/cwdfs"
"github.com/mandelsoft/vfs/pkg/osfs"
"github.com/mandelsoft/vfs/pkg/projectionfs"
"github.com/mandelsoft/vfs/pkg/vfs"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/tonglil/buflogr"

"ocm.software/ocm/api/oci/artdesc"
gitrepo "ocm.software/ocm/api/oci/extensions/repositories/git"
"ocm.software/ocm/api/ocm"
Expand All @@ -33,7 +36,6 @@ import (
"ocm.software/ocm/api/ocm/extensions/repositories/genericocireg"
"ocm.software/ocm/api/ocm/extensions/repositories/genericocireg/componentmapping"
"ocm.software/ocm/api/ocm/extensions/repositories/git"
. "ocm.software/ocm/api/ocm/testhelper"
techgit "ocm.software/ocm/api/tech/git"
"ocm.software/ocm/api/utils/accessio"
"ocm.software/ocm/api/utils/blobaccess"
Expand All @@ -49,7 +51,6 @@ const (
)

var _ = Describe("access method", func() {

// remoteFS contains the remote repository on the filesystem
// pathFS contains the local PWD
// repFS contains the local representation of the repository, meaning the cloned repo to work on the Repository
Expand All @@ -72,7 +73,7 @@ var _ = Describe("access method", func() {
basePath := GinkgoT().TempDir()
baseFS := Must(cwdfs.New(osfs.New(), basePath))
for _, dir := range []string{"remote", "path", "rep"} {
Expect(os.Mkdir(filepath.Join(basePath, dir), 0777)).To(Succeed())
Expect(os.Mkdir(filepath.Join(basePath, dir), 0o777)).To(Succeed())
}
remoteFS = Must(projectionfs.New(baseFS, "remote"))
pathFS = Must(projectionfs.New(baseFS, "path"))
Expand Down
6 changes: 3 additions & 3 deletions api/tech/git/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package git
import (
"errors"

"ocm.software/ocm/api/credentials"
"ocm.software/ocm/api/credentials/builtin/git/identity"

"github.com/go-git/go-git/v5/plumbing/transport"
"github.com/go-git/go-git/v5/plumbing/transport/http"
gssh "github.com/go-git/go-git/v5/plumbing/transport/ssh"

"ocm.software/ocm/api/credentials"
"ocm.software/ocm/api/credentials/builtin/git/identity"
)

var ErrNoValidGitCredentials = errors.New("no valid credentials found for git authentication")
Expand Down
Loading

0 comments on commit 23271da

Please sign in to comment.