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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
use std::collections::{BTreeMap, HashMap, HashSet};
use crate::backend::CommitId;
use crate::index::IndexRef;
use crate::op_store;
use crate::op_store::{BranchTarget, RefTarget, WorkspaceId};
use crate::refs::merge_ref_targets;
#[derive(PartialEq, Eq, Clone, Hash, Debug)]
pub enum RefName {
LocalBranch(String),
RemoteBranch { branch: String, remote: String },
Tag(String),
GitRef(String),
}
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct View {
data: op_store::View,
}
impl View {
pub fn new(op_store_view: op_store::View) -> Self {
View {
data: op_store_view,
}
}
pub fn checkouts(&self) -> &HashMap<WorkspaceId, CommitId> {
&self.data.checkouts
}
pub fn get_checkout(&self, workspace_id: &WorkspaceId) -> Option<&CommitId> {
self.data.checkouts.get(workspace_id)
}
pub fn heads(&self) -> &HashSet<CommitId> {
&self.data.head_ids
}
pub fn public_heads(&self) -> &HashSet<CommitId> {
&self.data.public_head_ids
}
pub fn branches(&self) -> &BTreeMap<String, BranchTarget> {
&self.data.branches
}
pub fn tags(&self) -> &BTreeMap<String, RefTarget> {
&self.data.tags
}
pub fn git_refs(&self) -> &BTreeMap<String, RefTarget> {
&self.data.git_refs
}
pub fn git_head(&self) -> Option<CommitId> {
self.data.git_head.clone()
}
pub fn set_checkout(&mut self, workspace_id: WorkspaceId, commit_id: CommitId) {
self.data.checkouts.insert(workspace_id, commit_id);
}
pub fn remove_checkout(&mut self, workspace_id: &WorkspaceId) {
self.data.checkouts.remove(workspace_id);
}
pub fn add_head(&mut self, head_id: &CommitId) {
self.data.head_ids.insert(head_id.clone());
}
pub fn remove_head(&mut self, head_id: &CommitId) {
self.data.head_ids.remove(head_id);
}
pub fn add_public_head(&mut self, head_id: &CommitId) {
self.data.public_head_ids.insert(head_id.clone());
}
pub fn remove_public_head(&mut self, head_id: &CommitId) {
self.data.public_head_ids.remove(head_id);
}
pub fn get_ref(&self, name: &RefName) -> Option<RefTarget> {
match &name {
RefName::LocalBranch(name) => self.get_local_branch(name),
RefName::RemoteBranch { branch, remote } => self.get_remote_branch(branch, remote),
RefName::Tag(name) => self.get_tag(name),
RefName::GitRef(name) => self.git_refs().get(name).cloned(),
}
}
pub fn set_or_remove_ref(&mut self, name: RefName, target: Option<RefTarget>) {
if let Some(target) = target {
match name {
RefName::LocalBranch(name) => {
self.set_local_branch(name, target);
}
RefName::RemoteBranch { branch, remote } => {
self.set_remote_branch(branch, remote, target);
}
RefName::Tag(name) => {
self.set_tag(name, target);
}
RefName::GitRef(name) => {
self.set_git_ref(name, target);
}
}
} else {
match name {
RefName::LocalBranch(name) => {
self.remove_local_branch(&name);
}
RefName::RemoteBranch { branch, remote } => {
self.remove_remote_branch(&branch, &remote);
}
RefName::Tag(name) => {
self.remove_tag(&name);
}
RefName::GitRef(name) => {
self.remove_git_ref(&name);
}
}
}
}
pub fn get_branch(&self, name: &str) -> Option<&BranchTarget> {
self.data.branches.get(name)
}
pub fn set_branch(&mut self, name: String, target: BranchTarget) {
self.data.branches.insert(name, target);
}
pub fn remove_branch(&mut self, name: &str) {
self.data.branches.remove(name);
}
pub fn get_local_branch(&self, name: &str) -> Option<RefTarget> {
self.data
.branches
.get(name)
.and_then(|branch_target| branch_target.local_target.clone())
}
pub fn set_local_branch(&mut self, name: String, target: RefTarget) {
self.data.branches.entry(name).or_default().local_target = Some(target);
}
pub fn remove_local_branch(&mut self, name: &str) {
if let Some(branch) = self.data.branches.get_mut(name) {
branch.local_target = None;
if branch.remote_targets.is_empty() {
self.remove_branch(name);
}
}
}
pub fn get_remote_branch(&self, name: &str, remote_name: &str) -> Option<RefTarget> {
self.data
.branches
.get(name)
.and_then(|branch_target| branch_target.remote_targets.get(remote_name).cloned())
}
pub fn set_remote_branch(&mut self, name: String, remote_name: String, target: RefTarget) {
self.data
.branches
.entry(name)
.or_default()
.remote_targets
.insert(remote_name, target);
}
pub fn remove_remote_branch(&mut self, name: &str, remote_name: &str) {
if let Some(branch) = self.data.branches.get_mut(name) {
branch.remote_targets.remove(remote_name);
if branch.remote_targets.is_empty() && branch.local_target.is_none() {
self.remove_branch(name);
}
}
}
pub fn get_tag(&self, name: &str) -> Option<RefTarget> {
self.data.tags.get(name).cloned()
}
pub fn set_tag(&mut self, name: String, target: RefTarget) {
self.data.tags.insert(name, target);
}
pub fn remove_tag(&mut self, name: &str) {
self.data.tags.remove(name);
}
pub fn set_git_ref(&mut self, name: String, target: RefTarget) {
self.data.git_refs.insert(name, target);
}
pub fn remove_git_ref(&mut self, name: &str) {
self.data.git_refs.remove(name);
}
pub fn set_git_head(&mut self, head_id: CommitId) {
self.data.git_head = Some(head_id);
}
pub fn clear_git_head(&mut self) {
self.data.git_head = None;
}
pub fn set_view(&mut self, data: op_store::View) {
self.data = data;
}
pub fn store_view(&self) -> &op_store::View {
&self.data
}
pub fn store_view_mut(&mut self) -> &mut op_store::View {
&mut self.data
}
pub fn merge(&mut self, index: IndexRef, base: &View, other: &View) {
for (workspace_id, base_checkout) in base.checkouts() {
let self_checkout = self.get_checkout(workspace_id);
let other_checkout = other.get_checkout(workspace_id);
if other_checkout == Some(base_checkout) || other_checkout == self_checkout {
} else if let Some(other_checkout) = other_checkout {
if self_checkout == Some(base_checkout) {
self.set_checkout(workspace_id.clone(), other_checkout.clone());
}
} else {
self.remove_checkout(workspace_id);
}
}
for (workspace_id, other_checkout) in other.checkouts() {
if self.get_checkout(workspace_id).is_none()
&& base.get_checkout(workspace_id).is_none()
{
self.set_checkout(workspace_id.clone(), other_checkout.clone());
}
}
for removed_head in base.public_heads().difference(other.public_heads()) {
self.remove_public_head(removed_head);
}
for added_head in other.public_heads().difference(base.public_heads()) {
self.add_public_head(added_head);
}
for removed_head in base.heads().difference(other.heads()) {
self.remove_head(removed_head);
}
for added_head in other.heads().difference(base.heads()) {
self.add_head(added_head);
}
let mut maybe_changed_ref_names = HashSet::new();
let base_branches: HashSet<_> = base.branches().keys().cloned().collect();
let other_branches: HashSet<_> = other.branches().keys().cloned().collect();
for branch_name in base_branches.union(&other_branches) {
let base_branch = base.branches().get(branch_name);
let other_branch = other.branches().get(branch_name);
if other_branch == base_branch {
continue;
}
maybe_changed_ref_names.insert(RefName::LocalBranch(branch_name.clone()));
if let Some(branch) = base_branch {
for remote in branch.remote_targets.keys() {
maybe_changed_ref_names.insert(RefName::RemoteBranch {
branch: branch_name.clone(),
remote: remote.clone(),
});
}
}
if let Some(branch) = other_branch {
for remote in branch.remote_targets.keys() {
maybe_changed_ref_names.insert(RefName::RemoteBranch {
branch: branch_name.clone(),
remote: remote.clone(),
});
}
}
}
for tag_name in base.tags().keys() {
maybe_changed_ref_names.insert(RefName::Tag(tag_name.clone()));
}
for tag_name in other.tags().keys() {
maybe_changed_ref_names.insert(RefName::Tag(tag_name.clone()));
}
for git_ref_name in base.git_refs().keys() {
maybe_changed_ref_names.insert(RefName::GitRef(git_ref_name.clone()));
}
for git_ref_name in other.git_refs().keys() {
maybe_changed_ref_names.insert(RefName::GitRef(git_ref_name.clone()));
}
for ref_name in maybe_changed_ref_names {
let base_target = base.get_ref(&ref_name);
let other_target = other.get_ref(&ref_name);
self.merge_single_ref(
index,
&ref_name,
base_target.as_ref(),
other_target.as_ref(),
);
}
}
pub fn merge_single_ref(
&mut self,
index: IndexRef,
ref_name: &RefName,
base_target: Option<&RefTarget>,
other_target: Option<&RefTarget>,
) {
if base_target != other_target {
let self_target = self.get_ref(ref_name);
let new_target =
merge_ref_targets(index, self_target.as_ref(), base_target, other_target);
if new_target != self_target {
self.set_or_remove_ref(ref_name.clone(), new_target);
}
}
}
}