hgext/imerge.py
author Alexis S. L. Carvalho <alexis@cecm.usp.br>
Fri, 08 Feb 2008 18:31:55 -0200
changeset 6048 cfb4a51da7d5
parent 6003 7855b88ba838
child 6211 f89fd07fc51d
permissions -rw-r--r--
Merge with crew-stable
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 *
6003
7855b88ba838 filemerge: pull file-merging code into its own module
Matt Mackall <mpm@selenic.com>
parents: 5241
diff changeset
    10
from mercurial import commands, cmdutil, dispatch, fancyopts
7855b88ba838 filemerge: pull file-merging code into its own module
Matt Mackall <mpm@selenic.com>
parents: 5241
diff changeset
    11
from mercurial import hg, filemerge, util
5044
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    12
import os, tarfile
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    13
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    14
class InvalidStateFileException(Exception): pass
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    15
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    16
class ImergeStateFile(object):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    17
    def __init__(self, im):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    18
        self.im = im
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    19
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    20
    def save(self, dest):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    21
        tf = tarfile.open(dest, 'w:gz')
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    22
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    23
        st = os.path.join(self.im.path, 'status')
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    24
        tf.add(st, os.path.join('.hg', 'imerge', 'status'))
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    25
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    26
        for f in self.im.resolved:
5109
86327d13d916 imerge: handle renames
Brendan Cully <brendan@kublai.com>
parents: 5054
diff changeset
    27
            (fd, fo) = self.im.conflicts[f]
86327d13d916 imerge: handle renames
Brendan Cully <brendan@kublai.com>
parents: 5054
diff changeset
    28
            abssrc = self.im.repo.wjoin(fd)
86327d13d916 imerge: handle renames
Brendan Cully <brendan@kublai.com>
parents: 5054
diff changeset
    29
            tf.add(abssrc, fd)
5044
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
        tf.close()
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    32
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    33
    def load(self, source):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    34
        wlock = self.im.repo.wlock()
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    35
        lock = self.im.repo.lock()
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    36
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    37
        tf = tarfile.open(source, 'r')
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    38
        contents = tf.getnames()
5164
1d5ebb0d366f imerge: fix status file lookups
Patrick Mezard <pmezard@gmail.com>
parents: 5143
diff changeset
    39
        # tarfile normalizes path separators to '/'
5165
ec24bfd852ab imerge: simplify 1d5ebb0d366f
Brendan Cully <brendan@kublai.com>
parents: 5164
diff changeset
    40
        statusfile = '.hg/imerge/status'
5044
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    41
        if statusfile not in contents:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    42
            raise InvalidStateFileException('no status file')
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    43
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    44
        tf.extract(statusfile, self.im.repo.root)
5109
86327d13d916 imerge: handle renames
Brendan Cully <brendan@kublai.com>
parents: 5054
diff changeset
    45
        p1, p2 = self.im.load()
86327d13d916 imerge: handle renames
Brendan Cully <brendan@kublai.com>
parents: 5054
diff changeset
    46
        if self.im.repo.dirstate.parents()[0] != p1.node():
86327d13d916 imerge: handle renames
Brendan Cully <brendan@kublai.com>
parents: 5054
diff changeset
    47
            hg.clean(self.im.repo, p1.node())
86327d13d916 imerge: handle renames
Brendan Cully <brendan@kublai.com>
parents: 5054
diff changeset
    48
        self.im.start(p2.node())
5055
56d48aed1f69 imerge: tarfile.extractall is only available in python2.5
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 5054
diff changeset
    49
        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
    50
            tf.extract(tarinfo, self.im.repo.root)
5044
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    51
        self.im.load()
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    52
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    53
class Imerge(object):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    54
    def __init__(self, ui, repo):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    55
        self.ui = ui
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    56
        self.repo = repo
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    57
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    58
        self.path = repo.join('imerge')
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    59
        self.opener = util.opener(self.path)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    60
5109
86327d13d916 imerge: handle renames
Brendan Cully <brendan@kublai.com>
parents: 5054
diff changeset
    61
        self.wctx = self.repo.workingctx()
5044
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):
5109
86327d13d916 imerge: handle renames
Brendan Cully <brendan@kublai.com>
parents: 5054
diff changeset
    66
        return len(self.wctx.parents()) > 1
5044
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:
5109
86327d13d916 imerge: handle renames
Brendan Cully <brendan@kublai.com>
parents: 5054
diff changeset
    80
            parents = [self.repo.changectx(n) for n in status[:2]]
