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 249b076 commit 441ffd1
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 18 deletions.
7 changes: 4 additions & 3 deletions api/oci/extensions/repositories/git/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,12 @@ func GenerateCommitMessageForArtifact(operation Operation, artifact cpi.Artifact
a := artifact.Artifact()

var typ string
if artifact.IsManifest() {
switch {
case artifact.IsManifest():
typ = "manifest"
} else if artifact.IsIndex() {
case artifact.IsIndex():
typ = "index"
} else {
default:
typ = "artifact"
}

Expand Down
5 changes: 0 additions & 5 deletions api/ocm/extensions/accessmethods/git/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package git
import (
"fmt"
"io"
"net/http"

"github.com/go-git/go-git/v5/plumbing"
"github.com/mandelsoft/goutils/errors"
Expand All @@ -15,7 +14,6 @@ import (
"ocm.software/ocm/api/ocm/cpi/accspeccpi"
"ocm.software/ocm/api/ocm/internal"
"ocm.software/ocm/api/utils/accessio"
"ocm.software/ocm/api/utils/accessio/downloader"
"ocm.software/ocm/api/utils/accessio/downloader/git"
"ocm.software/ocm/api/utils/accessobj"
"ocm.software/ocm/api/utils/blobaccess/blobaccess"
Expand Down Expand Up @@ -45,9 +43,6 @@ type AccessSpec struct {

// PathSpec is a path in the repository to download, can be a file or a regex matching multiple files
PathSpec string `json:"pathSpec"`

client *http.Client
downloader downloader.Downloader
}

// AccessSpecOptions defines a set of options which can be applied to the access spec.
Expand Down
2 changes: 1 addition & 1 deletion api/ocm/extensions/repositories/git/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ var _ = Describe("access method", func() {

BeforeEach(func() {
By("setting up local bare git repository to work against when pushing/updating")
billy := techgit.VFSBillyFS(remoteFS)
billy := Must(techgit.VFSBillyFS(remoteFS))
client.InstallProtocol("file", server.NewClient(server.NewFilesystemLoader(billy)))
remoteRepo = Must(newBareTestRepo(billy, REMOTE_REPO, gitgo.InitOptions{}))
// now that we have a bare repository, we can reference it via URL to access it like a remote repository
Expand Down
6 changes: 3 additions & 3 deletions api/tech/git/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ import (
"github.com/mandelsoft/vfs/pkg/vfs"
)

func VFSBillyFS(fsToWrap vfs.FileSystem) billy.Filesystem {
func VFSBillyFS(fsToWrap vfs.FileSystem) (billy.Filesystem, error) {
if fsToWrap == nil {
fsToWrap = vfs.New(memoryfs.New())
}
fi, err := fsToWrap.Stat(".")
if err != nil || !fi.IsDir() {
panic(fmt.Errorf("invalid vfs: %v", err))
return nil, fmt.Errorf("invalid vfs for billy conversion: %w", err)
}

return &fs{
vfs: fsToWrap,
}
}, nil
}

type fs struct {
Expand Down
14 changes: 11 additions & 3 deletions api/tech/git/ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import (
giturls "github.com/whilp/git-urls"
)

const urlToRefSeparator = "@"
const refToPathSeparator = "#"
const (
urlToRefSeparator = "@"
refToPathSeparator = "#"
)

// refRegexp is a regular expression that matches a git ref string.
// The ref string is expected to be in the format of:
Expand All @@ -20,7 +22,13 @@ const refToPathSeparator = "#"
// - 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.
var refRegexp = regexp.MustCompile(`^([^@#]+)(@[^#\n]+)?(#[^@\n]+)?`)
var refRegexp = regexp.MustCompile(
fmt.Sprintf(
`^([^%[1]s%[2]s]+)(%[1]s[^%[2]s\n]+)?(%[2]s[^%[1]s\n]+)?`,
urlToRefSeparator,
refToPathSeparator,
),
)

// gurl represents a git URL reference.
// It contains the URL of the git repository,
Expand Down
5 changes: 4 additions & 1 deletion api/tech/git/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ func (c *client) Repository(ctx context.Context) (*git.Repository, error) {
return c.repo, nil
}

billyFS := VFSBillyFS(c.vfs)
billyFS, err := VFSBillyFS(c.vfs)
if err != nil {
return nil, err
}

strg, err := GetStorage(billyFS)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions api/utils/accessio/downloader/git/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ type CloseableDownloader interface {
// Downloader simply uses the default HTTP client to download the contents of a URL.
type Downloader struct {
cloneOpts *git.CloneOptions
grepOpts *git.GrepOptions

matching *regexp.Regexp

Expand All @@ -52,7 +51,7 @@ func NewDownloader(url string, ref string, path string, auth techgit.AuthMethod)
Tags: git.NoTags,
Depth: 0,
},
matching: regexp.MustCompile(fmt.Sprintf(`%s`, path)),
matching: regexp.MustCompile(path),
buf: bytes.NewBuffer(make([]byte, 0, 4096)),
storage: memory.NewStorage(),
}
Expand Down

0 comments on commit 441ffd1

Please sign in to comment.