mercurial/merge.py
author Matt Mackall <mpm@selenic.com>
Tue, 10 Oct 2006 02:15:20 -0500
changeset 3314 b16456909a0a
parent 3312 c5075ad5e3e9
child 3315 38be819a1225
permissions -rwxr-xr-x
merge: various tidying update tests to handle corrected output and new workingctx display
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2775
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     1
# merge.py - directory-level update/merge handling for Mercurial
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     2
#
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     3
# Copyright 2006 Matt Mackall <mpm@selenic.com>
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     4
#
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     5
# This software may be used and distributed according to the terms
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     6
# of the GNU General Public License, incorporated herein by reference.
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     7
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     8
from node import *
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     9
from i18n import gettext as _
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    10
from demandload import *
3018
db3f42261452 fix errors reported by pychecker
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 2978
diff changeset
    11
demandload(globals(), "errno util os tempfile")
2775
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    12
3309
e8be5942335d merge: pull file copy/move out of filemerge
Matt Mackall <mpm@selenic.com>
parents: 3308
diff changeset
    13
def filemerge(repo, fw, fo, wctx, mctx):
3211
3fd098e0902d merge: extend file merge function for renames
Matt Mackall <mpm@selenic.com>
parents: 3169
diff changeset
    14
    """perform a 3-way merge in the working directory
2775
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    15
3211
3fd098e0902d merge: extend file merge function for renames
Matt Mackall <mpm@selenic.com>
parents: 3169
diff changeset
    16
    fw = filename in the working directory and first parent
3fd098e0902d merge: extend file merge function for renames
Matt Mackall <mpm@selenic.com>
parents: 3169
diff changeset
    17
    fo = filename in other parent
3297
69b9471f26bb merge: pass contexts to applyupdates
Matt Mackall <mpm@selenic.com>
parents: 3296
diff changeset
    18
    wctx, mctx = working and merge changecontexts
3211
3fd098e0902d merge: extend file merge function for renames
Matt Mackall <mpm@selenic.com>
parents: 3169
diff changeset
    19
3fd098e0902d merge: extend file merge function for renames
Matt Mackall <mpm@selenic.com>
parents: 3169
diff changeset
    20
    TODO:
3fd098e0902d merge: extend file merge function for renames
Matt Mackall <mpm@selenic.com>
parents: 3169
diff changeset
    21
      if fw is copied in the working directory, we get confused
3fd098e0902d merge: extend file merge function for renames
Matt Mackall <mpm@selenic.com>
parents: 3169
diff changeset
    22
      implement move and fd
3fd098e0902d merge: extend file merge function for renames
Matt Mackall <mpm@selenic.com>
parents: 3169
diff changeset
    23
    """
3fd098e0902d merge: extend file merge function for renames
Matt Mackall <mpm@selenic.com>
parents: 3169
diff changeset
    24
3fd098e0902d merge: extend file merge function for renames
Matt Mackall <mpm@selenic.com>
parents: 3169
diff changeset
    25
    def temp(prefix, ctx):
3fd098e0902d merge: extend file merge function for renames
Matt Mackall <mpm@selenic.com>
parents: 3169
diff changeset
    26
        pre = "%s~%s." % (os.path.basename(ctx.path()), prefix)
2775
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    27
        (fd, name) = tempfile.mkstemp(prefix=pre)
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    28
        f = os.fdopen(fd, "wb")
3211
3fd098e0902d merge: extend file merge function for renames
Matt Mackall <mpm@selenic.com>
parents: 3169
diff changeset
    29
        repo.wwrite(ctx.path(), ctx.data(), f)
2775
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    30
        f.close()
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    31
        return name
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    32
3299
e7abcf3a7c5f filemerge: use contexts rather than my and other
Matt Mackall <mpm@selenic.com>
parents: 3297
diff changeset
    33
    fcm = wctx.filectx(fw)
e7abcf3a7c5f filemerge: use contexts rather than my and other
Matt Mackall <mpm@selenic.com>
parents: 3297
diff changeset
    34
    fco = mctx.filectx(fo)
3311
966632304dde merge: shortcircuit filemerge for identical files
Matt Mackall <mpm@selenic.com>
parents: 3309
diff changeset
    35
966632304dde merge: shortcircuit filemerge for identical files
Matt Mackall <mpm@selenic.com>
parents: 3309
diff changeset
    36
    if not fco.cmp(fcm.data()): # files identical?
966632304dde merge: shortcircuit filemerge for identical files
Matt Mackall <mpm@selenic.com>
parents: 3309
diff changeset
    37
        return 0
966632304dde merge: shortcircuit filemerge for identical files
Matt Mackall <mpm@selenic.com>
parents: 3309
diff changeset
    38
3211
3fd098e0902d merge: extend file merge function for renames
Matt Mackall <mpm@selenic.com>
parents: 3169
diff changeset
    39
    fca = fcm.ancestor(fco)
3fd098e0902d merge: extend file merge function for renames
Matt Mackall <mpm@selenic.com>
parents: 3169
diff changeset
    40
    if not fca:
3fd098e0902d merge: extend file merge function for renames
Matt Mackall <mpm@selenic.com>
parents: 3169
diff changeset
    41
        fca = repo.filectx(fw, fileid=-1)
3fd098e0902d merge: extend file merge function for renames
Matt Mackall <mpm@selenic.com>
parents: 3169
diff changeset
    42
    a = repo.wjoin(fw)
3fd098e0902d merge: extend file merge function for renames
Matt Mackall <mpm@selenic.com>
parents: 3169
diff changeset
    43
    b = temp("base", fca)
3fd098e0902d merge: extend file merge function for renames
Matt Mackall <mpm@selenic.com>
parents: 3169
diff changeset
    44
    c = temp("other", fco)
2775
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    45
3311
966632304dde merge: shortcircuit filemerge for identical files
Matt Mackall <mpm@selenic.com>
parents: 3309
diff changeset
    46
    if fw != fo:
966632304dde merge: shortcircuit filemerge for identical files
Matt Mackall <mpm@selenic.com>
parents: 3309
diff changeset
    47
        repo.ui.status(_("merging %s and %s\n") % (fw, fo))
966632304dde merge: shortcircuit filemerge for identical files
Matt Mackall <mpm@selenic.com>
parents: 3309
diff changeset
    48
    else:
966632304dde merge: shortcircuit filemerge for identical files
Matt Mackall <mpm@selenic.com>
parents: 3309
diff changeset
    49
        repo.ui.status(_("merging %s\n") % fw)
966632304dde merge: shortcircuit filemerge for identical files
Matt Mackall <mpm@selenic.com>
parents: 3309
diff changeset
    50
3211
3fd098e0902d merge: extend file merge function for renames
Matt Mackall <mpm@selenic.com>
parents: 3169
diff changeset
    51
    repo.ui.debug(_("my %s other %s ancestor %s\n") % (fcm, fco, fca))
