//! The per-file three-way engine, shared by `pluck` and by combine's replay. //! //! One save's change is applied onto the current tree file by file. For each //! path in scope the current content must match either the base (clean apply, //! take the new content) or the target (already applied, leave it); anything //! else refuses rather than guessing. There is no line-level merging: the unit //! of resolution is the file, which is why a clash is always a hard conflict. module apply import src.snapshot pub struct Plan { pub mut: files []snapshot.FileEntry // the tree that results from applying applied []string // paths the plan changes conflict string // first path that does not apply cleanly } pub fn conflicts(plan Plan) bool { return plan.conflict != '' } // three_way applies the change from `base` to `target` onto `current`. // // `filters` limits it to those paths or folders; an empty list means // everything. `exclude` drops paths outright, which is how a combine leaves the // clash files for the caller to decide. pub fn three_way(current []snapshot.FileEntry, base []snapshot.FileEntry, target []snapshot.FileEntry, filters []string, exclude []string) Plan { base_map := snapshot.index_files(base) target_map := snapshot.index_files(target) current_map := snapshot.index_files(current) mut paths := []string{} for path in base_map.keys() { if in_scope(path, filters) && !excluded(path, exclude) { paths << path } } for path in target_map.keys() { if in_scope(path, filters) && !excluded(path, exclude) && path !in base_map { paths << path } } paths.sort() mut plan := Plan{ applied: []string{} } mut edits := []Edit{cap: paths.len} for path in paths { from := base_map[path] or { snapshot.FileEntry{ path: path } } to := target_map[path] or { snapshot.FileEntry{ path: path } } if same_entry(from, to) { continue } here := current_map[path] or { snapshot.FileEntry{ path: path } } if same_entry(here, from) { plan.applied << path edits << Edit{ path: path entry: to keep: path in target_map } continue } if same_entry(here, to) { // Somebody already applied this change; nothing to do. continue } plan.conflict = path plan.files = with_edits(current, edits) return plan } plan.files = with_edits(current, edits) return plan } pub fn in_scope(path string, filters []string) bool { if filters.len == 0 { return true } for raw in filters { filter := raw.trim_right('/') if path == filter || path.starts_with('${filter}/') { return true } } return false } fn excluded(path string, exclude []string) bool { for name in exclude { if path == name { return true } } return false } // same_entry treats two absent paths as equal, and compares presence, content // and the executable bit otherwise. fn same_entry(a snapshot.FileEntry, b snapshot.FileEntry) bool { if a.hash == '' && b.hash == '' { return true } return a.hash == b.hash && a.executable == b.executable } // Edit is one decided path: the entry to place, and whether it stays at all. struct Edit { path string entry snapshot.FileEntry keep bool } // with_edits applies the decided paths to the tree in one pass, adding, // replacing or removing each, and returns the list ordered by path. The input // is sorted here because callers have handed it unsorted trees before (a // pluck fixture, a hand-built list), and the old one-at-a-time insert did not // care. fn with_edits(files []snapshot.FileEntry, edits []Edit) []snapshot.FileEntry { mut sorted := files.clone() sorted.sort(a.path < b.path) mut out := []snapshot.FileEntry{cap: sorted.len + edits.len} mut at := 0 for file in sorted { // Edits only ever hold paths the decision reached, in path order. for at < edits.len && edits[at].path < file.path { if edits[at].keep { out << edits[at].entry } at++ } if at < edits.len && edits[at].path == file.path { if edits[at].keep { out << edits[at].entry } at++ continue } out << file } for at < edits.len { if edits[at].keep { out << edits[at].entry } at++ } return out }