use std::collections::BTreeMap;
use std::path::Path;
use std::time::SystemTime;
use git2::{MergeFileInput, Oid, Sort};
use toml_edit::{DocumentMut, Item, Table};
use crate::error::{CoreError, ErrorCode};
use crate::fs::Fs;
use crate::sources::SOURCES_FILE;
use crate::store::transaction::Changes;
use crate::store::{
CommitId, LOCK_TIMEOUT, Machine, Operation, Store, StoreLock, git_error, io_error,
};
pub type Files = BTreeMap<String, Vec<u8>>;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Side {
Ours,
Theirs,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Conflict {
pub file: String,
pub field: Option<String>,
pub base: Option<String>,
pub ours: Option<String>,
pub theirs: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Merged {
pub files: Files,
pub conflicts: Vec<Conflict>,
}
pub struct MergePolicy<'a> {
pub machine_id: &'a str,
pub newer: &'a dyn Fn(&str) -> Side,
}
pub fn merge_trees(base: &Files, ours: &Files, theirs: &Files, policy: &MergePolicy<'_>) -> Merged {
let mut paths: Vec<&String> = base
.keys()
.chain(ours.keys())
.chain(theirs.keys())
.collect();
paths.sort();
paths.dedup();
let mut files = Files::new();
let mut conflicts = Vec::new();
for path in paths {
let (b, o, t) = (base.get(path), ours.get(path), theirs.get(path));
let pick = |side: Option<&Vec<u8>>, files: &mut Files| {
if let Some(bytes) = side {
files.insert(path.clone(), bytes.clone());
}
};
if o == t || t == b {
pick(o, &mut files);
continue;
}
if o == b {
pick(t, &mut files);
continue;
}
if let Some(owner) = machine_file_owner(path) {
let side = if owner == policy.machine_id {
Side::Ours
} else {
(policy.newer)(path)
};
pick(if side == Side::Ours { o } else { t }, &mut files);
continue;
}
let (Some(o), Some(t)) = (o, t) else {
conflicts.push(Conflict {
file: path.clone(),
field: None,
base: b.map(|bytes| text(bytes)),
ours: o.map(|bytes| text(bytes)),
theirs: t.map(|bytes| text(bytes)),
});
continue;
};
let merged = if path == SOURCES_FILE {
merge_sources(path, b, o, t)
} else if path.ends_with(".toml") {
merge_toml(path, b, o, t)
} else {
merge_text(path, b, o, t)
};
match merged {
Ok(bytes) => {
files.insert(path.clone(), bytes);
}
Err(found) => conflicts.extend(found),
}
}
Merged { files, conflicts }
}
fn merge_sources(
path: &str,
b: Option<&Vec<u8>>,
o: &[u8],
t: &[u8],
) -> Result<Vec<u8>, Vec<Conflict>> {
let parse = |bytes: &[u8]| std::str::from_utf8(bytes).ok()?.parse::<toml::Table>().ok();
let base = match b {
Some(bytes) => parse(bytes),
None => Some(toml::Table::new()),
};
let (Some(mut base), Some(ours), Some(mut theirs)) = (base, parse(o), parse(t)) else {
return Err(whole_file(path, b, o, t));
};
let records = |table: &toml::Table| match table.get("sources") {
None => Some(toml::Table::new()),
Some(toml::Value::Table(records)) => Some(records.clone()),
Some(_) => None,
};
let (Some(base_records), Some(our_records), Some(their_records)) =
(records(&base), records(&ours), records(&theirs))
else {
return merge_toml(path, b, o, t);
};
let mut keys: Vec<&String> = base_records
.keys()
.chain(our_records.keys())
.chain(their_records.keys())
.collect();
keys.sort();
keys.dedup();
let mut new_base = base_records.clone();
let mut new_theirs = their_records.clone();
let mut conflicts = Vec::new();
for key in keys {
let (bv, ov, tv) = (
base_records.get(key),
our_records.get(key),
their_records.get(key),
);
if ov == tv || tv == bv || ov == bv {
continue;
}
let Some(side) = settle_record(bv, ov, tv) else {
conflicts.push(Conflict {
file: path.to_owned(),
field: Some(dotted(&["sources".to_owned(), key.clone()])),
base: bv.map(ToString::to_string),
ours: ov.map(ToString::to_string),
theirs: tv.map(ToString::to_string),
});
continue;
};
let put = |table: &mut toml::Table| match ov {
Some(value) => table.insert(key.clone(), value.clone()),
None => table.remove(key),
};
put(&mut new_base);
if side == Side::Ours {
put(&mut new_theirs);
}
}
if !conflicts.is_empty() {
return Err(conflicts);
}
let text = |table: &mut toml::Table, records: toml::Table| {
if records.is_empty() {
table.remove("sources");
} else {
table.insert("sources".to_owned(), toml::Value::Table(records));
}
toml::to_string(table).ok().map(String::into_bytes)
};
let (Some(base), Some(theirs)) = (text(&mut base, new_base), text(&mut theirs, new_theirs))
else {
return Err(whole_file(path, b, o, t));
};
merge_toml(path, Some(&base), o, &theirs)
}
fn settle_record(
b: Option<&toml::Value>,
o: Option<&toml::Value>,
t: Option<&toml::Value>,
) -> Option<Side> {
fn lapse(side: Option<&toml::Value>) -> Option<&toml::Value> {
side.and_then(|record| record.get("lapsed"))
}
let decision = |side: Option<&toml::Value>| {
side.map(|record| {
let mut record = record.clone();
if let toml::Value::Table(table) = &mut record {
table.remove("lapsed");
}
record
})
};
let lapse_only = |side: Option<&toml::Value>| {
lapse(side).is_some() && (b.is_none() || decision(side) == decision(b))
};
match (lapse_only(o), lapse_only(t)) {
(true, true) if decision(o) == decision(t) => {
let at = |side| {
lapse(side)
.and_then(|lapsed| lapsed.get("at"))
.map(ToString::to_string)
};
Some(if at(t) < at(o) {
Side::Theirs
} else {
Side::Ours
})
}
(true, false) => Some(Side::Theirs),
(false, true) => Some(Side::Ours),
_ => None,
}
}
fn machine_file_owner(path: &str) -> Option<&str> {
let name = path.strip_prefix("machines/")?;
name.strip_suffix(".applied.toml")
.or_else(|| name.strip_suffix(".toml"))
.filter(|id| !id.contains('/'))
}
fn text(bytes: &[u8]) -> String {
String::from_utf8_lossy(bytes).into_owned()
}
fn whole_file(path: &str, b: Option<&Vec<u8>>, o: &[u8], t: &[u8]) -> Vec<Conflict> {
vec![Conflict {
file: path.to_owned(),
field: None,
base: b.map(|bytes| text(bytes)),
ours: Some(text(o)),
theirs: Some(text(t)),
}]
}
fn merge_text(
path: &str,
b: Option<&Vec<u8>>,
o: &[u8],
t: &[u8],
) -> Result<Vec<u8>, Vec<Conflict>> {
let binary = |bytes: &[u8]| std::str::from_utf8(bytes).is_err() || bytes.contains(&0);
if binary(o) || binary(t) || b.is_some_and(|b| binary(b)) {
return Err(whole_file(path, b, o, t));
}
let empty = Vec::new();
let mut ancestor = MergeFileInput::new();
ancestor.content(b.unwrap_or(&empty)).path(path);
let mut ours = MergeFileInput::new();
ours.content(o).path(path);
let mut theirs = MergeFileInput::new();
theirs.content(t).path(path);
let _ = git2::Oid::hash_object(git2::ObjectType::Blob, b.unwrap_or(&empty));
match git2::merge_file(&ancestor, &ours, &theirs, None) {
Ok(result) if result.is_automergeable() => Ok(result.content().to_vec()),
_ => Err(whole_file(path, b, o, t)),
}
}
type Fields = BTreeMap<Vec<String>, toml::Value>;
fn flatten(table: &toml::Table) -> Fields {
fn walk(prefix: &mut Vec<String>, table: &toml::Table, out: &mut Fields) {
for (key, value) in table {
prefix.push(key.clone());
match value {
toml::Value::Table(inner) => walk(prefix, inner, out),
other => {
out.insert(prefix.clone(), other.clone());
}
}
prefix.pop();
}
}
let mut out = Fields::new();
walk(&mut Vec::new(), table, &mut out);
out
}
fn dotted(path: &[String]) -> String {
path.iter()
.map(|key| {
let bare = !key.is_empty()
&& key
.bytes()
.all(|b| b.is_ascii_alphanumeric() || b == b'_' || b == b'-');
if bare {
key.clone()
} else {
toml::Value::String(key.clone()).to_string()
}
})
.collect::<Vec<_>>()
.join(".")
}
fn merge_toml(
path: &str,
b: Option<&Vec<u8>>,
o: &[u8],
t: &[u8],
) -> Result<Vec<u8>, Vec<Conflict>> {
let parse = |bytes: &[u8]| -> Option<Fields> {
std::str::from_utf8(bytes)
.ok()?
.parse::<toml::Table>()
.ok()
.map(|table| flatten(&table))
};
let base = match b {
Some(bytes) => parse(bytes),
None => Some(Fields::new()),
};
let (Some(base), Some(ours), Some(theirs)) = (base, parse(o), parse(t)) else {
return Err(whole_file(path, b, o, t));
};
let mut keys: Vec<&Vec<String>> = base
.keys()
.chain(ours.keys())
.chain(theirs.keys())
.collect();
keys.sort();
keys.dedup();
let mut conflicts = Vec::new();
let mut edits: Vec<(&Vec<String>, Option<&toml::Value>)> = Vec::new();
for key in keys {
let (bv, ov, tv) = (base.get(key), ours.get(key), theirs.get(key));
if ov == tv || tv == bv {
continue;
}
if ov == bv {
edits.push((key, tv));
continue;
}
conflicts.push(Conflict {
file: path.to_owned(),
field: Some(dotted(key)),
base: bv.map(ToString::to_string),
ours: ov.map(ToString::to_string),
theirs: tv.map(ToString::to_string),
});
}
if !conflicts.is_empty() {
return Err(conflicts);
}
let text = std::str::from_utf8(o).expect("parsed above");
let mut doc: DocumentMut = text.parse().map_err(|_| whole_file(path, b, o, t))?;
for (key, value) in edits {
let applied = match value {
Some(value) => set_path(&mut doc, key, value),
None => remove_path(&mut doc, key),
};
if !applied {
return Err(whole_file(path, b, o, t));
}
}
let merged = doc.to_string().into_bytes();
let mut expected = ours.clone();
for key in base.keys().chain(theirs.keys()) {
let (bv, ov, tv) = (base.get(key), ours.get(key), theirs.get(key));
if ov == bv && tv != bv {
match tv {
Some(value) => expected.insert(key.clone(), value.clone()),
None => expected.remove(key),
};
}
}
match parse(&merged) {
Some(fields) if fields == expected => Ok(merged),
_ => Err(whole_file(path, b, o, t)),
}
}
fn set_path(doc: &mut DocumentMut, path: &[String], value: &toml::Value) -> bool {
let Some((last, parents)) = path.split_last() else {
return false;
};
let Ok(value) = value.to_string().parse::<toml_edit::Value>() else {
return false;
};
let mut table: &mut dyn toml_edit::TableLike = doc.as_table_mut();
for key in parents {
let item = table.entry(key).or_insert_with(|| {
let mut created = Table::new();
created.set_implicit(true);
Item::Table(created)
});
match item.as_table_like_mut() {
Some(inner) => table = inner,
None => return false,
}
}
match table.get_mut(last) {
Some(item) if item.is_value() => {
if let Some(old) = item.as_value() {
let decor = old.decor().clone();
let mut value = value;
*value.decor_mut() = decor;
*item = Item::Value(value);
}
}
Some(_) => return false,
None => {
table.insert(last, Item::Value(value));
}
}
true
}
fn remove_path(doc: &mut DocumentMut, path: &[String]) -> bool {
fn remove(table: &mut dyn toml_edit::TableLike, path: &[String]) -> bool {
match path {
[] => false,
[last] => table.remove(last).is_some(),
[first, rest @ ..] => {
let Some(inner) = table.get_mut(first).and_then(Item::as_table_like_mut) else {
return false;
};
let removed = remove(inner, rest);
if removed && inner.is_empty() {
table.remove(first);
}
removed
}
}
}
remove(doc.as_table_mut(), path)
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MergeOutcome {
UpToDate,
FastForward(CommitId),
Merged(CommitId),
Conflicts {
base: Option<CommitId>,
conflicts: Vec<Conflict>,
},
}
impl Store<'_> {
pub fn merge_incoming(
&self,
incoming: &CommitId,
label: &str,
) -> Result<MergeOutcome, CoreError> {
let theirs_oid = Oid::from_str(incoming.as_str()).map_err(git_error)?;
self.repo.find_commit(theirs_oid).map_err(|_| {
CoreError::new(
ErrorCode::RemoteIncomplete,
format!("commit {} is not in the store", incoming.short()),
)
})?;
let _lock = StoreLock::acquire(self.fs, &self.state_dir, LOCK_TIMEOUT)?;
let machine = Machine::load_or_create(&self.env, self.fs, &self.state_dir)?;
let head = self.head()?;
let ours_oid = Oid::from_str(head.as_str()).map_err(git_error)?;
if ours_oid == theirs_oid
|| self
.repo
.graph_descendant_of(ours_oid, theirs_oid)
.map_err(git_error)?
{
return Ok(MergeOutcome::UpToDate);
}
if self
.repo
.graph_descendant_of(theirs_oid, ours_oid)
.map_err(git_error)?
{
self.fast_forward(&head, incoming)?;
return Ok(MergeOutcome::FastForward(incoming.clone()));
}
let base = self.repo.merge_base(ours_oid, theirs_oid).ok();
let base_files = match base {
Some(base) => self.files_at(&CommitId::from_oid(base))?,
None => Files::new(),
};
let ours = self.files_at(&head)?;
let theirs = self.files_at(incoming)?;
let newer = |path: &str| {
let ours_time = self.last_touch(ours_oid, path);
let theirs_time = self.last_touch(theirs_oid, path);
if ours_time > theirs_time {
Side::Ours
} else {
Side::Theirs
}
};
let merged = merge_trees(
&base_files,
&ours,
&theirs,
&MergePolicy {
machine_id: &machine.id,
newer: &newer,
},
);
if !merged.conflicts.is_empty() {
return Ok(MergeOutcome::Conflicts {
base: base.map(CommitId::from_oid),
conflicts: merged.conflicts,
});
}
let mut changes = Changes::new();
for path in ours.keys().chain(merged.files.keys()) {
if ours.get(path) != merged.files.get(path) {
changes.insert(path.clone(), merged.files.get(path).cloned());
}
}
let operation = Operation::new("store.merge", format!("merge: {label}"));
let commit = self.commit_merge(&machine, &operation, changes, theirs_oid)?;
Ok(MergeOutcome::Merged(commit))
}
pub(crate) fn files_at(&self, id: &CommitId) -> Result<Files, CoreError> {
let revision = self.revision(id)?;
let mut files = Files::new();
for file in revision.files()? {
if let Some(bytes) = revision.read_bytes(&file)? {
files.insert(file, bytes);
}
}
Ok(files)
}
fn last_touch(&self, start: Oid, path: &str) -> Option<SystemTime> {
let mut walk = self.repo.revwalk().ok()?;
walk.set_sorting(Sort::TOPOLOGICAL | Sort::TIME).ok()?;
walk.push(start).ok()?;
let paths = [path.to_owned()];
for oid in walk.flatten() {
let commit = self.repo.find_commit(oid).ok()?;
if crate::store::log::touches(&commit, &paths).ok()? {
return Some(crate::store::log::commit_time(&commit));
}
}
None
}
fn fast_forward(&self, from: &CommitId, to: &CommitId) -> Result<(), CoreError> {
let old = self.files_at(from)?;
let new = self.files_at(to)?;
let mut changes = Changes::new();
for path in old.keys().chain(new.keys()) {
if old.get(path) != new.get(path) {
changes.insert(path.clone(), new.get(path).cloned());
}
}
self.write_worktree(&changes)?;
let oid = Oid::from_str(to.as_str()).map_err(git_error)?;
let tree = self
.repo
.find_commit(oid)
.and_then(|commit| commit.tree())
.map_err(git_error)?;
let mut index = self.repo.index().map_err(git_error)?;
index.read_tree(&tree).map_err(git_error)?;
index.write().map_err(git_error)?;
self.repo
.reference(
&format!("refs/heads/{}", crate::store::BRANCH),
oid,
true,
&format!("fast-forward to {}", to.short()),
)
.map_err(git_error)?;
Ok(())
}
}
pub const MERGE_STATE: &str = "MERGE_STATE.json";
pub(crate) const MERGE_STATE_SCHEMA: u32 = 1;
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct PendingMerge {
pub source: String,
pub incoming: String,
pub base: Option<String>,
pub conflicts: Vec<RecordedConflict>,
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct RecordedConflict {
pub file: String,
pub field: Option<String>,
pub base: Option<String>,
pub ours: Option<String>,
pub theirs: Option<String>,
}
impl From<&Conflict> for RecordedConflict {
fn from(conflict: &Conflict) -> Self {
Self {
file: conflict.file.clone(),
field: conflict.field.clone(),
base: conflict.base.clone(),
ours: conflict.ours.clone(),
theirs: conflict.theirs.clone(),
}
}
}
pub fn read_merge_state(fs: &dyn Fs, state_dir: &Path) -> Vec<PendingMerge> {
#[derive(serde::Deserialize)]
struct State {
merges: Vec<PendingMerge>,
}
fs.read_to_string(&state_dir.join("merge").join(MERGE_STATE))
.ok()
.and_then(|text| serde_json::from_str::<State>(&text).ok())
.map(|state| state.merges)
.unwrap_or_default()
}
pub fn write_merge_state(
fs: &dyn Fs,
state_dir: &Path,
attempted: &[String],
pending: &[PendingMerge],
) -> Result<(), CoreError> {
let path = state_dir.join("merge").join(MERGE_STATE);
if crate::versions::written_by_newer(
fs,
&path,
crate::versions::Format::Json,
i64::from(MERGE_STATE_SCHEMA),
) {
return Ok(());
}
let mut merges = read_merge_state(fs, state_dir);
merges.retain(|merge| !attempted.contains(&merge.source));
merges.extend(pending.iter().cloned());
if merges.is_empty() {
if fs.exists(&path) {
fs.remove_file(&path)
.map_err(|err| io_error("cannot remove file", &path, &err))?;
}
return Ok(());
}
let text = serde_json::to_string_pretty(&serde_json::json!({
"schema": MERGE_STATE_SCHEMA,
"merges": merges,
}))
.expect("merge state serializes");
crate::apply::write_atomic(fs, &path, text.as_bytes())
}