From 1ad7d8dd024abca82779e2dfdff69d9712161ab8 Mon Sep 17 00:00:00 2001 From: ThinkChaos Date: Wed, 31 May 2023 11:58:44 -0400 Subject: git: add `PlainInitOptions.Bare` Refactor `PlainInit` to call `PlainInitWithOptions` --- options.go | 2 ++ repository.go | 23 ++++++++++++----------- repository_test.go | 15 +++++++++++++++ 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/options.go b/options.go index 757bdc8..00d9b97 100644 --- a/options.go +++ b/options.go @@ -737,6 +737,8 @@ type PlainOpenOptions struct { func (o *PlainOpenOptions) Validate() error { return nil } type PlainInitOptions struct { + // Determines if the repository will have a worktree (non-bare) or not (bare). + Bare bool ObjectFormat formatcfg.ObjectFormat } diff --git a/repository.go b/repository.go index 3154ac0..e94fc48 100644 --- a/repository.go +++ b/repository.go @@ -235,9 +235,19 @@ func CloneContext( // if the repository will have worktree (non-bare) or not (bare), if the path // is not empty ErrRepositoryAlreadyExists is returned. func PlainInit(path string, isBare bool) (*Repository, error) { + return PlainInitWithOptions(path, &PlainInitOptions{ + Bare: isBare, + }) +} + +func PlainInitWithOptions(path string, opts *PlainInitOptions) (*Repository, error) { + if opts == nil { + opts = &PlainInitOptions{} + } + var wt, dot billy.Filesystem - if isBare { + if opts.Bare { dot = osfs.New(path) } else { wt = osfs.New(path) @@ -246,15 +256,6 @@ func PlainInit(path string, isBare bool) (*Repository, error) { s := filesystem.NewStorage(dot, cache.NewObjectLRUDefault()) - 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 @@ -265,7 +266,7 @@ func PlainInitWithOptions(path string, opts *PlainInitOptions) (*Repository, err return nil, err } - if opts != nil { + if opts.ObjectFormat != "" { if opts.ObjectFormat == formatcfg.SHA256 && hash.CryptoType != crypto.SHA256 { return nil, ErrSHA256NotSupported } diff --git a/repository_test.go b/repository_test.go index 9e000a3..bc6b188 100644 --- a/repository_test.go +++ b/repository_test.go @@ -518,6 +518,21 @@ func (s *RepositorySuite) TestPlainInit(c *C) { c.Assert(cfg.Core.IsBare, Equals, true) } +func (s *RepositorySuite) TestPlainInitWithOptions(c *C) { + dir, clean := s.TemporalDir() + defer clean() + + r, err := PlainInitWithOptions(dir, &PlainInitOptions{ + Bare: true, + }) + c.Assert(err, IsNil) + c.Assert(r, NotNil) + + cfg, err := r.Config() + c.Assert(err, IsNil) + c.Assert(cfg.Core.IsBare, Equals, true) +} + func (s *RepositorySuite) TestPlainInitAlreadyExists(c *C) { dir, clean := s.TemporalDir() defer clean() -- cgit From 644929ade3ac7c07c370be8065fa2ac6faf081be Mon Sep 17 00:00:00 2001 From: ThinkChaos Date: Wed, 31 May 2023 11:54:26 -0400 Subject: git: allow using `InitOptions` with `PlainInitWithOptions` --- options.go | 1 + repository.go | 2 +- repository_test.go | 13 +++++++++++-- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/options.go b/options.go index 00d9b97..c68bf2b 100644 --- a/options.go +++ b/options.go @@ -737,6 +737,7 @@ type PlainOpenOptions struct { func (o *PlainOpenOptions) Validate() error { return nil } type PlainInitOptions struct { + InitOptions // Determines if the repository will have a worktree (non-bare) or not (bare). Bare bool ObjectFormat formatcfg.ObjectFormat diff --git a/repository.go b/repository.go index e94fc48..013b53f 100644 --- a/repository.go +++ b/repository.go @@ -256,7 +256,7 @@ func PlainInitWithOptions(path string, opts *PlainInitOptions) (*Repository, err s := filesystem.NewStorage(dot, cache.NewObjectLRUDefault()) - r, err := Init(s, wt) + r, err := InitWithOptions(s, wt, opts.InitOptions) if err != nil { return nil, err } diff --git a/repository_test.go b/repository_test.go index bc6b188..3154f1d 100644 --- a/repository_test.go +++ b/repository_test.go @@ -523,14 +523,23 @@ func (s *RepositorySuite) TestPlainInitWithOptions(c *C) { defer clean() r, err := PlainInitWithOptions(dir, &PlainInitOptions{ - Bare: true, + InitOptions: InitOptions{ + DefaultBranch: "refs/heads/foo", + }, + Bare: false, }) c.Assert(err, IsNil) c.Assert(r, NotNil) cfg, err := r.Config() c.Assert(err, IsNil) - c.Assert(cfg.Core.IsBare, Equals, true) + c.Assert(cfg.Core.IsBare, Equals, false) + + createCommit(c, r) + + ref, err := r.Head() + c.Assert(err, IsNil) + c.Assert(ref.Name().String(), Equals, "refs/heads/foo") } func (s *RepositorySuite) TestPlainInitAlreadyExists(c *C) { -- cgit