2775
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    52
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    53
    cmd = (os.environ.get("HGMERGE") or repo.ui.config("ui", "merge")
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    54
           or "hgmerge")
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    55
    r = util.system('%s "%s" "%s" "%s"' % (cmd, a, b, c), cwd=repo.root,
3211
3fd098e0902d merge: extend file merge function for renames
Matt Mackall <mpm@selenic.com>
parents: 3169
diff changeset
    56
                    environ={'HG_FILE': fw,
3297
69b9471f26bb merge: pass contexts to applyupdates
Matt Mackall <mpm@selenic.com>
parents: 3296
diff changeset
    57
                             'HG_MY_NODE': str(wctx.parents()[0]),
69b9471f26bb merge: pass contexts to applyupdates
Matt Mackall <mpm@selenic.com>
parents: 3296
diff changeset
    58
                             'HG_OTHER_NODE': str(mctx)})
2775
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    59
    if r:
3211
3fd098e0902d merge: extend file merge function for renames
Matt Mackall <mpm@selenic.com>
parents: 3169
diff changeset
    60
        repo.ui.warn(_("merging %s failed!\n") % fw)
2775
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    61
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    62
    os.unlink(b)
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    63
    os.unlink(c)
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    64
    return r
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    65
3312
c5075ad5e3e9 merge: use contexts in checkunknown and forgetremoved
Matt Mackall <mpm@selenic.com>
parents: 3311
diff changeset
    66
def checkunknown(wctx, mctx):
3107
3bd05ad67f45 merge: pull manifest checks and updates into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3106
diff changeset
    67
    """
3bd05ad67f45 merge: pull manifest checks and updates into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3106
diff changeset
    68
    check for collisions between unknown files and files in m2
3bd05ad67f45 merge: pull manifest checks and updates into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3106
diff changeset
    69
    """
3312
c5075ad5e3e9 merge: use contexts in checkunknown and forgetremoved
Matt Mackall <mpm@selenic.com>
parents: 3311
diff changeset
    70
    man = mctx.manifest()
3218
8d4855fd9d7b merge: use new working context object in update
Matt Mackall <mpm@selenic.com>
parents: 3212
diff changeset
    71
    for f in wctx.unknown():
3312
c5075ad5e3e9 merge: use contexts in checkunknown and forgetremoved
Matt Mackall <mpm@selenic.com>
parents: 3311
diff changeset
    72
        if f in man:
c5075ad5e3e9 merge: use contexts in checkunknown and forgetremoved
Matt Mackall <mpm@selenic.com>
parents: 3311
diff changeset
    73
            if mctx.filectx(f).cmp(wctx.filectx(f).data()):
3107
3bd05ad67f45 merge: pull manifest checks and updates into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3106
diff changeset
    74
                raise util.Abort(_("'%s' already exists in the working"
3bd05ad67f45 merge: pull manifest checks and updates into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3106
diff changeset
    75
                                   " dir and differs from remote") % f)
3bd05ad67f45 merge: pull manifest checks and updates into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3106
diff changeset
    76
3312
c5075ad5e3e9 merge: use contexts in checkunknown and forgetremoved
Matt Mackall <mpm@selenic.com>
parents: 3311
diff changeset
    77
def forgetremoved(wctx, mctx):
3107
3bd05ad67f45 merge: pull manifest checks and updates into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3106
diff changeset
    78
    """
3bd05ad67f45 merge: pull manifest checks and updates into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3106
diff changeset
    79
    Forget removed files
3bd05ad67f45 merge: pull manifest checks and updates into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3106
diff changeset
    80
3bd05ad67f45 merge: pull manifest checks and updates into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3106
diff changeset
    81
    If we're jumping between revisions (as opposed to merging), and if
3bd05ad67f45 merge: pull manifest checks and updates into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3106
diff changeset
    82
    neither the working directory nor the target rev has the file,
3bd05ad67f45 merge: pull manifest checks and updates into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3106
diff changeset
    83
    then we need to remove it from the dirstate, to prevent the
3bd05ad67f45 merge: pull manifest checks and updates into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3106
diff changeset
    84
    dirstate from listing the file when it is no longer in the
3bd05ad67f45 merge: pull manifest checks and updates into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3106
diff changeset
    85
    manifest.
3bd05ad67f45 merge: pull manifest checks and updates into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3106
diff changeset
    86
    """
3bd05ad67f45 merge: pull manifest checks and updates into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3106
diff changeset
    87
3bd05ad67f45 merge: pull manifest checks and updates into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3106
diff changeset
    88
    action = []
3312
c5075ad5e3e9 merge: use contexts in checkunknown and forgetremoved
Matt Mackall <mpm@selenic.com>
parents: 3311
diff changeset
    89
    man = mctx.manifest()
3218
8d4855fd9d7b merge: use new working context object in update
Matt Mackall <mpm@selenic.com>
parents: 3212
diff changeset
    90
    for f in wctx.deleted() + wctx.removed():
3312
c5075ad5e3e9 merge: use contexts in checkunknown and forgetremoved
Matt Mackall <mpm@selenic.com>
parents: 3311
diff changeset
    91
        if f not in man:
3107
3bd05ad67f45 merge: pull manifest checks and updates into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3106
diff changeset
    92
            action.append((f, "f"))
3bd05ad67f45 merge: pull manifest checks and updates into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3106
diff changeset
    93
3bd05ad67f45 merge: pull manifest checks and updates into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3106
diff changeset
    94
    return action
3bd05ad67f45 merge: pull manifest checks and updates into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3106
diff changeset
    95
3153
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
    96
def nonoverlap(d1, d2):
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
    97
    """
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
    98
    Return list of elements in d1 not in d2
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
    99
    """
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   100
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   101
    l = []
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   102
    for d in d1:
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   103
        if d not in d2:
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   104
            l.append(d)
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   105
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   106
    l.sort()
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   107
    return l
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   108
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   109
def findold(fctx, limit):
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   110
    """
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   111
    find files that path was copied from, back to linkrev limit
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   112
    """
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   113
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   114
    old = {}
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   115
    orig = fctx.path()
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   116
    visit = [fctx]
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   117
    while visit:
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   118
        fc = visit.pop()
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   119
        if fc.rev() < limit:
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   120
            continue
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   121
        if fc.path() != orig and fc.path() not in old:
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   122
            old[fc.path()] = 1
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   123
        visit += fc.parents()
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   124
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   125
    old = old.keys()
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   126
    old.sort()
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   127
    return old
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   128
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   129
def findcopies(repo, m1, m2, limit):
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   130
    """
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   131
    Find moves and copies between m1 and m2 back to limit linkrev
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   132
    """
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   133
3249
f05c182430a0 merge: add rename following
Matt Mackall <mpm@selenic.com>
parents: 3248
diff changeset
   134
    if not repo.ui.config("merge", "followcopies"):
f05c182430a0 merge: add rename following
Matt Mackall <mpm@selenic.com>
parents: 3248
diff changeset
   135
        return {}
f05c182430a0 merge: add rename following
Matt Mackall <mpm@selenic.com>
parents: 3248
diff changeset
   136
3160
1839e6e91c3a findcopies: shortcut for empty working dir
Matt Mackall <mpm@selenic.com>
parents: 3155
diff changeset
   137
    # avoid silly behavior for update from empty dir
1839e6e91c3a findcopies: shortcut for empty working dir
Matt Mackall <mpm@selenic.com>
parents: 3155
diff changeset
   138
    if not m1:
1839e6e91c3a findcopies: shortcut for empty working dir
Matt Mackall <mpm@selenic.com>
parents: 3155
diff changeset
   139
        return {}
1839e6e91c3a findcopies: shortcut for empty working dir
Matt Mackall <mpm@selenic.com>
parents: 3155
diff changeset
   140