5044
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
5109
86327d13d916 imerge: handle renames
Brendan Cully <brendan@kublai.com>
parents: 5054
diff changeset
    90
        return parents
86327d13d916 imerge: handle renames
Brendan Cully <brendan@kublai.com>
parents: 5054
diff changeset
    91
5044
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    92
    def save(self):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    93
        lock = self.repo.lock()
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    94
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    95
        if not os.path.isdir(self.path):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    96
            os.mkdir(self.path)
5056
34a54cc5df1b imerge: sorted() is only available in python2.4 and above
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5055
diff changeset
    97
        statusfile = self.opener('status', 'wb')
5044
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
    98
5109
86327d13d916 imerge: handle renames
Brendan Cully <brendan@kublai.com>
parents: 5054
diff changeset
    99
        out = [hex(n.node()) for n in self.wctx.parents()]
5044
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   100
        out.append(str(len(self.conflicts)))
5056
34a54cc5df1b imerge: sorted() is only available in python2.4 and above
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5055
diff changeset
   101
        conflicts = self.conflicts.items()
34a54cc5df1b imerge: sorted() is only available in python2.4 and above
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5055
diff changeset
   102
        conflicts.sort()
34a54cc5df1b imerge: sorted() is only available in python2.4 and above
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5055
diff changeset
   103
        for fw, fd_fo in conflicts:
34a54cc5df1b imerge: sorted() is only available in python2.4 and above
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5055
diff changeset
   104
            out.append(fw)
34a54cc5df1b imerge: sorted() is only available in python2.4 and above
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5055
diff changeset
   105
            out.extend(fd_fo)
5044
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   106
        out.extend(self.resolved)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   107
5056
34a54cc5df1b imerge: sorted() is only available in python2.4 and above
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5055
diff changeset
   108
        statusfile.write('\0'.join(out))
5044
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   109
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   110
    def remaining(self):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   111
        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
   112
5241
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   113
    def filemerge(self, fn, interactive=True):
5044
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   114
        wlock = self.repo.wlock()
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   115
5054
ec70fd08e16c Update imerge for new filemerge interface
Brendan Cully <brendan@kublai.com>
parents: 5044
diff changeset
   116
        (fd, fo) = self.conflicts[fn]
5239
8860f29447c1 imerge: fix ancestor calculation
Brendan Cully <brendan@kublai.com>
parents: 5165
diff changeset
   117
        p1, p2 = self.wctx.parents()
5241
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   118
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   119
        # this could be greatly improved
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   120
        realmerge = os.environ.get('HGMERGE')
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   121
        if not interactive:
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   122
            os.environ['HGMERGE'] = 'merge'
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   123
5239
8860f29447c1 imerge: fix ancestor calculation
Brendan Cully <brendan@kublai.com>
parents: 5165
diff changeset
   124
        # The filemerge ancestor algorithm does not work if self.wctx
8860f29447c1 imerge: fix ancestor calculation
Brendan Cully <brendan@kublai.com>
parents: 5165
diff changeset
   125
        # already has two parents (in normal merge it doesn't yet). But
8860f29447c1 imerge: fix ancestor calculation
Brendan Cully <brendan@kublai.com>
parents: 5165
diff changeset
   126
        # this is very dirty.
8860f29447c1 imerge: fix ancestor calculation
Brendan Cully <brendan@kublai.com>
parents: 5165
diff changeset
   127
        self.wctx._parents.pop()
8860f29447c1 imerge: fix ancestor calculation
Brendan Cully <brendan@kublai.com>
parents: 5165
diff changeset
   128
        try:
5241
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   129
            # TODO: we should probably revert the file if merge fails
6003
7855b88ba838 filemerge: pull file-merging code into its own module
Matt Mackall <mpm@selenic.com>
parents: 5241
diff changeset
   130
            return filemerge.filemerge(self.repo, fn, fd, fo, self.wctx, p2)
5239
8860f29447c1 imerge: fix ancestor calculation
Brendan Cully <brendan@kublai.com>
parents: 5165
diff changeset
   131
        finally:
8860f29447c1 imerge: fix ancestor calculation
Brendan Cully <brendan@kublai.com>
parents: 5165
diff changeset
   132
            self.wctx._parents.append(p2)
5241
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   133
            if realmerge:
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   134
                os.environ['HGMERGE'] = realmerge
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   135
            elif not interactive:
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   136
                del os.environ['HGMERGE']
