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