hgext/imerge.py
author Benoit Boissinot <benoit.boissinot@ens-lyon.org>
Thu, 02 Aug 2007 14:04:00 +0200
changeset 5055 56d48aed1f69
parent 5054 ec70fd08e16c
child 5056 34a54cc5df1b
permissions -rw-r--r--
imerge: tarfile.extractall is only available in python2.5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5044
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
     1
# Copyright (C) 2007 Brendan Cully <brendan@kublai.com>
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
     2
# Published under the GNU GPL
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
     3
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
     4
'''
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
     5
imerge - interactive merge
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
     6
'''
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
     7
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
     8
from mercurial.i18n import _
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
     9
from mercurial.node import *
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    10
from mercurial import commands, cmdutil, hg, merge, util
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    11
import os, tarfile
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    12
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    13
class InvalidStateFileException(Exception): pass
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    14
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    15
class ImergeStateFile(object):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    16
    def __init__(self, im):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    17
        self.im = im
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    18
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    19
    def save(self, dest):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    20
        tf = tarfile.open(dest, 'w:gz')
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    21
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    22
        st = os.path.join(self.im.path, 'status')
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    23
        tf.add(st, os.path.join('.hg', 'imerge', 'status'))
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    24
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    25
        for f in self.im.resolved:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    26
            abssrc = self.im.repo.wjoin(f)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    27
            tf.add(abssrc, f)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    28
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    29
        tf.close()
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    30
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    31
    def load(self, source):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    32
        wlock = self.im.repo.wlock()
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    33
        lock = self.im.repo.lock()
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    34
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    35
        tf = tarfile.open(source, 'r')
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    36
        contents = tf.getnames()
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    37
        statusfile = os.path.join('.hg', 'imerge', 'status')
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    38
        if statusfile not in contents:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    39
            raise InvalidStateFileException('no status file')
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    40
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    41
        tf.extract(statusfile, self.im.repo.root)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    42
        self.im.load()
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    43
        p1 = self.im.parents[0].node()
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    44
        p2 = self.im.parents[1].node()
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    45
        if self.im.repo.dirstate.parents()[0] != p1:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    46
            hg.clean(self.im.repo, self.im.parents[0].node())
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    47
        self.im.start(p2)
5055
56d48aed1f69 imerge: tarfile.extractall is only available in python2.5
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 5054
diff changeset
    48
        for tarinfo in tf:
56d48aed1f69 imerge: tarfile.extractall is only available in python2.5
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 5054
diff changeset
    49
            tf.extract(tarinfo, self.im.repo.root)
5044
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    50
        self.im.load()
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    51
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    52
class Imerge(object):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    53
    def __init__(self, ui, repo):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    54
        self.ui = ui
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    55
        self.repo = repo
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    56
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    57
        self.path = repo.join('imerge')
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    58
        self.opener = util.opener(self.path)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    59
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    60
        self.parents = [self.repo.changectx(n)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    61
                        for n in self.repo.dirstate.parents()]
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    62
        self.conflicts = {}
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    63
        self.resolved = []
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    64
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    65
    def merging(self):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    66
        return self.parents[1].node() != nullid
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    67
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    68
    def load(self):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    69
        # status format. \0-delimited file, fields are
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    70
        # p1, p2, conflict count, conflict filenames, resolved filenames
5054
ec70fd08e16c Update imerge for new filemerge interface
Brendan Cully <brendan@kublai.com>
parents: 5044
diff changeset
    71
        # conflict filenames are tuples of localname, remoteorig, remotenew
5044
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    72
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    73
        statusfile = self.opener('status')
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    74
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    75
        status = statusfile.read().split('\0')
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    76
        if len(status) < 3:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    77
            raise util.Abort('invalid imerge status file')
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    78
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    79
        try:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    80
            self.parents = [self.repo.changectx(n) for n in status[:2]]
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    81
        except LookupError:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    82
            raise util.Abort('merge parent %s not in repository' % short(p))
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    83
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    84
        status = status[2:]
5054
ec70fd08e16c Update imerge for new filemerge interface
Brendan Cully <brendan@kublai.com>
parents: 5044
diff changeset
    85
        conflicts = int(status.pop(0)) * 3
5044
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    86
        self.resolved = status[conflicts:]
5054
ec70fd08e16c Update imerge for new filemerge interface
Brendan Cully <brendan@kublai.com>
parents: 5044
diff changeset
    87
        for i in xrange(0, conflicts, 3):
ec70fd08e16c Update imerge for new filemerge interface
Brendan Cully <brendan@kublai.com>
parents: 5044
diff changeset
    88
            self.conflicts[status[i]] = (status[i+1], status[i+2])
5044
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    89
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    90
    def save(self):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    91
        lock = self.repo.lock()
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    92
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    93
        if not os.path.isdir(self.path):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    94
            os.mkdir(self.path)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    95
        fd = self.opener('status', 'wb')
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    96
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    97
        out = [hex(n.node()) for n in self.parents]
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    98
        out.append(str(len(self.conflicts)))
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    99
        for f in sorted(self.conflicts):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   100
            out.append(f)
