hgext/convert/hg.py
author Matt Mackall <mpm@selenic.com>
Mon, 12 Jan 2009 10:42:31 -0600
changeset 7637 1d54e2f6c0b7
parent 7633 08cabecfa8a8
child 7815 bcd364b247ba
permissions -rw-r--r--
error: move repo errors rename NoCapability to CapabilityError
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4536
cc9b79216a76 Split convert extension into common and repository type modules
Brendan Cully <brendan@kublai.com>
parents: 4532
diff changeset
     1
# hg backend for convert extension
3953
fad134931327 convert-repo: add basic CVS import support
Matt Mackall <mpm@selenic.com>
parents: 3939
diff changeset
     2
5556
61fdf2558c0a convert: some tidyups, doc improvements, and test fixes
Bryan O'Sullivan <bos@serpentine.com>
parents: 5554
diff changeset
     3
# Notes for hg->hg conversion:
61fdf2558c0a convert: some tidyups, doc improvements, and test fixes
Bryan O'Sullivan <bos@serpentine.com>
parents: 5554
diff changeset
     4
#
61fdf2558c0a convert: some tidyups, doc improvements, and test fixes
Bryan O'Sullivan <bos@serpentine.com>
parents: 5554
diff changeset
     5
# * Old versions of Mercurial didn't trim the whitespace from the ends
61fdf2558c0a convert: some tidyups, doc improvements, and test fixes
Bryan O'Sullivan <bos@serpentine.com>
parents: 5554
diff changeset
     6
#   of commit messages, but new versions do.  Changesets created by
61fdf2558c0a convert: some tidyups, doc improvements, and test fixes
Bryan O'Sullivan <bos@serpentine.com>
parents: 5554
diff changeset
     7
#   those older versions, then converted, may thus have different
61fdf2558c0a convert: some tidyups, doc improvements, and test fixes
Bryan O'Sullivan <bos@serpentine.com>
parents: 5554
diff changeset
     8
#   hashes for changesets that are otherwise identical.
61fdf2558c0a convert: some tidyups, doc improvements, and test fixes
Bryan O'Sullivan <bos@serpentine.com>
parents: 5554
diff changeset
     9
#
61fdf2558c0a convert: some tidyups, doc improvements, and test fixes
Bryan O'Sullivan <bos@serpentine.com>
parents: 5554
diff changeset
    10
# * By default, the source revision is stored in the converted
61fdf2558c0a convert: some tidyups, doc improvements, and test fixes
Bryan O'Sullivan <bos@serpentine.com>
parents: 5554
diff changeset
    11
#   revision.  This will cause the converted revision to have a
61fdf2558c0a convert: some tidyups, doc improvements, and test fixes
Bryan O'Sullivan <bos@serpentine.com>
parents: 5554
diff changeset
    12
#   different identity than the source.  To avoid this, use the
61fdf2558c0a convert: some tidyups, doc improvements, and test fixes
Bryan O'Sullivan <bos@serpentine.com>
parents: 5554
diff changeset
    13
#   following option: "--config convert.hg.saverev=false"
5013
6c1029aacc9a convert: Support Mercurial as a source, as well as a sink
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
    14
6c1029aacc9a convert: Support Mercurial as a source, as well as a sink
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
    15
4536
cc9b79216a76 Split convert extension into common and repository type modules
Brendan Cully <brendan@kublai.com>
parents: 4532
diff changeset
    16
import os, time
5014
914054ca532e convert: acquire/release locks periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 5013
diff changeset
    17
from mercurial.i18n import _
6211
f89fd07fc51d Expand import * to allow Pyflakes to find problems
Joel Rosdahl <joel@rosdahl.net>
parents: 6210
diff changeset
    18
from mercurial.node import bin, hex, nullid
7633
08cabecfa8a8 errors: move revlog errors
Matt Mackall <mpm@selenic.com>
parents: 7236
diff changeset
    19
from mercurial import hg, util, context, error
3953
fad134931327 convert-repo: add basic CVS import support
Matt Mackall <mpm@selenic.com>
parents: 3939
diff changeset
    20
5013
6c1029aacc9a convert: Support Mercurial as a source, as well as a sink
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
    21
from common import NoRepo, commit, converter_source, converter_sink
694
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
    22
5013
6c1029aacc9a convert: Support Mercurial as a source, as well as a sink
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
    23
class mercurial_sink(converter_sink):
4763
8e9d3faec270 convert: split converter into convertsource and convertsink
Brendan Cully <brendan@kublai.com>
parents: 4760
diff changeset
    24
    def __init__(self, ui, path):
5440
b4ae8535f834 convert: add default constructor for converter_sink
Bryan O'Sullivan <bos@serpentine.com>
parents: 5439
diff changeset
    25
        converter_sink.__init__(self, ui, path)
5173
6b4c332f241b convert: hg: optionally create branches as clones
Brendan Cully <brendan@kublai.com>
parents: 5143
diff changeset
    26
        self.branchnames = ui.configbool('convert', 'hg.usebranchnames', True)
6b4c332f241b convert: hg: optionally create branches as clones
Brendan Cully <brendan@kublai.com>
parents: 5143
diff changeset
    27
        self.clonebranches = ui.configbool('convert', 'hg.clonebranches', False)
5260
be4835ad9a85 convert: new config variable hg.tagsbranch controls which branch tags are committed to
Brendan Cully <brendan@kublai.com>
parents: 5195
diff changeset
    28
        self.tagsbranch = ui.config('convert', 'hg.tagsbranch', 'default')
5173
6b4c332f241b convert: hg: optionally create branches as clones
Brendan Cully <brendan@kublai.com>
parents: 5143
diff changeset
    29
        self.lastbranch = None
5441
71e7c86adcb7 convert: refactor sink initialisation, to remove hardcoding of hg
Bryan O'Sullivan <bos@serpentine.com>
parents: 5440
diff changeset
    30
        if os.path.isdir(path) and len(os.listdir(path)) > 0:
71e7c86adcb7 convert: refactor sink initialisation, to remove hardcoding of hg
Bryan O'Sullivan <bos@serpentine.com>
parents: 5440
diff changeset
    31
            try:
71e7c86adcb7 convert: refactor sink initialisation, to remove hardcoding of hg
Bryan O'Sullivan <bos@serpentine.com>
parents: 5440
diff changeset
    32
                self.repo = hg.repository(self.ui, path)
5918
1716c8a0bd09 convert: mercurial sink must be local
Patrick Mezard <pmezard@gmail.com>
parents: 5522
diff changeset
    33
                if not self.repo.local():
1716c8a0bd09 convert: mercurial sink must be local
Patrick Mezard <pmezard@gmail.com>
parents: 5522
diff changeset
    34
                    raise NoRepo(_('%s is not a local Mercurial repo') % path)
7637
1d54e2f6c0b7 error: move repo errors
Matt Mackall <mpm@selenic.com>
parents: 7633
diff changeset
    35
            except error.RepoError, err:
5441
71e7c86adcb7 convert: refactor sink initialisation, to remove hardcoding of hg
Bryan O'Sullivan <bos@serpentine.com>
parents: 5440
diff changeset
    36
                ui.print_exc()