3155
56c59ba7aa76 findcopies: use dirstate rename information
Matt Mackall <mpm@selenic.com>
parents: 3153
diff changeset
   141
    dcopies = repo.dirstate.copies()
3153
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   142
    copy = {}
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   143
    match = {}
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   144
    u1 = nonoverlap(m1, m2)
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   145
    u2 = nonoverlap(m2, m1)
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   146
    ctx = util.cachefunc(lambda f,n: repo.filectx(f, fileid=n[:20]))
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   147
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   148
    def checkpair(c, f2, man):
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   149
        ''' check if an apparent pair actually matches '''
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   150
        c2 = ctx(f2, man[f2])
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   151
        ca = c.ancestor(c2)
3251
c93ce7f10f85 merge: fixes for merge+rename
Matt Mackall <mpm@selenic.com>
parents: 3249
diff changeset
   152
        if ca and ca.path() == c.path() or ca.path() == c2.path():
3153
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   153
            copy[c.path()] = f2
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   154
            copy[f2] = c.path()
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   155
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   156
    for f in u1:
3155
56c59ba7aa76 findcopies: use dirstate rename information
Matt Mackall <mpm@selenic.com>
parents: 3153
diff changeset
   157
        c = ctx(dcopies.get(f, f), m1[f])
3153
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   158
        for of in findold(c, limit):
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   159
            if of in m2:
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   160
                checkpair(c, of, m2)
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   161
            else:
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   162
                match.setdefault(of, []).append(f)
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   163
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   164
    for f in u2:
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   165
        c = ctx(f, m2[f])
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   166
        for of in findold(c, limit):
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   167
            if of in m1:
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   168
                checkpair(c, of, m1)
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   169
            elif of in match:
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   170
                for mf in match[of]:
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   171
                    checkpair(c, mf, m1)
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   172
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   173
    return copy
c82ea81d6850 Add core copy detection algorithm
Matt Mackall <mpm@selenic.com>
parents: 3121
diff changeset
   174
3295
72d1e521da77 merge: use contexts for manifestmerge
Matt Mackall <mpm@selenic.com>
parents: 3292
diff changeset
   175
def manifestmerge(repo, p1, p2, pa, overwrite, partial):
3105
7c7469d41ade merge: pull manifest comparison out into separate function
Matt Mackall <mpm@selenic.com>
parents: 3104
diff changeset
   176
    """
7c7469d41ade merge: pull manifest comparison out into separate function
Matt Mackall <mpm@selenic.com>
parents: 3104
diff changeset
   177
    Merge manifest m1 with m2 using ancestor ma and generate merge action list
7c7469d41ade merge: pull manifest comparison out into separate function
Matt Mackall <mpm@selenic.com>
parents: 3104
diff changeset
   178
    """
7c7469d41ade merge: pull manifest comparison out into separate function
Matt Mackall <mpm@selenic.com>
parents: 3104
diff changeset
   179
3314
b16456909a0a merge: various tidying
Matt Mackall <mpm@selenic.com>
parents: 3312
diff changeset
   180
    repo.ui.note(_("resolving manifests\n"))
b16456909a0a merge: various tidying
Matt Mackall <mpm@selenic.com>
parents: 3312
diff changeset
   181
    repo.ui.debug(_(" overwrite %s partial %s\n") % (overwrite, bool(partial)))
b16456909a0a merge: various tidying
Matt Mackall <mpm@selenic.com>
parents: 3312
diff changeset
   182
    repo.ui.debug(_(" ancestor %s local %s remote %s\n") % (pa, p1, p2))
b16456909a0a merge: various tidying
Matt Mackall <mpm@selenic.com>
parents: 3312
diff changeset
   183
3295
72d1e521da77 merge: use contexts for manifestmerge
Matt Mackall <mpm@selenic.com>
parents: 3292
diff changeset
   184
    m1 = p1.manifest()
72d1e521da77 merge: use contexts for manifestmerge
Matt Mackall <mpm@selenic.com>
parents: 3292
diff changeset
   185
    m2 = p2.manifest()
72d1e521da77 merge: use contexts for manifestmerge
Matt Mackall <mpm@selenic.com>
parents: 3292
diff changeset
   186
    ma = pa.manifest()
72d1e521da77 merge: use contexts for manifestmerge
Matt Mackall <mpm@selenic.com>
parents: 3292
diff changeset
   187
    backwards = (pa == p2)
3314
b16456909a0a merge: various tidying
Matt Mackall <mpm@selenic.com>
parents: 3312
diff changeset
   188
    action = []
b16456909a0a merge: various tidying
Matt Mackall <mpm@selenic.com>
parents: 3312
diff changeset
   189
    copy = {}
3295
72d1e521da77 merge: use contexts for manifestmerge
Matt Mackall <mpm@selenic.com>
parents: 3292
diff changeset
   190
3249
f05c182430a0 merge: add rename following
Matt Mackall <mpm@selenic.com>
parents: 3248
diff changeset
   191
    def fmerge(f, f2=None, fa=None):
3118
5644a05a608c merge: simplify exec flag handling
Matt Mackall <mpm@selenic.com>
parents: 3117
diff changeset
   192
        """merge executable flags"""
3249
f05c182430a0 merge: add rename following
Matt Mackall <mpm@selenic.com>
parents: 3248
diff changeset
   193
        if not f2:
f05c182430a0 merge: add rename following
Matt Mackall <mpm@selenic.com>
parents: 3248
diff changeset
   194
            f2 = f
f05c182430a0 merge: add rename following
Matt Mackall <mpm@selenic.com>
parents: 3248
diff changeset
   195
            fa = f
f05c182430a0 merge: add rename following
Matt Mackall <mpm@selenic.com>
parents: 3248
diff changeset
   196
        a, b, c = ma.execf(fa), m1.execf(f), m2.execf(f2)
3118
5644a05a608c merge: simplify exec flag handling
Matt Mackall <mpm@selenic.com>
parents: 3117
diff changeset
   197
        return ((a^b) | (a^c)) ^ a
5644a05a608c merge: simplify exec flag handling
Matt Mackall <mpm@selenic.com>
parents: 3117
diff changeset
   198
3307
f009a6f12a59 merge: swap file and mode args for act()
Matt Mackall <mpm@selenic.com>
parents: 3306
diff changeset
   199
    def act(msg, m, f, *args):
3295
72d1e521da77 merge: use contexts for manifestmerge
Matt Mackall <mpm@selenic.com>
parents: 3292
diff changeset
   200
        repo.ui.debug(" %s: %s -> %s\n" % (f, msg, m))
3121
2ef0b3aae186 merge: simplify actions with helper function
Matt Mackall <mpm@selenic.com>
parents: 3120
diff changeset
   201
        action.append((f, m) + args)
2ef0b3aae186 merge: simplify actions with helper function
Matt Mackall <mpm@selenic.com>
parents: 3120
diff changeset
   202
3295
72d1e521da77 merge: use contexts for manifestmerge
Matt Mackall <mpm@selenic.com>
parents: 3292
diff changeset
   203
    if not (backwards or overwrite):
72d1e521da77 merge: use contexts for manifestmerge
Matt Mackall <mpm@selenic.com>
parents: 3292
diff changeset
   204
        copy = findcopies(repo, m1, m2, pa.rev())
