#!/usr/bin/env bash
# What a paused setup says a conflict is. A path the repository cannot write
# because something unusable sits there is not a type change on either side,
# and neither resolution can decide it: only moving that path away can.
require_cmd git
export MISE_HISTORY_NOTIFY=false
origin="$PWD/origin.git"
git init -q --bare -b main "$origin"

echo base-one >~/.one
assert_succeed 'mise dot track ~/.one'
assert_succeed "mise dot origin set file://$origin --sync manual --yes"

second="$(dirname "$HOME")/reasons-b"
mkdir -p "$second"
b() {
  (
    cd "$second" || exit
    env HOME="$second" XDG_CONFIG_HOME="$second/.config" MISE_CONFIG_DIR="$second/.config/mise" \
      MISE_STATE_DIR="$second/.local/state/mise" MISE_DATA_DIR="$second/.local/share/mise" \
      MISE_CACHE_DIR="$second/.cache/mise" MISE_TRUSTED_CONFIG_PATHS="$second" mise "$@"
  )
}
export second
export -f b
assert_succeed "b bootstrap --from-git file://$origin --yes"

# ---- a directory where the repository has a file is the path's own problem
echo a-one >~/.one
assert_succeed 'mise dot save'
assert_succeed 'mise dot sync'
assert_succeed 'b dot sync --fetch-only'
rm -f "$second/.one"
mkdir -p "$second/.one"
# the reason names what is really there, and is not the type-change wording
assert_contains 'b dot conflicts' 'directory or unreadable path stands where the repository has a file'
assert_not_contains 'b dot conflicts' 'changed type on one side'
# the pull is paused and nothing is written over the directory
assert_fail 'b dot pull --yes' 'sync paused'
assert_succeed "test -d $second/.one"
# and the status it leaves behind advises the fix that actually applies,
# rather than the two resolutions that cannot decide this path
assert_contains 'b dot status' 'directory or unreadable path stands where the repository has a file'
assert_contains 'b dot status' 'move ~/.one away or make it a readable file'
assert_contains 'b dot status' 'cannot decide it'
# neither resolution pretends to decide it
assert_fail "b dot pull --take-remote $second/.one --yes" 'not a regular file or symlink'
# moving the path away is what finishes the pull
rm -rf "$second/.one"
echo b-one >"$second/.one"
assert_succeed 'b dot pull --take-remote-all --yes'
assert "cat $second/.one" a-one
