Skip to content

Commit

Permalink
Merge pull request #782 from ThinkChaos/feat/plain-init-branch
Browse files Browse the repository at this point in the history
git: add PlainInitOptions.Bare and allow using InitOptions with PlainInitWithOptions
  • Loading branch information
pjbgf committed Sep 15, 2023
2 parents e24e0f7 + 644929a commit c208b4d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
3 changes: 3 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,9 @@ 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
}

Expand Down
25 changes: 13 additions & 12 deletions repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -246,16 +256,7 @@ 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)
r, err := InitWithOptions(s, wt, opts.InitOptions)
if err != nil {
return nil, err
}
Expand All @@ -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
}
Expand Down
24 changes: 24 additions & 0 deletions repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,30 @@ 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{
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, 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) {
dir, clean := s.TemporalDir()
defer clean()
Expand Down

0 comments on commit c208b4d

Please sign in to comment.