72d1e521da77 merge: use contexts for manifestmerge
Matt Mackall <mpm@selenic.com>
parents: 3292
diff changeset
   205
3105
7c7469d41ade merge: pull manifest comparison out into separate function
Matt Mackall <mpm@selenic.com>
parents: 3104
diff changeset
   206
    # Compare manifests
7c7469d41ade merge: pull manifest comparison out into separate function
Matt Mackall <mpm@selenic.com>
parents: 3104
diff changeset
   207
    for f, n in m1.iteritems():
3248
751840e739a1 merge: reduce manifest copying
Matt Mackall <mpm@selenic.com>
parents: 3247
diff changeset
   208
        if partial and not partial(f):
751840e739a1 merge: reduce manifest copying
Matt Mackall <mpm@selenic.com>
parents: 3247
diff changeset
   209
            continue
3105
7c7469d41ade merge: pull manifest comparison out into separate function
Matt Mackall <mpm@selenic.com>
parents: 3104
diff changeset
   210
        if f in m2:
7c7469d41ade merge: pull manifest comparison out into separate function
Matt Mackall <mpm@selenic.com>
parents: 3104
diff changeset
   211
            # are files different?
7c7469d41ade merge: pull manifest comparison out into separate function
Matt Mackall <mpm@selenic.com>
parents: 3104
diff changeset
   212
            if n != m2[f]:
7c7469d41ade merge: pull manifest comparison out into separate function
Matt Mackall <mpm@selenic.com>
parents: 3104
diff changeset
   213
                a = ma.get(f, nullid)
7c7469d41ade merge: pull manifest comparison out into separate function
Matt Mackall <mpm@selenic.com>
parents: 3104
diff changeset
   214
                # are both different from the ancestor?
7c7469d41ade merge: pull manifest comparison out into separate function
Matt Mackall <mpm@selenic.com>
parents: 3104
diff changeset
   215
                if not overwrite and n != a and m2[f] != a:
3308
ecc1bf27378c merge: unify merge and copy actions
Matt Mackall <mpm@selenic.com>
parents: 3307
diff changeset
   216
                    act("versions differ", "m", f, f, f, fmerge(f), False)
3105
7c7469d41ade merge: pull manifest comparison out into separate function
Matt Mackall <mpm@selenic.com>
parents: 3104
diff changeset
   217
                # are we clobbering?
7c7469d41ade merge: pull manifest comparison out into separate function
Matt Mackall <mpm@selenic.com>
parents: 3104
diff changeset
   218
                # is remote's version newer?
7c7469d41ade merge: pull manifest comparison out into separate function
Matt Mackall <mpm@selenic.com>
parents: 3104
diff changeset
   219
                # or are we going back in time and clean?
7c7469d41ade merge: pull manifest comparison out into separate function
Matt Mackall <mpm@selenic.com>
parents: 3104
diff changeset
   220
                elif overwrite or m2[f] != a or (backwards and not n[20:]):
3307
f009a6f12a59 merge: swap file and mode args for act()
Matt Mackall <mpm@selenic.com>
parents: 3306
diff changeset
   221
                    act("remote is newer", "g", f, m2.execf(f))
3113
d1d1cd5b9484 merge: eliminate confusing queued variable
Matt Mackall <mpm@selenic.com>
parents: 3112
diff changeset
   222
                # local is newer, not overwrite, check mode bits
3118
5644a05a608c merge: simplify exec flag handling
Matt Mackall <mpm@selenic.com>
parents: 3117
diff changeset
   223
                elif fmerge(f) != m1.execf(f):
3307
f009a6f12a59 merge: swap file and mode args for act()
Matt Mackall <mpm@selenic.com>
parents: 3306
diff changeset
   224
                    act("update permissions", "e", f, m2.execf(f))
3113
d1d1cd5b9484 merge: eliminate confusing queued variable
Matt Mackall <mpm@selenic.com>
parents: 3112
diff changeset
   225
            # contents same, check mode bits
d1d1cd5b9484 merge: eliminate confusing queued variable
Matt Mackall <mpm@selenic.com>
parents: 3112
diff changeset
   226
            elif m1.execf(f) != m2.execf(f):
3120
1c1e59aac82a merge: simplify local created logic
Matt Mackall <mpm@selenic.com>
parents: 3119
diff changeset
   227
                if overwrite or fmerge(f) != m1.execf(f):
3307
f009a6f12a59 merge: swap file and mode args for act()
Matt Mackall <mpm@selenic.com>
parents: 3306
diff changeset
   228
                    act("update permissions", "e", f, m2.execf(f))
3249
f05c182430a0 merge: add rename following
Matt Mackall <mpm@selenic.com>
parents: 3248
diff changeset
   229
        elif f in copy:
f05c182430a0 merge: add rename following
Matt Mackall <mpm@selenic.com>
parents: 3248
diff changeset
   230
            f2 = copy[f]
f05c182430a0 merge: add rename following
Matt Mackall <mpm@selenic.com>
parents: 3248
diff changeset
   231
            if f in ma: # case 3,20 A/B/A
3308
ecc1bf27378c merge: unify merge and copy actions
Matt Mackall <mpm@selenic.com>
parents: 3307
diff changeset
   232
                act("remote moved", "m", f, f2, f2, fmerge(f, f2, f), True)
3249
f05c182430a0 merge: add rename following
Matt Mackall <mpm@selenic.com>
parents: 3248
diff changeset
   233
            else:
f05c182430a0 merge: add rename following
Matt Mackall <mpm@selenic.com>
parents: 3248
diff changeset
   234
                if f2 in m1: # case 2 A,B/B/B
3308
ecc1bf27378c merge: unify merge and copy actions
Matt Mackall <mpm@selenic.com>
parents: 3307
diff changeset
   235
                    act("local copied", "m",
3307
f009a6f12a59 merge: swap file and mode args for act()
Matt Mackall <mpm@selenic.com>
parents: 3306
diff changeset
   236
                        f, f2, f, fmerge(f, f2, f2), False)
3249
f05c182430a0 merge: add rename following
Matt Mackall <mpm@selenic.com>
parents: 3248
diff changeset
   237
                else: # case 4,21 A/B/B
3308
ecc1bf27378c merge: unify merge and copy actions
Matt Mackall <mpm@selenic.com>
parents: 3307
diff changeset
   238
                    act("local moved", "m",
3307
f009a6f12a59 merge: swap file and mode args for act()
Matt Mackall <mpm@selenic.com>
parents: 3306
diff changeset
   239
                        f, f2, f, fmerge(f, f2, f2), False)
3105
7c7469d41ade merge: pull manifest comparison out into separate function
Matt Mackall <mpm@selenic.com>
parents: 3104
diff changeset
   240
        elif f in ma:
3117
7a635ef25132 merge: simplify tests for local changed/remote deleted
Matt Mackall <mpm@selenic.com>
parents: 3116
diff changeset
   241
            if n != ma[f] and not overwrite:
3295
72d1e521da77 merge: use contexts for manifestmerge
Matt Mackall <mpm@selenic.com>
parents: 3292
diff changeset
   242
                if repo.ui.prompt(
3117
7a635ef25132 merge: simplify tests for local changed/remote deleted
Matt Mackall <mpm@selenic.com>
parents: 3116
diff changeset
   243
                    (_(" local changed %s which remote deleted\n") % f) +
3119
b1de36a4b4df merge: simplify prompt code
Matt Mackall <mpm@selenic.com>
parents: 3118
diff changeset
   244
                    _("(k)eep or (d)elete?"), _("[kd]"), _("k")) == _("d"):