71e7c86adcb7 convert: refactor sink initialisation, to remove hardcoding of hg
Bryan O'Sullivan <bos@serpentine.com>
parents: 5440
diff changeset
    37
                raise NoRepo(err.args[0])
71e7c86adcb7 convert: refactor sink initialisation, to remove hardcoding of hg
Bryan O'Sullivan <bos@serpentine.com>
parents: 5440
diff changeset
    38
        else:
71e7c86adcb7 convert: refactor sink initialisation, to remove hardcoding of hg
Bryan O'Sullivan <bos@serpentine.com>
parents: 5440
diff changeset
    39
            try:
71e7c86adcb7 convert: refactor sink initialisation, to remove hardcoding of hg
Bryan O'Sullivan <bos@serpentine.com>
parents: 5440
diff changeset
    40
                ui.status(_('initializing destination %s repository\n') % path)
71e7c86adcb7 convert: refactor sink initialisation, to remove hardcoding of hg
Bryan O'Sullivan <bos@serpentine.com>
parents: 5440
diff changeset
    41
                self.repo = hg.repository(self.ui, path, create=True)
5918
1716c8a0bd09 convert: mercurial sink must be local
Patrick Mezard <pmezard@gmail.com>
parents: 5522
diff changeset
    42
                if not self.repo.local():
1716c8a0bd09 convert: mercurial sink must be local
Patrick Mezard <pmezard@gmail.com>
parents: 5522
diff changeset
    43
                    raise NoRepo(_('%s is not a local Mercurial repo') % path)
5441
71e7c86adcb7 convert: refactor sink initialisation, to remove hardcoding of hg
Bryan O'Sullivan <bos@serpentine.com>
parents: 5440
diff changeset
    44
                self.created.append(path)
7637
1d54e2f6c0b7 error: move repo errors
Matt Mackall <mpm@selenic.com>
parents: 7633
diff changeset
    45
            except error.RepoError, err:
5441
71e7c86adcb7 convert: refactor sink initialisation, to remove hardcoding of hg
Bryan O'Sullivan <bos@serpentine.com>
parents: 5440
diff changeset
    46
                ui.print_exc()
71e7c86adcb7 convert: refactor sink initialisation, to remove hardcoding of hg
Bryan O'Sullivan <bos@serpentine.com>
parents: 5440
diff changeset
    47
                raise NoRepo("could not create hg repo %s as sink" % path)
5014
914054ca532e convert: acquire/release locks periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 5013
diff changeset
    48
        self.lock = None
914054ca532e convert: acquire/release locks periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 5013
diff changeset
    49
        self.wlock = None
5378
8a2915f57dfc convert: add a mode where mercurial_sink skips empty revisions.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5360
diff changeset
    50
        self.filemapmode = False
5014
914054ca532e convert: acquire/release locks periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 5013
diff changeset
    51
914054ca532e convert: acquire/release locks periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 5013
diff changeset
    52
    def before(self):
5805
e422305e0853 test-convert: test before() and after() conversion actions
Patrick Mezard <pmezard@gmail.com>
parents: 5556
diff changeset
    53
        self.ui.debug(_('run hg sink pre-conversion action\n'))
5052
a11e8a181bd5 convert: fix locking order
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5038
diff changeset
    54
        self.wlock = self.repo.wlock()
5014
914054ca532e convert: acquire/release locks periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 5013
diff changeset
    55
        self.lock = self.repo.lock()
914054ca532e convert: acquire/release locks periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 5013
diff changeset
    56
914054ca532e convert: acquire/release locks periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 5013
diff changeset
    57
    def after(self):
5805
e422305e0853 test-convert: test before() and after() conversion actions
Patrick Mezard <pmezard@gmail.com>
parents: 5556
diff changeset
    58
        self.ui.debug(_('run hg sink post-conversion action\n'))
5014
914054ca532e convert: acquire/release locks periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 5013
diff changeset
    59
        self.lock = None
914054ca532e convert: acquire/release locks periodically
Bryan O'Sullivan <bos@serpentine.com>
parents: 5013
diff changeset
    60
        self.wlock = None
3938
0fab73b3f453 convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents: 3917
diff changeset
    61
5011
89fbb0a5e8e3 convert: rename mapfile to revmapfile, so we can map more than just revs
Bryan O'Sullivan <bos@serpentine.com>
parents: 4965
diff changeset
    62
    def revmapfile(self):
3938
0fab73b3f453 convert-repo: add some smarts
Matt Mackall <mpm@selenic.com>
parents: 3917
diff changeset
    63
        return os.path.join(self.path, ".hg", "shamap")
316
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    64
4589
451e91ed535e convert extension: Add support for username mapping
Edouard Gomez <ed.gomez@free.fr>
parents: 4536
diff changeset
    65
    def authorfile(self):
451e91ed535e convert extension: Add support for username mapping
Edouard Gomez <ed.gomez@free.fr>
parents: 4536
diff changeset
    66
        return os.path.join(self.path, ".hg", "authormap")
451e91ed535e convert extension: Add support for username mapping
Edouard Gomez <ed.gomez@free.fr>
parents: 4536
diff changeset
    67
316
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    68
    def getheads(self):
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
    69
        h = self.repo.changelog.heads()
5017
06329efa722d convert: get rid of "hg." prefix where not needed
Bryan O'Sullivan <bos@serpentine.com>
parents: 5016
diff changeset
    70
        return [ hex(x) for x in h ]
692
695dd9a491da convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents: 450
diff changeset
    71
5934
e495f3f35b2d convert: hg.clonebranches must pull missing parents (issue941)
Patrick Mezard <pmezard@gmail.com>
parents: 5918
diff changeset
    72
    def setbranch(self, branch, pbranches):
e495f3f35b2d convert: hg.clonebranches must pull missing parents (issue941)
Patrick Mezard <pmezard@gmail.com>
parents: 5918
diff changeset
    73
        if not self.clonebranches:
5173
6b4c332f241b convert: hg: optionally create branches as clones
Brendan Cully <brendan@kublai.com>
parents: 5143
diff changeset
    74
            return
6b4c332f241b convert: hg: optionally create branches as clones
Brendan Cully <brendan@kublai.com>
parents: 5143
diff changeset
    75
5934
e495f3f35b2d convert: hg.clonebranches must pull missing parents (issue941)
Patrick Mezard <pmezard@gmail.com>
parents: 5918
diff changeset
    76
        setbranch = (branch != self.lastbranch)
5173
6b4c332f241b convert: hg: optionally create branches as clones
Brendan Cully <brendan@kublai.com>
parents: 5143
diff changeset
    77
        self.lastbranch = branch
6b4c332f241b convert: hg: optionally create branches as clones
Brendan Cully <brendan@kublai.com>
parents: 5143
diff changeset
    78
        if not branch:
6b4c332f241b convert: hg: optionally create branches as clones
Brendan Cully <brendan@kublai.com>
parents: 5143
diff changeset
    79
            branch = 'default'
5934
e495f3f35b2d convert: hg.clonebranches must pull missing parents (issue941)
Patrick Mezard <pmezard@gmail.com>
parents: 5918
diff changeset
    80
        pbranches = [(b[0], b[1] and b[1] or 'default') for b in pbranches]
