aboutsummaryrefslogtreecommitdiffstats
path: root/repository.go
diff options
context:
space:
mode:
authorPaulo Gomes <pjbgf@linux.com>2023-03-07 23:16:17 +0000
committerPaulo Gomes <pjbgf@linux.com>2023-03-08 00:04:25 +0000
commit99e2f85843878671b028d4d01bd4668676226dd1 (patch)
treee7b8a2214ac29d9ee6807c48001f493bc158dd88 /repository.go
parent02494219682689ccfae6d4ffed2734509eed02d0 (diff)
downloadgo-git-99e2f85843878671b028d4d01bd4668676226dd1.tar.gz
config: Add Repository Format Extension
Relates to the SHA256 implementation, defined in #706. Signed-off-by: Paulo Gomes <pjbgf@linux.com>
Diffstat (limited to 'repository.go')
-rw-r--r--repository.go37
1 files changed, 37 insertions, 0 deletions
diff --git a/repository.go b/repository.go
index 2a06f8b..56ae976 100644
--- a/repository.go
+++ b/repository.go
@@ -3,6 +3,7 @@ package git
import (
"bytes"
"context"
+ "crypto"
"encoding/hex"
"errors"
"fmt"
@@ -21,7 +22,9 @@ import (
"github.com/go-git/go-git/v5/internal/revision"
"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"
"github.com/go-git/go-git/v5/plumbing/format/packfile"
+ "github.com/go-git/go-git/v5/plumbing/hash"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/storer"
"github.com/go-git/go-git/v5/storage"
@@ -57,6 +60,7 @@ var (
ErrIsBareRepository = errors.New("worktree not available in a bare repository")
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")
)
// Repository represents a git repository
@@ -228,6 +232,39 @@ func PlainInit(path string, isBare bool) (*Repository, error) {
return Init(s, wt)
}
+func PlainInitWithOptions(path string, opts *PlainInitOptions) (*Repository, error) {
+ wt := osfs.New(path)
+ dot, _ := wt.Chroot(GitDirName)
+
+ s := filesystem.NewStorage(dot, cache.NewObjectLRUDefault())
+
+ r, err := Init(s, wt)
+ if err != nil {
+ return nil, err
+ }
+
+ cfg, err := r.Config()
+ if err != nil {
+ return nil, err
+ }
+
+ if opts != nil {
+ if opts.ObjectFormat == formatcfg.SHA256 && hash.CryptoType != crypto.SHA256 {
+ return nil, ErrSHA256NotSupported
+ }
+
+ cfg.Core.RepositoryFormatVersion = formatcfg.Version_1
+ cfg.Extensions.ObjectFormat = opts.ObjectFormat
+ }
+
+ err = r.Storer.SetConfig(cfg)
+ if err != nil {
+ return nil, err
+ }
+
+ return r, err
+}
+
// PlainOpen opens a git repository from the given path. It detects if the
// repository is bare or a normal one. If the path doesn't contain a valid
// repository ErrRepositoryNotExists is returned