3307
f009a6f12a59 merge: swap file and mode args for act()
Matt Mackall <mpm@selenic.com>
parents: 3306
diff changeset
   245
                    act("prompt delete", "r", f)
3105
7c7469d41ade merge: pull manifest comparison out into separate function
Matt Mackall <mpm@selenic.com>
parents: 3104
diff changeset
   246
            else:
3307
f009a6f12a59 merge: swap file and mode args for act()
Matt Mackall <mpm@selenic.com>
parents: 3306
diff changeset
   247
                act("other deleted", "r", f)
3105
7c7469d41ade merge: pull manifest comparison out into separate function
Matt Mackall <mpm@selenic.com>
parents: 3104
diff changeset
   248
        else:
7c7469d41ade merge: pull manifest comparison out into separate function
Matt Mackall <mpm@selenic.com>
parents: 3104
diff changeset
   249
            # file is created on branch or in working directory
3120
1c1e59aac82a merge: simplify local created logic
Matt Mackall <mpm@selenic.com>
parents: 3119
diff changeset
   250
            if (overwrite and n[20:] != "u") or (backwards and not n[20:]):
3307
f009a6f12a59 merge: swap file and mode args for act()
Matt Mackall <mpm@selenic.com>
parents: 3306
diff changeset
   251
                act("remote deleted", "r", f)
3105
7c7469d41ade merge: pull manifest comparison out into separate function
Matt Mackall <mpm@selenic.com>
parents: 3104
diff changeset
   252
7c7469d41ade merge: pull manifest comparison out into separate function
Matt Mackall <mpm@selenic.com>
parents: 3104
diff changeset
   253
    for f, n in m2.iteritems():
3248
751840e739a1 merge: reduce manifest copying
Matt Mackall <mpm@selenic.com>
parents: 3247
diff changeset
   254
        if partial and not partial(f):
751840e739a1 merge: reduce manifest copying
Matt Mackall <mpm@selenic.com>
parents: 3247
diff changeset
   255
            continue
751840e739a1 merge: reduce manifest copying
Matt Mackall <mpm@selenic.com>
parents: 3247
diff changeset
   256
        if f in m1:
751840e739a1 merge: reduce manifest copying
Matt Mackall <mpm@selenic.com>
parents: 3247
diff changeset
   257
            continue
3249
f05c182430a0 merge: add rename following
Matt Mackall <mpm@selenic.com>
parents: 3248
diff changeset
   258
        if f in copy:
f05c182430a0 merge: add rename following
Matt Mackall <mpm@selenic.com>
parents: 3248
diff changeset
   259
            f2 = copy[f]
3252
ae85272b59a4 merge: copy fixes and tests
Matt Mackall <mpm@selenic.com>
parents: 3251
diff changeset
   260
            if f2 not in m2: # already seen
3249
f05c182430a0 merge: add rename following
Matt Mackall <mpm@selenic.com>
parents: 3248
diff changeset
   261
                continue
f05c182430a0 merge: add rename following
Matt Mackall <mpm@selenic.com>
parents: 3248
diff changeset
   262
            # rename case 1, A/A,B/A
3308
ecc1bf27378c merge: unify merge and copy actions
Matt Mackall <mpm@selenic.com>
parents: 3307
diff changeset
   263
            act("remote copied", "m", f2, f, f, fmerge(f2, f, f2), False)
3249
f05c182430a0 merge: add rename following
Matt Mackall <mpm@selenic.com>
parents: 3248
diff changeset
   264
        elif f in ma:
3116
920f54a2249e merge: more simplification of m2 manifest scanning
Matt Mackall <mpm@selenic.com>
parents: 3115
diff changeset
   265
            if overwrite or backwards:
3307
f009a6f12a59 merge: swap file and mode args for act()
Matt Mackall <mpm@selenic.com>
parents: 3306
diff changeset
   266
                act("recreating", "g", f, m2.execf(f))
3116
920f54a2249e merge: more simplification of m2 manifest scanning
Matt Mackall <mpm@selenic.com>
parents: 3115
diff changeset
   267
            elif n != ma[f]:
3295
72d1e521da77 merge: use contexts for manifestmerge
Matt Mackall <mpm@selenic.com>
parents: 3292
diff changeset
   268
                if repo.ui.prompt(
3116
920f54a2249e merge: more simplification of m2 manifest scanning
Matt Mackall <mpm@selenic.com>
parents: 3115
diff changeset
   269
                    (_("remote changed %s which local deleted\n") % f) +
3119
b1de36a4b4df merge: simplify prompt code
Matt Mackall <mpm@selenic.com>
parents: 3118
diff changeset
   270
                    _("(k)eep or (d)elete?"), _("[kd]"), _("k")) == _("k"):
3307
f009a6f12a59 merge: swap file and mode args for act()
Matt Mackall <mpm@selenic.com>
parents: 3306
diff changeset
   271
                    act("prompt recreating", "g", f, m2.execf(f))
3115
bb74f809bc95 merge: reorder tests on m2 items in manifestmerge
Matt Mackall <mpm@selenic.com>
parents: 3114
diff changeset
   272
        else:
3307
f009a6f12a59 merge: swap file and mode args for act()
Matt Mackall <mpm@selenic.com>
parents: 3306
diff changeset
   273
            act("remote created", "g", f, m2.execf(f))
3105
7c7469d41ade merge: pull manifest comparison out into separate function
Matt Mackall <mpm@selenic.com>
parents: 3104
diff changeset
   274
7c7469d41ade merge: pull manifest comparison out into separate function
Matt Mackall <mpm@selenic.com>
parents: 3104
diff changeset
   275
    return action
7c7469d41ade merge: pull manifest comparison out into separate function
Matt Mackall <mpm@selenic.com>
parents: 3104
diff changeset
   276
3297
69b9471f26bb merge: pass contexts to applyupdates
Matt Mackall <mpm@selenic.com>
parents: 3296
diff changeset
   277
def applyupdates(repo, action, wctx, mctx):
3111
5cc62d99b785 merge: move apply and dirstate code into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3110
diff changeset
   278
    updated, merged, removed, unresolved = 0, 0, 0, 0
5cc62d99b785 merge: move apply and dirstate code into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3110
diff changeset
   279
    action.sort()
5cc62d99b785 merge: move apply and dirstate code into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3110
diff changeset
   280
    for a in action:
5cc62d99b785 merge: move apply and dirstate code into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3110
diff changeset
   281
        f, m = a[:2]
5cc62d99b785 merge: move apply and dirstate code into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3110
diff changeset
   282
        if f[0] == "/":
5cc62d99b785 merge: move apply and dirstate code into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3110
diff changeset
   283
            continue
5cc62d99b785 merge: move apply and dirstate code into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3110
diff changeset
   284
        if m == "r": # remove
5cc62d99b785 merge: move apply and dirstate code into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3110
diff changeset
   285
            repo.ui.note(_("removing %s\n") % f)
5cc62d99b785 merge: move apply and dirstate code into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3110
diff changeset
   286
            util.audit_path(f)