e495f3f35b2d convert: hg.clonebranches must pull missing parents (issue941)
Patrick Mezard <pmezard@gmail.com>
parents: 5918
diff changeset
    81
        pbranch = pbranches and pbranches[0][1] or 'default'
5173
6b4c332f241b convert: hg: optionally create branches as clones
Brendan Cully <brendan@kublai.com>
parents: 5143
diff changeset
    82
6b4c332f241b convert: hg: optionally create branches as clones
Brendan Cully <brendan@kublai.com>
parents: 5143
diff changeset
    83
        branchpath = os.path.join(self.path, branch)
5934
e495f3f35b2d convert: hg.clonebranches must pull missing parents (issue941)
Patrick Mezard <pmezard@gmail.com>
parents: 5918
diff changeset
    84
        if setbranch:
e495f3f35b2d convert: hg.clonebranches must pull missing parents (issue941)
Patrick Mezard <pmezard@gmail.com>
parents: 5918
diff changeset
    85
            self.after()
e495f3f35b2d convert: hg.clonebranches must pull missing parents (issue941)
Patrick Mezard <pmezard@gmail.com>
parents: 5918
diff changeset
    86
            try:
e495f3f35b2d convert: hg.clonebranches must pull missing parents (issue941)
Patrick Mezard <pmezard@gmail.com>
parents: 5918
diff changeset
    87
                self.repo = hg.repository(self.ui, branchpath)
e495f3f35b2d convert: hg.clonebranches must pull missing parents (issue941)
Patrick Mezard <pmezard@gmail.com>
parents: 5918
diff changeset
    88
            except:
5173
6b4c332f241b convert: hg: optionally create branches as clones
Brendan Cully <brendan@kublai.com>
parents: 5143
diff changeset
    89
                self.repo = hg.repository(self.ui, branchpath, create=True)
5934
e495f3f35b2d convert: hg.clonebranches must pull missing parents (issue941)
Patrick Mezard <pmezard@gmail.com>
parents: 5918
diff changeset
    90
            self.before()
e495f3f35b2d convert: hg.clonebranches must pull missing parents (issue941)
Patrick Mezard <pmezard@gmail.com>
parents: 5918
diff changeset
    91
e495f3f35b2d convert: hg.clonebranches must pull missing parents (issue941)
Patrick Mezard <pmezard@gmail.com>
parents: 5918
diff changeset
    92
        # pbranches may bring revisions from other branches (merge parents)
e495f3f35b2d convert: hg.clonebranches must pull missing parents (issue941)
Patrick Mezard <pmezard@gmail.com>
parents: 5918
diff changeset
    93
        # Make sure we have them, or pull them.
e495f3f35b2d convert: hg.clonebranches must pull missing parents (issue941)
Patrick Mezard <pmezard@gmail.com>
parents: 5918
diff changeset
    94
        missings = {}
e495f3f35b2d convert: hg.clonebranches must pull missing parents (issue941)
Patrick Mezard <pmezard@gmail.com>
parents: 5918
diff changeset
    95
        for b in pbranches:
e495f3f35b2d convert: hg.clonebranches must pull missing parents (issue941)
Patrick Mezard <pmezard@gmail.com>
parents: 5918
diff changeset
    96
            try:
e495f3f35b2d convert: hg.clonebranches must pull missing parents (issue941)
Patrick Mezard <pmezard@gmail.com>
parents: 5918
diff changeset
    97
                self.repo.lookup(b[0])
e495f3f35b2d convert: hg.clonebranches must pull missing parents (issue941)
Patrick Mezard <pmezard@gmail.com>
parents: 5918
diff changeset
    98
            except:
e495f3f35b2d convert: hg.clonebranches must pull missing parents (issue941)
Patrick Mezard <pmezard@gmail.com>
parents: 5918
diff changeset
    99
                missings.setdefault(b[1], []).append(b[0])
6210
942287cb1f57 Removed trailing spaces from everything except test output
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5959
diff changeset
   100
5934
e495f3f35b2d convert: hg.clonebranches must pull missing parents (issue941)
Patrick Mezard <pmezard@gmail.com>
parents: 5918
diff changeset
   101
        if missings:
e495f3f35b2d convert: hg.clonebranches must pull missing parents (issue941)
Patrick Mezard <pmezard@gmail.com>
parents: 5918
diff changeset
   102
            self.after()
e495f3f35b2d convert: hg.clonebranches must pull missing parents (issue941)
Patrick Mezard <pmezard@gmail.com>
parents: 5918
diff changeset
   103
            for pbranch, heads in missings.iteritems():
e495f3f35b2d convert: hg.clonebranches must pull missing parents (issue941)
Patrick Mezard <pmezard@gmail.com>
parents: 5918
diff changeset
   104
                pbranchpath = os.path.join(self.path, pbranch)
e495f3f35b2d convert: hg.clonebranches must pull missing parents (issue941)
Patrick Mezard <pmezard@gmail.com>
parents: 5918
diff changeset
   105
                prepo = hg.repository(self.ui, pbranchpath)
e495f3f35b2d convert: hg.clonebranches must pull missing parents (issue941)
Patrick Mezard <pmezard@gmail.com>
parents: 5918
diff changeset
   106
                self.ui.note(_('pulling from %s into %s\n') % (pbranch, branch))
e495f3f35b2d convert: hg.clonebranches must pull missing parents (issue941)
Patrick Mezard <pmezard@gmail.com>
parents: 5918
diff changeset
   107
                self.repo.pull(prepo, [prepo.lookup(h) for h in heads])
e495f3f35b2d convert: hg.clonebranches must pull missing parents (issue941)
Patrick Mezard <pmezard@gmail.com>
parents: 5918
diff changeset
   108
            self.before()
5173
6b4c332f241b convert: hg: optionally create branches as clones
Brendan Cully <brendan@kublai.com>
parents: 5143
diff changeset
   109
6716
c9b8d2565b92 convert: reintegrate file retrieval code in sinks
Patrick Mezard <pmezard@gmail.com>
parents: 6217
diff changeset
   110
    def putcommit(self, files, copies, parents, commit, source):
c9b8d2565b92 convert: reintegrate file retrieval code in sinks
Patrick Mezard <pmezard@gmail.com>
parents: 6217
diff changeset
   111
6717
2011bb8ada9a convert: hg sink commits without working dir
Patrick Mezard <pmezard@gmail.com>
parents: 6716
diff changeset
   112
        files = dict(files)
2011bb8ada9a convert: hg sink commits without working dir
Patrick Mezard <pmezard@gmail.com>
parents: 6716
diff changeset
   113
        def getfilectx(repo, memctx, f):
2011bb8ada9a convert: hg sink commits without working dir
Patrick Mezard <pmezard@gmail.com>
parents: 6716
diff changeset
   114
            v = files[f]
2011bb8ada9a convert: hg sink commits without working dir
Patrick Mezard <pmezard@gmail.com>
parents: 6716
diff changeset
   115
            data = source.getfile(f, v)
2011bb8ada9a convert: hg sink commits without working dir
Patrick Mezard <pmezard@gmail.com>
parents: 6716
diff changeset
   116
            e = source.getmode(f, v)
2011bb8ada9a convert: hg sink commits without working dir
Patrick Mezard <pmezard@gmail.com>
parents: 6716
diff changeset
   117
            return context.memfilectx(f, data, 'l' in e, 'x' in e, copies.get(f))
