# Copyright (C) 2006, 2007, 2009, 2010 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA


import os

from bzrlib.tests import TestCaseWithTransport
from bzrlib.workingtree import WorkingTree


class TestRemerge(TestCaseWithTransport):

    def make_file(self, name, contents):
        f = open(name, 'wb')
        try:
            f.write(contents)
        finally:
            f.close()

    def create_conflicts(self):
        """Create a conflicted tree"""
        os.mkdir('base')
        os.chdir('base')
        self.make_file('hello', "hi world")
        self.make_file('answer', "42")
        self.run_bzr('init')
        self.run_bzr('add')
        self.run_bzr('commit -m base')
        self.run_bzr('branch . ../other')
        self.run_bzr('branch . ../this')
        os.chdir('../other')
        self.make_file('hello', "Hello.")
        self.make_file('answer', "Is anyone there?")
        self.run_bzr('commit -m other')
        os.chdir('../this')
        self.make_file('hello', "Hello, world")
        self.run_bzr('mv answer question')
        self.make_file('question', "What do you get when you multiply six"
                                   "times nine?")
        self.run_bzr('commit -m this')

    def test_remerge(self):
        """Remerge command works as expected"""
        self.create_conflicts()
        self.run_bzr('merge ../other --show-base', retcode=1)
        conflict_text = open('hello').read()
        self.assertTrue('|||||||' in conflict_text)
        self.assertTrue('hi world' in conflict_text)

        self.run_bzr_error(['conflicts encountered'], 'remerge', retcode=1)
        conflict_text = open('hello').read()
        self.assertFalse('|||||||' in conflict_text)
        self.assertFalse('hi world' in conflict_text)

        os.unlink('hello.OTHER')
        os.unlink('question.OTHER')

        self.run_bzr_error(['jello is not versioned'],
                     'remerge jello --merge-type weave')
        self.run_bzr_error(['conflicts encountered'],
                           'remerge hello --merge-type weave',
                           retcode=1)

        self.assertPathExists('hello.OTHER')
        self.assertPathDoesNotExist('question.OTHER')

        file_id = self.run_bzr('file-id hello')[0]
        self.run_bzr_error(['hello.THIS is not versioned'],
                           'file-id hello.THIS')

        self.run_bzr_error(['conflicts encountered'],
                           'remerge --merge-type weave', retcode=1)

        self.assertPathExists('hello.OTHER')
        self.assertTrue('hello.BASE')
        self.assertFalse('|||||||' in conflict_text)
        self.assertFalse('hi world' in conflict_text)

        self.run_bzr_error(['Showing base is not supported.*Weave'],
                           'remerge . --merge-type weave --show-base')
        self.run_bzr_error(['Can\'t reprocess and show base'],
                           'remerge . --show-base --reprocess')
        self.run_bzr_error(['conflicts encountered'],
                           'remerge . --merge-type weave --reprocess',
                           retcode=1)
        self.run_bzr_error(['conflicts encountered'],
                           'remerge hello --show-base',
                           retcode=1)
        self.run_bzr_error(['conflicts encountered'],
                           'remerge hello --reprocess', retcode=1)

        self.run_bzr('resolve --all')
        self.run_bzr('commit -m done')

        self.run_bzr_error(['remerge only works after normal merges',
                            'Not cherrypicking or multi-merges'],
                           'remerge')

    def test_conflicts(self):
        self.create_conflicts()
        self.run_bzr('merge ../other', retcode=1)
        wt = WorkingTree.open('.')
        self.assertEqual(2, len(wt.conflicts()))
        self.run_bzr('remerge', retcode=1)
        wt = WorkingTree.open('.')
        self.assertEqual(2, len(wt.conflicts()))
        self.run_bzr('remerge hello', retcode=1)
        self.assertEqual(2, len(wt.conflicts()))