5cc62d99b785 merge: move apply and dirstate code into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3110
diff changeset
   287
            try:
5cc62d99b785 merge: move apply and dirstate code into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3110
diff changeset
   288
                util.unlink(repo.wjoin(f))
5cc62d99b785 merge: move apply and dirstate code into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3110
diff changeset
   289
            except OSError, inst:
5cc62d99b785 merge: move apply and dirstate code into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3110
diff changeset
   290
                if inst.errno != errno.ENOENT:
5cc62d99b785 merge: move apply and dirstate code into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3110
diff changeset
   291
                    repo.ui.warn(_("update failed to remove %s: %s!\n") %
5cc62d99b785 merge: move apply and dirstate code into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3110
diff changeset
   292
                                 (f, inst.strerror))
5cc62d99b785 merge: move apply and dirstate code into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3110
diff changeset
   293
            removed +=1
3308
ecc1bf27378c merge: unify merge and copy actions
Matt Mackall <mpm@selenic.com>
parents: 3307
diff changeset
   294
        elif m == "m": # merge
3303
488d3062d225 merge: eliminate nodes from action list
Matt Mackall <mpm@selenic.com>
parents: 3299
diff changeset
   295
            f2, fd, flag, move = a[2:]
3309
e8be5942335d merge: pull file copy/move out of filemerge
Matt Mackall <mpm@selenic.com>
parents: 3308
diff changeset
   296
            if filemerge(repo, f, f2, wctx, mctx):
3249
f05c182430a0 merge: add rename following
Matt Mackall <mpm@selenic.com>
parents: 3248
diff changeset
   297
                unresolved += 1
3309
e8be5942335d merge: pull file copy/move out of filemerge
Matt Mackall <mpm@selenic.com>
parents: 3308
diff changeset
   298
            else:
e8be5942335d merge: pull file copy/move out of filemerge
Matt Mackall <mpm@selenic.com>
parents: 3308
diff changeset
   299
                if f != fd:
e8be5942335d merge: pull file copy/move out of filemerge
Matt Mackall <mpm@selenic.com>
parents: 3308
diff changeset
   300
                    repo.ui.debug(_("copying %s to %s\n") % (f, fd))
e8be5942335d merge: pull file copy/move out of filemerge
Matt Mackall <mpm@selenic.com>
parents: 3308
diff changeset
   301
                    repo.wwrite(fd, repo.wread(f))
e8be5942335d merge: pull file copy/move out of filemerge
Matt Mackall <mpm@selenic.com>
parents: 3308
diff changeset
   302
                    if move:
e8be5942335d merge: pull file copy/move out of filemerge
Matt Mackall <mpm@selenic.com>
parents: 3308
diff changeset
   303
                        repo.ui.debug(_("removing %s\n") % f)
e8be5942335d merge: pull file copy/move out of filemerge
Matt Mackall <mpm@selenic.com>
parents: 3308
diff changeset
   304
                        os.unlink(repo.wjoin(f))
e8be5942335d merge: pull file copy/move out of filemerge
Matt Mackall <mpm@selenic.com>
parents: 3308
diff changeset
   305
3249
f05c182430a0 merge: add rename following
Matt Mackall <mpm@selenic.com>
parents: 3248
diff changeset
   306
            util.set_exec(repo.wjoin(fd), flag)
f05c182430a0 merge: add rename following
Matt Mackall <mpm@selenic.com>
parents: 3248
diff changeset
   307
            merged += 1
3111
5cc62d99b785 merge: move apply and dirstate code into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3110
diff changeset
   308
        elif m == "g": # get
3306
5c9806554d65 merge: finish removing nodes from action list
Matt Mackall <mpm@selenic.com>
parents: 3303
diff changeset
   309
            flag = a[2]
3111
5cc62d99b785 merge: move apply and dirstate code into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3110
diff changeset
   310
            repo.ui.note(_("getting %s\n") % f)
3303
488d3062d225 merge: eliminate nodes from action list
Matt Mackall <mpm@selenic.com>
parents: 3299
diff changeset
   311
            t = mctx.filectx(f).data()
3111
5cc62d99b785 merge: move apply and dirstate code into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3110
diff changeset
   312
            repo.wwrite(f, t)
5cc62d99b785 merge: move apply and dirstate code into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3110
diff changeset
   313
            util.set_exec(repo.wjoin(f), flag)
5cc62d99b785 merge: move apply and dirstate code into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3110
diff changeset
   314
            updated += 1
5cc62d99b785 merge: move apply and dirstate code into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3110
diff changeset
   315
        elif m == "e": # exec
3303
488d3062d225 merge: eliminate nodes from action list
Matt Mackall <mpm@selenic.com>
parents: 3299
diff changeset
   316
            flag = a[2]
3111
5cc62d99b785 merge: move apply and dirstate code into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3110
diff changeset
   317
            util.set_exec(repo.wjoin(f), flag)
5cc62d99b785 merge: move apply and dirstate code into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3110
diff changeset
   318
5cc62d99b785 merge: move apply and dirstate code into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3110
diff changeset
   319
    return updated, merged, removed, unresolved
5cc62d99b785 merge: move apply and dirstate code into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3110
diff changeset
   320
3303
488d3062d225 merge: eliminate nodes from action list
Matt Mackall <mpm@selenic.com>
parents: 3299
diff changeset
   321
def recordupdates(repo, action, branchmerge, mctx):
3111
5cc62d99b785 merge: move apply and dirstate code into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3110
diff changeset
   322
    for a in action:
5cc62d99b785 merge: move apply and dirstate code into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3110
diff changeset
   323
        f, m = a[:2]
5cc62d99b785 merge: move apply and dirstate code into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3110
diff changeset
   324
        if m == "r": # remove
5cc62d99b785 merge: move apply and dirstate code into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3110
diff changeset
   325
            if branchmerge:
5cc62d99b785 merge: move apply and dirstate code into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3110
diff changeset
   326
                repo.dirstate.update([f], 'r')
5cc62d99b785 merge: move apply and dirstate code into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3110
diff changeset
   327
            else:
5cc62d99b785 merge: move apply and dirstate code into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3110
diff changeset
   328
                repo.dirstate.forget([f])
5cc62d99b785 merge: move apply and dirstate code into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3110
diff changeset
   329
        elif m == "f": # forget
5cc62d99b785 merge: move apply and dirstate code into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3110
diff changeset
   330
            repo.dirstate.forget([f])
5cc62d99b785 merge: move apply and dirstate code into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3110
diff changeset
   331
        elif m == "g": # get
5cc62d99b785 merge: move apply and dirstate code into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3110
diff changeset
   332
            if branchmerge:
5cc62d99b785 merge: move apply and dirstate code into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3110
diff changeset
   333
                repo.dirstate.update([f], 'n', st_mtime=-1)
5cc62d99b785 merge: move apply and dirstate code into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3110
diff changeset
   334
            else:
5cc62d99b785 merge: move apply and dirstate code into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3110
diff changeset
   335
                repo.dirstate.update([f], 'n')
5cc62d99b785 merge: move apply and dirstate code into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3110
diff changeset
   336
        elif m == "m": # merge
3303
488d3062d225 merge: eliminate nodes from action list
Matt Mackall <mpm@selenic.com>
parents: 3299
diff changeset
   337
            f2, fd, flag, move = a[2:]