5054
ec70fd08e16c Update imerge for new filemerge interface
Brendan Cully <brendan@kublai.com>
parents: 5044
diff changeset
   101
            out.extend(self.conflicts[f])
5044
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   102
        out.extend(self.resolved)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   103
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   104
        fd.write('\0'.join(out))
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   105
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   106
    def remaining(self):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   107
        return [f for f in self.conflicts if f not in self.resolved]
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   108
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   109
    def filemerge(self, fn):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   110
        wlock = self.repo.wlock()
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   111
5054
ec70fd08e16c Update imerge for new filemerge interface
Brendan Cully <brendan@kublai.com>
parents: 5044
diff changeset
   112
        (fd, fo) = self.conflicts[fn]
ec70fd08e16c Update imerge for new filemerge interface
Brendan Cully <brendan@kublai.com>
parents: 5044
diff changeset
   113
        return merge.filemerge(self.repo, fn, fd, fo, self.parents[0],
5044
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   114
                               self.parents[1])
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   115
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   116
    def start(self, rev=None):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   117
        _filemerge = merge.filemerge
5054
ec70fd08e16c Update imerge for new filemerge interface
Brendan Cully <brendan@kublai.com>
parents: 5044
diff changeset
   118
        def filemerge(repo, fw, fd, fo, wctx, mctx):
ec70fd08e16c Update imerge for new filemerge interface
Brendan Cully <brendan@kublai.com>
parents: 5044
diff changeset
   119
            self.conflicts[fw] = (fd, fo)
5044
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   120
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   121
        merge.filemerge = filemerge
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   122
        commands.merge(self.ui, self.repo, rev=rev)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   123
        merge.filemerge = _filemerge
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   124
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   125
        self.parents = [self.repo.changectx(n)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   126
                        for n in self.repo.dirstate.parents()]
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   127
        self.save()
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   128
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   129
    def resume(self):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   130
        self.load()
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   131
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   132
        dp = self.repo.dirstate.parents()
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   133
        if self.parents[0].node() != dp[0] or self.parents[1].node() != dp[1]:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   134
            raise util.Abort('imerge state does not match working directory')
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   135
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   136
    def status(self):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   137
        self.ui.write('merging %s and %s\n' % \
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   138
                      (short(self.parents[0].node()),
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   139
                       short(self.parents[1].node())))
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   140
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   141
        if self.resolved:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   142
            self.ui.write('resolved:\n')
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   143
            for fn in self.resolved:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   144
                self.ui.write('  %s\n' % fn)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   145
        remaining = [f for f in self.conflicts if f not in self.resolved]
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   146
        if remaining:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   147
            self.ui.write('remaining:\n')
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   148
            for fn in remaining:
5054
ec70fd08e16c Update imerge for new filemerge interface
Brendan Cully <brendan@kublai.com>
parents: 5044
diff changeset
   149
                (fd, fo) = self.conflicts[fn]
5044
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   150
                if fn == fo:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   151
                    self.ui.write('  %s\n' % (fn,))
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   152
                else:
5054
ec70fd08e16c Update imerge for new filemerge interface
Brendan Cully <brendan@kublai.com>
parents: 5044
diff changeset
   153
                    self.ui.write('  %s (%s)\n' % (fn, fd))
