aboutsummaryrefslogtreecommitdiffstats
path: root/repository.go
diff options
context:
space:
mode:
Diffstat (limited to 'repository.go')
-rw-r--r--repository.go26
1 files changed, 26 insertions, 0 deletions
diff --git a/repository.go b/repository.go
index 013b53f..4898838 100644
--- a/repository.go
+++ b/repository.go
@@ -22,6 +22,7 @@ import (
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/internal/path_util"
"github.com/go-git/go-git/v5/internal/revision"
+ "github.com/go-git/go-git/v5/internal/url"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/cache"
formatcfg "github.com/go-git/go-git/v5/plumbing/format/config"
@@ -62,6 +63,7 @@ var (
ErrUnableToResolveCommit = errors.New("unable to resolve commit")
ErrPackedObjectsNotSupported = errors.New("packed objects not supported")
ErrSHA256NotSupported = errors.New("go-git was not compiled with SHA256 support")
+ ErrAlternatePathNotSupported = errors.New("alternate path must use the file scheme")
)
// Repository represents a git repository
@@ -887,6 +889,30 @@ func (r *Repository) clone(ctx context.Context, o *CloneOptions) error {
return err
}
+ // When the repository to clone is on the local machine,
+ // instead of using hard links, automatically setup .git/objects/info/alternates
+ // to share the objects with the source repository
+ if o.Shared {
+ if !url.IsLocalEndpoint(o.URL) {
+ return ErrAlternatePathNotSupported
+ }
+ altpath := o.URL
+ remoteRepo, err := PlainOpen(o.URL)
+ if err != nil {
+ return fmt.Errorf("failed to open remote repository: %w", err)
+ }
+ conf, err := remoteRepo.Config()
+ if err != nil {
+ return fmt.Errorf("failed to read remote repository configuration: %w", err)
+ }
+ if !conf.Core.IsBare {
+ altpath = path.Join(altpath, GitDirName)
+ }
+ if err := r.Storer.AddAlternate(altpath); err != nil {
+ return fmt.Errorf("failed to add alternate file to git objects dir: %w", err)
+ }
+ }
+
ref, err := r.fetchAndUpdateReferences(ctx, &FetchOptions{
RefSpecs: c.Fetch,
Depth: o.Depth,