3251
c93ce7f10f85 merge: fixes for merge+rename
Matt Mackall <mpm@selenic.com>
parents: 3249
diff changeset
   338
            if branchmerge:
c93ce7f10f85 merge: fixes for merge+rename
Matt Mackall <mpm@selenic.com>
parents: 3249
diff changeset
   339
                # We've done a branch merge, mark this file as merged
c93ce7f10f85 merge: fixes for merge+rename
Matt Mackall <mpm@selenic.com>
parents: 3249
diff changeset
   340
                # so that we properly record the merger later
c93ce7f10f85 merge: fixes for merge+rename
Matt Mackall <mpm@selenic.com>
parents: 3249
diff changeset
   341
                repo.dirstate.update([fd], 'm')
c93ce7f10f85 merge: fixes for merge+rename
Matt Mackall <mpm@selenic.com>
parents: 3249
diff changeset
   342
            else:
c93ce7f10f85 merge: fixes for merge+rename
Matt Mackall <mpm@selenic.com>
parents: 3249
diff changeset
   343
                # We've update-merged a locally modified file, so
c93ce7f10f85 merge: fixes for merge+rename
Matt Mackall <mpm@selenic.com>
parents: 3249
diff changeset
   344
                # we set the dirstate to emulate a normal checkout
c93ce7f10f85 merge: fixes for merge+rename
Matt Mackall <mpm@selenic.com>
parents: 3249
diff changeset
   345
                # of that file some time in the past. Thus our
c93ce7f10f85 merge: fixes for merge+rename
Matt Mackall <mpm@selenic.com>
parents: 3249
diff changeset
   346
                # merge will appear as a normal local file
c93ce7f10f85 merge: fixes for merge+rename
Matt Mackall <mpm@selenic.com>
parents: 3249
diff changeset
   347
                # modification.
3303
488d3062d225 merge: eliminate nodes from action list
Matt Mackall <mpm@selenic.com>
parents: 3299
diff changeset
   348
                f_len = mctx.filectx(f).size()
3251
c93ce7f10f85 merge: fixes for merge+rename
Matt Mackall <mpm@selenic.com>
parents: 3249
diff changeset
   349
                repo.dirstate.update([fd], 'n', st_size=f_len, st_mtime=-1)
3308
ecc1bf27378c merge: unify merge and copy actions
Matt Mackall <mpm@selenic.com>
parents: 3307
diff changeset
   350
            if f != f2: # copy/rename
ecc1bf27378c merge: unify merge and copy actions
Matt Mackall <mpm@selenic.com>
parents: 3307
diff changeset
   351
                if move:
ecc1bf27378c merge: unify merge and copy actions
Matt Mackall <mpm@selenic.com>
parents: 3307
diff changeset
   352
                    repo.dirstate.update([f], 'r')
ecc1bf27378c merge: unify merge and copy actions
Matt Mackall <mpm@selenic.com>
parents: 3307
diff changeset
   353
                if f != fd:
ecc1bf27378c merge: unify merge and copy actions
Matt Mackall <mpm@selenic.com>
parents: 3307
diff changeset
   354
                    repo.dirstate.copy(f, fd)
ecc1bf27378c merge: unify merge and copy actions
Matt Mackall <mpm@selenic.com>
parents: 3307
diff changeset
   355
                else:
ecc1bf27378c merge: unify merge and copy actions
Matt Mackall <mpm@selenic.com>
parents: 3307
diff changeset
   356
                    repo.dirstate.copy(f2, fd)
3111
5cc62d99b785 merge: move apply and dirstate code into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3110
diff changeset
   357
2811
1ea086bc2086 Merge: combine choose and moddirstate to partial
Matt Mackall <mpm@selenic.com>
parents: 2810
diff changeset
   358
def update(repo, node, branchmerge=False, force=False, partial=None,
2815
4870f795f681 Merge: combine force and forcemerge arguments
Matt Mackall <mpm@selenic.com>
parents: 2814
diff changeset
   359
           wlock=None, show_stats=True, remind=True):
4870f795f681 Merge: combine force and forcemerge arguments
Matt Mackall <mpm@selenic.com>
parents: 2814
diff changeset
   360
2812
3aeab7bb5adc Refactor update locking slightly
Matt Mackall <mpm@selenic.com>
parents: 2811
diff changeset
   361
    if not wlock:
3aeab7bb5adc Refactor update locking slightly
Matt Mackall <mpm@selenic.com>
parents: 2811
diff changeset
   362
        wlock = repo.wlock()
3aeab7bb5adc Refactor update locking slightly
Matt Mackall <mpm@selenic.com>
parents: 2811
diff changeset
   363
3314
b16456909a0a merge: various tidying
Matt Mackall <mpm@selenic.com>
parents: 3312
diff changeset
   364
    overwrite = force and not branchmerge
b16456909a0a merge: various tidying
Matt Mackall <mpm@selenic.com>
parents: 3312
diff changeset
   365
    forcemerge = force and branchmerge
3218
8d4855fd9d7b merge: use new working context object in update
Matt Mackall <mpm@selenic.com>
parents: 3212
diff changeset
   366
    wc = repo.workingctx()
8d4855fd9d7b merge: use new working context object in update
Matt Mackall <mpm@selenic.com>
parents: 3212
diff changeset
   367
    pl = wc.parents()
3314
b16456909a0a merge: various tidying
Matt Mackall <mpm@selenic.com>
parents: 3312
diff changeset
   368
    p1, p2 = pl[0], repo.changectx(node)
b16456909a0a merge: various tidying
Matt Mackall <mpm@selenic.com>
parents: 3312
diff changeset
   369
    pa = p1.ancestor(p2)
b16456909a0a merge: various tidying
Matt Mackall <mpm@selenic.com>
parents: 3312
diff changeset
   370
    fp1, fp2, xp1, xp2 = p1.node(), p2.node(), str(p1), str(p2)
b16456909a0a merge: various tidying
Matt Mackall <mpm@selenic.com>
parents: 3312
diff changeset
   371
b16456909a0a merge: various tidying
Matt Mackall <mpm@selenic.com>
parents: 3312
diff changeset
   372
    ### check phase
3167
e67c22bc8bba merge: use repo.parents and parent contexts in update
Matt Mackall <mpm@selenic.com>
parents: 3162
diff changeset
   373
    if not overwrite and len(pl) > 1:
2775
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   374
        raise util.Abort(_("outstanding uncommitted merges"))
3314
b16456909a0a merge: various tidying
Matt Mackall <mpm@selenic.com>
parents: 3312
diff changeset
   375
    if pa == p1 or pa == p2: # is there a linear path from p1 to p2?
3110
40e777bda455 merge: remove linear variable
Matt Mackall <mpm@selenic.com>
parents: 3109
diff changeset
   376
        if branchmerge:
40e777bda455 merge: remove linear variable
Matt Mackall <mpm@selenic.com>
parents: 3109
diff changeset
   377
            raise util.Abort(_("there is nothing to merge, just use "
40e777bda455 merge: remove linear variable
Matt Mackall <mpm@selenic.com>
parents: 3109
diff changeset
   378
                               "'hg update' or look at 'hg heads'"))
40e777bda455 merge: remove linear variable
Matt Mackall <mpm@selenic.com>
parents: 3109
diff changeset
   379
    elif not (overwrite or branchmerge):
