#!/bin/sh

test_description='test rebase --autostash'

. ./test-lib.sh

test_expect_success 'Setup a multi-commit branch and fork an stgit stack' '
    test_commit_bulk --filename="file%s" --contents="foo" 2 &&

    stg branch --create intervening &&
    stg new -m "intervening-patch" &&

    stg branch --create stack &&
    echo bar >>file1 &&
    stg new -rm "p" &&
    git notes add -m note
'

test_expect_success 'dirty workdir aborts rebase' '
    echo foo >>file1 &&
    command_error stg rebase master 2>err &&
    grep -e "worktree not clean." err
'

test_expect_success 'dirty workdir works with --autostash' '
    stg rebase master --autostash &&
    test $(stg series --applied -c) = 1 &&
    git diff-index HEAD |
    grep -e "file1"
'

test_expect_success 'dirty workdir works with stgit.autostash config' '
    test_config stgit.autostash "yes" &&
    stg rebase master~1 &&
    test $(stg series --applied -c) = 1 &&
    git diff-index HEAD |
    grep -e "file1"
'

test_expect_success 'Setup fake editor' '
    write_script fake-editor <<-\EOF
	echo "" >"$1"
	EOF
'

test_expect_success 'rebase --autostash throws helpful error message in conflict' '
    test_set_editor "$(pwd)/fake-editor" &&
    test_when_finished test_set_editor false &&
    git checkout --force &&
    stg rebase master &&
    echo baz >file2 &&
    conflict stg rebase master~1 --interactive --autostash 2>err &&
    grep -e "stash pop resulted in conflicts" err
'

test_done
