-
Notifications
You must be signed in to change notification settings - Fork 1
/
mirror.go
213 lines (177 loc) · 4.41 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package main
import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/reconquest/executil-go"
)
const (
hashZero = "0000000000000000000000000000000000000000"
)
// Mirror of remote git repository.
type Mirror struct {
// Name is unique identifier.
Name string
// URL will be passed to git for cloning and fetching updates.
URL string
// Dir is relative path to storage dir where mirror will be stored
Dir string
}
// String returns a string representation of mirror (name + url)
func (mirror *Mirror) String() string {
return mirror.Name + " (" + mirror.URL + ")"
}
// CreateMirror creates new git mirror repository in specified storage, and
// clones remote data using given url
func CreateMirror(
storageDir string, name string, url string,
) (Mirror, error) {
mirrorDir := filepath.Join(storageDir, name)
_, err := os.Stat(mirrorDir)
if err == nil {
return Mirror{}, fmt.Errorf(
"directory %s already exists",
mirrorDir,
)
} else if !os.IsNotExist(err) {
return Mirror{}, err
}
err = os.MkdirAll(mirrorDir, 0770)
if err != nil {
return Mirror{}, err
}
mirror := Mirror{
Name: name,
URL: url,
Dir: mirrorDir,
}
err = mirror.Clone(url)
if err != nil {
cleanupError := os.RemoveAll(mirrorDir)
if cleanupError != nil {
logger.Errorf(
"can't remove mirror directory after failed clone: %s",
mirrorDir,
)
}
return Mirror{}, err
}
return mirror, err
}
// GetMirror looks for the mirror with specified name in specified mirror
// storage directory.
func GetMirror(
storageDir string, name string,
) (Mirror, error) {
mirrorDir := filepath.Join(storageDir, name)
_, err := os.Stat(mirrorDir)
if err != nil {
return Mirror{}, err
}
mirror := Mirror{
Name: name,
Dir: mirrorDir,
}
url, err := mirror.GetURL()
if err != nil {
return mirror, err
}
mirror.URL = url
return mirror, nil
}
// Archive of specified git treeish will be written to given stdout writer.
func (mirror *Mirror) Archive(
stdout io.Writer, treeish string,
) error {
cmd := exec.Command("git", "archive", treeish)
cmd.Dir = mirror.Dir
cmd.Stdout = stdout
_, _, err := executil.Run(cmd, executil.IgnoreStdout)
return err
}
// Fetch remote changeset using git remote update.
func (mirror *Mirror) Fetch() error {
cmd := exec.Command("git", "remote", "update")
cmd.Dir = mirror.Dir
_, _, err := executil.Run(cmd)
return err
}
// SpoofChangeset forcely sets branch label on specified "deattached" tag and
// removes that tag from mirror repository.
func (mirror *Mirror) SpoofChangeset(branch, tag string) error {
if tag == hashZero {
cmd := exec.Command("git", "branch", "--delete", "--force", branch)
cmd.Dir = mirror.Dir
_, _, err := executil.Run(cmd)
if err != nil {
return err
}
return nil
}
cmd := exec.Command("git", "branch", "--force", branch, tag)
cmd.Dir = mirror.Dir
_, _, err := executil.Run(cmd)
if err != nil {
return err
}
return mirror.removeTag(tag)
}
func (mirror *Mirror) removeTag(tag string) error {
cmd := exec.Command("git", "tag", "-d", tag)
cmd.Dir = mirror.Dir
_, _, err := executil.Run(cmd)
return err
}
// Clone remote repository to directory of given mirror.
func (mirror *Mirror) Clone(url string) error {
cmd := exec.Command("git", "clone", "--recursive", "--mirror", url, ".")
cmd.Dir = mirror.Dir
_, _, err := executil.Run(cmd)
return err
}
// GetURL from git configuration which mirror uses for fetching changesets.
func (mirror *Mirror) GetURL() (string, error) {
cmd := exec.Command("git", "config", "--get", "remote.origin.url")
cmd.Dir = mirror.Dir
stdout, _, err := executil.Run(cmd)
return strings.TrimSpace(string(stdout)), err
}
// GetModifyDateUnix calls GetModifyDate and returns unix timestamp
// representation.
func (mirror *Mirror) GetModifyDateUnix() (int64, error) {
modDate, err := mirror.GetModifyDate()
if err != nil {
return 0, err
}
return modDate.Unix(), nil
}
// GetModifyDate returns last modify date of given mirror repository.
func (mirror *Mirror) GetModifyDate() (time.Time, error) {
dirs := []string{
"refs/heads",
"refs/tags",
}
var (
modDate time.Time
err error
fileinfo os.FileInfo
)
for _, dir := range dirs {
fileinfo, err = os.Stat(mirror.Dir + "/" + dir)
if err != nil {
if os.IsNotExist(err) {
continue
}
break
}
newModDate := fileinfo.ModTime()
if newModDate.Unix() > modDate.Unix() {
modDate = newModDate
}
}
return modDate, err
}