/
gitpack.go
106 lines (86 loc) · 2.07 KB
/
gitpack.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
package pack
import (
"fmt"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing/object"
"os"
"path"
)
type GitPack struct {
RepoPath string
ContentDir string
Packs []*Pack
}
func GetPackFromGit(contentdir string, repopath string) (*GitPack, error) {
// Attempt to pull the content pack from GIT
gp := GitPack{
RepoPath: repopath,
ContentDir: contentdir,
}
err := gp.Clone()
if err != nil {
return nil, err
}
return &gp, nil
}
func (gp *GitPack) Clone() error {
fmt.Printf("%v %v", gp.ContentDir, gp.RepoPath)
_, err := git.PlainClone(gp.ContentDir, false, &git.CloneOptions{
URL: gp.RepoPath,
Progress: os.Stdout,
})
if err != nil {
return fmt.Errorf("Failed to clone %v (%v)", gp.RepoPath, err)
}
return nil
}
func (gp *GitPack) Update(pn string) (*string, error) {
repo, err := git.PlainOpen(path.Join(gp.ContentDir, pn))
if err != nil {
return nil, fmt.Errorf("Failed to open repository for udpate.")
}
w, err := repo.Worktree()
if err != nil {
return nil, fmt.Errorf("Couldn't checkout a worktree.")
}
err = w.Pull(&git.PullOptions{RemoteName: "origin"})
if err != nil {
return nil, err
}
ref, _ := repo.Head()
hs := ref.Hash().String()
return &hs, nil
}
func (gp *GitPack) Status(pn string) (git.Status, error) {
repo, err := git.PlainOpen(path.Join(gp.ContentDir, pn))
if err != nil {
return nil, fmt.Errorf("Failed to open repository for udpate.")
}
w, err := repo.Worktree()
if err != nil {
return nil, fmt.Errorf("Couldn't checkout a worktree.")
}
s, err := w.Status()
return s, err
}
func (gp *GitPack) Save(pn string, commitmsg string, author *object.Signature) error {
repo, err := git.PlainOpen(path.Join(gp.ContentDir, pn))
if err != nil {
return fmt.Errorf("Failed to open repository for udpate.")
}
w, err := repo.Worktree()
if err != nil {
return fmt.Errorf("Couldn't checkout a worktree.")
}
s, err := w.Status()
for fp, _ := range s {
_, err := w.Add(fp)
if err != nil {
return err
}
}
_, err = w.Commit(commitmsg, &git.CommitOptions{
Author: author,
})
return err
}