Skip to content

Commit 1aa8e89

Browse files
git: Allow Initial Branch to be configurable
1 parent 1dbd729 commit 1aa8e89

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

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

0 commit comments

Comments
 (0)