-
Notifications
You must be signed in to change notification settings - Fork 31
/
clone.go
144 lines (127 loc) · 3.1 KB
/
clone.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package pkg
import (
"io"
"os"
"path"
"github.com/octavore/nagax/util/errors"
git "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/object"
"github.com/ketchuphq/ketchup/proto/ketchup/packages"
)
// Clone the given package to {data_dir}/{dataSubdir}/{package name}
func (m *Module) Clone(p *packages.Package, dataSubdir string) error {
packagePath := m.Config.DataPath(path.Join(dataSubdir, p.GetName()), "")
return CloneToDir(packagePath, p.GetVcsUrl())
}
func getRepoMasterIterator(repo *git.Repository) (*object.FileIter, error) {
ref, err := repo.Reference("refs/remotes/origin/master", true)
if err != nil {
return nil, errors.Wrap(err)
}
commit, err := repo.CommitObject(ref.Hash())
if err != nil {
return nil, errors.Wrap(err)
}
iter, err := commit.Files()
if err != nil {
return nil, errors.Wrap(err)
}
// defer iter.Close()
return iter, nil
}
type setDatar interface {
SetData(v *string)
}
// Clone the given url to the given dest folder
func CloneToDir(dest, url string) error {
r, err := git.PlainClone(dest, false, &git.CloneOptions{URL: url})
if err != nil {
return errors.Wrap(err)
}
iter, err := getRepoMasterIterator(r)
if err != nil {
return errors.Wrap(err)
}
defer iter.Close()
return iter.ForEach(func(f *object.File) error {
pth := path.Join(dest, f.Name)
err := os.MkdirAll(path.Dir(pth), 0700)
if err != nil {
return errors.Wrap(err)
}
mode, err := f.Mode.ToOSFileMode()
if err != nil {
return errors.Wrap(err)
}
g, err := os.OpenFile(pth, os.O_CREATE|os.O_RDWR|os.O_TRUNC, mode)
if err != nil {
return errors.Wrap(err)
}
defer g.Close()
rdr, err := f.Reader()
if err != nil {
return errors.Wrap(err)
}
defer rdr.Close()
_, err = io.Copy(g, rdr)
return errors.Wrap(err)
})
}
// Fetch does a git fetch to the given ref
func FetchDir(dir, ref string) error {
r, err := git.PlainOpen(dir)
if err != nil {
return errors.Wrap(err)
}
// ensure worktree is clean
wt, err := r.Worktree()
if err != nil {
return errors.Wrap(err)
}
// todo: need support for gitignore
status, err := wt.Status()
if err != nil {
return errors.Wrap(err)
}
if !status.IsClean() {
return errors.New("%s is not clean; refusing to update. %s", dir)
}
// fetch
err = r.Fetch(&git.FetchOptions{RemoteName: "origin"})
if err != nil && err != git.NoErrAlreadyUpToDate {
return errors.Wrap(err)
}
// get current head
head, err := r.Head()
if err != nil {
return errors.Wrap(err)
}
headHash := head.Hash().String
// get commit for the desired ref
remoteHash := plumbing.NewHash(ref)
// check that head is an ancestor of remoteRef
remoteCommit, err := r.CommitObject(remoteHash)
if err != nil {
return errors.Wrap(err)
}
iter := remoteCommit.Parents()
found := false
for {
commit, err := iter.Next()
if err == io.EOF {
break
}
if commit.Hash.String() == headHash() {
found = true
break
}
}
if !found {
return errors.New("%s is not a descendant of the current head", ref)
}
err = wt.Checkout(&git.CheckoutOptions{
Hash: remoteCommit.Hash,
})
return errors.Wrap(err)
}