/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

Add basic infrastructure for dpush.

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
 
 
18
17
"""An adapter between a Git index and a Bazaar Working Tree"""
19
18
 
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
19
import os
31
 
import stat
32
20
 
33
21
from bzrlib import (
34
 
    errors,
35
 
    ignores,
36
22
    inventory,
37
23
    lockable_files,
38
24
    lockdir,
39
 
    osutils,
40
25
    transport,
41
26
    urlutils,
42
27
    workingtree,
43
28
    )
44
 
from bzrlib.decorators import (
45
 
    needs_read_lock,
46
 
    needs_write_lock,
47
 
    )
48
 
 
49
 
 
50
 
from bzrlib.plugins.git.inventory import (
51
 
    GitIndexInventory,
52
 
    )
53
 
 
54
 
 
55
 
IGNORE_FILENAME = ".gitignore"
56
 
 
 
29
 
 
30
from dulwich.index import Index
57
31
 
58
32
class GitWorkingTree(workingtree.WorkingTree):
59
33
    """A Git working tree."""
60
34
 
61
35
    def __init__(self, bzrdir, repo, branch):
62
 
        self.basedir = bzrdir.root_transport.local_abspath('.')
 
36
        self.basedir = bzrdir.transport.base
63
37
        self.bzrdir = bzrdir
64
38
        self.repository = repo
65
 
        self.mapping = self.repository.get_mapping()
66
39
        self._branch = branch
67
40
        self._transport = bzrdir.transport
68
41
 
69
 
        self.controldir = urlutils.join(self.repository._git._controldir, 'bzr')
 
42
        self.controldir = urlutils.join(self.repository._git.path, 'bzr')
70
43
 
71
44
        try:
72
45
            os.makedirs(self.controldir)
79
52
 
80
53
        self._format = GitWorkingTreeFormat()
81
54
 
82
 
        self.index_path = os.path.join(self.repository._git.controldir(), 
83
 
                                       "index")
84
 
        self.index = Index(self.index_path)
85
 
        self.views = self._make_views()
86
 
        self._detect_case_handling()
 
55
        self.index = Index(os.path.join(self.repository._git.controldir(), 
 
56
            "index"))
 
57
 
 
58
    def lock_read(self):
 
59
        pass
87
60
 
88
61
    def unlock(self):
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()
 
62
        pass
97
63
 
98
64
    def is_control_filename(self, path):
99
65
        return os.path.basename(path) == ".git"
100
66
 
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()
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)
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
126
 
            self.index[path.encode("utf-8")] = (ctime, mtime, ino, dev, mode, uid, gid, size, blob.id, flags)
127
 
 
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)
132
 
        self._rewrite_index()           
133
 
        self.index.write()
134
 
        self._inventory_is_modified = False
135
 
 
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
 
 
153
 
    def _reset_data(self):
154
 
        self._inventory_is_modified = False
155
 
        basis_inv = self.repository.get_inventory(self.mapping.revision_id_foreign_to_bzr(self.repository._git.head()))
156
 
        result = GitIndexInventory(basis_inv, self.mapping, self.index)
157
 
        self._set_inventory(result, dirty=False)
158
 
 
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)
163
 
        return osutils.sha_file_by_name(self.abspath(path).encode(osutils._fs_enc))
 
67
    def _get_inventory(self):
 
68
        return inventory.Inventory()
 
69
 
 
70
    inventory = property(_get_inventory,
 
71
                         doc="Inventory of this Tree")
164
72
 
165
73
 
166
74
class GitWorkingTreeFormat(workingtree.WorkingTreeFormat):