#! /bin/sh

set -ex

BASE="$(dirname "$(cd $(dirname "$0") && pwd)")"
. "$BASE/t/test-lib.sh"

GIT_IMERGE="git-imerge"

EXPECTED_TREE=ffa191c987a8d3f597376744a95439fa1b4a55c5

test_conflict () {
    local conflict=$1

    TMP="$BASE/t/tmp/conflicted-$conflict"
    DESCRIPTION="git-imerge test repository with conflict at $conflict"

    # Set up a test repo with two branches, `c` and `d`, that each have a
    # commit making incompatible changes to the same file `conflict.txt`:
    init_test_repo "$TMP" "$DESCRIPTION"
    cd "$TMP"

    modify c.txt 0
    modify d.txt 0
    modify conflict.txt "original version"
    commit -m 'm⇒0'

    git checkout -b c master --

    for i in $(seq 9)
    do
        modify c.txt $i

        # Is this the commit that should conflict?
        case $conflict in
        $i-*) modify conflict.txt "c version" ;;
        esac

        commit -m "c⇒$i"
    done

    git checkout -b d master --

    for i in $(seq 6)
    do
        modify d.txt $i

        # Is this the commit that should conflict?
        case $conflict in
        *-$i) modify conflict.txt "d version" ;;
        esac

        commit -m "d⇒$i"
    done

    git checkout c
    "$GIT_IMERGE" init --name=c-d d
    "$GIT_IMERGE" list
    "$GIT_IMERGE" diagram --commits --frontier --html=imerge0.html
    "$GIT_IMERGE" autofill 2>&1 | tee autofill.out
    if grep -q Traceback autofill.out
    then
        exit 1
    fi

    if ! grep -q "suggest manual merge of $conflict" autofill.out
    then
        echo "conflict not detected"
        exit 1
    fi

    "$GIT_IMERGE" diagram --commits --frontier --html=imerge1.html
    "$GIT_IMERGE" continue --edit
    echo 'merged version' >conflict.txt
    git add conflict.txt
    "$GIT_IMERGE" continue --no-edit
    "$GIT_IMERGE" diagram --commits --frontier --html=imerge2.html
    GIT_EDITOR=cat "$GIT_IMERGE" simplify --goal=merge --branch=c-d-merge
    check_tree c-d-merge $EXPECTED_TREE
    "$GIT_IMERGE" simplify --goal=rebase --branch=c-d-rebase
    check_tree c-d-rebase $EXPECTED_TREE
    "$GIT_IMERGE" simplify --goal=rebase-with-history --branch=c-d-rebase-with-history
    check_tree c-d-rebase-with-history $EXPECTED_TREE
    "$GIT_IMERGE" simplify --goal=border --branch=c-d-border
    check_tree c-d-border $EXPECTED_TREE
    "$GIT_IMERGE" simplify --goal=border-with-history --branch=c-d-border-with-history
    check_tree c-d-border-with-history $EXPECTED_TREE
    "$GIT_IMERGE" simplify --goal=border-with-history2 --branch=c-d-border-with-history2
    check_tree c-d-border-with-history2 $EXPECTED_TREE
    "$GIT_IMERGE" remove

    git checkout c
    "$GIT_IMERGE" start --goal=full --first-parent --name=c-d d 2>&1 | tee start.out
    if grep -q Traceback start.out
    then
        exit 1
    fi

    if ! grep -q "suggest manual merge of $conflict" autofill.out
    then
        echo "conflict not detected"
        exit 1
    fi

    "$GIT_IMERGE" diagram --commits --frontier --html=imerge3.html
    echo 'merged version' >conflict.txt
    git add conflict.txt
    GIT_EDITOR=cat git commit
    "$GIT_IMERGE" continue --edit
    "$GIT_IMERGE" diagram --commits --frontier --html=imerge4.html
    "$GIT_IMERGE" finish --branch=c-d-full
    check_tree c-d-full $EXPECTED_TREE
}

# Run tests with conflicts at various locations...

# A generic location:
test_conflict 4-3

# The four corners:
test_conflict 1-1
test_conflict 9-1
test_conflict 1-6
test_conflict 9-6

# Along each of the edges:
test_conflict 1-2
test_conflict 9-5
test_conflict 5-1
test_conflict 6-6
