-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository.go
55 lines (44 loc) · 1.09 KB
/
repository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package gitdoc
import (
"errors"
"io"
"os"
git "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing/object"
)
type Repository struct {
gitRepo *git.Repository
gitRepoPath string
gitSignature *object.Signature
}
type InitOptions struct {
Path string
// Author** fields used as signature for commit.
AuthorName string // deafult value: "gitdoc"
AuthorEmail string // default value: "-"
}
var (
ErrInitNotEmpty = errors.New("path to initialized not empty")
ErrInitFailed = errors.New("failed to initialize repository")
)
func Init(opt *InitOptions) (*Repository, error) {
os.MkdirAll(opt.Path, 0744)
f, _ := os.Open(opt.Path)
if _, err := f.Readdirnames(1); err != io.EOF {
return nil, ErrInitNotEmpty
}
// Make new directories.
os.Mkdir(opt.Path+"/docs", 0744)
r, err := git.PlainInit(opt.Path, false)
if err != nil {
return nil, ErrInitFailed
}
return &Repository{
gitRepo: r,
gitRepoPath: opt.Path,
gitSignature: &object.Signature{
Name: stringOrDefault(opt.AuthorName, "gitdoc"),
Email: stringOrDefault(opt.AuthorEmail, "-"),
},
}, nil
}