/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 workingtree.py

  • Committer: John Arbash Meinel
  • Date: 2010-01-13 16:23:07 UTC
  • mto: (4634.119.7 2.0)
  • mto: This revision was merged to the branch mainline in revision 4959.
  • Revision ID: john@arbash-meinel.com-20100113162307-0bs82td16gzih827
Update the MANIFEST.in file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
17
 
 
18
 
"""An adapter between a Git index and a Bazaar Working Tree"""
19
 
 
20
 
 
21
 
from cStringIO import (
22
 
    StringIO,
23
 
    )
24
 
from dulwich.index import (
25
 
    Index,
26
 
    )
27
 
from dulwich.objects import (
28
 
    Blob,
29
 
    )
30
 
import os
31
 
import stat
32
 
 
33
 
from bzrlib import (
34
 
    errors,
35
 
    inventory,
36
 
    lockable_files,
37
 
    lockdir,
38
 
    osutils,
39
 
    transport,
40
 
    urlutils,
41
 
    workingtree,
42
 
    )
43
 
from bzrlib.decorators import (
44
 
    needs_read_lock,
45
 
    needs_write_lock,
46
 
    )
47
 
 
48
 
 
49
 
def inventory_from_index(basis_inventory, mapping, index):
50
 
    inv = inventory.Inventory(root_id=None)
51
 
    def add_parents(path):
52
 
        dirname, _ = osutils.split(path)
53
 
        file_id = inv.path2id(dirname)
54
 
        if file_id is None:
55
 
            if dirname == "":
56
 
                parent_fid = None
57
 
            else:
58
 
                parent_fid = add_parents(dirname)
59
 
            ie = inv.add_path(dirname, 'directory', mapping.generate_file_id(dirname), parent_fid)
60
 
            if ie.file_id in basis_inventory:
61
 
                ie.revision = basis_inventory[ie.file_id].revision
62
 
            file_id = ie.file_id
63
 
        return file_id
64
 
    for path, value in index.iteritems():
65
 
        assert isinstance(path, str)
66
 
        assert isinstance(value, tuple) and len(value) == 10
67
 
        (ctime, mtime, ino, dev, mode, uid, gid, size, sha, flags) = value
68
 
        old_file_id = basis_inventory.path2id(path)
69
 
        if old_file_id is None:
70
 
            file_id = mapping.generate_file_id(path)
71
 
        else:
72
 
            file_id = old_file_id
73
 
        if stat.S_ISLNK(mode):
74
 
            kind = 'link'
75
 
        else:
76
 
            assert stat.S_ISREG(mode)
77
 
            kind = 'file'
78
 
        ie = inv.add_path(path, kind, file_id, add_parents(path))
79
 
        if old_file_id is not None:
80
 
            ie.revision = basis_inventory[old_file_id].revision
81
 
 
82
 
    return inv
83
 
 
84
 
 
85
 
class GitWorkingTree(workingtree.WorkingTree):
86
 
    """A Git working tree."""
87
 
 
88
 
    def __init__(self, bzrdir, repo, branch):
89
 
        self.basedir = bzrdir.root_transport.local_abspath('.')
90
 
        self.bzrdir = bzrdir
91
 
        self.repository = repo
92
 
        self.mapping = self.repository.get_mapping()
93
 
        self._branch = branch
94
 
        self._transport = bzrdir.transport
95
 
 
96
 
        self.controldir = urlutils.join(self.repository._git._controldir, 'bzr')
97
 
 
98
 
        try:
99
 
            os.makedirs(self.controldir)
100
 
            os.makedirs(os.path.join(self.controldir, 'lock'))
101
 
        except OSError:
102
 
            pass
103
 
 
104
 
        self._control_files = lockable_files.LockableFiles(
105
 
            transport.get_transport(self.controldir), 'lock', lockdir.LockDir)
106
 
 
107
 
        self._format = GitWorkingTreeFormat()
108
 
 
109
 
        self.index_path = os.path.join(self.repository._git.controldir(), 
110
 
                                       "index")
111
 
        self.index = Index(self.index_path)
112
 
        self.views = self._make_views()
113
 
        self._detect_case_handling()
114
 
 
115
 
    def unlock(self):
116
 
        # non-implementation specific cleanup
117
 
        self._cleanup()
118
 
 
119
 
        # reverse order of locking.
120
 
        try:
121
 
            return self._control_files.unlock()
122
 
        finally:
123
 
            self.branch.unlock()
124
 
 
125
 
    def is_control_filename(self, path):
126
 
        return os.path.basename(path) == ".git"
127
 
 
128
 
    def _rewrite_index(self):
129
 
        self.index.clear()
130
 
        for path, entry in self._inventory.iter_entries():
131
 
            if entry.kind == "directory":
132
 
                # Git indexes don't contain directories
133
 
                continue
134
 
            if entry.kind == "file":
135
 
                blob = Blob()
136
 
                try:
137
 
                    file, stat_val = self.get_file_with_stat(entry.file_id, path)
138
 
                except (errors.NoSuchFile, IOError):
139
 
                    # TODO: Rather than come up with something here, use the old index
140
 
                    file = StringIO()
141
 
                    stat_val = (0, 0, 0, 0, stat.S_IFREG | 0644, 0, 0, 0, 0, 0)
142
 
                blob._text = file.read()
143
 
            elif entry.kind == "symlink":
144
 
                blob = Blob()
145
 
                stat_val = os.stat(self.abspath(path))
146
 
                blob._text = entry.symlink_target
147
 
            # Add object to the repository if it didn't exist yet
148
 
            if not blob.id in self.repository._git.object_store:
149
 
                self.repository._git.object_store.add_object(blob)
150
 
            # Add an entry to the index or update the existing entry
151
 
            (mode, ino, dev, links, uid, gid, size, atime, mtime, ctime) = stat_val
152
 
            flags = 0
153
 
            self.index[path.encode("utf-8")] = (ctime, mtime, ino, dev, mode, uid, gid, size, blob.id, flags)
154
 
 
155
 
    def flush(self):
156
 
        # TODO: Maybe this should only write on dirty ?
157
 
        if self._control_files._lock_mode != 'w':
158
 
            raise errors.NotWriteLocked(self)
159
 
        self._rewrite_index()           
160
 
        self.index.write()
161
 
        self._inventory_is_modified = False
162
 
 
163
 
    def _reset_data(self):
164
 
        self._inventory_is_modified = False
165
 
        basis_inv = self.repository.get_inventory(self.mapping.revision_id_foreign_to_bzr(self.repository._git.head()))
166
 
        result = inventory_from_index(basis_inv, self.mapping, self.index)
167
 
        self._set_inventory(result, dirty=False)
168
 
 
169
 
    @needs_read_lock
170
 
    def get_file_sha1(self, file_id, path=None, stat_value=None):
171
 
        if not path:
172
 
            path = self._inventory.id2path(file_id)
173
 
        return osutils.sha_file_by_name(self.abspath(path).encode(osutils._fs_enc))
174
 
 
175
 
 
176
 
class GitWorkingTreeFormat(workingtree.WorkingTreeFormat):
177
 
 
178
 
    def get_format_description(self):
179
 
        return "Git Working Tree"