Skip to content

Commit

Permalink
Merge pull request #764 from techknowlogick/init-options
Browse files Browse the repository at this point in the history
git: Allow Initial Branch to be configurable
  • Loading branch information
pjbgf committed May 23, 2023
2 parents 90bfbf2 + 1aa8e89 commit 1feac1b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
1 change: 1 addition & 0 deletions plumbing/reference.go
Expand Up @@ -126,6 +126,7 @@ func (r ReferenceName) Short() string {
const (
HEAD ReferenceName = "HEAD"
Master ReferenceName = "refs/heads/master"
Main ReferenceName = "refs/heads/main"
)

// Reference is a representation of git reference
Expand Down
18 changes: 17 additions & 1 deletion repository.go
Expand Up @@ -71,14 +71,30 @@ type Repository struct {
wt billy.Filesystem
}

type InitOptions struct {
// The default branch (e.g. "refs/heads/master")
DefaultBranch plumbing.ReferenceName
}

// Init creates an empty git repository, based on the given Storer and worktree.
// The worktree Filesystem is optional, if nil a bare repository is created. If
// the given storer is not empty ErrRepositoryAlreadyExists is returned
func Init(s storage.Storer, worktree billy.Filesystem) (*Repository, error) {
options := InitOptions{
DefaultBranch: plumbing.Master,
}
return InitWithOptions(s, worktree, options)
}

func InitWithOptions(s storage.Storer, worktree billy.Filesystem, options InitOptions) (*Repository, error) {
if err := initStorer(s); err != nil {
return nil, err
}

if options.DefaultBranch == "" {
options.DefaultBranch = plumbing.Master
}

r := newRepository(s, worktree)
_, err := r.Reference(plumbing.HEAD, false)
switch err {
Expand All @@ -89,7 +105,7 @@ func Init(s storage.Storer, worktree billy.Filesystem) (*Repository, error) {
return nil, err
}

h := plumbing.NewSymbolicReference(plumbing.HEAD, plumbing.Master)
h := plumbing.NewSymbolicReference(plumbing.HEAD, options.DefaultBranch)
if err := s.SetReference(h); err != nil {
return nil, err
}
Expand Down
48 changes: 48 additions & 0 deletions repository_test.go
Expand Up @@ -51,6 +51,54 @@ func (s *RepositorySuite) TestInit(c *C) {
cfg, err := r.Config()
c.Assert(err, IsNil)
c.Assert(cfg.Core.IsBare, Equals, false)

// check the HEAD to see what the default branch is
createCommit(c, r)
ref, err := r.Head()
c.Assert(err, IsNil)
c.Assert(ref.Name().String(), Equals, plumbing.Master.String())
}

func (s *RepositorySuite) TestInitWithOptions(c *C) {
r, err := InitWithOptions(memory.NewStorage(), memfs.New(), InitOptions{
DefaultBranch: "refs/heads/foo",
})
c.Assert(err, IsNil)
c.Assert(r, NotNil)
createCommit(c, r)

ref, err := r.Head()
c.Assert(err, IsNil)
c.Assert(ref.Name().String(), Equals, "refs/heads/foo")

}

func createCommit(c *C, r *Repository) {
// Create a commit so there is a HEAD to check
wt, err := r.Worktree()
c.Assert(err, IsNil)

rm, err := wt.Filesystem.Create("foo.txt")
c.Assert(err, IsNil)

_, err = rm.Write([]byte("foo text"))
c.Assert(err, IsNil)

_, err = wt.Add("foo.txt")
c.Assert(err, IsNil)

author := object.Signature{
Name: "go-git",
Email: "go-git@fake.local",
When: time.Now(),
}
_, err = wt.Commit("test commit message", &CommitOptions{
All: true,
Author: &author,
Committer: &author,
})
c.Assert(err, IsNil)

}

func (s *RepositorySuite) TestInitNonStandardDotGit(c *C) {
Expand Down

0 comments on commit 1feac1b

Please sign in to comment.