-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcommit.go
More file actions
239 lines (203 loc) · 6.02 KB
/
commit.go
File metadata and controls
239 lines (203 loc) · 6.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
/*
* Copyright 2016 Frank Wessels <fwessels@xs4all.nl>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package s3git
import (
"encoding/hex"
"errors"
"github.com/s3git/s3git-go/internal/core"
"github.com/s3git/s3git-go/internal/kv"
"time"
)
type Commit struct {
Hash string
Message string
TimeStamp string
Parent string
IsSnapshot bool
}
// Perform a commit for the repository
func (repo Repository) Commit(message string) (hash string, empty bool, err error) {
return repo.commit(message, "master", "", []string{})
}
// Perform a commit for the named branch of the repository
func (repo Repository) CommitToBranch(message, branch string) (hash string, empty bool, err error) {
return repo.commit(message, branch, "", []string{})
}
func (repo Repository) commit(message, branch, snapshot string, parents []string) (hash string, empty bool, err error) {
warmParents := []string{}
coldParents := []string{}
commits, err := kv.ListTopMostCommits()
if err != nil {
return "", false, err
}
if len(parents) == 0 {
for c := range commits {
warmParents = append(warmParents, hex.EncodeToString(c))
}
if len(warmParents) > 1 {
// TODO: Do extra check whether the trees are the same, in that case we can safely ignore the warning
// TODO: Consider doing Merkle tree kind of check on list of blobs
return "", false, errors.New("Multiple top most commits founds as parents")
}
} else {
for c := range commits {
p := hex.EncodeToString(c)
if contains(parents, p) {
warmParents = append(warmParents, p)
} else {
coldParents = append(coldParents, p)
}
}
}
return repo.commitWithWarmAndColdParents(message, branch, snapshot, warmParents, coldParents)
}
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
func (repo Repository) commitWithWarmAndColdParents(message, branch, snapshot string, warmParents, coldParents []string) (hash string, empty bool, err error) {
list, err := kv.ListStage()
if err != nil {
return "", false, err
}
// Create commit object on disk
commitHash, empty, err := core.StoreCommitObject(message, branch, snapshot, warmParents, coldParents, list, []string{})
if err != nil {
return "", false, err
}
if empty {
return "", true, nil
}
// Remove added blobs from staging area
err = kv.ClearStage()
if err != nil {
return "", false, err
}
err = core.StorePrefixObject(commitHash)
if err != nil {
return "", false, err
}
return commitHash, false, nil
}
type listCommitOptions struct {
onlySnapshots bool
}
func ListCommitOptionSetOnlySnapshots(onlySnapshots bool) func(optns *listCommitOptions) {
return func(optns *listCommitOptions) {
optns.onlySnapshots = onlySnapshots
}
}
type ListCommitOptions func(*listCommitOptions)
// List the commits for a repository
func (repo Repository) ListCommits(branch string, options ...ListCommitOptions) (<-chan Commit, error) {
optns := &listCommitOptions{}
for _, op := range options {
op(optns)
}
commits, err := kv.ListTopMostCommits()
if err != nil {
return nil, err
}
inputs := []Commit{}
for c := range commits {
commit := hex.EncodeToString(c)
start, _, err := getCommit(commit)
if err != nil {
return nil, err
}
inputs = append(inputs, *start)
}
result := make(chan Commit)
go func() {
defer close(result)
for {
if len(inputs) == 0 {
return // nothing to do (new repo?) --> we are done
}
if len(inputs) == 1 {
sendCommitResult(result, inputs[0], optns.onlySnapshots)
input, done, err := getCommit(inputs[0].Parent)
if err != nil {
return
} else if done {
return // no more new parent --> we are done
}
inputs[0] = *input
} else if len(inputs) == 2 {
t1, _ := time.Parse(time.RFC3339, inputs[0].TimeStamp)
t2, _ := time.Parse(time.RFC3339, inputs[1].TimeStamp)
if inputs[0].Hash == inputs[1].Hash {
// Same commit object so discard second instance
pos := 0
inputs = append(inputs[0:pos], inputs[pos+1:len(inputs)]...)
sendCommitResult(result, inputs[pos], optns.onlySnapshots)
input, done, err := getCommit(inputs[pos].Parent)
if err != nil {
return
} else if done {
return // no more new parent --> we are done
}
inputs[pos] = *input
} else if t1.After(t2) {
sendCommitResult(result, inputs[0], optns.onlySnapshots)
input, done, err := getCommit(inputs[0].Parent)
if err != nil {
return
} else if done {
return // no more new parent --> we are done
}
inputs[0] = *input
} else {
sendCommitResult(result, inputs[1], optns.onlySnapshots)
input, done, err := getCommit(inputs[1].Parent)
if err != nil {
return
} else if done {
return // no more new parent --> we are done
}
inputs[1] = *input
}
}
}
}()
return result, nil
}
func sendCommitResult(result chan<- Commit, cmt Commit, onlySnapshots bool) {
if !onlySnapshots || onlySnapshots && cmt.IsSnapshot {
result <- cmt
}
}
func getCommit(commit string) (*Commit, bool, error) {
if commit == "" {
return nil, true, nil // we are done
}
co, err := core.GetCommitObject(commit)
if err != nil {
return nil, false, err
}
result := Commit{Hash: commit, Message: co.S3gitMessage, TimeStamp: co.S3gitTimeStamp, IsSnapshot: co.S3gitSnapshot != ""}
if len(co.S3gitWarmParents) == 1 {
result.Parent = co.S3gitWarmParents[0]
} else if len(co.S3gitWarmParents) > 1 {
// TODO: Add other parents to inputs
result.Parent = co.S3gitWarmParents[0]
}
return &result, false, err
}