5044
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   137
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   138
    def start(self, rev=None):
6003
7855b88ba838 filemerge: pull file-merging code into its own module
Matt Mackall <mpm@selenic.com>
parents: 5241
diff changeset
   139
        _filemerge = filemerge.filemerge
7855b88ba838 filemerge: pull file-merging code into its own module
Matt Mackall <mpm@selenic.com>
parents: 5241
diff changeset
   140
        def filemerge_(repo, fw, fd, fo, wctx, mctx):
5054
ec70fd08e16c Update imerge for new filemerge interface
Brendan Cully <brendan@kublai.com>
parents: 5044
diff changeset
   141
            self.conflicts[fw] = (fd, fo)
5044
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   142
6003
7855b88ba838 filemerge: pull file-merging code into its own module
Matt Mackall <mpm@selenic.com>
parents: 5241
diff changeset
   143
        filemerge.filemerge = filemerge_
5044
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   144
        commands.merge(self.ui, self.repo, rev=rev)
6003
7855b88ba838 filemerge: pull file-merging code into its own module
Matt Mackall <mpm@selenic.com>
parents: 5241
diff changeset
   145
        filemerge.filemerge = _filemerge
5044
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   146
5109
86327d13d916 imerge: handle renames
Brendan Cully <brendan@kublai.com>
parents: 5054
diff changeset
   147
        self.wctx = self.repo.workingctx()
5044
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   148
        self.save()
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   149
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   150
    def resume(self):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   151
        self.load()
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   152
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   153
        dp = self.repo.dirstate.parents()
5109
86327d13d916 imerge: handle renames
Brendan Cully <brendan@kublai.com>
parents: 5054
diff changeset
   154
        p1, p2 = self.wctx.parents()
86327d13d916 imerge: handle renames
Brendan Cully <brendan@kublai.com>
parents: 5054
diff changeset
   155
        if p1.node() != dp[0] or p2.node() != dp[1]:
5044
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   156
            raise util.Abort('imerge state does not match working directory')
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   157
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   158
    def next(self):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   159
        remaining = self.remaining()
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   160
        return remaining and remaining[0]
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   161
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   162
    def resolve(self, files):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   163
        resolved = dict.fromkeys(self.resolved)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   164
        for fn in files:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   165
            if fn not in self.conflicts:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   166
                raise util.Abort('%s is not in the merge set' % fn)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   167
            resolved[fn] = True
5056
34a54cc5df1b imerge: sorted() is only available in python2.4 and above
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5055
diff changeset
   168
        self.resolved = resolved.keys()
34a54cc5df1b imerge: sorted() is only available in python2.4 and above
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5055
diff changeset
   169
        self.resolved.sort()
5044
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   170
        self.save()
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   171
        return 0
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   172
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   173
    def unresolve(self, files):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   174
        resolved = dict.fromkeys(self.resolved)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   175
        for fn in files:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   176
            if fn not in resolved:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   177
                raise util.Abort('%s is not resolved' % fn)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   178
            del resolved[fn]
5056
34a54cc5df1b imerge: sorted() is only available in python2.4 and above
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5055
diff changeset
   179
        self.resolved = resolved.keys()
34a54cc5df1b imerge: sorted() is only available in python2.4 and above
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5055
diff changeset
   180
        self.resolved.sort()
5044
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   181
        self.save()
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   182
        return 0
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   183
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   184
    def pickle(self, dest):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   185
        '''write current merge state to file to be resumed elsewhere'''
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   186
        state = ImergeStateFile(self)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   187
        return state.save(dest)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   188
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   189
    def unpickle(self, source):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   190
        '''read merge state from file'''
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   191
        state = ImergeStateFile(self)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   192
        return state.load(source)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   193
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   194
def load(im, source):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   195
    if im.merging():
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   196
        raise util.Abort('there is already a merge in progress '
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   197
                         '(update -C <rev> to abort it)' )
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   198
    m, a, r, d =  im.repo.status()[:4]
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   199
    if m or a or r or d:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   200
        raise util.Abort('working directory has uncommitted changes')
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   201
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   202
    rc = im.unpickle(source)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   203
    if not rc:
5111
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   204
        status(im)
