-
Notifications
You must be signed in to change notification settings - Fork 0
/
mirror.go
64 lines (55 loc) · 1.14 KB
/
mirror.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
56
57
58
59
60
61
62
63
64
package main
import (
"errors"
"net/url"
"os"
"github.com/libgit2/git2go"
)
type Mirror struct {
path string
remote url.URL
credentialsCallback git.CredentialsCallback
}
func NewMirror(path string, remote url.URL, credentialsCallback git.CredentialsCallback) *Mirror {
return &Mirror{
path,
remote,
credentialsCallback,
}
}
func (b *Mirror) Fetch() error {
if b == nil {
return nil
}
// check whether the backup already exists
if stat, err := os.Stat(b.path); os.IsNotExist(err) {
err = os.MkdirAll(b.path, 0777)
if err != nil {
return errors.New("could not create " + b.path)
}
opt := &git.CloneOptions{
RemoteCallbacks: &git.RemoteCallbacks{
CredentialsCallback: b.credentialsCallback,
},
Bare: true,
}
_, err := git.Clone(b.remote.String(), b.path, opt)
if err != nil {
return err
}
} else {
if !stat.IsDir() {
return errors.New(b.path + " exists, but is a file")
}
}
repo, err := git.OpenRepository(b.path)
if err != nil {
return err
}
remote, err := repo.LoadRemote("origin")
if err != nil {
return err
}
err = remote.Fetch(nil, "")
return err
}