42 lines
946 B
1
package git
2
3
import (
4
"errors"
5
"fmt"
6
"os"
7
"path/filepath"
8
9
gogit "github.com/go-git/go-git/v5"
10
"github.com/go-git/go-git/v5/config"
11
"github.com/go-git/go-git/v5/plumbing"
12
)
13
14
func InitBare(path, defaultBranch string) error {
15
parent := filepath.Dir(path)
16
17
if err := os.MkdirAll(parent, 0755); errors.Is(err, os.ErrExist) {
18
return fmt.Errorf("error creating user directory: %w", err)
19
}
20
21
repository, err := gogit.PlainInit(path, true)
22
if err != nil {
23
return err
24
}
25
26
// set up default branch
27
err = repository.CreateBranch(&config.Branch{
28
Name: defaultBranch,
29
})
30
if err != nil {
31
return fmt.Errorf("creating branch: %w", err)
32
}
33
34
defaultReference := plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", defaultBranch))
35
36
ref := plumbing.NewSymbolicReference(plumbing.HEAD, defaultReference)
37
if err = repository.Storer.SetReference(ref); err != nil {
38
return fmt.Errorf("creating symbolic reference: %w", err)
39
}
40
41
return nil
42
}
43