5044
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   205
    return rc
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   206
5241
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   207
def merge_(im, filename=None, auto=False):
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   208
    success = True
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   209
    if auto and not filename:
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   210
        for fn in im.remaining():
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   211
            rc = im.filemerge(fn, interactive=False)
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   212
            if rc:
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   213
                success = False
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   214
            else:
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   215
                im.resolve([fn])
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   216
        if success:
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   217
            im.ui.write('all conflicts resolved\n')
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   218
        else:
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   219
            status(im)
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   220
        return 0
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   221
5044
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   222
    if not filename:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   223
        filename = im.next()
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   224
        if not filename:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   225
            im.ui.write('all conflicts resolved\n')
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   226
            return 0
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   227
5241
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   228
    rc = im.filemerge(filename, interactive=not auto)
5044
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   229
    if not rc:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   230
        im.resolve([filename])
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   231
        if not im.next():
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   232
            im.ui.write('all conflicts resolved\n')
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   233
    return rc
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 next(im):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   236
    n = im.next()
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   237
    if n:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   238
        im.ui.write('%s\n' % n)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   239
    else:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   240
        im.ui.write('all conflicts resolved\n')
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   241
    return 0
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
def resolve(im, *files):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   244
    if not files:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   245
        raise util.Abort('resolve requires at least one filename')
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   246
    return im.resolve(files)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   247
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   248
def save(im, dest):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   249
    return im.pickle(dest)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   250
5111
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   251
def status(im, **opts):
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   252
    if not opts.get('resolved') and not opts.get('unresolved'):
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   253
        opts['resolved'] = True
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   254
        opts['unresolved'] = True
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   255
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   256
    if im.ui.verbose:
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   257
        p1, p2 = [short(p.node()) for p in im.wctx.parents()]
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   258
        im.ui.note(_('merging %s and %s\n') % (p1, p2))
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   259
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   260
    conflicts = im.conflicts.keys()
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   261
    conflicts.sort()
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   262
    remaining = dict.fromkeys(im.remaining())
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   263
    st = []
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   264
    for fn in conflicts:
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   265
        if opts.get('no_status'):
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   266
            mode = ''
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   267
        elif fn in remaining:
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   268
            mode = 'U '
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   269
        else:
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   270
            mode = 'R '
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   271
        if ((opts.get('resolved') and fn not in remaining)
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   272
            or (opts.get('unresolved') and fn in remaining)):
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   273
            st.append((mode, fn))
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   274
    st.sort()
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   275
    for (mode, fn) in st:
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   276
        if im.ui.verbose:
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   277
            fo, fd = im.conflicts[fn]
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   278
            if fd != fn:
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   279
                fn = '%s (%s)' % (fn, fd)
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   280
        im.ui.write('%s%s\n' % (mode, fn))
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   281
    if opts.get('unresolved') and not remaining:
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   282
        im.ui.write(_('all conflicts resolved\n'))
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   283
5044
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   284
    return 0
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   285
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   286
def unresolve(im, *files):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   287
    if not files:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   288
        raise util.Abort('unresolve requires at least one filename')
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   289
    return im.unresolve(files)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   290
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   291
subcmdtable = {
5111
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   292
    'load': (load, []),
5241
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   293
    'merge':
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   294
        (merge_,
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   295
         [('a', 'auto', None, _('automatically resolve if possible'))]),
5111
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   296
    'next': (next, []),
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   297
    'resolve': (resolve, []),
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   298
    'save': (save, []),
5241
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   299
    'status':
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   300
        (status,
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   301
         [('n', 'no-status', None, _('hide status prefix')),
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   302
          ('', 'resolved', None, _('only show resolved conflicts')),
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   303
          ('', 'unresolved', None, _('only show unresolved conflicts'))]),
5111
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   304
    'unresolve': (unresolve, [])
5044
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   305
}
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   306
5239
8860f29447c1 imerge: fix ancestor calculation
Brendan Cully <brendan@kublai.com>
parents: 5165
diff changeset
   307
def dispatch_(im, args, opts):
5044
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   308
    def complete(s, choices):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   309
        candidates = []
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   310
        for choice in choices:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   311
            if choice.startswith(s):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   312
                candidates.append(choice)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   313
        return candidates
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   314
5111
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   315
    c, args = args[0], list(args[1:])
5044
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   316
    cmd = complete(c, subcmdtable.keys())
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   317
    if not cmd:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   318
        raise cmdutil.UnknownCommand('imerge ' + c)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   319
    if len(cmd) > 1:
5056
34a54cc5df1b imerge: sorted() is only available in python2.4 and above
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5055
diff changeset
   320
        cmd.sort()
34a54cc5df1b imerge: sorted() is only available in python2.4 and above
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5055
diff changeset
   321
        raise cmdutil.AmbiguousCommand('imerge ' + c, cmd)
