/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,
0.200.519 by Jelmer Vernooij
Move imports down, might not be available in older bzr-git versions.
41
    tree,
0.200.90 by Jelmer Vernooij
Basic support for opening working trees.
42
    urlutils,
43
    workingtree,
44
    )
0.200.381 by Jelmer Vernooij
Support working trees properly, status and ls.
45
from bzrlib.decorators import (
46
    needs_read_lock,
47
    needs_write_lock,
48
    )
49
50
0.200.401 by Jelmer Vernooij
Move working tree inventory code to inventory.
51
from bzrlib.plugins.git.inventory import (
52
    GitIndexInventory,
53
    )
0.200.381 by Jelmer Vernooij
Support working trees properly, status and ls.
54
0.200.90 by Jelmer Vernooij
Basic support for opening working trees.
55
0.200.409 by Jelmer Vernooij
Support parsing .gitignore.
56
IGNORE_FILENAME = ".gitignore"
57
58
0.200.90 by Jelmer Vernooij
Basic support for opening working trees.
59
class GitWorkingTree(workingtree.WorkingTree):
60
    """A Git working tree."""
61
62
    def __init__(self, bzrdir, repo, branch):
0.200.379 by Jelmer Vernooij
Re-enable working tree support.
63
        self.basedir = bzrdir.root_transport.local_abspath('.')
0.200.90 by Jelmer Vernooij
Basic support for opening working trees.
64
        self.bzrdir = bzrdir
65
        self.repository = repo
0.200.384 by Jelmer Vernooij
Fix reading of inventory from index.
66
        self.mapping = self.repository.get_mapping()
0.200.90 by Jelmer Vernooij
Basic support for opening working trees.
67
        self._branch = branch
68
        self._transport = bzrdir.transport
69
0.200.381 by Jelmer Vernooij
Support working trees properly, status and ls.
70
        self.controldir = urlutils.join(self.repository._git._controldir, 'bzr')
0.200.90 by Jelmer Vernooij
Basic support for opening working trees.
71
72
        try:
73
            os.makedirs(self.controldir)
74
            os.makedirs(os.path.join(self.controldir, 'lock'))
75
        except OSError:
76
            pass
77
78
        self._control_files = lockable_files.LockableFiles(
79
            transport.get_transport(self.controldir), 'lock', lockdir.LockDir)
80
        self._format = GitWorkingTreeFormat()
0.200.414 by Jelmer Vernooij
Use new convenience function in dulwich to open index.
81
        self.index = self.repository._git.open_index()
0.200.239 by Jelmer Vernooij
Provide views.
82
        self.views = self._make_views()
0.200.381 by Jelmer Vernooij
Support working trees properly, status and ls.
83
        self._detect_case_handling()
0.200.173 by Jelmer Vernooij
Merge changes, open index.
84
0.200.90 by Jelmer Vernooij
Basic support for opening working trees.
85
    def unlock(self):
0.200.224 by Jelmer Vernooij
Fix working tree locking.
86
        # non-implementation specific cleanup
87
        self._cleanup()
88
89
        # reverse order of locking.
90
        try:
91
            return self._control_files.unlock()
92
        finally:
93
            self.branch.unlock()
0.200.90 by Jelmer Vernooij
Basic support for opening working trees.
94
95
    def is_control_filename(self, path):
96
        return os.path.basename(path) == ".git"
97
0.200.383 by Jelmer Vernooij
Simplify, support rewriting index based on inventory.
98
    def _rewrite_index(self):
99
        self.index.clear()
100
        for path, entry in self._inventory.iter_entries():
101
            if entry.kind == "directory":
102
                # Git indexes don't contain directories
103
                continue
104
            if entry.kind == "file":
105
                blob = Blob()
0.200.385 by Jelmer Vernooij
Cope with removed files.
106
                try:
107
                    file, stat_val = self.get_file_with_stat(entry.file_id, path)
108
                except (errors.NoSuchFile, IOError):
109
                    # TODO: Rather than come up with something here, use the old index
110
                    file = StringIO()
111
                    stat_val = (0, 0, 0, 0, stat.S_IFREG | 0644, 0, 0, 0, 0, 0)
0.200.502 by Jelmer Vernooij
Fill in sha1s in inventory.
112
                blob.set_raw_string(file.read())
0.200.383 by Jelmer Vernooij
Simplify, support rewriting index based on inventory.
113
            elif entry.kind == "symlink":
114
                blob = Blob()
0.200.502 by Jelmer Vernooij
Fill in sha1s in inventory.
115
                try:
116
                    stat_val = os.lstat(self.abspath(path))
117
                except (errors.NoSuchFile, OSError):
118
                    # TODO: Rather than come up with something here, use the 
119
                    # old index
120
                    stat_val = (0, 0, 0, 0, stat.S_IFLNK, 0, 0, 0, 0, 0)
121
                blob.set_raw_string(entry.symlink_target)
0.200.383 by Jelmer Vernooij
Simplify, support rewriting index based on inventory.
122
            # Add object to the repository if it didn't exist yet
123
            if not blob.id in self.repository._git.object_store:
124
                self.repository._git.object_store.add_object(blob)
125
            # Add an entry to the index or update the existing entry
0.200.522 by Jelmer Vernooij
Simplify index handling a bit more.
126
            flags = 0 # FIXME
127
            self.index[path.encode("utf-8")] = (stat_val.st_ctime, stat_val.st_mtime, stat_val.st_dev, stat_val.st_ino, stat_val.st_mode, stat_val.st_uid, stat_val.st_gid, stat_val.st_size, blob.id, flags)
0.200.383 by Jelmer Vernooij
Simplify, support rewriting index based on inventory.
128
0.200.382 by Jelmer Vernooij
Support flushing index.
129
    def flush(self):
130
        # TODO: Maybe this should only write on dirty ?
131
        if self._control_files._lock_mode != 'w':
132
            raise errors.NotWriteLocked(self)
0.200.383 by Jelmer Vernooij
Simplify, support rewriting index based on inventory.
133
        self._rewrite_index()           
0.200.385 by Jelmer Vernooij
Cope with removed files.
134
        self.index.write()
0.200.382 by Jelmer Vernooij
Support flushing index.
135
        self._inventory_is_modified = False
136
0.200.409 by Jelmer Vernooij
Support parsing .gitignore.
137
    def get_ignore_list(self):
138
        ignoreset = getattr(self, '_ignoreset', None)
139
        if ignoreset is not None:
140
            return ignoreset
141
142
        ignore_globs = set()
143
        ignore_globs.update(ignores.get_runtime_ignores())
144
        ignore_globs.update(ignores.get_user_ignores())
145
        if self.has_filename(IGNORE_FILENAME):
146
            f = self.get_file_byname(IGNORE_FILENAME)
147
            try:
148
                ignore_globs.update(ignores.parse_ignore_file(f))
149
            finally:
150
                f.close()
151
        self._ignoreset = ignore_globs
152
        return ignore_globs
153
0.200.508 by Jelmer Vernooij
Skip inventory caching bits.
154
    def set_last_revision(self, revid):
155
        self._change_last_revision(revid)
156
0.200.379 by Jelmer Vernooij
Re-enable working tree support.
157
    def _reset_data(self):
0.200.381 by Jelmer Vernooij
Support working trees properly, status and ls.
158
        self._inventory_is_modified = False
0.200.384 by Jelmer Vernooij
Fix reading of inventory from index.
159
        basis_inv = self.repository.get_inventory(self.mapping.revision_id_foreign_to_bzr(self.repository._git.head()))
0.200.502 by Jelmer Vernooij
Fill in sha1s in inventory.
160
        result = GitIndexInventory(basis_inv, self.mapping, self.index,
161
            self.repository._git.object_store)
0.200.381 by Jelmer Vernooij
Support working trees properly, status and ls.
162
        self._set_inventory(result, dirty=False)
0.200.379 by Jelmer Vernooij
Re-enable working tree support.
163
0.200.381 by Jelmer Vernooij
Support working trees properly, status and ls.
164
    @needs_read_lock
165
    def get_file_sha1(self, file_id, path=None, stat_value=None):
166
        if not path:
167
            path = self._inventory.id2path(file_id)
0.200.383 by Jelmer Vernooij
Simplify, support rewriting index based on inventory.
168
        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.
169
0.200.519 by Jelmer Vernooij
Move imports down, might not be available in older bzr-git versions.
170
    def iter_changes(self, from_tree, include_unchanged=False,
171
                     specific_files=None, pb=None, extra_trees=None,
172
                     require_versioned=True, want_unversioned=False):
173
174
        intertree = tree.InterTree.get(from_tree, self)
175
        return intertree.iter_changes(include_unchanged, specific_files, pb,
176
            extra_trees, require_versioned, want_unversioned=want_unversioned)
177
0.200.90 by Jelmer Vernooij
Basic support for opening working trees.
178
179
class GitWorkingTreeFormat(workingtree.WorkingTreeFormat):
180
181
    def get_format_description(self):
182
        return "Git Working Tree"