5044
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   154
        else:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   155
            self.ui.write('all conflicts resolved\n')
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   156
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   157
    def next(self):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   158
        remaining = self.remaining()
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   159
        return remaining and remaining[0]
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   160
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   161
    def resolve(self, files):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   162
        resolved = dict.fromkeys(self.resolved)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   163
        for fn in files:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   164
            if fn not in self.conflicts:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   165
                raise util.Abort('%s is not in the merge set' % fn)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   166
            resolved[fn] = True
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   167
        self.resolved = sorted(resolved)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   168
        self.save()
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   169
        return 0
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   170
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   171
    def unresolve(self, files):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   172
        resolved = dict.fromkeys(self.resolved)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   173
        for fn in files:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   174
            if fn not in resolved:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   175
                raise util.Abort('%s is not resolved' % fn)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   176
            del resolved[fn]
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   177
        self.resolved = sorted(resolved)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   178
        self.save()
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   179
        return 0
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   180
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   181
    def pickle(self, dest):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   182
        '''write current merge state to file to be resumed elsewhere'''
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   183
        state = ImergeStateFile(self)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   184
        return state.save(dest)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   185
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   186
    def unpickle(self, source):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   187
        '''read merge state from file'''
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   188
        state = ImergeStateFile(self)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   189
        return state.load(source)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   190
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   191
def load(im, source):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   192
    if im.merging():
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   193
        raise util.Abort('there is already a merge in progress '
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   194
                         '(update -C <rev> to abort it)' )
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   195
    m, a, r, d =  im.repo.status()[:4]
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   196
    if m or a or r or d:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   197
        raise util.Abort('working directory has uncommitted changes')
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   198
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   199
    rc = im.unpickle(source)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   200
    if not rc:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   201
        im.status()
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   202
    return rc
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   203
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   204
def merge_(im, filename=None):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   205
    if not filename:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   206
        filename = im.next()
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   207
        if not filename:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   208
            im.ui.write('all conflicts resolved\n')
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   209
            return 0
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   210
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   211
    rc = im.filemerge(filename)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   212
    if not rc:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   213
        im.resolve([filename])
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   214
        if not im.next():
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   215
            im.ui.write('all conflicts resolved\n')
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   216
            return 0
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   217
    return rc
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   218
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   219
def next(im):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   220
    n = im.next()
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   221
    if n:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   222
        im.ui.write('%s\n' % n)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   223
    else:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   224
        im.ui.write('all conflicts resolved\n')
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   225
    return 0
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   226
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   227
def resolve(im, *files):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   228
    if not files:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   229
        raise util.Abort('resolve requires at least one filename')
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   230
    return im.resolve(files)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   231
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   232
def save(im, dest):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   233
    return im.pickle(dest)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   234
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   235
def status(im):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   236
    im.status()
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   237
    return 0
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   238
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   239
def unresolve(im, *files):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   240
    if not files:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   241
        raise util.Abort('unresolve requires at least one filename')
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   242
    return im.unresolve(files)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   243
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   244
subcmdtable = {
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   245
    'load': load,
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   246
    'merge': merge_,
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   247
    'next': next,
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   248
    'resolve': resolve,
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   249
    'save': save,
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   250
    'status': status,
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   251
    'unresolve': unresolve
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   252
}
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   253
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   254
def dispatch(im, args, opts):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   255
    def complete(s, choices):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   256
        candidates = []
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   257
        for choice in choices:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   258
            if choice.startswith(s):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   259
                candidates.append(choice)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   260
        return candidates
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   261
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   262
    c, args = args[0], args[1:]
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   263
    cmd = complete(c, subcmdtable.keys())
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   264
    if not cmd:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   265
        raise cmdutil.UnknownCommand('imerge ' + c)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   266
    if len(cmd) > 1:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   267
        raise cmdutil.AmbiguousCommand('imerge ' + c, sorted(cmd))
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   268
    cmd = cmd[0]
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   269
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   270
    func = subcmdtable[cmd]
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   271
    try:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   272
        return func(im, *args)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   273
    except TypeError:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   274
        raise cmdutil.ParseError('imerge', '%s: invalid arguments' % cmd)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   275
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   276
def imerge(ui, repo, *args, **opts):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   277
    '''interactive merge
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   278
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   279
    imerge lets you split a merge into pieces. When you start a merge
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   280
    with imerge, the names of all files with conflicts are recorded.
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   281
    You can then merge any of these files, and if the merge is
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   282
    successful, they will be marked as resolved. When all files are
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   283
    resolved, the merge is complete.
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   284
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   285
    If no merge is in progress, hg imerge [rev] will merge the working
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   286
    directory with rev (defaulting to the other head if the repository
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   287
    only has two heads). You may also resume a saved merge with
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   288
    hg imerge load <file>.
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   289
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   290
    If a merge is in progress, hg imerge will default to merging the
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   291
    next unresolved file.
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   292
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   293
    The following subcommands are available:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   294
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   295
    status:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   296
      show the current state of the merge
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   297
    next:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   298
      show the next unresolved file merge
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   299
    merge [<file>]:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   300
      merge <file>. If the file merge is successful, the file will be
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   301
      recorded as resolved. If no file is given, the next unresolved
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   302
      file will be merged.
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   303
    resolve <file>...:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   304
      mark files as successfully merged
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   305
    unresolve <file>...:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   306
      mark files as requiring merging.
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   307
    save <file>:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   308
      save the state of the merge to a file to be resumed elsewhere
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   309
    load <file>:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   310
      load the state of the merge from a file created by save
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   311
    '''
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   312
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   313
    im = Imerge(ui, repo)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   314
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   315
    if im.merging():
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   316
        im.resume()
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   317
    else:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   318
        rev = opts.get('rev')
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   319
        if rev and args:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   320
            raise util.Abort('please specify just one revision')
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   321
        
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   322
        if len(args) == 2 and args[0] == 'load':
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   323
            pass
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   324
        else:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   325
            if args:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   326
                rev = args[0]
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   327
            im.start(rev=rev)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   328
            args = ['status']
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   329
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   330
    if not args:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   331
        args = ['merge']
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   332
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   333
    return dispatch(im, args, opts)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   334
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   335
cmdtable = {
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   336
    '^imerge':
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   337
    (imerge,
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   338
     [('r', 'rev', '', _('revision to merge'))], 'hg imerge [command]')
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   339
}