summaryrefslogtreecommitdiff
path: root/hgsubversion/svnrepo.py
blob: 6bb3cd962f7b82258c608108ce213efc72f50500 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
"""
repository class-based interface for hgsubversion

  Copyright (C) 2009, Dan Villiom Podlaski Christiansen <danchr@gmail.com>
  See parent package for licensing.

Internally, Mercurial assumes that every single repository is a localrepository
subclass: pull() is called on the instance pull *to*, but not the one pulled
*from*. To work around this, we create two classes:

- svnremoterepo for Subversion repositories, but it doesn't really do anything.
- svnlocalrepo for local repositories which handles both operations on itself --
  the local, hgsubversion-enabled clone -- and the remote repository. Decorators
  are used to distinguish and filter these operations from others.
"""

import errno

from mercurial import error
from mercurial import util as hgutil
from mercurial import httprepo
import mercurial.repo

import util
import wrappers
import svnwrap
import svnmeta

propertycache = hgutil.propertycache

class ctxctx(object):
    """Proxies a ctx object and ensures files is never empty."""
    def __init__(self, ctx):
        self._ctx = ctx

    def files(self):
        return self._ctx.files() or ['.svn']

    def filectx(self, path, filelog=None):
        if path == '.svn':
            raise IOError(errno.ENOENT, '.svn is a fake file')
        return self._ctx.filectx(path, filelog=filelog)

    def __getattr__(self, name):
        return getattr(self._ctx, name)

    def __getitem__(self, key):
        return self._ctx[key]

def generate_repo_class(ui, repo):
    """ This function generates the local repository wrapper. """

    superclass = repo.__class__

    def remotesvn(fn):
        """
        Filter for instance methods which require the first argument
        to be a remote Subversion repository instance.
        """
        original = getattr(repo, fn.__name__, None)

        # remove when dropping support for hg < 1.6.
        if original is None and fn.__name__ == 'findoutgoing':
            return

        def wrapper(self, *args, **opts):
            capable = getattr(args[0], 'capable', lambda x: False)
            if capable('subversion'):
                return fn(self, *args, **opts)
            else:
                return original(*args, **opts)
        wrapper.__name__ = fn.__name__ + '_wrapper'
        wrapper.__doc__ = fn.__doc__
        return wrapper

    class svnlocalrepo(superclass):
        def svn_commitctx(self, ctx):
            """Commits a ctx, but defeats manifest recycling introduced in hg 1.9."""
            return self.commitctx(ctxctx(ctx))

        # TODO use newbranch to allow branch creation in Subversion?
        @remotesvn
        def push(self, remote, force=False, revs=None, newbranch=None):
            return wrappers.push(self, remote, force, revs)

        @remotesvn
        def pull(self, remote, heads=[], force=False):
            return wrappers.pull(self, remote, heads, force)

        @remotesvn
        def findoutgoing(self, remote, base=None, heads=None, force=False):
            return wrappers.findoutgoing(repo, remote, heads, force)

        def svnmeta(self, uuid=None, subdir=None):
            return svnmeta.SVNMeta(self, uuid, subdir)

    repo.__class__ = svnlocalrepo

class svnremoterepo(mercurial.repo.repository):
    """ the dumb wrapper for actual Subversion repositories """

    def __init__(self, ui, path=None):
        self.ui = ui
        if path is None:
            path = self.ui.config('paths', 'default')
        if not path:
            raise hgutil.Abort('no Subversion URL specified')
        self.path = path
        self.capabilities = set(['lookup', 'subversion'])

    @propertycache
    def svnauth(self):
        # DO NOT default the user to hg's getuser(). If you provide
        # *any* default username to Subversion, it won't use any remembered
        # username for the desired realm, breaking OS X Keychain support,
        # GNOME keyring support, and all similar tools.
        user = self.ui.config('hgsubversion', 'username')
        passwd = self.ui.config('hgsubversion', 'password')
        url = util.normalize_url(self.path)
        user, passwd, url = svnwrap.parse_url(url, user, passwd)
        return url, user, passwd

    @property
    def svnurl(self):
        return self.svn.svn_url

    @propertycache
    def svn(self):
        try:
            return svnwrap.SubversionRepo(*self.svnauth)
        except svnwrap.SubversionConnectionException, e:
            self.ui.traceback()
            raise hgutil.Abort(e)

    def url(self):
        return self.path

    def lookup(self, key):
        return key

    def cancopy(self):
        return False

    def heads(self, *args, **opts):
        """
        Whenever this function is hit, we abort. The traceback is useful for
        figuring out where to intercept the functionality.
        """
        raise hgutil.Abort('command unavailable for Subversion repositories')

    def pushkey(self, namespace, key, old, new):
        return False

    def listkeys(self, namespace):
        return {}

def instance(ui, url, create):
    if url.startswith('http://') or url.startswith('https://'):
        try:
            # may yield a bogus 'real URL...' message
            return httprepo.instance(ui, url, create)
        except error.RepoError:
            ui.traceback()
            ui.note('(falling back to Subversion support)\n')

    if create:
        raise hgutil.Abort('cannot create new remote Subversion repository')

    return svnremoterepo(ui, url)