Skip to content

Commit 1feac1b

Browse files
authored
Merge pull request go-git#764 from techknowlogick/init-options
git: Allow Initial Branch to be configurable
2 parents 90bfbf2 + 1aa8e89 commit 1feac1b

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

plumbing/reference.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ func (r ReferenceName) Short() string {
126126
const (
127127
HEAD ReferenceName = "HEAD"
128128
Master ReferenceName = "refs/heads/master"
129+
Main ReferenceName = "refs/heads/main"
129130
)
130131

131132
// Reference is a representation of git reference

repository.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,30 @@ type Repository struct {
7171
wt billy.Filesystem
7272
}
7373

74+
type InitOptions struct {
75+
// The default branch (e.g. "refs/heads/master")
76+
DefaultBranch plumbing.ReferenceName
77+
}
78+
7479
// Init creates an empty git repository, based on the given Storer and worktree.
7580
// The worktree Filesystem is optional, if nil a bare repository is created. If
7681
// the given storer is not empty ErrRepositoryAlreadyExists is returned
7782
func Init(s storage.Storer, worktree billy.Filesystem) (*Repository, error) {
83+
options := InitOptions{
84+
DefaultBranch: plumbing.Master,
85+
}
86+
return InitWithOptions(s, worktree, options)
87+
}
88+
89+
func InitWithOptions(s storage.Storer, worktree billy.Filesystem, options InitOptions) (*Repository, error) {
7890
if err := initStorer(s); err != nil {
7991
return nil, err
8092
}
8193

94+
if options.DefaultBranch == "" {
95+
options.DefaultBranch = plumbing.Master
96+
}
97+
8298
r := newRepository(s, worktree)
8399
_, err := r.Reference(plumbing.HEAD, false)
84100
switch err {
@@ -89,7 +105,7 @@ func Init(s storage.Storer, worktree billy.Filesystem) (*Repository, error) {
89105
return nil, err
90106
}
91107

92-
h := plumbing.NewSymbolicReference(plumbing.HEAD, plumbing.Master)
108+
h := plumbing.NewSymbolicReference(plumbing.HEAD, options.DefaultBranch)
93109
if err := s.SetReference(h); err != nil {
94110
return nil, err
95111
}

repository_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,54 @@ func (s *RepositorySuite) TestInit(c *C) {
5151
cfg, err := r.Config()
5252
c.Assert(err, IsNil)
5353
c.Assert(cfg.Core.IsBare, Equals, false)
54+
55+
// check the HEAD to see what the default branch is
56+
createCommit(c, r)
57+
ref, err := r.Head()
58+
c.Assert(err, IsNil)
59+
c.Assert(ref.Name().String(), Equals, plumbing.Master.String())
60+
}
61+
62+
func (s *RepositorySuite) TestInitWithOptions(c *C) {
63+
r, err := InitWithOptions(memory.NewStorage(), memfs.New(), InitOptions{
64+
DefaultBranch: "refs/heads/foo",
65+
})
66+
c.Assert(err, IsNil)
67+
c.Assert(r, NotNil)
68+
createCommit(c, r)
69+
70+
ref, err := r.Head()
71+
c.Assert(err, IsNil)
72+
c.Assert(ref.Name().String(), Equals, "refs/heads/foo")
73+
74+
}
75+
76+
func createCommit(c *C, r *Repository) {
77+
// Create a commit so there is a HEAD to check
78+
wt, err := r.Worktree()
79+
c.Assert(err, IsNil)
80+
81+
rm, err := wt.Filesystem.Create("foo.txt")
82+
c.Assert(err, IsNil)
83+
84+
_, err = rm.Write([]byte("foo text"))
85+
c.Assert(err, IsNil)
86+
87+
_, err = wt.Add("foo.txt")
88+
c.Assert(err, IsNil)
89+
90+
author := object.Signature{
91+
Name: "go-git",
92+
Email: "go-git@fake.local",
93+
When: time.Now(),
94+
}
95+
_, err = wt.Commit("test commit message", &CommitOptions{
96+
All: true,
97+
Author: &author,
98+
Committer: &author,
99+
})
100+
c.Assert(err, IsNil)
101+
54102
}
55103

56104
func (s *RepositorySuite) TestInitNonStandardDotGit(c *C) {

0 commit comments

Comments
 (0)