2011bb8ada9a convert: hg sink commits without working dir
Patrick Mezard <pmezard@gmail.com>
parents: 6716
diff changeset
   118
431
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   119
        pl = []
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   120
        for p in parents:
6717
2011bb8ada9a convert: hg sink commits without working dir
Patrick Mezard <pmezard@gmail.com>
parents: 6716
diff changeset
   121
            if p not in pl:
431
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   122
                pl.append(p)
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   123
        parents = pl
5378
8a2915f57dfc convert: add a mode where mercurial_sink skips empty revisions.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5360
diff changeset
   124
        nparents = len(parents)
8a2915f57dfc convert: add a mode where mercurial_sink skips empty revisions.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5360
diff changeset
   125
        if self.filemapmode and nparents == 1:
8a2915f57dfc convert: add a mode where mercurial_sink skips empty revisions.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5360
diff changeset
   126
            m1node = self.repo.changelog.read(bin(parents[0]))[0]
8a2915f57dfc convert: add a mode where mercurial_sink skips empty revisions.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5360
diff changeset
   127
            parent = parents[0]
316
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   128
692
695dd9a491da convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents: 450
diff changeset
   129
        if len(parents) < 2: parents.append("0" * 40)
695dd9a491da convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents: 450
diff changeset
   130
        if len(parents) < 2: parents.append("0" * 40)
431
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   131
        p2 = parents.pop(0)
692
695dd9a491da convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents: 450
diff changeset
   132
3954
9af4b853ed4d convert-repo: add CVS branch support
Matt Mackall <mpm@selenic.com>
parents: 3953
diff changeset
   133
        text = commit.desc
5439
d0c67b52ac01 convert: make contents of "extra" dict available from sources, for sinks.
Bryan O'Sullivan <bos@serpentine.com>
parents: 5437
diff changeset
   134
        extra = commit.extra.copy()
5038
8f157190075e convert: add config option to turn off use of branch names
Bryan O'Sullivan <bos@serpentine.com>
parents: 5017
diff changeset
   135
        if self.branchnames and commit.branch:
4873
28b23b9073a8 convert: record the source revision in the changelog
Brendan Cully <brendan@kublai.com>
parents: 4765
diff changeset
   136
            extra['branch'] = commit.branch
28b23b9073a8 convert: record the source revision in the changelog
Brendan Cully <brendan@kublai.com>
parents: 4765
diff changeset
   137
        if commit.rev:
28b23b9073a8 convert: record the source revision in the changelog
Brendan Cully <brendan@kublai.com>
parents: 4765
diff changeset
   138
            extra['convert_revision'] = commit.rev
4957
cdd33a048289 removed trailing whitespace
Thomas Arendsen Hein <thomas@intevation.de>
parents: 4873
diff changeset
   139
431
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   140
        while parents:
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   141
            p1 = p2
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   142
            p2 = parents.pop(0)
6717
2011bb8ada9a convert: hg sink commits without working dir
Patrick Mezard <pmezard@gmail.com>
parents: 6716
diff changeset
   143
            ctx = context.memctx(self.repo, (p1, p2), text, files.keys(), getfilectx,
2011bb8ada9a convert: hg sink commits without working dir
Patrick Mezard <pmezard@gmail.com>
parents: 6716
diff changeset
   144
                                 commit.author, commit.date, extra)
2011bb8ada9a convert: hg sink commits without working dir
Patrick Mezard <pmezard@gmail.com>
parents: 6716
diff changeset
   145
            a = self.repo.commitctx(ctx)
431
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   146
            text = "(octopus merge fixup)\n"
6217
fe8dbbe9520d Avoid importing mercurial.node/mercurial.repo stuff from mercurial.hg
Joel Rosdahl <joel@rosdahl.net>
parents: 6212
diff changeset
   147
            p2 = hex(self.repo.changelog.tip())
431
dfc44f3f587c convert-repo fixups
mpm@selenic.com
parents: 316
diff changeset
   148
5378
8a2915f57dfc convert: add a mode where mercurial_sink skips empty revisions.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5360
diff changeset
   149
        if self.filemapmode and nparents == 1:
8a2915f57dfc convert: add a mode where mercurial_sink skips empty revisions.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5360
diff changeset
   150
            man = self.repo.manifest
8a2915f57dfc convert: add a mode where mercurial_sink skips empty revisions.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5360
diff changeset
   151
            mnode = self.repo.changelog.read(bin(p2))[0]
8a2915f57dfc convert: add a mode where mercurial_sink skips empty revisions.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5360
diff changeset
   152
            if not man.cmp(m1node, man.revision(mnode)):
8a2915f57dfc convert: add a mode where mercurial_sink skips empty revisions.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5360
diff changeset
   153
                self.repo.rollback()
8a2915f57dfc convert: add a mode where mercurial_sink skips empty revisions.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5360
diff changeset
   154
                return parent
1389
9b3ef6f3cef5 convert-repo: fix up octopus merge conversion
Matt Mackall <mpm@selenic.com>
parents: 1388
diff changeset
   155
        return p2
316
c48d069163d6 Add new convert-repo script
mpm@selenic.com
parents:
diff changeset
   156
694
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
   157
    def puttags(self, tags):
6717
2011bb8ada9a convert: hg sink commits without working dir
Patrick Mezard <pmezard@gmail.com>
parents: 6716
diff changeset
   158
         try:
6747
f6c00b17387c use repo[changeid] to get a changectx
Matt Mackall <mpm@selenic.com>
parents: 6717
diff changeset
   159
             parentctx = self.repo[self.tagsbranch]
6717
2011bb8ada9a convert: hg sink commits without working dir
Patrick Mezard <pmezard@gmail.com>
parents: 6716
diff changeset
   160
             tagparent = parentctx.node()
7637
1d54e2f6c0b7 error: move repo errors
Matt Mackall <mpm@selenic.com>
parents: 7633
diff changeset
   161
         except error.RepoError, inst:
6717
2011bb8ada9a convert: hg sink commits without working dir
Patrick Mezard <pmezard@gmail.com>
parents: 6716
diff changeset
   162
             parentctx = None
2011bb8ada9a convert: hg sink commits without working dir
Patrick Mezard <pmezard@gmail.com>
parents: 6716
diff changeset
   163
             tagparent = nullid
694
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
   164
6717
2011bb8ada9a convert: hg sink commits without working dir
Patrick Mezard <pmezard@gmail.com>
parents: 6716
diff changeset
   165
         try:
6762
f67d1468ac50 util: add sort helper
Matt Mackall <mpm@selenic.com>
parents: 6749
diff changeset
   166
             oldlines = util.sort(parentctx['.hgtags'].data().splitlines(1))
6717
2011bb8ada9a convert: hg sink commits without working dir
Patrick Mezard <pmezard@gmail.com>
parents: 6716
diff changeset
   167
         except:
2011bb8ada9a convert: hg sink commits without working dir
Patrick Mezard <pmezard@gmail.com>
parents: 6716
diff changeset
   168
             oldlines = []
694
51eb248d3348 Teach convert-repo about tags
mpm@selenic.com
parents: 692
diff changeset
   169