5044
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   322
    cmd = cmd[0]
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   323
5111
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   324
    func, optlist = subcmdtable[cmd]
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   325
    opts = {}
5044
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   326
    try:
5111
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   327
        args = fancyopts.fancyopts(args, optlist, opts)
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   328
        return func(im, *args, **opts)
12930b97a729 imerge: gussy up dispatcher to support subcommand opts.
Brendan Cully <brendan@kublai.com>
parents: 5110
diff changeset
   329
    except fancyopts.getopt.GetoptError, inst:
5239
8860f29447c1 imerge: fix ancestor calculation
Brendan Cully <brendan@kublai.com>
parents: 5165
diff changeset
   330
        raise dispatch.ParseError('imerge', '%s: %s' % (cmd, inst))
5044
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   331
    except TypeError:
5239
8860f29447c1 imerge: fix ancestor calculation
Brendan Cully <brendan@kublai.com>
parents: 5165
diff changeset
   332
        raise dispatch.ParseError('imerge', _('%s: invalid arguments') % cmd)
5044
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
def imerge(ui, repo, *args, **opts):
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   335
    '''interactive merge
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   336
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   337
    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
   338
    with imerge, the names of all files with conflicts are recorded.
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   339
    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
   340
    successful, they will be marked as resolved. When all files are
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   341
    resolved, the merge is complete.
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   342
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   343
    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
   344
    directory with rev (defaulting to the other head if the repository
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   345
    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
   346
    hg imerge load <file>.
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   347
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   348
    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
   349
    next unresolved file.
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   350
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   351
    The following subcommands are available:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   352
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   353
    status:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   354
      show the current state of the merge
5239
8860f29447c1 imerge: fix ancestor calculation
Brendan Cully <brendan@kublai.com>
parents: 5165
diff changeset
   355
      options:
8860f29447c1 imerge: fix ancestor calculation
Brendan Cully <brendan@kublai.com>
parents: 5165
diff changeset
   356
        -n --no-status:  do not print the status prefix
8860f29447c1 imerge: fix ancestor calculation
Brendan Cully <brendan@kublai.com>
parents: 5165
diff changeset
   357
           --resolved:   only print resolved conflicts
8860f29447c1 imerge: fix ancestor calculation
Brendan Cully <brendan@kublai.com>
parents: 5165
diff changeset
   358
           --unresolved: only print unresolved conflicts
5044
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   359
    next:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   360
      show the next unresolved file merge
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   361
    merge [<file>]:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   362
      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
   363
      recorded as resolved. If no file is given, the next unresolved
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   364
      file will be merged.
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   365
    resolve <file>...:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   366
      mark files as successfully merged
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   367
    unresolve <file>...:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   368
      mark files as requiring merging.
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   369
    save <file>:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   370
      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
   371
    load <file>:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   372
      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
   373
    '''
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   374
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   375
    im = Imerge(ui, repo)
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   376
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   377
    if im.merging():
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   378
        im.resume()
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   379
    else:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   380
        rev = opts.get('rev')
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   381
        if rev and args:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   382
            raise util.Abort('please specify just one revision')
5143
d4fa6bafc43a Remove trailing spaces, fix indentation
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5111
diff changeset
   383
5044
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   384
        if len(args) == 2 and args[0] == 'load':
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   385
            pass
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   386
        else:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   387
            if args:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   388
                rev = args[0]
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   389
            im.start(rev=rev)
5241
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   390
            if opts.get('auto'):
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   391
                args = ['merge', '--auto']
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   392
            else:
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   393
                args = ['status']
5044
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   394
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   395
    if not args:
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   396
        args = ['merge']
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   397
5239
8860f29447c1 imerge: fix ancestor calculation
Brendan Cully <brendan@kublai.com>
parents: 5165
diff changeset
   398
    return dispatch_(im, args, opts)
5044
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   399
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   400
cmdtable = {
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   401
    '^imerge':
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   402
    (imerge,
5241
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   403
     [('r', 'rev', '', _('revision to merge')),
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   404
      ('a', 'auto', None, _('automatically merge where possible'))],
3786ef8877d5 imerge: add automerge flag to attempt to batch merge all conflicts
Brendan Cully <brendan@kublai.com>
parents: 5239
diff changeset
   405
      'hg imerge [command]')
5044
58006f8b8275 imerge extension and test
Brendan Cully <brendan@kublai.com>
parents:
diff changeset
   406
}