/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to dir.py

Fix typo in git-svn-id parser, return revnum as integer.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""An adapter between a Git control dir and a Bazaar BzrDir."""
 
18
 
 
19
from bzrlib import (
 
20
    bzrdir,
 
21
    errors as bzr_errors,
 
22
    lockable_files,
 
23
    urlutils,
 
24
    )
 
25
 
 
26
LockWarner = getattr(lockable_files, "_LockWarner", None)
 
27
 
 
28
from bzrlib.plugins.git import (
 
29
    LocalGitBzrDirFormat,
 
30
    get_rich_root_format,
 
31
    )
 
32
 
 
33
 
 
34
class GitLock(object):
 
35
    """A lock that thunks through to Git."""
 
36
 
 
37
    def lock_write(self, token=None):
 
38
        pass
 
39
 
 
40
    def lock_read(self):
 
41
        pass
 
42
 
 
43
    def unlock(self):
 
44
        pass
 
45
 
 
46
    def peek(self):
 
47
        pass
 
48
 
 
49
    def validate_token(self, token):
 
50
        pass
 
51
 
 
52
    def break_lock(self):
 
53
        pass
 
54
 
 
55
 
 
56
class GitLockableFiles(lockable_files.LockableFiles):
 
57
    """Git specific lockable files abstraction."""
 
58
 
 
59
    def __init__(self, transport, lock):
 
60
        self._lock = lock
 
61
        self._transaction = None
 
62
        self._lock_mode = None
 
63
        self._transport = transport
 
64
        if LockWarner is None:
 
65
            # Bzr 1.13
 
66
            self._lock_count = 0
 
67
        else:
 
68
            self._lock_warner = LockWarner(repr(self))
 
69
 
 
70
 
 
71
class GitDir(bzrdir.BzrDir):
 
72
    """An adapter to the '.git' dir used by git."""
 
73
 
 
74
    def is_supported(self):
 
75
        return True
 
76
 
 
77
    def cloning_metadir(self, stacked=False):
 
78
        return get_rich_root_format(stacked)
 
79
 
 
80
 
 
81
class LocalGitDir(GitDir):
 
82
    """An adapter to the '.git' dir used by git."""
 
83
 
 
84
    def _get_gitrepository_class(self):
 
85
        from bzrlib.plugins.git.repository import LocalGitRepository
 
86
        return LocalGitRepository
 
87
 
 
88
    _gitrepository_class = property(_get_gitrepository_class)
 
89
 
 
90
    def __init__(self, transport, lockfiles, gitrepo, format):
 
91
        self._format = format
 
92
        self.root_transport = transport
 
93
        self._git = gitrepo
 
94
        if gitrepo.bare:
 
95
            self.transport = transport
 
96
        else:
 
97
            self.transport = transport.clone('.git')
 
98
        self._lockfiles = lockfiles
 
99
        self._mode_check_done = None
 
100
 
 
101
    def is_control_filename(self, filename):
 
102
        return filename == '.git' or filename.startswith('.git/')
 
103
 
 
104
    def get_branch_transport(self, branch_format):
 
105
        if branch_format is None:
 
106
            return self.transport
 
107
        if isinstance(branch_format, LocalGitBzrDirFormat):
 
108
            return self.transport
 
109
        raise bzr_errors.IncompatibleFormat(branch_format, self._format)
 
110
 
 
111
    get_repository_transport = get_branch_transport
 
112
    get_workingtree_transport = get_branch_transport
 
113
 
 
114
    def open_branch(self, ignore_fallbacks=None):
 
115
        """'create' a branch for this dir."""
 
116
        repo = self.open_repository()
 
117
        from bzrlib.plugins.git.branch import LocalGitBranch
 
118
        return LocalGitBranch(self, repo, "HEAD", self._lockfiles)
 
119
 
 
120
    def open_repository(self, shared=False):
 
121
        """'open' a repository for this dir."""
 
122
        return self._gitrepository_class(self, self._lockfiles)
 
123
 
 
124
    def open_workingtree(self, recommend_upgrade=True):
 
125
        if not self._git.bare and self._git.has_index():
 
126
            from bzrlib.plugins.git.workingtree import GitWorkingTree
 
127
            return GitWorkingTree(self, self.open_repository(), 
 
128
                                                  self.open_branch())
 
129
        loc = urlutils.unescape_for_display(self.root_transport.base, 'ascii')
 
130
        raise bzr_errors.NoWorkingTree(loc)
 
131
 
 
132
    def create_repository(self, shared=False):
 
133
        return self.open_repository()
 
134
 
 
135
    def create_branch(self):
 
136
        return self.open_branch()
 
137
 
 
138
    def backup_bzrdir(self):
 
139
        if self._git.bare:
 
140
            self.root_transport.copy_tree(".git", ".git.backup")
 
141
            return (self.root_transport.abspath(".git"),
 
142
                    self.root_transport.abspath(".git.backup"))
 
143
        else:
 
144
            raise bzr_errors.BzrError("Unable to backup bare repositories")
 
145
 
 
146
    def create_workingtree(self, revision_id=None, from_branch=None,
 
147
        accelerator_tree=None, hardlink=False):
 
148
        if self._git.bare:
 
149
            raise bzr_errors.BzrError("Can't create working tree in a bare repo")
 
150
        from dulwich.index import write_index
 
151
        from dulwich.pack import SHA1Writer
 
152
        f = open(self.transport.local_abspath("index"), 'w+')
 
153
        try:
 
154
            f = SHA1Writer(f)
 
155
            write_index(f, [])
 
156
        finally:
 
157
            f.close()
 
158
        return self.open_workingtree()