6762
f67d1468ac50 util: add sort helper
Matt Mackall <mpm@selenic.com>
parents: 6749
diff changeset
   170
         newlines = util.sort([("%s %s\n" % (tags[tag], tag)) for tag in tags])
6717
2011bb8ada9a convert: hg sink commits without working dir
Patrick Mezard <pmezard@gmail.com>
parents: 6716
diff changeset
   171
2011bb8ada9a convert: hg sink commits without working dir
Patrick Mezard <pmezard@gmail.com>
parents: 6716
diff changeset
   172
         if newlines == oldlines:
2011bb8ada9a convert: hg sink commits without working dir
Patrick Mezard <pmezard@gmail.com>
parents: 6716
diff changeset
   173
             return None
2011bb8ada9a convert: hg sink commits without working dir
Patrick Mezard <pmezard@gmail.com>
parents: 6716
diff changeset
   174
         data = "".join(newlines)
2011bb8ada9a convert: hg sink commits without working dir
Patrick Mezard <pmezard@gmail.com>
parents: 6716
diff changeset
   175
2011bb8ada9a convert: hg sink commits without working dir
Patrick Mezard <pmezard@gmail.com>
parents: 6716
diff changeset
   176
         def getfilectx(repo, memctx, f):
2011bb8ada9a convert: hg sink commits without working dir
Patrick Mezard <pmezard@gmail.com>
parents: 6716
diff changeset
   177
            return context.memfilectx(f, data, False, False, None)
2011bb8ada9a convert: hg sink commits without working dir
Patrick Mezard <pmezard@gmail.com>
parents: 6716
diff changeset
   178
6956
12472a240398 i18n: mark strings for translation in convert extension
Martin Geisler <mg@daimi.au.dk>
parents: 6923
diff changeset
   179
         self.ui.status(_("updating tags\n"))
6717
2011bb8ada9a convert: hg sink commits without working dir
Patrick Mezard <pmezard@gmail.com>
parents: 6716
diff changeset
   180
         date = "%s 0" % int(time.mktime(time.gmtime()))
2011bb8ada9a convert: hg sink commits without working dir
Patrick Mezard <pmezard@gmail.com>
parents: 6716
diff changeset
   181
         extra = {'branch': self.tagsbranch}
2011bb8ada9a convert: hg sink commits without working dir
Patrick Mezard <pmezard@gmail.com>
parents: 6716
diff changeset
   182
         ctx = context.memctx(self.repo, (tagparent, None), "update tags",
2011bb8ada9a convert: hg sink commits without working dir
Patrick Mezard <pmezard@gmail.com>
parents: 6716
diff changeset
   183
                              [".hgtags"], getfilectx, "convert-repo", date,
2011bb8ada9a convert: hg sink commits without working dir
Patrick Mezard <pmezard@gmail.com>
parents: 6716
diff changeset
   184
                              extra)
2011bb8ada9a convert: hg sink commits without working dir
Patrick Mezard <pmezard@gmail.com>
parents: 6716
diff changeset
   185
         self.repo.commitctx(ctx)
2011bb8ada9a convert: hg sink commits without working dir
Patrick Mezard <pmezard@gmail.com>
parents: 6716
diff changeset
   186
         return hex(self.repo.changelog.tip())
5013
6c1029aacc9a convert: Support Mercurial as a source, as well as a sink
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
   187
5378
8a2915f57dfc convert: add a mode where mercurial_sink skips empty revisions.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5360
diff changeset
   188
    def setfilemapmode(self, active):
8a2915f57dfc convert: add a mode where mercurial_sink skips empty revisions.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5360
diff changeset
   189
        self.filemapmode = active
8a2915f57dfc convert: add a mode where mercurial_sink skips empty revisions.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5360
diff changeset
   190
5013
6c1029aacc9a convert: Support Mercurial as a source, as well as a sink
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
   191
class mercurial_source(converter_source):
6c1029aacc9a convert: Support Mercurial as a source, as well as a sink
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
   192
    def __init__(self, ui, path, rev=None):
6c1029aacc9a convert: Support Mercurial as a source, as well as a sink
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
   193
        converter_source.__init__(self, ui, path, rev)
7231
8e7130a10f3b convert: ignore hg source errors with hg.ignoreerrors (issue 1357)
Patrick Mezard <pmezard@gmail.com>
parents: 6956
diff changeset
   194
        self.ignoreerrors = ui.configbool('convert', 'hg.ignoreerrors', False)
8e7130a10f3b convert: ignore hg source errors with hg.ignoreerrors (issue 1357)
Patrick Mezard <pmezard@gmail.com>
parents: 6956
diff changeset
   195
        self.ignored = {}
5556
61fdf2558c0a convert: some tidyups, doc improvements, and test fixes
Bryan O'Sullivan <bos@serpentine.com>
parents: 5554
diff changeset
   196
        self.saverev = ui.configbool('convert', 'hg.saverev', True)
5358
4fbd27bf04b1 convert: fail properly if we can't read a source hg repository
Bryan O'Sullivan <bos@serpentine.com>
parents: 5352
diff changeset
   197
        try:
4fbd27bf04b1 convert: fail properly if we can't read a source hg repository
Bryan O'Sullivan <bos@serpentine.com>
parents: 5352
diff changeset
   198
            self.repo = hg.repository(self.ui, path)
5437
4d34f8b12a9e convert: report errors more meaningfully if run with --traceback
Bryan O'Sullivan <bos@serpentine.com>
parents: 5402
diff changeset
   199
            # try to provoke an exception if this isn't really a hg
4d34f8b12a9e convert: report errors more meaningfully if run with --traceback
Bryan O'Sullivan <bos@serpentine.com>
parents: 5402
diff changeset
   200
            # repo, but some other bogus compatible-looking url
5522
f5345a2d2391 convert: make sure mercurial_source has a local hg repo
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5521
diff changeset
   201
            if not self.repo.local():
7637
1d54e2f6c0b7 error: move repo errors
Matt Mackall <mpm@selenic.com>
parents: 7633
diff changeset
   202
                raise error.RepoError()
1d54e2f6c0b7 error: move repo errors
Matt Mackall <mpm@selenic.com>
parents: 7633
diff changeset
   203
        except error.RepoError:
5437
4d34f8b12a9e convert: report errors more meaningfully if run with --traceback
Bryan O'Sullivan <bos@serpentine.com>
parents: 5402
diff changeset
   204
            ui.print_exc()
5522
f5345a2d2391 convert: make sure mercurial_source has a local hg repo
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5521
diff changeset
   205
            raise NoRepo("%s is not a local Mercurial repo" % path)
5013
6c1029aacc9a convert: Support Mercurial as a source, as well as a sink
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
   206
        self.lastrev = None
6c1029aacc9a convert: Support Mercurial as a source, as well as a sink
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
   207
        self.lastctx = None
5379
d3e51dc804f8 mercurial_source: add --filemap support
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5378
diff changeset
   208
        self._changescache = None
5554
2147a734dcf9 convert: tell the source repository when a rev has been converted
Bryan O'Sullivan <bos@serpentine.com>
parents: 5553
diff changeset
   209
        self.convertfp = None
