Skip to content

Commit 437b702

Browse files
authoredJan 10, 2025··
Merge pull request #20 from drewmccormack/feature/salvaging
Introduce “salvaging” (2-way merge) for bootstrapping
2 parents af52c37 + 20926a3 commit 437b702

File tree

457 files changed

+548
-413
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

457 files changed

+548
-413
lines changed
 

‎Samples/Forkers/Forkers/Models/Forker.swift

+15
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,18 @@ struct Forker: Identifiable, Codable, Hashable {
3030
@Merged var notes: String = ""
3131
@Merged var tags: Set<String> = []
3232
}
33+
34+
extension Forkers {
35+
36+
func salvaging(from other: Forkers) throws -> Forkers {
37+
// When two devices have unrelated histories, they can't be
38+
// 3-way merged. Instead, we will start with the dominant
39+
// forker values (eg from the cloud), and copy in any forkers unique
40+
// to the subordinate data (eg local)
41+
var new = self
42+
let ids = Set(self.forkers.map(\.id))
43+
new.forkers += other.forkers.filter { !ids.contains($0.id) }
44+
return new
45+
}
46+
47+
}

‎Sources/Forked/ForkedResource+Merging.swift

+4-1
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,13 @@ public extension ForkedResource where RepositoryType.Resource: Mergeable {
9696
switch (commits.dominant.content, commits.subordinate.content, ancestorCommit.content) {
9797
case (.none, .none, _):
9898
return .none
99-
case (.resource, .none, _), (.resource, .resource, .none):
99+
case (.resource, .none, _):
100100
return commits.dominant.content
101101
case (.none, .resource, _):
102102
return commits.subordinate.content
103+
case (.resource(let r1), .resource(let r2), .none):
104+
let resource = try r1.salvaging(from: r2)
105+
return .resource(resource)
103106
case (.resource(let r1), .resource(let r2), .resource(let ra)):
104107
let resource = try r1.merged(withSubordinate: r2, commonAncestor: ra)
105108
return .resource(resource)

0 commit comments

Comments
 (0)
Please sign in to comment.