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