/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.200.90 by Jelmer Vernooij
Basic support for opening working trees.
1
# Copyright (C) 2008 Jelmer Vernooij <jelmer@samba.org>
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
0.200.381 by Jelmer Vernooij
Support working trees properly, status and ls.
17
0.200.90 by Jelmer Vernooij
Basic support for opening working trees.
18
"""An adapter between a Git index and a Bazaar Working Tree"""
19
0.200.381 by Jelmer Vernooij
Support working trees properly, status and ls.
20
0.200.385 by Jelmer Vernooij
Cope with removed files.
21
from cStringIO import (
22
    StringIO,
23
    )
0.200.292 by Jelmer Vernooij
Fix formatting.
24
from dulwich.index import (
25
    Index,
26
    )
0.200.383 by Jelmer Vernooij
Simplify, support rewriting index based on inventory.
27
from dulwich.objects import (
28
    Blob,
29
    )
0.200.90 by Jelmer Vernooij
Basic support for opening working trees.
30
import os
0.200.384 by Jelmer Vernooij
Fix reading of inventory from index.
31
import stat
0.200.90 by Jelmer Vernooij
Basic support for opening working trees.
32
33
from bzrlib import (
0.200.382 by Jelmer Vernooij
Support flushing index.
34
    errors,
0.200.409 by Jelmer Vernooij
Support parsing .gitignore.
35
    ignores,
0.200.90 by Jelmer Vernooij
Basic support for opening working trees.
36
    inventory,
37
    lockable_files,
38
    lockdir,
0.200.381 by Jelmer Vernooij
Support working trees properly, status and ls.
39
    osutils,
0.200.90 by Jelmer Vernooij
Basic support for opening working trees.
40
    transport,
41
    urlutils,
42
    workingtree,
43
    )
0.200.381 by Jelmer Vernooij
Support working trees properly, status and ls.
44
from bzrlib.decorators import (
45
    needs_read_lock,
46
    needs_write_lock,
47
    )
48
49
0.200.401 by Jelmer Vernooij
Move working tree inventory code to inventory.
50
from bzrlib.plugins.git.inventory import (
51
    GitIndexInventory,
52
    )
0.200.381 by Jelmer Vernooij
Support working trees properly, status and ls.
53
0.200.90 by Jelmer Vernooij
Basic support for opening working trees.
54
0.200.409 by Jelmer Vernooij
Support parsing .gitignore.
55
IGNORE_FILENAME = ".gitignore"
56
57
0.200.90 by Jelmer Vernooij
Basic support for opening working trees.
58
class GitWorkingTree(workingtree.WorkingTree):
59
    """A Git working tree."""
60
61
    def __init__(self, bzrdir, repo, branch):
0.200.379 by Jelmer Vernooij
Re-enable working tree support.
62
        self.basedir = bzrdir.root_transport.local_abspath('.')
0.200.90 by Jelmer Vernooij
Basic support for opening working trees.
63
        self.bzrdir = bzrdir
64
        self.repository = repo
0.200.384 by Jelmer Vernooij
Fix reading of inventory from index.
65
        self.mapping = self.repository.get_mapping()
0.200.90 by Jelmer Vernooij
Basic support for opening working trees.
66
        self._branch = branch
67
        self._transport = bzrdir.transport
68
0.200.381 by Jelmer Vernooij
Support working trees properly, status and ls.
69
        self.controldir = urlutils.join(self.repository._git._controldir, 'bzr')
0.200.90 by Jelmer Vernooij
Basic support for opening working trees.
70
71
        try:
72
            os.makedirs(self.controldir)
73
            os.makedirs(os.path.join(self.controldir, 'lock'))
74
        except OSError:
75
            pass
76
77
        self._control_files = lockable_files.LockableFiles(
78
            transport.get_transport(self.controldir), 'lock', lockdir.LockDir)
79
80
        self._format = GitWorkingTreeFormat()
81
0.200.382 by Jelmer Vernooij
Support flushing index.
82
        self.index_path = os.path.join(self.repository._git.controldir(), 
83
                                       "index")
84
        self.index = Index(self.index_path)
0.200.239 by Jelmer Vernooij
Provide views.
85
        self.views = self._make_views()
0.200.381 by Jelmer Vernooij
Support working trees properly, status and ls.
86
        self._detect_case_handling()
0.200.173 by Jelmer Vernooij
Merge changes, open index.
87
0.200.90 by Jelmer Vernooij
Basic support for opening working trees.
88
    def unlock(self):
0.200.224 by Jelmer Vernooij
Fix working tree locking.
89
        # non-implementation specific cleanup
90
        self._cleanup()
91
92
        # reverse order of locking.
93
        try:
94
            return self._control_files.unlock()
95
        finally:
