forked from libfuse/libfuse
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
view: add support for merging git ref targets
When there are two concurrent operations, we would resolve conflicting updates of git refs quite arbitrarily before this change. This change introduces a new `refs` module with a function for doing a 3-way merge of ref targets. For example, if both sides moved a ref forward but by different amounts, we pick the descendant-most target. If we can't resolve it, we leave it as a conflict. That's fine to do for git refs because they can be resolved by simply running `jj git refresh` to import refs again (the underlying git repo is the source of truth). As with the previous change, I'm doing this now because mostly because it is a good stepping stone towards branch support (issue libfuse#21). We'll soon use the same 3-way merging for updating the local branch definition (once we add that) when a branch changes in the git repo or on a remote.
- Loading branch information
1 parent
0aa738a
commit 6b1ccd4
Showing
8 changed files
with
528 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// Copyright 2021 Google LLC | ||
// | ||
// 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 | ||
// | ||
// https://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. | ||
|
||
use crate::index::IndexRef; | ||
use crate::op_store::RefTarget; | ||
use crate::store::CommitId; | ||
|
||
pub fn merge_ref_targets( | ||
index: IndexRef, | ||
left: Option<&RefTarget>, | ||
base: Option<&RefTarget>, | ||
right: Option<&RefTarget>, | ||
) -> Option<RefTarget> { | ||
if left == base || left == right { | ||
right.cloned() | ||
} else if base == right { | ||
left.cloned() | ||
} else { | ||
let mut adds = vec![]; | ||
let mut removes = vec![]; | ||
if let Some(left) = left { | ||
adds.extend(left.adds()); | ||
removes.extend(left.removes()); | ||
} | ||
if let Some(base) = base { | ||
// Note that these are backwards (because the base is subtracted). | ||
adds.extend(base.removes()); | ||
removes.extend(base.adds()); | ||
} | ||
if let Some(right) = right { | ||
adds.extend(right.adds()); | ||
removes.extend(right.removes()); | ||
} | ||
|
||
while let Some((maybe_remove_index, add_index)) = | ||
find_pair_to_remove(index, &adds, &removes) | ||
{ | ||
if let Some(remove_index) = maybe_remove_index { | ||
removes.remove(remove_index); | ||
} | ||
adds.remove(add_index); | ||
} | ||
|
||
if adds.is_empty() { | ||
None | ||
} else if adds.len() == 1 && removes.is_empty() { | ||
Some(RefTarget::Normal(adds[0].clone())) | ||
} else { | ||
Some(RefTarget::Conflict { removes, adds }) | ||
} | ||
} | ||
} | ||
|
||
fn find_pair_to_remove( | ||
index: IndexRef, | ||
adds: &[CommitId], | ||
removes: &[CommitId], | ||
) -> Option<(Option<usize>, usize)> { | ||
// Removes pairs of matching adds and removes. | ||
for (add_index, add) in adds.iter().enumerate() { | ||
for (remove_index, remove) in removes.iter().enumerate() { | ||
if add == remove { | ||
return Some((Some(remove_index), add_index)); | ||
} | ||
} | ||
} | ||
|
||
// If a "remove" is an ancestor of two different "adds" and one of the | ||
// "adds" is an ancestor of the other, then pick the descendant. | ||
for (add_index1, add1) in adds.iter().enumerate() { | ||
for (add_index2, add2) in adds.iter().enumerate().skip(add_index1 + 1) { | ||
let first_add_is_ancestor; | ||
if add1 == add2 || index.is_ancestor(add1, add2) { | ||
first_add_is_ancestor = true; | ||
} else if index.is_ancestor(add2, add1) { | ||
first_add_is_ancestor = false; | ||
} else { | ||
continue; | ||
} | ||
if removes.is_empty() { | ||
if first_add_is_ancestor { | ||
return Some((None, add_index1)); | ||
} else { | ||
return Some((None, add_index2)); | ||
} | ||
} | ||
for (remove_index, remove) in removes.iter().enumerate() { | ||
if first_add_is_ancestor && index.is_ancestor(remove, add1) { | ||
return Some((Some(remove_index), add_index1)); | ||
} else if !first_add_is_ancestor && index.is_ancestor(remove, add2) { | ||
return Some((Some(remove_index), add_index2)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
None | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.