This repository was archived by the owner on Mar 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathmerge.go
337 lines (291 loc) · 11.4 KB
/
merge.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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package common
import (
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"time"
sqlite "github.com/gwenn/gosqlite"
)
// Merge merges the commits in commitDiffList into the destination branch destBranch of the given database
func Merge(destOwner, destFolder, destName, destBranch, srcOwner, srcFolder, srcName string, commitDiffList []CommitEntry, message, loggedInUser string) (newCommitID string, err error) {
// Get the details of the head commit for the destination database branch
branchList, err := GetBranches(destOwner, destFolder, destName) // Destination branch list
if err != nil {
return
}
branchDetails, ok := branchList[destBranch]
if !ok {
return "", fmt.Errorf("Could not retrieve details for the destination branch")
}
destCommitID := branchDetails.Commit
// Check if the MR commits will still apply cleanly to the destination branch so we can fast-forward
finalCommit := commitDiffList[len(commitDiffList)-1]
fastForwardPossible := finalCommit.Parent == destCommitID
// If fast-forwarding is possible just add a merge commit and save the new commit list.
// If it is not possible save the source commits and perform the actual merging which creates its own merge commit.
if fastForwardPossible {
// We can fast-forward. So simply add a merge commit on top of the just added source commits and save
// the new commit list and branch details.
newCommitID, err = performFastForward(destOwner, destFolder, destName, destBranch, destCommitID, commitDiffList, message, loggedInUser)
if err != nil {
return
}
} else {
// We cannot fast-forward. This means we have to perform an actual merge. A merge commit is automatically created
// by the performMerge() function so we do not have to worry about that.
// Perform merge
newCommitID, err = performMerge(destOwner, destFolder, destName, destBranch, destCommitID, srcOwner, srcFolder, srcName, commitDiffList, message, loggedInUser)
if err != nil {
return
}
}
return
}
// addCommitsForMerging simply adds the commits listed in commitDiffList to the destination branch of the databases.
// It neither performs any merging nor does it create a merge commit.
func addCommitsForMerging(destOwner, destFolder, destName, destBranch string, commitDiffList []CommitEntry, newHead bool) (err error) {
// Get the details of the head commit for the destination database branch
branchList, err := GetBranches(destOwner, destFolder, destName) // Destination branch list
if err != nil {
return err
}
branchDetails, ok := branchList[destBranch]
if !ok {
return fmt.Errorf("Could not retrieve details for the destination branch")
}
// Get destination commit list
destCommitList, err := GetCommitList(destOwner, destFolder, destName)
if err != nil {
return err
}
// Add the source commits directly to the destination commit list
for _, j := range commitDiffList {
destCommitList[j.ID] = j
}
// New head commit id
var newHeadCommitId string
if newHead {
newHeadCommitId = commitDiffList[0].ID
} else {
newHeadCommitId = branchDetails.Commit
}
// Update the branch list
b := BranchEntry{
Commit: newHeadCommitId,
CommitCount: branchDetails.CommitCount + len(commitDiffList),
Description: branchDetails.Description,
}
branchList[destBranch] = b
err = StoreCommits(destOwner, destFolder, destName, destCommitList)
if err != nil {
return err
}
err = StoreBranches(destOwner, destFolder, destName, branchList)
if err != nil {
return err
}
return
}
// performFastForward performs a merge by simply fast-forwarding to a new head.
func performFastForward(destOwner, destFolder, destName, destBranch, destCommitID string, commitDiffList []CommitEntry, message, loggedInUser string) (newCommitID string, err error) {
// Retrieve details for the logged in user
usr, err := User(loggedInUser)
if err != nil {
return
}
// Create a merge commit, using the details of the source commit (this gets us a correctly filled in DB tree
// structure easily)
mrg := commitDiffList[0]
mrg.AuthorEmail = usr.Email
mrg.AuthorName = usr.DisplayName
mrg.Message = message
mrg.Parent = commitDiffList[0].ID
mrg.OtherParents = append(mrg.OtherParents, destCommitID)
mrg.Timestamp = time.Now().UTC()
mrg.ID = CreateCommitID(mrg)
// Add the merge commit to the list of new commits to be added to the destination branch
var newCommitDiffList []CommitEntry
newCommitDiffList = append(newCommitDiffList, mrg)
newCommitDiffList = append(newCommitDiffList, commitDiffList...)
// Add the source commits and the new merge commit to the destination db commit list and update the branch list with it
err = addCommitsForMerging(destOwner, destFolder, destName, destBranch, newCommitDiffList, true)
if err != nil {
return
}
return mrg.ID, nil
}
// performMerge takes the destination database and applies the changes from commitDiffList on it.
func performMerge(destOwner, destFolder, destName, destBranch, destCommitID, srcOwner, srcFolder, srcName string, commitDiffList []CommitEntry, message, loggedInUser string) (newCommitID string, err error) {
// Figure out the last common ancestor and the current head of the branch to merge
lastCommonAncestorId := commitDiffList[len(commitDiffList)-1].Parent
currentHeadToMerge := commitDiffList[0].ID
// Figure out the changes made to the destination branch since this common ancestor.
// For this we don't need any SQLs generated because this information is only required
// for checking for conflicts.
destDiffs, err := Diff(destOwner, destFolder, destName, lastCommonAncestorId, destOwner, destFolder, destName, destCommitID, loggedInUser, NoMerge, false)
if err != nil {
return
}
// Figure out the changes made to the source branch since this common ancestor.
// For this we do want SQLs generated because these need to be applied on top of
// the destination branch head.
srcDiffs, err := Diff(srcOwner, srcFolder, srcName, lastCommonAncestorId, srcOwner, srcFolder, srcName, currentHeadToMerge, loggedInUser, NewPkMerge, false)
if err != nil {
return
}
// Check for conflicts
conflicts := checkForConflicts(srcDiffs, destDiffs, NewPkMerge)
if conflicts != nil {
// TODO We don't have developed an intelligent conflict strategy yet.
// So in the case of a conflict, just abort with an error message.
return "", fmt.Errorf("The two branches are in conflict. Please fix this manually.\n" + strings.Join(conflicts, "\n"))
}
// Get Minio location
bucket, id, _, err := MinioLocation(destOwner, destFolder, destName, destCommitID, loggedInUser)
if err != nil {
return
}
// Sanity check
if id == "" {
// The requested database wasn't found, or the user doesn't have permission to access it
return "", fmt.Errorf("Requested database not found")
}
// Retrieve database file from Minio, using locally cached version if it's already there
dbFile, err := RetrieveDatabaseFile(bucket, id)
if err != nil {
return
}
// Create a temporary file for the new database
tmpFile, err := ioutil.TempFile(Conf.DiskCache.Directory, "dbhub-merge-*.db")
if err != nil {
return
}
// Delete the file when we are done
defer os.Remove(tmpFile.Name())
defer tmpFile.Close()
// Copy destination database to temporary location
err = func() (err error) {
inFile, err := os.Open(dbFile)
if err != nil {
return
}
defer inFile.Close()
_, err = io.Copy(tmpFile, inFile)
if err != nil {
return
}
return
}()
if err != nil {
return
}
// Open temporary database file for writing
err = func() (err error) {
var sdb *sqlite.Conn
sdb, err = sqlite.Open(tmpFile.Name(), sqlite.OpenReadWrite)
if err != nil {
return
}
defer sdb.Close()
if err = sdb.EnableExtendedResultCodes(true); err != nil {
return
}
// Apply all the SQL statements from the diff on the temporary database
for _, diff := range srcDiffs.Diff {
// First apply schema changes
if diff.Schema != nil {
err = sdb.Exec(diff.Schema.Sql)
if err != nil {
return
}
}
// Then apply data changes
for _, row := range diff.Data {
err = sdb.Exec(row.Sql)
if err != nil {
return
}
}
}
return
}()
if err != nil {
return
}
// Retrieve details for the logged in user
usr, err := User(loggedInUser)
if err != nil {
return
}
// Seek to start of temporary file. When not doing this AddDatabase() cannot copy the file
_, err = tmpFile.Seek(0, 0)
if err != nil {
return
}
// The merging was successful. This means we can add the list of source commits to the destination branch.
// This needs to be done before calling AddDatabase() because AddDatabase() adds its own merge commit on
// top of the just updated commit list.
err = addCommitsForMerging(destOwner, destFolder, destName, destBranch, commitDiffList, false)
if err != nil {
return
}
// Store merged database
_, newCommitID, _, err = AddDatabase(loggedInUser, destOwner, destFolder, destName, false, destBranch, destCommitID,
KeepCurrentAccessType, "", message, "", tmpFile, time.Now(), time.Time{}, usr.DisplayName, usr.Email, usr.DisplayName, usr.Email,
[]string{currentHeadToMerge}, "")
if err != nil {
return
}
return
}
// checkForConflicts takes two diff changesets and checks whether they are compatible or not.
// Compatible changesets don't change the same objects or rows and thus can be combined without
// side effects. The function returns an empty slice if there are no conflicts. If there are
// conflicts the returned slice contains a list of the detected conflicts.
func checkForConflicts(srcDiffs Diffs, destDiffs Diffs, mergeStrategy MergeStrategy) (conflicts []string) {
// Check if an object in the source diff is also part of the destination diff
for _, srcDiff := range srcDiffs.Diff {
for _, destDiff := range destDiffs.Diff {
// Check if the object names are the same
if srcDiff.ObjectName == destDiff.ObjectName {
// If the schema of this object has changed in one of the branches, this is
// a conflict we cannot solve
if srcDiff.Schema != nil || destDiff.Schema != nil {
conflicts = append(conflicts, "Schema for "+srcDiff.ObjectName+" has changed")
// No need to look further in this case
break
}
// Check if there are any changed rows with the same primary key
for _, srcRow := range srcDiff.Data {
for _, destRow := range destDiff.Data {
if DataValuesMatch(srcRow.Pk, destRow.Pk) {
// We have found two changes which affect the same primary key. So this is a potential
// conflict. The question now is whether it is actually a problem or not.
// TODO For two UPDATE statements we could check whether they only change different
// columns and allow them in this case.
// Every combination of updates, inserts, and deletes is a conflict except for the
// case where the source row is inserted using the NewPkMerge strategy which generates
// a new primary key which doesn't conflict.
if !(srcRow.ActionType == "add" && mergeStrategy == NewPkMerge) {
// Generate and add conflict description
conflictString := "Conflict in " + srcDiff.ObjectName + " for "
for _, pk := range srcRow.Pk {
conflictString += pk.Name + "=" + pk.Value.(string) + ","
}
conflicts = append(conflicts, strings.TrimSuffix(conflictString, ","))
}
// No need to look through the rest of the destination rows
break
}
}
}
// No need to look through the remaining destination diff items.
// Just continue with the next source diff item.
break
}
}
}
return
}