6885
6e253aa04ff7 convert: implement startrev for hg source
Patrick Mezard <pmezard@gmail.com>
parents: 6762
diff changeset
   210
        # Restrict converted revisions to startrev descendants
6e253aa04ff7 convert: implement startrev for hg source
Patrick Mezard <pmezard@gmail.com>
parents: 6762
diff changeset
   211
        startnode = ui.config('convert', 'hg.startrev')
6e253aa04ff7 convert: implement startrev for hg source
Patrick Mezard <pmezard@gmail.com>
parents: 6762
diff changeset
   212
        if startnode is not None:
6e253aa04ff7 convert: implement startrev for hg source
Patrick Mezard <pmezard@gmail.com>
parents: 6762
diff changeset
   213
            try:
6e253aa04ff7 convert: implement startrev for hg source
Patrick Mezard <pmezard@gmail.com>
parents: 6762
diff changeset
   214
                startnode = self.repo.lookup(startnode)
7637
1d54e2f6c0b7 error: move repo errors
Matt Mackall <mpm@selenic.com>
parents: 7633
diff changeset
   215
            except error.RepoError:
6923
ebf1462f2145 strip trailing whitespace, replace tabs by spaces
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6885
diff changeset
   216
                raise util.Abort(_('%s is not a valid start revision')
6885
6e253aa04ff7 convert: implement startrev for hg source
Patrick Mezard <pmezard@gmail.com>
parents: 6762
diff changeset
   217
                                 % startnode)
6e253aa04ff7 convert: implement startrev for hg source
Patrick Mezard <pmezard@gmail.com>
parents: 6762
diff changeset
   218
            startrev = self.repo.changelog.rev(startnode)
6e253aa04ff7 convert: implement startrev for hg source
Patrick Mezard <pmezard@gmail.com>
parents: 6762
diff changeset
   219
            children = {startnode: 1}
6e253aa04ff7 convert: implement startrev for hg source
Patrick Mezard <pmezard@gmail.com>
parents: 6762
diff changeset
   220
            for rev in self.repo.changelog.descendants(startrev):
6e253aa04ff7 convert: implement startrev for hg source
Patrick Mezard <pmezard@gmail.com>
parents: 6762
diff changeset
   221
                children[self.repo.changelog.node(rev)] = 1
6e253aa04ff7 convert: implement startrev for hg source
Patrick Mezard <pmezard@gmail.com>
parents: 6762
diff changeset
   222
            self.keep = children.__contains__
6e253aa04ff7 convert: implement startrev for hg source
Patrick Mezard <pmezard@gmail.com>
parents: 6762
diff changeset
   223
        else:
6e253aa04ff7 convert: implement startrev for hg source
Patrick Mezard <pmezard@gmail.com>
parents: 6762
diff changeset
   224
            self.keep = util.always
5013
6c1029aacc9a convert: Support Mercurial as a source, as well as a sink
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
   225
6c1029aacc9a convert: Support Mercurial as a source, as well as a sink
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
   226
    def changectx(self, rev):
6c1029aacc9a convert: Support Mercurial as a source, as well as a sink
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
   227
        if self.lastrev != rev:
6747
f6c00b17387c use repo[changeid] to get a changectx
Matt Mackall <mpm@selenic.com>
parents: 6717
diff changeset
   228
            self.lastctx = self.repo[rev]
5013
6c1029aacc9a convert: Support Mercurial as a source, as well as a sink
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
   229
            self.lastrev = rev
6c1029aacc9a convert: Support Mercurial as a source, as well as a sink
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
   230
        return self.lastctx
6c1029aacc9a convert: Support Mercurial as a source, as well as a sink
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
   231
6885
6e253aa04ff7 convert: implement startrev for hg source
Patrick Mezard <pmezard@gmail.com>
parents: 6762
diff changeset
   232
    def parents(self, ctx):
6923
ebf1462f2145 strip trailing whitespace, replace tabs by spaces
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents: 6885
diff changeset
   233
        return [p.node() for p in ctx.parents()
6885
6e253aa04ff7 convert: implement startrev for hg source
Patrick Mezard <pmezard@gmail.com>
parents: 6762
diff changeset
   234
                if p and self.keep(p.node())]
6e253aa04ff7 convert: implement startrev for hg source
Patrick Mezard <pmezard@gmail.com>
parents: 6762
diff changeset
   235
5013
6c1029aacc9a convert: Support Mercurial as a source, as well as a sink
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
   236
    def getheads(self):
5131
2f9edf6bae95 convert: only get history for requested revs when converting hg repo
Bryan O'Sullivan <bos@serpentine.com>
parents: 5121
diff changeset
   237
        if self.rev:
6885
6e253aa04ff7 convert: implement startrev for hg source
Patrick Mezard <pmezard@gmail.com>
parents: 6762
diff changeset
   238
            heads = [self.repo[self.rev].node()]
5131
2f9edf6bae95 convert: only get history for requested revs when converting hg repo
Bryan O'Sullivan <bos@serpentine.com>
parents: 5121
diff changeset
   239
        else:
6885
6e253aa04ff7 convert: implement startrev for hg source
Patrick Mezard <pmezard@gmail.com>
parents: 6762
diff changeset
   240
            heads = self.repo.heads()
6e253aa04ff7 convert: implement startrev for hg source
Patrick Mezard <pmezard@gmail.com>
parents: 6762
diff changeset
   241
        return [hex(h) for h in heads if self.keep(h)]
5013
6c1029aacc9a convert: Support Mercurial as a source, as well as a sink
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
   242
6c1029aacc9a convert: Support Mercurial as a source, as well as a sink
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
   243
    def getfile(self, name, rev):
6c1029aacc9a convert: Support Mercurial as a source, as well as a sink
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
   244
        try:
6747
f6c00b17387c use repo[changeid] to get a changectx
Matt Mackall <mpm@selenic.com>
parents: 6717
diff changeset
   245
            return self.changectx(rev)[name].data()
7633
08cabecfa8a8 errors: move revlog errors
Matt Mackall <mpm@selenic.com>
parents: 7236
diff changeset
   246
        except error.LookupError, err:
5013
6c1029aacc9a convert: Support Mercurial as a source, as well as a sink
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
   247
            raise IOError(err)
6c1029aacc9a convert: Support Mercurial as a source, as well as a sink
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
   248
6c1029aacc9a convert: Support Mercurial as a source, as well as a sink
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
   249
    def getmode(self, name, rev):
6749
51b0e799352f manifest: remove execf/linkf methods
Matt Mackall <mpm@selenic.com>
parents: 6747
diff changeset
   250
        return self.changectx(rev).manifest().flags(name)
5013
6c1029aacc9a convert: Support Mercurial as a source, as well as a sink
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
   251
6c1029aacc9a convert: Support Mercurial as a source, as well as a sink
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
   252
    def getchanges(self, rev):
6c1029aacc9a convert: Support Mercurial as a source, as well as a sink
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
   253
        ctx = self.changectx(rev)
6885
6e253aa04ff7 convert: implement startrev for hg source
Patrick Mezard <pmezard@gmail.com>
parents: 6762
diff changeset
   254
        parents = self.parents(ctx)
6e253aa04ff7 convert: implement startrev for hg source
Patrick Mezard <pmezard@gmail.com>
parents: 6762
diff changeset
   255
        if not parents:
