-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathgit-s3-push.go
More file actions
239 lines (194 loc) · 5.02 KB
/
git-s3-push.go
File metadata and controls
239 lines (194 loc) · 5.02 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package s3push
import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"github.com/deckarep/golang-set"
"github.com/speedata/gogit"
)
const refS3Push string = "refs/heads/s3-pushed"
const prefixRefS3 string = "refs/heads/git-s3-push/"
const configFilePath string = ".git_s3_push"
// Repository represents a git-s3-push enabled git repository
type Repository struct {
GitRepo *gogit.Repository
HeadCommit *gogit.Commit
LastPushCommit *gogit.Commit
UnpushedFiles mapset.Set
Config repoConfig
IgnoreRegexes []*regexp.Regexp
s3Uploader S3Uploader
}
type repoConfig struct {
S3Region string
S3Bucket string
Public bool
Prefix string
Ignore []string
IncludeNonGit []string
}
// OpenRepository opens and initialises a 'git-s3-push' enabled git repository
func OpenRepository() (*Repository, error) {
repo := new(Repository)
repo.UnpushedFiles = mapset.NewSet()
wd, err := os.Getwd()
if err != nil {
return nil, err
}
path := filepath.Join(wd, ".git")
if _, err = os.Stat(path); os.IsNotExist(err) {
return nil, err
}
gitRepo, err := gogit.OpenRepository(path)
if err != nil {
return nil, err
}
repo.GitRepo = gitRepo
return repo, nil
}
// ReadConfigFile reads .git_s3_push configuration file from repo
func (repo *Repository) ReadConfigFile() error {
file, err := ioutil.ReadFile(configFilePath)
if err != nil {
return err
}
err = json.Unmarshal(file, &repo.Config)
if err != nil {
return err
}
err = repo.CompileIgnoreRegexes()
if err != nil {
return err
}
return nil
}
// CompileIgnoreRegexes compiles the regexes in the Ignore configuration directive
func (repo *Repository) CompileIgnoreRegexes() error {
for _, regexStr := range repo.Config.Ignore {
regexStr = strings.Replace(regexStr, "*", "(.*)", -1)
regex, err := regexp.Compile(regexStr)
if err != nil {
return err
}
repo.IgnoreRegexes = append(repo.IgnoreRegexes, regex)
}
return nil
}
// SaveConfigToFile marshals the current configuration to JSON and saves it to .git_s3_push
func (repo Repository) SaveConfigToFile() error {
jsonData, err := json.Marshal(repo.Config)
if err != nil {
return err
}
err = ioutil.WriteFile(configFilePath, jsonData, 0644)
if err != nil {
return err
}
return nil
}
// FindRelevantCommits calls git to find commits not pushed to S3
func (repo *Repository) FindRelevantCommits() error {
headRef, err := repo.GitRepo.LookupReference("HEAD")
if err != nil {
return err
}
headCommit, err := repo.GitRepo.LookupCommit(headRef.Target())
if err != nil {
return err
}
repo.HeadCommit = headCommit
lastPushRef, err := repo.GitRepo.LookupReference(repo.getRefName())
if err != nil {
return nil
}
lastPushCommit, err := repo.GitRepo.LookupCommit(lastPushRef.Target())
if err != nil {
return nil
}
repo.LastPushCommit = lastPushCommit
return nil
}
// ReadGitModifiedFiles reads the git output describe files modified since last S3 push
func (repo *Repository) ReadGitModifiedFiles(scanner *bufio.Scanner) {
for scanner.Scan() {
file := scanner.Text()
matched := false
for _, regex := range repo.IgnoreRegexes {
if regex.Match([]byte(file)) {
fmt.Println("Skipping file " + file + " matches ignore spec " + regex.String())
matched = true
break
}
}
if !matched {
repo.UnpushedFiles.Add(scanner.Text())
}
}
}
// FindCommitModifiedFiles finds files modified in given commit
func (repo *Repository) FindCommitModifiedFiles(commit *gogit.Commit) error {
cmd := exec.Command("git", "show", "--name-only", "--oneline", commit.Id().String())
out, err := cmd.StdoutPipe()
if err != nil {
return err
}
err = cmd.Start()
if err != nil {
return err
}
scanner := bufio.NewScanner(out)
repo.ReadGitModifiedFiles(scanner)
cmd.Wait()
return nil
}
// FindUnpushedModifiedFiles finds files that have been modified since last push to S3
func (repo *Repository) FindUnpushedModifiedFiles() error {
queue := []*gogit.Commit{}
visited := mapset.NewSet()
currentCommit := repo.HeadCommit
for currentCommit != nil {
if repo.LastPushCommit != nil && repo.LastPushCommit.Id().Equal(currentCommit.Id()) {
break
}
err := repo.FindCommitModifiedFiles(currentCommit)
if err != nil {
return err
}
for i := 0; i < currentCommit.ParentCount(); i++ {
parentCommit := currentCommit.Parent(i)
if !visited.Contains(parentCommit) {
queue = append(queue, parentCommit)
}
}
if len(queue) < 1 {
break
}
currentCommit = queue[0]
queue = queue[1:]
}
return nil
}
// UpdateGitLastPushRef sets the git-s3-push branch to the latest commit pushed
func (repo Repository) UpdateGitLastPushRef() error {
newLastPushRef := repo.HeadCommit.Id().String()
cmd := exec.Command("git", "update-ref", repo.getRefName(), newLastPushRef)
err := cmd.Start()
if err != nil {
return err
}
cmd.Wait()
return nil
}
func (repo *Repository) getRefName() string {
ref := refS3Push
if len(repo.Config.Prefix) > 0 {
ref = prefixRefS3 + repo.Config.Prefix
}
return ref
}