/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

More work on commit.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
 
17
 
17
18
"""An adapter between a Git index and a Bazaar Working Tree"""
18
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
    )
19
30
import os
 
31
import stat
20
32
 
21
33
from bzrlib import (
 
34
    errors,
22
35
    inventory,
23
36
    lockable_files,
24
37
    lockdir,
 
38
    osutils,
25
39
    transport,
26
40
    urlutils,
27
41
    workingtree,
28
42
    )
 
43
from bzrlib.decorators import (
 
44
    needs_read_lock,
 
45
    needs_write_lock,
 
46
    )
 
47
 
 
48
 
 
49
from bzrlib.plugins.git.inventory import (
 
50
    GitIndexInventory,
 
51
    )
 
52
 
29
53
 
30
54
class GitWorkingTree(workingtree.WorkingTree):
31
55
    """A Git working tree."""
32
56
 
33
57
    def __init__(self, bzrdir, repo, branch):
34
 
        self.basedir = bzrdir.transport.base
 
58
        self.basedir = bzrdir.root_transport.local_abspath('.')
35
59
        self.bzrdir = bzrdir
36
60
        self.repository = repo
 
61
        self.mapping = self.repository.get_mapping()
37
62
        self._branch = branch
38
63
        self._transport = bzrdir.transport
39
64
 
40
 
        self.controldir = urlutils.join(self.repository._git.path, 'bzr')
 
65
        self.controldir = urlutils.join(self.repository._git._controldir, 'bzr')
41
66
 
42
67
        try:
43
68
            os.makedirs(self.controldir)
50
75
 
51
76
        self._format = GitWorkingTreeFormat()
52
77
 
53
 
    def lock_read(self):
54
 
        pass
 
78
        self.index_path = os.path.join(self.repository._git.controldir(), 
 
79
                                       "index")
 
80
        self.index = Index(self.index_path)
 
81
        self.views = self._make_views()
 
82
        self._detect_case_handling()
55
83
 
56
84
    def unlock(self):
57
 
        pass
 
85
        # non-implementation specific cleanup
 
86
        self._cleanup()
 
87
 
 
88
        # reverse order of locking.
 
89
        try:
 
90
            return self._control_files.unlock()
 
91
        finally:
 
92
            self.branch.unlock()
58
93
 
59
94
    def is_control_filename(self, path):
60
95
        return os.path.basename(path) == ".git"
61
96
 
62
 
    def _get_inventory(self):
63
 
        return inventory.Inventory()
64
 
 
65
 
    inventory = property(_get_inventory,
66
 
                         doc="Inventory of this Tree")
 
97
    def _rewrite_index(self):
 
98
        self.index.clear()
 
99
        for path, entry in self._inventory.iter_entries():
 
100
            if entry.kind == "directory":
 
101
                # Git indexes don't contain directories
 
102
                continue
 
103
            if entry.kind == "file":
 
104
                blob = Blob()
 
105
                try:
 
106
                    file, stat_val = self.get_file_with_stat(entry.file_id, path)
 
107
                except (errors.NoSuchFile, IOError):
 
108
                    # TODO: Rather than come up with something here, use the old index
 
109
                    file = StringIO()
 
110
                    stat_val = (0, 0, 0, 0, stat.S_IFREG | 0644, 0, 0, 0, 0, 0)
 
111
                blob._text = file.read()
 
112
            elif entry.kind == "symlink":
 
113
                blob = Blob()
 
114
                stat_val = os.stat(self.abspath(path))
 
115
                blob._text = entry.symlink_target
 
116
            # Add object to the repository if it didn't exist yet
 
117
            if not blob.id in self.repository._git.object_store:
 
118
                self.repository._git.object_store.add_object(blob)
 
119
            # Add an entry to the index or update the existing entry
 
120
            (mode, ino, dev, links, uid, gid, size, atime, mtime, ctime) = stat_val
 
121
            flags = 0
 
122
            self.index[path.encode("utf-8")] = (ctime, mtime, ino, dev, mode, uid, gid, size, blob.id, flags)
 
123
 
 
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)
 
128
        self._rewrite_index()           
 
129
        self.index.write()
 
130
        self._inventory_is_modified = False
 
131
 
 
132
    def _reset_data(self):
 
133
        self._inventory_is_modified = False
 
134
        basis_inv = self.repository.get_inventory(self.mapping.revision_id_foreign_to_bzr(self.repository._git.head()))
 
135
        result = GitIndexInventory(basis_inv, self.mapping, self.index)
 
136
        self._set_inventory(result, dirty=False)
 
137
 
 
138
    @needs_read_lock
 
139
    def get_file_sha1(self, file_id, path=None, stat_value=None):
 
140
        if not path:
 
141
            path = self._inventory.id2path(file_id)
 
142
        return osutils.sha_file_by_name(self.abspath(path).encode(osutils._fs_enc))
67
143
 
68
144
 
69
145
class GitWorkingTreeFormat(workingtree.WorkingTreeFormat):