6e253aa04ff7 convert: implement startrev for hg source
Patrick Mezard <pmezard@gmail.com>
parents: 6762
diff changeset
   256
            files = util.sort(ctx.manifest().keys())
7232
c2ac09f81ec9 convert: correctly detect missing revlog for root revisions
Patrick Mezard <pmezard@gmail.com>
parents: 7231
diff changeset
   257
            if self.ignoreerrors:
c2ac09f81ec9 convert: correctly detect missing revlog for root revisions
Patrick Mezard <pmezard@gmail.com>
parents: 7231
diff changeset
   258
                # calling getcopies() is a simple way to detect missing
c2ac09f81ec9 convert: correctly detect missing revlog for root revisions
Patrick Mezard <pmezard@gmail.com>
parents: 7231
diff changeset
   259
                # revlogs and populate self.ignored
c2ac09f81ec9 convert: correctly detect missing revlog for root revisions
Patrick Mezard <pmezard@gmail.com>
parents: 7231
diff changeset
   260
                self.getcopies(ctx, files)
7231
8e7130a10f3b convert: ignore hg source errors with hg.ignoreerrors (issue 1357)
Patrick Mezard <pmezard@gmail.com>
parents: 6956
diff changeset
   261
            return [(f, rev) for f in files if f not in self.ignored], {}
5379
d3e51dc804f8 mercurial_source: add --filemap support
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5378
diff changeset
   262
        if self._changescache and self._changescache[0] == rev:
d3e51dc804f8 mercurial_source: add --filemap support
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5378
diff changeset
   263
            m, a, r = self._changescache[1]
d3e51dc804f8 mercurial_source: add --filemap support
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5378
diff changeset
   264
        else:
6885
6e253aa04ff7 convert: implement startrev for hg source
Patrick Mezard <pmezard@gmail.com>
parents: 6762
diff changeset
   265
            m, a, r = self.repo.status(parents[0], ctx.node())[:3]
7231
8e7130a10f3b convert: ignore hg source errors with hg.ignoreerrors (issue 1357)
Patrick Mezard <pmezard@gmail.com>
parents: 6956
diff changeset
   266
        # getcopies() detects missing revlogs early, run it before
8e7130a10f3b convert: ignore hg source errors with hg.ignoreerrors (issue 1357)
Patrick Mezard <pmezard@gmail.com>
parents: 6956
diff changeset
   267
        # filtering the changes.
8e7130a10f3b convert: ignore hg source errors with hg.ignoreerrors (issue 1357)
Patrick Mezard <pmezard@gmail.com>
parents: 6956
diff changeset
   268
        copies = self.getcopies(ctx, m + a)
7236
db6fbb785800 Remove trailing spaces.
Thomas Arendsen Hein <thomas@intevation.de>
parents: 7232
diff changeset
   269
        changes = [(name, rev) for name in m + a + r
7231
8e7130a10f3b convert: ignore hg source errors with hg.ignoreerrors (issue 1357)
Patrick Mezard <pmezard@gmail.com>
parents: 6956
diff changeset
   270
                   if name not in self.ignored]
8e7130a10f3b convert: ignore hg source errors with hg.ignoreerrors (issue 1357)
Patrick Mezard <pmezard@gmail.com>
parents: 6956
diff changeset
   271
        return util.sort(changes), copies
5013
6c1029aacc9a convert: Support Mercurial as a source, as well as a sink
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
   272
5280
11e1e574da02 convert: mercurial_source: also search for copies in modified files
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5279
diff changeset
   273
    def getcopies(self, ctx, files):
5013
6c1029aacc9a convert: Support Mercurial as a source, as well as a sink
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
   274
        copies = {}
5280
11e1e574da02 convert: mercurial_source: also search for copies in modified files
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5279
diff changeset
   275
        for name in files:
7231
8e7130a10f3b convert: ignore hg source errors with hg.ignoreerrors (issue 1357)
Patrick Mezard <pmezard@gmail.com>
parents: 6956
diff changeset
   276
            if name in self.ignored:
8e7130a10f3b convert: ignore hg source errors with hg.ignoreerrors (issue 1357)
Patrick Mezard <pmezard@gmail.com>
parents: 6956
diff changeset
   277
                continue
5013
6c1029aacc9a convert: Support Mercurial as a source, as well as a sink
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
   278
            try:
7231
8e7130a10f3b convert: ignore hg source errors with hg.ignoreerrors (issue 1357)
Patrick Mezard <pmezard@gmail.com>
parents: 6956
diff changeset
   279
                copysource, copynode = ctx.filectx(name).renamed()
8e7130a10f3b convert: ignore hg source errors with hg.ignoreerrors (issue 1357)
Patrick Mezard <pmezard@gmail.com>
parents: 6956
diff changeset
   280
                if copysource in self.ignored or not self.keep(copynode):
8e7130a10f3b convert: ignore hg source errors with hg.ignoreerrors (issue 1357)
Patrick Mezard <pmezard@gmail.com>
parents: 6956
diff changeset
   281
                    continue
8e7130a10f3b convert: ignore hg source errors with hg.ignoreerrors (issue 1357)
Patrick Mezard <pmezard@gmail.com>
parents: 6956
diff changeset
   282
                copies[name] = copysource
5013
6c1029aacc9a convert: Support Mercurial as a source, as well as a sink
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
   283
            except TypeError:
6c1029aacc9a convert: Support Mercurial as a source, as well as a sink
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
   284
                pass
7633
08cabecfa8a8 errors: move revlog errors
Matt Mackall <mpm@selenic.com>
parents: 7236
diff changeset
   285
            except error.LookupError, e:
7231
8e7130a10f3b convert: ignore hg source errors with hg.ignoreerrors (issue 1357)
Patrick Mezard <pmezard@gmail.com>
parents: 6956
diff changeset
   286
                if not self.ignoreerrors:
8e7130a10f3b convert: ignore hg source errors with hg.ignoreerrors (issue 1357)
Patrick Mezard <pmezard@gmail.com>
parents: 6956
diff changeset
   287
                    raise
8e7130a10f3b convert: ignore hg source errors with hg.ignoreerrors (issue 1357)
Patrick Mezard <pmezard@gmail.com>
parents: 6956
diff changeset
   288
                self.ignored[name] = 1
8e7130a10f3b convert: ignore hg source errors with hg.ignoreerrors (issue 1357)
Patrick Mezard <pmezard@gmail.com>
parents: 6956
diff changeset
   289
                self.ui.warn(_('ignoring: %s\n') % e)
5013
6c1029aacc9a convert: Support Mercurial as a source, as well as a sink
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
   290
        return copies
5143
d4fa6bafc43a Remove trailing spaces, fix indentation
Thomas Arendsen Hein <thomas@intevation.de>
parents: 5131
diff changeset
   291
5013
6c1029aacc9a convert: Support Mercurial as a source, as well as a sink
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
   292
    def getcommit(self, rev):
6c1029aacc9a convert: Support Mercurial as a source, as well as a sink
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
   293
        ctx = self.changectx(rev)
6885
6e253aa04ff7 convert: implement startrev for hg source
Patrick Mezard <pmezard@gmail.com>
parents: 6762
diff changeset
   294
        parents = [hex(p) for p in self.parents(ctx)]