96
            self.branch.unlock()
0.200.90 by Jelmer Vernooij
Basic support for opening working trees.
97
98
    def is_control_filename(self, path):
99
        return os.path.basename(path) == ".git"
100
0.200.383 by Jelmer Vernooij
Simplify, support rewriting index based on inventory.
101
    def _rewrite_index(self):
102
        self.index.clear()
103
        for path, entry in self._inventory.iter_entries():
104
            if entry.kind == "directory":
105
                # Git indexes don't contain directories
106
                continue
107
            if entry.kind == "file":
108
                blob = Blob()
0.200.385 by Jelmer Vernooij
Cope with removed files.
109
                try:
110
                    file, stat_val = self.get_file_with_stat(entry.file_id, path)
111
                except (errors.NoSuchFile, IOError):
112
                    # TODO: Rather than come up with something here, use the old index
113
                    file = StringIO()
114
                    stat_val = (0, 0, 0, 0, stat.S_IFREG | 0644, 0, 0, 0, 0, 0)
0.200.383 by Jelmer Vernooij
Simplify, support rewriting index based on inventory.
115
                blob._text = file.read()
116
            elif entry.kind == "symlink":
117
                blob = Blob()
118
                stat_val = os.stat(self.abspath(path))
119
                blob._text = entry.symlink_target
120
            # Add object to the repository if it didn't exist yet
121
            if not blob.id in self.repository._git.object_store:
122
                self.repository._git.object_store.add_object(blob)
123
            # Add an entry to the index or update the existing entry
124
            (mode, ino, dev, links, uid, gid, size, atime, mtime, ctime) = stat_val
125
            flags = 0
0.200.385 by Jelmer Vernooij
Cope with removed files.
126
            self.index[path.encode("utf-8")] = (ctime, mtime, ino, dev, mode, uid, gid, size, blob.id, flags)
0.200.383 by Jelmer Vernooij
Simplify, support rewriting index based on inventory.
127
0.200.382 by Jelmer Vernooij
Support flushing index.
128
    def flush(self):
129
        # TODO: Maybe this should only write on dirty ?
130
        if self._control_files._lock_mode != 'w':
131
            raise errors.NotWriteLocked(self)
0.200.383 by Jelmer Vernooij
Simplify, support rewriting index based on inventory.
132
        self._rewrite_index()           
0.200.385 by Jelmer Vernooij
Cope with removed files.
133
        self.index.write()
0.200.382 by Jelmer Vernooij
Support flushing index.
134
        self._inventory_is_modified = False
135
0.200.409 by Jelmer Vernooij
Support parsing .gitignore.
136
    def get_ignore_list(self):
137
        ignoreset = getattr(self, '_ignoreset', None)
138
        if ignoreset is not None:
139
            return ignoreset
140
141
        ignore_globs = set()
142
        ignore_globs.update(ignores.get_runtime_ignores())
143
        ignore_globs.update(ignores.get_user_ignores())
144
        if self.has_filename(IGNORE_FILENAME):
145
            f = self.get_file_byname(IGNORE_FILENAME)
146
            try:
147
                ignore_globs.update(ignores.parse_ignore_file(f))
148
            finally:
149
                f.close()
150
        self._ignoreset = ignore_globs
151
        return ignore_globs
152
0.200.379 by Jelmer Vernooij
Re-enable working tree support.
153
    def _reset_data(self):
0.200.381 by Jelmer Vernooij
Support working trees properly, status and ls.
154
        self._inventory_is_modified = False
0.200.384 by Jelmer Vernooij
Fix reading of inventory from index.
155
        basis_inv = self.repository.get_inventory(self.mapping.revision_id_foreign_to_bzr(self.repository._git.head()))
0.200.401 by Jelmer Vernooij
Move working tree inventory code to inventory.
156
        result = GitIndexInventory(basis_inv, self.mapping, self.index)
0.200.381 by Jelmer Vernooij
Support working trees properly, status and ls.
157
        self._set_inventory(result, dirty=False)
0.200.379 by Jelmer Vernooij
Re-enable working tree support.
158
0.200.381 by Jelmer Vernooij
Support working trees properly, status and ls.
159
    @needs_read_lock
160
    def get_file_sha1(self, file_id, path=None, stat_value=None):
161
        if not path:
162
            path = self._inventory.id2path(file_id)
0.200.383 by Jelmer Vernooij
Simplify, support rewriting index based on inventory.
163
        return osutils.sha_file_by_name(self.abspath(path).encode(osutils._fs_enc))
0.200.90 by Jelmer Vernooij
Basic support for opening working trees.
164
165
166
class GitWorkingTreeFormat(workingtree.WorkingTreeFormat):
167
168
    def get_format_description(self):
169
        return "Git Working Tree"