///| Merge implementation with content-level 3-way merge

///|
pub enum MergeStatus {
  AlreadyUpToDate
  FastForward
  Merged
  Conflicted
}

///|
pub struct MergeResult {
  status : MergeStatus
  commit_id : @bit.ObjectId?
  conflicts : Array[String]
  /// Conflict type per path: "content", "rename/delete", "rename/add", etc.
  conflict_types : Map[String, String]
}




///|
fn merge_fast_forward(
  fs : &@bit.FileSystem,
  rfs : &@bit.RepoFileSystem,
  root : String,
  git_dir : String,
  target : @bit.ObjectId,
) -> Unit raise @bit.GitError {
  // A fast-forward is a two-tree checkout like git's: local changes are
  // carried over and block it only where the target differs.
  let object_git_dir = resolve_common_git_dir(rfs, git_dir)
  let sparse = is_sparse_checkout_enabled(rfs, git_dir)
  let before : Map[String, Bool] = Map([])
  if sparse {
    for e in read_index_entries(rfs, git_dir) {
      before[e.path] = true
    }
  }
  let updated : Array[String] = []
  checkout_two_way(
    fs,
    rfs,
    root,
    git_dir,
    object_git_dir,
    target,
    updated_paths=updated,
  )
  update_head_ref(fs, rfs, git_dir, target)
  // Sparse: what the switch wrote outside the cone is removed again and
  // gets the skip-worktree bit; a new entry whose file was already there
  // is left alone (git).
  if sparse {
    let written : Map[String, Bool] = Map([])
    for p in updated {
      written[p] = true
    }
    let leave : Map[String, Bool] = Map([])
    for e in read_index_entries(rfs, git_dir) {
      if !before.contains(e.path) && !written.contains(e.path) {
        leave[e.path] = true
      }
    }
    sparse_checkout_reapply(fs, rfs, root, leave~)
  }
}

///|
/// Outcome of a three-way merge of trees (files keyed by path).
priv struct MergeThreeWay {
  merged : Map[String, TreeFileEntry]
  conflicts : Array[String]
  conflict_types : Map[String, String]
  rename_conflict_stages : Map[String, (TreeFileEntry, TreeFileEntry)]
  removed_paths : Array[String]
  /// "distinct types" conflicts (a regular file on one side, a symlink on
  /// the other): path -> (path the regular file is recorded at, its stage)
  distinct_types : Map[String, (String, Int)]
  /// rename/delete conflicts: new path -> base entry (index stage 1)
  rename_delete_base : Map[String, TreeFileEntry]
  /// "file location" conflicts: new path -> (stage, entry)
  location_stages : Map[String, (Int, TreeFileEntry)]
  /// content merges at a rename destination: the three sides
  rename_sides : Map[String, RenameMergeSides]
}

///|
/// The sides of a content merge at a rename destination: each side's
/// entry and the path it had there (conflict labels and index stages).
priv struct RenameMergeSides {
  base : TreeFileEntry?
  ours_path : String
  ours : TreeFileEntry
  theirs_path : String
  theirs : TreeFileEntry
}

///|
/// Three-way merge of `ours` and `theirs` against `base_files`: file-level
/// resolution, content merges (conflict markers are written to the
/// worktree), rename and file/directory conflicts. Shared by merge,
/// cherry-pick and revert, which differ only in which trees they pass.
fn merge_three_way(
  db : ObjectDb,
  fs : &@bit.FileSystem,
  rfs : &@bit.RepoFileSystem,
  root : String,
  git_dir : String,
  base_files : Map[String, TreeFileEntry],
  ours : Map[String, TreeFileEntry],
  theirs : Map[String, TreeFileEntry],
  ours_label~ : String,
  theirs_label~ : String,
) -> MergeThreeWay raise @bit.GitError {
  let file_result = merge_files(base_files, ours, theirs, db, rfs)
  let merged = file_result.merged
  let conflicts = file_result.conflicts
  let all_conflict_types = file_result.conflict_types
  let rename_conflict_stages : Map[String, (TreeFileEntry, TreeFileEntry)] = Map([],
  )
  // Try content-level merge for files modified on both sides
  let distinct_types : Map[String, (String, Int)] = Map([])
  for candidate in file_result.needs_content_merge {
    // Distinct types (git merge-ort): a regular file on one side and a
    // symlink on the other cannot be content-merged. The non-regular entry
    // keeps the path, the regular file is recorded at `~