5556
61fdf2558c0a convert: some tidyups, doc improvements, and test fixes
Bryan O'Sullivan <bos@serpentine.com>
parents: 5554
diff changeset
   295
        if self.saverev:
61fdf2558c0a convert: some tidyups, doc improvements, and test fixes
Bryan O'Sullivan <bos@serpentine.com>
parents: 5554
diff changeset
   296
            crev = rev
61fdf2558c0a convert: some tidyups, doc improvements, and test fixes
Bryan O'Sullivan <bos@serpentine.com>
parents: 5554
diff changeset
   297
        else:
61fdf2558c0a convert: some tidyups, doc improvements, and test fixes
Bryan O'Sullivan <bos@serpentine.com>
parents: 5554
diff changeset
   298
            crev = None
5013
6c1029aacc9a convert: Support Mercurial as a source, as well as a sink
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
   299
        return commit(author=ctx.user(), date=util.datestr(ctx.date()),
5556
61fdf2558c0a convert: some tidyups, doc improvements, and test fixes
Bryan O'Sullivan <bos@serpentine.com>
parents: 5554
diff changeset
   300
                      desc=ctx.description(), rev=crev, parents=parents,
5439
d0c67b52ac01 convert: make contents of "extra" dict available from sources, for sinks.
Bryan O'Sullivan <bos@serpentine.com>
parents: 5437
diff changeset
   301
                      branch=ctx.branch(), extra=ctx.extra())
5013
6c1029aacc9a convert: Support Mercurial as a source, as well as a sink
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
   302
6c1029aacc9a convert: Support Mercurial as a source, as well as a sink
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
   303
    def gettags(self):
6c1029aacc9a convert: Support Mercurial as a source, as well as a sink
Bryan O'Sullivan <bos@serpentine.com>
parents: 5011
diff changeset
   304
        tags = [t for t in self.repo.tagslist() if t[0] != 'tip']
6885
6e253aa04ff7 convert: implement startrev for hg source
Patrick Mezard <pmezard@gmail.com>
parents: 6762
diff changeset
   305
        return dict([(name, hex(node)) for name, node in tags
6e253aa04ff7 convert: implement startrev for hg source
Patrick Mezard <pmezard@gmail.com>
parents: 6762
diff changeset
   306
                     if self.keep(node)])
5379
d3e51dc804f8 mercurial_source: add --filemap support
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5378
diff changeset
   307
d3e51dc804f8 mercurial_source: add --filemap support
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5378
diff changeset
   308
    def getchangedfiles(self, rev, i):
d3e51dc804f8 mercurial_source: add --filemap support
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5378
diff changeset
   309
        ctx = self.changectx(rev)
6885
6e253aa04ff7 convert: implement startrev for hg source
Patrick Mezard <pmezard@gmail.com>
parents: 6762
diff changeset
   310
        parents = self.parents(ctx)
6e253aa04ff7 convert: implement startrev for hg source
Patrick Mezard <pmezard@gmail.com>
parents: 6762
diff changeset
   311
        if not parents and i is None:
6e253aa04ff7 convert: implement startrev for hg source
Patrick Mezard <pmezard@gmail.com>
parents: 6762
diff changeset
   312
            i = 0
6e253aa04ff7 convert: implement startrev for hg source
Patrick Mezard <pmezard@gmail.com>
parents: 6762
diff changeset
   313
            changes = [], ctx.manifest().keys(), []
6e253aa04ff7 convert: implement startrev for hg source
Patrick Mezard <pmezard@gmail.com>
parents: 6762
diff changeset
   314
        else:
6e253aa04ff7 convert: implement startrev for hg source
Patrick Mezard <pmezard@gmail.com>
parents: 6762
diff changeset
   315
            i = i or 0
6e253aa04ff7 convert: implement startrev for hg source
Patrick Mezard <pmezard@gmail.com>
parents: 6762
diff changeset
   316
            changes = self.repo.status(parents[i], ctx.node())[:3]
7231
8e7130a10f3b convert: ignore hg source errors with hg.ignoreerrors (issue 1357)
Patrick Mezard <pmezard@gmail.com>
parents: 6956
diff changeset
   317
        changes = [[f for f in l if f not in self.ignored] for l in changes]
5379
d3e51dc804f8 mercurial_source: add --filemap support
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5378
diff changeset
   318
d3e51dc804f8 mercurial_source: add --filemap support
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5378
diff changeset
   319
        if i == 0:
d3e51dc804f8 mercurial_source: add --filemap support
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5378
diff changeset
   320
            self._changescache = (rev, changes)
d3e51dc804f8 mercurial_source: add --filemap support
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5378
diff changeset
   321
d3e51dc804f8 mercurial_source: add --filemap support
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5378
diff changeset
   322
        return changes[0] + changes[1] + changes[2]
d3e51dc804f8 mercurial_source: add --filemap support
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents: 5378
diff changeset
   323
5554
2147a734dcf9 convert: tell the source repository when a rev has been converted
Bryan O'Sullivan <bos@serpentine.com>
parents: 5553
diff changeset
   324
    def converted(self, rev, destrev):
2147a734dcf9 convert: tell the source repository when a rev has been converted
Bryan O'Sullivan <bos@serpentine.com>
parents: 5553
diff changeset
   325
        if self.convertfp is None:
2147a734dcf9 convert: tell the source repository when a rev has been converted
Bryan O'Sullivan <bos@serpentine.com>
parents: 5553
diff changeset
   326
            self.convertfp = open(os.path.join(self.path, '.hg', 'shamap'),
2147a734dcf9 convert: tell the source repository when a rev has been converted
Bryan O'Sullivan <bos@serpentine.com>
parents: 5553
diff changeset
   327
                                  'a')
2147a734dcf9 convert: tell the source repository when a rev has been converted
Bryan O'Sullivan <bos@serpentine.com>
parents: 5553
diff changeset
   328
        self.convertfp.write('%s %s\n' % (destrev, rev))
2147a734dcf9 convert: tell the source repository when a rev has been converted
Bryan O'Sullivan <bos@serpentine.com>
parents: 5553
diff changeset
   329
        self.convertfp.flush()
5805
e422305e0853 test-convert: test before() and after() conversion actions
Patrick Mezard <pmezard@gmail.com>
parents: 5556
diff changeset
   330
e422305e0853 test-convert: test before() and after() conversion actions
Patrick Mezard <pmezard@gmail.com>
parents: 5556
diff changeset
   331
    def before(self):
e422305e0853 test-convert: test before() and after() conversion actions
Patrick Mezard <pmezard@gmail.com>
parents: 5556
diff changeset
   332
        self.ui.debug(_('run hg source pre-conversion action\n'))
e422305e0853 test-convert: test before() and after() conversion actions
Patrick Mezard <pmezard@gmail.com>
parents: 5556
diff changeset
   333
e422305e0853 test-convert: test before() and after() conversion actions
Patrick Mezard <pmezard@gmail.com>
parents: 5556
diff changeset
   334
    def after(self):
e422305e0853 test-convert: test before() and after() conversion actions
Patrick Mezard <pmezard@gmail.com>
parents: 5556
diff changeset
   335
        self.ui.debug(_('run hg source post-conversion action\n'))