2815
4870f795f681 Merge: combine force and forcemerge arguments
Matt Mackall <mpm@selenic.com>
parents: 2814
diff changeset
   380
        raise util.Abort(_("update spans branches, use 'hg merge' "
2814
0f787997e3c2 Merge: move most tests to the beginning
Matt Mackall <mpm@selenic.com>
parents: 2813
diff changeset
   381
                           "or 'hg update -C' to lose changes"))
0f787997e3c2 Merge: move most tests to the beginning
Matt Mackall <mpm@selenic.com>
parents: 2813
diff changeset
   382
    if branchmerge and not forcemerge:
3218
8d4855fd9d7b merge: use new working context object in update
Matt Mackall <mpm@selenic.com>
parents: 3212
diff changeset
   383
        if wc.modified() or wc.added() or wc.removed():
2814
0f787997e3c2 Merge: move most tests to the beginning
Matt Mackall <mpm@selenic.com>
parents: 2813
diff changeset
   384
            raise util.Abort(_("outstanding uncommitted changes"))
0f787997e3c2 Merge: move most tests to the beginning
Matt Mackall <mpm@selenic.com>
parents: 2813
diff changeset
   385
3314
b16456909a0a merge: various tidying
Matt Mackall <mpm@selenic.com>
parents: 3312
diff changeset
   386
    ### calculate phase
3100
87ea5a71f7b9 merge: convert actions to list
Matt Mackall <mpm@selenic.com>
parents: 2981
diff changeset
   387
    action = []
3107
3bd05ad67f45 merge: pull manifest checks and updates into separate functions
Matt Mackall <mpm@selenic.com>
parents: 3106
diff changeset
   388
    if not force:
3312
c5075ad5e3e9 merge: use contexts in checkunknown and forgetremoved
Matt Mackall <mpm@selenic.com>
parents: 3311
diff changeset
   389
        checkunknown(wc, p2)
3110
40e777bda455 merge: remove linear variable
Matt Mackall <mpm@selenic.com>
parents: 3109
diff changeset
   390
    if not branchmerge:
3312
c5075ad5e3e9 merge: use contexts in checkunknown and forgetremoved
Matt Mackall <mpm@selenic.com>
parents: 3311
diff changeset
   391
        action += forgetremoved(wc, p2)
3295
72d1e521da77 merge: use contexts for manifestmerge
Matt Mackall <mpm@selenic.com>
parents: 3292
diff changeset
   392
    action += manifestmerge(repo, wc, p2, pa, overwrite, partial)
2775
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   393
2897
dd032b0f02ac merge: move forgets to the apply stage
Matt Mackall <mpm@selenic.com>
parents: 2896
diff changeset
   394
    ### apply phase
3314
b16456909a0a merge: various tidying
Matt Mackall <mpm@selenic.com>
parents: 3312
diff changeset
   395
    if not branchmerge: # just jump to the new rev
b16456909a0a merge: various tidying
Matt Mackall <mpm@selenic.com>
parents: 3312
diff changeset
   396
        fp1, fp2, xp1, xp2 = fp2, nullid, xp2, ''
3296
20087b4bc6f9 merge: don't call hooks for revert
Matt Mackall <mpm@selenic.com>
parents: 3295
diff changeset
   397
    if not partial:
20087b4bc6f9 merge: don't call hooks for revert
Matt Mackall <mpm@selenic.com>
parents: 3295
diff changeset
   398
        repo.hook('preupdate', throw=True, parent1=xp1, parent2=xp2)
2775
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   399
3297
69b9471f26bb merge: pass contexts to applyupdates
Matt Mackall <mpm@selenic.com>
parents: 3296
diff changeset
   400
    updated, merged, removed, unresolved = applyupdates(repo, action, wc, p2)
2899
8743188f4d2e merge: consolidate dirstate updates
Matt Mackall <mpm@selenic.com>
parents: 2898
diff changeset
   401
2775
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   402
    if show_stats:
2977
87a0332ab58b merge: combine merge and get lists
Matt Mackall <mpm@selenic.com>
parents: 2976
diff changeset
   403
        stats = ((updated, _("updated")),
2980
54d85098fb82 merge: make unresolved a counter
Matt Mackall <mpm@selenic.com>
parents: 2979
diff changeset
   404
                 (merged - unresolved, _("merged")),
2978
962b9c7df641 merge: add remove to the action hash
Matt Mackall <mpm@selenic.com>
parents: 2977
diff changeset
   405
                 (removed, _("removed")),
2980
54d85098fb82 merge: make unresolved a counter
Matt Mackall <mpm@selenic.com>
parents: 2979
diff changeset
   406
                 (unresolved, _("unresolved")))
2775
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   407
        note = ", ".join([_("%d files %s") % s for s in stats])
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   408
        repo.ui.status("%s\n" % note)
2811
1ea086bc2086 Merge: combine choose and moddirstate to partial
Matt Mackall <mpm@selenic.com>
parents: 2810
diff changeset
   409
    if not partial:
3314
b16456909a0a merge: various tidying
Matt Mackall <mpm@selenic.com>
parents: 3312
diff changeset
   410
        recordupdates(repo, action, branchmerge, p2)
b16456909a0a merge: various tidying
Matt Mackall <mpm@selenic.com>
parents: 3312
diff changeset
   411
        repo.dirstate.setparents(fp1, fp2)
b16456909a0a merge: various tidying
Matt Mackall <mpm@selenic.com>
parents: 3312
diff changeset
   412
        repo.hook('update', parent1=xp1, parent2=xp2, error=unresolved)
b16456909a0a merge: various tidying
Matt Mackall <mpm@selenic.com>
parents: 3312
diff changeset
   413
2810
ca06d35af65e Rename merge.allow -> merge.branchmerge
Matt Mackall <mpm@selenic.com>
parents: 2803
diff changeset
   414
        if branchmerge:
2813
56f99f5aab34 Merge: refactor err and failedmerge -> unresolved
Matt Mackall <mpm@selenic.com>
parents: 2812
diff changeset
   415
            if unresolved:
2775
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   416
                repo.ui.status(_("There are unresolved merges,"
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   417
                                " you can redo the full merge using:\n"
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   418
                                "  hg update -C %s\n"
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   419
                                "  hg merge %s\n"
3167
e67c22bc8bba merge: use repo.parents and parent contexts in update
Matt Mackall <mpm@selenic.com>
parents: 3162
diff changeset
   420
                                % (p1.rev(), p2.rev())))
2803
987c31e2a08c Merge with crew
Matt Mackall <mpm@selenic.com>
parents: 2775
diff changeset
   421
            elif remind:
2775
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   422
                repo.ui.status(_("(branch merge, don't forget to commit)\n"))
2813
56f99f5aab34 Merge: refactor err and failedmerge -> unresolved
Matt Mackall <mpm@selenic.com>
parents: 2812
diff changeset
   423
        elif unresolved:
2775
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   424
            repo.ui.status(_("There are unresolved merges with"
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   425
                             " locally modified files.\n"))
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   426
2980
54d85098fb82 merge: make unresolved a counter
Matt Mackall <mpm@selenic.com>
parents: 2979
diff changeset
   427
    return unresolved
2775
b550cd82f92a Move merge code to its own module
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   428