2022-11-26 23:57:50 +00:00
|
|
|
// Copyright 2021 The Jujutsu Authors
|
2021-07-19 06:04:21 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2023-07-10 15:17:00 +00:00
|
|
|
#![allow(missing_docs)]
|
|
|
|
|
2021-09-12 06:52:38 +00:00
|
|
|
use crate::backend::CommitId;
|
2023-08-06 13:52:40 +00:00
|
|
|
use crate::conflicts::Merge;
|
2023-02-13 05:57:49 +00:00
|
|
|
use crate::index::Index;
|
2023-05-14 13:34:55 +00:00
|
|
|
use crate::merge::trivial_merge;
|
2023-07-12 22:20:44 +00:00
|
|
|
use crate::op_store::{BranchTarget, RefTarget, RefTargetOptionExt};
|
2021-07-19 06:04:21 +00:00
|
|
|
|
|
|
|
pub fn merge_ref_targets(
|
2023-02-13 05:57:49 +00:00
|
|
|
index: &dyn Index,
|
2023-07-12 22:20:44 +00:00
|
|
|
left: &RefTarget,
|
|
|
|
base: &RefTarget,
|
|
|
|
right: &RefTarget,
|
|
|
|
) -> RefTarget {
|
2023-07-12 19:17:40 +00:00
|
|
|
if let Some(&resolved) = trivial_merge(&[base], &[left, right]) {
|
|
|
|
return resolved.clone();
|
2023-05-14 13:34:55 +00:00
|
|
|
}
|
2021-07-19 06:04:21 +00:00
|
|
|
|
2023-08-06 13:52:40 +00:00
|
|
|
let merge = Merge::new(
|
2023-07-12 22:20:44 +00:00
|
|
|
vec![base.as_conflict().clone()],
|
|
|
|
vec![left.as_conflict().clone(), right.as_conflict().clone()],
|
2023-07-07 10:57:34 +00:00
|
|
|
)
|
|
|
|
.flatten()
|
|
|
|
.simplify();
|
|
|
|
|
2023-08-06 13:52:40 +00:00
|
|
|
if merge.is_resolved() {
|
|
|
|
RefTarget::from_merge(merge)
|
2023-07-07 12:11:16 +00:00
|
|
|
} else {
|
2023-08-06 13:52:40 +00:00
|
|
|
let merge = merge_ref_targets_non_trivial(index, merge);
|
|
|
|
RefTarget::from_merge(merge)
|
2023-05-14 13:34:55 +00:00
|
|
|
}
|
2023-07-07 10:57:34 +00:00
|
|
|
}
|
2021-07-19 06:04:21 +00:00
|
|
|
|
2023-07-07 10:57:34 +00:00
|
|
|
fn merge_ref_targets_non_trivial(
|
|
|
|
index: &dyn Index,
|
2023-08-06 13:52:40 +00:00
|
|
|
conflict: Merge<Option<CommitId>>,
|
|
|
|
) -> Merge<Option<CommitId>> {
|
2023-07-07 12:11:16 +00:00
|
|
|
let (mut removes, mut adds) = conflict.take();
|
|
|
|
while let Some((remove_index, add_index)) = find_pair_to_remove(index, &removes, &adds) {
|
|
|
|
removes.remove(remove_index);
|
2023-05-14 13:34:55 +00:00
|
|
|
adds.remove(add_index);
|
|
|
|
}
|
2023-08-06 13:52:40 +00:00
|
|
|
Merge::new(removes, adds)
|
2021-07-19 06:04:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn find_pair_to_remove(
|
2023-02-13 05:57:49 +00:00
|
|
|
index: &dyn Index,
|
2023-07-07 12:11:16 +00:00
|
|
|
removes: &[Option<CommitId>],
|
|
|
|
adds: &[Option<CommitId>],
|
|
|
|
) -> Option<(usize, usize)> {
|
2021-07-19 06:04:21 +00:00
|
|
|
// 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) {
|
2023-07-07 12:11:16 +00:00
|
|
|
// TODO: Instead of relying on the list order, maybe ((add1, add2), remove)
|
|
|
|
// combination should be somehow weighted?
|
|
|
|
let (add_index, add_id) = match (add1, add2) {
|
|
|
|
(Some(id1), Some(id2)) if id1 == id2 => (add_index1, id1),
|
|
|
|
(Some(id1), Some(id2)) if index.is_ancestor(id1, id2) => (add_index1, id1),
|
|
|
|
(Some(id1), Some(id2)) if index.is_ancestor(id2, id1) => (add_index2, id2),
|
|
|
|
_ => continue,
|
2023-07-07 15:30:48 +00:00
|
|
|
};
|
2023-07-07 12:11:16 +00:00
|
|
|
if let Some(remove_index) = removes.iter().position(|remove| match remove {
|
|
|
|
Some(id) => index.is_ancestor(id, add_id),
|
|
|
|
None => true, // Absent ref can be considered a root
|
|
|
|
}) {
|
|
|
|
return Some((remove_index, add_index));
|
2021-07-19 06:04:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
2021-09-11 18:14:22 +00:00
|
|
|
|
2022-12-19 02:25:04 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
|
2021-09-12 00:09:48 +00:00
|
|
|
pub struct BranchPushUpdate {
|
|
|
|
pub old_target: Option<CommitId>,
|
|
|
|
pub new_target: Option<CommitId>,
|
|
|
|
}
|
|
|
|
|
2021-09-11 18:14:22 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq, Clone)]
|
|
|
|
pub enum BranchPushAction {
|
2021-09-12 00:09:48 +00:00
|
|
|
Update(BranchPushUpdate),
|
2021-09-11 18:14:22 +00:00
|
|
|
AlreadyMatches,
|
|
|
|
LocalConflicted,
|
|
|
|
RemoteConflicted,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Figure out what changes (if any) need to be made to the remote when pushing
|
|
|
|
/// this branch.
|
|
|
|
pub fn classify_branch_push_action(
|
|
|
|
branch_target: &BranchTarget,
|
|
|
|
remote_name: &str,
|
|
|
|
) -> BranchPushAction {
|
2023-07-12 14:41:38 +00:00
|
|
|
let local_target = &branch_target.local_target;
|
|
|
|
let remote_target = branch_target.remote_targets.get(remote_name).flatten();
|
2023-07-11 17:48:55 +00:00
|
|
|
if local_target == remote_target {
|
|
|
|
BranchPushAction::AlreadyMatches
|
2023-07-19 12:31:49 +00:00
|
|
|
} else if local_target.has_conflict() {
|
2023-07-11 17:48:55 +00:00
|
|
|
BranchPushAction::LocalConflicted
|
2023-07-19 12:31:49 +00:00
|
|
|
} else if remote_target.has_conflict() {
|
2023-07-11 17:48:55 +00:00
|
|
|
BranchPushAction::RemoteConflicted
|
|
|
|
} else {
|
|
|
|
BranchPushAction::Update(BranchPushUpdate {
|
|
|
|
old_target: remote_target.as_normal().cloned(),
|
|
|
|
new_target: local_target.as_normal().cloned(),
|
|
|
|
})
|
2021-09-11 18:14:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use maplit::btreemap;
|
|
|
|
|
|
|
|
use super::*;
|
2023-01-01 03:24:32 +00:00
|
|
|
use crate::backend::ObjectId;
|
2021-09-11 18:14:22 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_classify_branch_push_action_unchanged() {
|
|
|
|
let commit_id1 = CommitId::from_hex("11");
|
|
|
|
let branch = BranchTarget {
|
2023-07-11 13:14:59 +00:00
|
|
|
local_target: RefTarget::normal(commit_id1.clone()),
|
2023-07-15 15:54:50 +00:00
|
|
|
remote_targets: btreemap! {
|
2023-07-12 14:41:38 +00:00
|
|
|
"origin".to_string() => RefTarget::normal(commit_id1),
|
2023-07-15 15:54:50 +00:00
|
|
|
},
|
2021-09-11 18:14:22 +00:00
|
|
|
};
|
|
|
|
assert_eq!(
|
|
|
|
classify_branch_push_action(&branch, "origin"),
|
|
|
|
BranchPushAction::AlreadyMatches
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_classify_branch_push_action_added() {
|
|
|
|
let commit_id1 = CommitId::from_hex("11");
|
|
|
|
let branch = BranchTarget {
|
2023-07-11 13:14:59 +00:00
|
|
|
local_target: RefTarget::normal(commit_id1.clone()),
|
2023-07-15 15:54:50 +00:00
|
|
|
remote_targets: btreemap! {},
|
2021-09-11 18:14:22 +00:00
|
|
|
};
|
|
|
|
assert_eq!(
|
|
|
|
classify_branch_push_action(&branch, "origin"),
|
2021-09-12 00:09:48 +00:00
|
|
|
BranchPushAction::Update(BranchPushUpdate {
|
2021-09-11 18:14:22 +00:00
|
|
|
old_target: None,
|
|
|
|
new_target: Some(commit_id1),
|
2021-09-12 00:09:48 +00:00
|
|
|
})
|
2021-09-11 18:14:22 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_classify_branch_push_action_removed() {
|
|
|
|
let commit_id1 = CommitId::from_hex("11");
|
|
|
|
let branch = BranchTarget {
|
2023-07-12 16:56:02 +00:00
|
|
|
local_target: RefTarget::absent(),
|
2023-07-15 15:54:50 +00:00
|
|
|
remote_targets: btreemap! {
|
2023-07-12 14:41:38 +00:00
|
|
|
"origin".to_string() => RefTarget::normal(commit_id1.clone()),
|
2023-07-15 15:54:50 +00:00
|
|
|
},
|
2021-09-11 18:14:22 +00:00
|
|
|
};
|
|
|
|
assert_eq!(
|
|
|
|
classify_branch_push_action(&branch, "origin"),
|
2021-09-12 00:09:48 +00:00
|
|
|
BranchPushAction::Update(BranchPushUpdate {
|
2021-09-11 18:14:22 +00:00
|
|
|
old_target: Some(commit_id1),
|
|
|
|
new_target: None,
|
2021-09-12 00:09:48 +00:00
|
|
|
})
|
2021-09-11 18:14:22 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_classify_branch_push_action_updated() {
|
|
|
|
let commit_id1 = CommitId::from_hex("11");
|
|
|
|
let commit_id2 = CommitId::from_hex("22");
|
|
|
|
let branch = BranchTarget {
|
2023-07-11 13:14:59 +00:00
|
|
|
local_target: RefTarget::normal(commit_id2.clone()),
|
2023-07-15 15:54:50 +00:00
|
|
|
remote_targets: btreemap! {
|
2023-07-12 14:41:38 +00:00
|
|
|
"origin".to_string() => RefTarget::normal(commit_id1.clone()),
|
2023-07-15 15:54:50 +00:00
|
|
|
},
|
2021-09-11 18:14:22 +00:00
|
|
|
};
|
|
|
|
assert_eq!(
|
|
|
|
classify_branch_push_action(&branch, "origin"),
|
2021-09-12 00:09:48 +00:00
|
|
|
BranchPushAction::Update(BranchPushUpdate {
|
2021-09-11 18:14:22 +00:00
|
|
|
old_target: Some(commit_id1),
|
|
|
|
new_target: Some(commit_id2),
|
2021-09-12 00:09:48 +00:00
|
|
|
})
|
2021-09-11 18:14:22 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_classify_branch_push_action_local_conflicted() {
|
|
|
|
let commit_id1 = CommitId::from_hex("11");
|
|
|
|
let commit_id2 = CommitId::from_hex("22");
|
|
|
|
let branch = BranchTarget {
|
2023-07-11 15:22:21 +00:00
|
|
|
local_target: RefTarget::from_legacy_form([], [commit_id1.clone(), commit_id2]),
|
2023-07-15 15:54:50 +00:00
|
|
|
remote_targets: btreemap! {
|
2023-07-12 14:41:38 +00:00
|
|
|
"origin".to_string() => RefTarget::normal(commit_id1),
|
2023-07-15 15:54:50 +00:00
|
|
|
},
|
2021-09-11 18:14:22 +00:00
|
|
|
};
|
|
|
|
assert_eq!(
|
|
|
|
classify_branch_push_action(&branch, "origin"),
|
|
|
|
BranchPushAction::LocalConflicted
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_classify_branch_push_action_remote_conflicted() {
|
|
|
|
let commit_id1 = CommitId::from_hex("11");
|
|
|
|
let commit_id2 = CommitId::from_hex("22");
|
|
|
|
let branch = BranchTarget {
|
2023-07-11 13:14:59 +00:00
|
|
|
local_target: RefTarget::normal(commit_id1.clone()),
|
2023-07-15 15:54:50 +00:00
|
|
|
remote_targets: btreemap! {
|
2023-07-11 15:22:21 +00:00
|
|
|
"origin".to_string() => RefTarget::from_legacy_form(
|
|
|
|
[],
|
|
|
|
[commit_id1, commit_id2],
|
2023-07-12 14:41:38 +00:00
|
|
|
),
|
2023-07-15 15:54:50 +00:00
|
|
|
},
|
2021-09-11 18:14:22 +00:00
|
|
|
};
|
|
|
|
assert_eq!(
|
|
|
|
classify_branch_push_action(&branch, "origin"),
|
|
|
|
BranchPushAction::RemoteConflicted
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|