1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
|
# Copyright (C) 2008-2011 Jelmer Vernooij <jelmer@samba.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""An adapter between a Git index and a Bazaar Working Tree"""
from cStringIO import (
StringIO,
)
from collections import defaultdict
import errno
from dulwich.index import (
Index,
)
from dulwich.object_store import (
tree_lookup_path,
)
from dulwich.objects import (
Blob,
ZERO_SHA,
)
import os
import posix
import posixpath
import stat
from bzrlib import (
errors,
conflicts as _mod_conflicts,
ignores,
inventory,
lockable_files,
lockdir,
osutils,
transport,
tree,
workingtree,
)
from bzrlib.decorators import (
needs_read_lock,
)
from bzrlib.plugins.git.dir import (
LocalGitDir,
)
from bzrlib.plugins.git.tree import (
changes_from_git_changes,
tree_delta_from_git_changes,
)
from bzrlib.plugins.git.mapping import (
GitFileIdMap,
mode_kind,
)
IGNORE_FILENAME = ".gitignore"
class GitWorkingTree(workingtree.WorkingTree):
"""A Git working tree."""
def __init__(self, bzrdir, repo, branch, index):
self.basedir = bzrdir.root_transport.local_abspath('.')
self.bzrdir = bzrdir
self.repository = repo
self.store = self.repository._git.object_store
self.mapping = self.repository.get_mapping()
self._branch = branch
self._transport = bzrdir.transport
self.controldir = self.bzrdir.transport.local_abspath('bzr')
try:
os.makedirs(self.controldir)
os.makedirs(os.path.join(self.controldir, 'lock'))
except OSError:
pass
self._control_files = lockable_files.LockableFiles(
transport.get_transport(self.controldir), 'lock', lockdir.LockDir)
self._format = GitWorkingTreeFormat()
self.index = index
self.views = self._make_views()
self._rules_searcher = None
self._detect_case_handling()
self._reset_data()
self._fileid_map = self._basis_fileid_map.copy()
def _index_add_entry(self, path, file_id, kind):
assert isinstance(path, basestring)
assert type(file_id) == str
if kind == "directory":
# Git indexes don't contain directories
return
if kind == "file":
blob = Blob()
try:
file, stat_val = self.get_file_with_stat(file_id, path)
except (errors.NoSuchFile, IOError):
# TODO: Rather than come up with something here, use the old index
file = StringIO()
from posix import stat_result
stat_val = stat_result((stat.S_IFREG | 0644, 0, 0, 0, 0, 0, 0, 0, 0, 0))
blob.set_raw_string(file.read())
elif kind == "symlink":
blob = Blob()
try:
stat_val = os.lstat(self.abspath(path))
except (errors.NoSuchFile, OSError):
# TODO: Rather than come up with something here, use the
# old index
from posix import stat_result
stat_val = stat_result((stat.S_IFLNK, 0, 0, 0, 0, 0, 0, 0, 0, 0))
blob.set_raw_string(self.get_symlink_target(file_id).encode("utf-8"))
else:
raise AssertionError("unknown kind '%s'" % kind)
# Add object to the repository if it didn't exist yet
if not blob.id in self.store:
self.store.add_object(blob)
# Add an entry to the index or update the existing entry
flags = 0 # FIXME
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)
def unversion(self, file_ids):
for file_id in file_ids:
path = self.id2path(file_id)
del self.index[path.encode("utf-8")]
def _add(self, files, ids, kinds):
for (path, file_id, kind) in zip(files, ids, kinds):
if file_id is not None:
self._fileid_map.set_file_id(path.encode("utf-8"), file_id)
else:
file_id = self._fileid_map.lookup_file_id(path.encode("utf-8"))
self._index_add_entry(path, file_id, kind)
def _set_root_id(self, file_id):
self._fileid_map.set_file_id("", file_id)
def move(self, from_paths, to_dir=None, after=False):
rename_tuples = []
to_abs = self.abspath(to_dir)
if not os.path.isdir(to_abs):
raise errors.BzrMoveFailedError('', to_dir,
errors.NotADirectory(to_abs))
for from_rel in from_paths:
from_tail = os.path.split(from_rel)[-1]
to_rel = os.path.join(to_dir, from_tail)
self.rename_one(from_rel, to_rel, after=after)
rename_tuples.append((from_rel, to_rel))
return rename_tuples
def rename_one(self, from_rel, to_rel, after=False):
if not after:
os.rename(self.abspath(from_rel), self.abspath(to_rel))
from_path = from_rel.encode("utf-8")
to_path = to_rel.encode("utf-8")
if not self.has_filename(to_rel):
raise errors.BzrMoveFailedError(from_rel, to_rel,
errors.NoSuchFile(to_rel))
if not from_path in self.index:
raise errors.BzrMoveFailedError(from_rel, to_rel,
errors.NotVersionedError(path=from_rel))
self.index[to_path] = self.index[from_path]
del self.index[from_path]
def get_root_id(self):
return self.path2id("")
@needs_read_lock
def path2id(self, path):
return self._fileid_map.lookup_file_id(path.encode("utf-8"))
def extras(self):
"""Yield all unversioned files in this WorkingTree.
"""
for (dirpath, dirnames, filenames) in os.walk(self.basedir):
if self.bzrdir.is_control_filename(dirpath[len(self.basedir):].strip("/")):
continue
for filename in filenames:
relpath = os.path.join(dirpath[len(self.basedir):].strip("/"), filename)
if not relpath in self.index:
yield relpath
def unlock(self):
# non-implementation specific cleanup
self._cleanup()
# reverse order of locking.
try:
return self._control_files.unlock()
finally:
self.branch.unlock()
def flush(self):
# TODO: Maybe this should only write on dirty ?
if self._control_files._lock_mode != 'w':
raise errors.NotWriteLocked(self)
self.index.write()
def __iter__(self):
for path in self.index:
yield self.path2id(path)
def has_or_had_id(self, file_id):
if self.has_id(file_id):
return True
if self.had_id(file_id):
return True
return False
def had_id(self, file_id):
path = self._basis_fileid_map.lookup_file_id(file_id)
try:
head = self.repository._git.head()
except KeyError:
# Assume no if basis is not accessible
return False
if head == ZERO_SHA:
return False
root_tree = self.store[head].tree
try:
tree_lookup_path(self.store.__getitem__, root_tree, path)
except KeyError:
return False
else:
return True
def has_id(self, file_id):
try:
self.id2path(file_id)
except errors.NoSuchId:
return False
else:
return True
def id2path(self, file_id):
if type(file_id) != str:
raise AssertionError
path = self._fileid_map.lookup_path(file_id)
if path in self.index:
return path
raise errors.NoSuchId(None, file_id)
def get_file_mtime(self, file_id, path=None):
"""See Tree.get_file_mtime."""
if not path:
path = self.id2path(file_id)
return os.lstat(self.abspath(path)).st_mtime
def get_ignore_list(self):
ignoreset = getattr(self, '_ignoreset', None)
if ignoreset is not None:
return ignoreset
ignore_globs = set()
ignore_globs.update(ignores.get_runtime_ignores())
ignore_globs.update(ignores.get_user_ignores())
if self.has_filename(IGNORE_FILENAME):
f = self.get_file_byname(IGNORE_FILENAME)
try:
# FIXME: Parse git file format, rather than assuming it's
# the same as for bzr's native formats.
ignore_globs.update(ignores.parse_ignore_file(f))
finally:
f.close()
self._ignoreset = ignore_globs
return ignore_globs
def set_last_revision(self, revid):
self._change_last_revision(revid)
def _reset_data(self):
try:
head = self.repository._git.head()
except KeyError, name:
raise errors.NotBranchError("branch %s at %s" % (name, self.repository.base))
if head == ZERO_SHA:
self._basis_fileid_map = GitFileIdMap({}, self.mapping)
else:
self._basis_fileid_map = self.mapping.get_fileid_map(self.store.__getitem__,
self.store[head].tree)
@needs_read_lock
def get_file_sha1(self, file_id, path=None, stat_value=None):
if not path:
path = self.id2path(file_id)
try:
return osutils.sha_file_by_name(self.abspath(path).encode(osutils._fs_enc))
except OSError, (num, msg):
if num in (errno.EISDIR, errno.ENOENT):
return None
raise
def revision_tree(self, revid):
return self.repository.revision_tree(revid)
def _get_dir_ie(self, path, parent_id):
file_id = self.path2id(path)
return inventory.InventoryDirectory(file_id,
posixpath.basename(path).strip("/"), parent_id)
def _add_missing_parent_ids(self, path, dir_ids):
if path in dir_ids:
return []
parent = posixpath.dirname(path).strip("/")
ret = self._add_missing_parent_ids(parent, dir_ids)
parent_id = dir_ids[parent]
ie = self._get_dir_ie(path, parent_id)
dir_ids[path] = ie.file_id
ret.append((path, ie))
return ret
def _get_file_ie(self, path, value, parent_id):
assert isinstance(path, unicode)
assert isinstance(value, tuple) and len(value) == 10
(ctime, mtime, dev, ino, mode, uid, gid, size, sha, flags) = value
file_id = self.path2id(path)
if type(file_id) != str:
raise AssertionError
kind = mode_kind(mode)
ie = inventory.entry_factory[kind](file_id,
posixpath.basename(path), parent_id)
if kind == 'symlink':
ie.symlink_target = self.get_symlink_target(file_id)
else:
data = self.get_file_text(file_id, path)
ie.text_sha1 = osutils.sha_string(data)
ie.text_size = len(data)
ie.executable = self.is_executable(file_id, path)
ie.revision = None
return ie
def _is_executable_from_path_and_stat_from_stat(self, path, stat_result):
mode = stat_result.st_mode
return bool(stat.S_ISREG(mode) and stat.S_IEXEC & mode)
def stored_kind(self, file_id, path=None):
if path is None:
path = self.id2path(file_id)
head = self.repository._git.head()
if head == ZERO_SHA:
raise errors.NoSuchId(self, file_id)
root_tree = self.store[head].tree
(mode, hexsha) = tree_lookup_path(self.store.__getitem__, root_tree, path)
return mode_kind(mode)
if not osutils.supports_executable():
def is_executable(self, file_id, path=None):
basis_tree = self.basis_tree()
if file_id in basis_tree:
return basis_tree.is_executable(file_id)
# Default to not executable
return False
else:
def is_executable(self, file_id, path=None):
if not path:
path = self.id2path(file_id)
mode = os.lstat(self.abspath(path)).st_mode
return bool(stat.S_ISREG(mode) and stat.S_IEXEC & mode)
_is_executable_from_path_and_stat = \
_is_executable_from_path_and_stat_from_stat
def list_files(self, include_root=False, from_dir=None, recursive=True):
# FIXME: Yield non-versioned files
# FIXME: support from_dir
# FIXME: Support recursive
dir_ids = {}
root_ie = self._get_dir_ie(u"", None)
if include_root and not from_dir:
yield "", "V", root_ie.kind, root_ie.file_id, root_ie
dir_ids[u""] = root_ie.file_id
for path, value in self.index.iteritems():
path = path.decode("utf-8")
parent = posixpath.dirname(path).strip("/")
for dir_path, dir_ie in self._add_missing_parent_ids(parent, dir_ids):
yield dir_path, "V", dir_ie.kind, dir_ie.file_id, dir_ie
ie = self._get_file_ie(path, value, dir_ids[parent])
yield path, "V", ie.kind, ie.file_id, ie
def all_file_ids(self):
ids = {u"": self.path2id("")}
for path in self.index:
path = path.decode("utf-8")
parent = posixpath.dirname(path).strip("/")
for e in self._add_missing_parent_ids(parent, ids):
pass
ids[path] = self.path2id(path)
return set(ids.values())
def iter_entries_by_dir(self, specific_file_ids=None, yield_parents=False):
# FIXME: Support specific_file_ids
# FIXME: Is return order correct?
if specific_file_ids is not None:
raise NotImplementedError(self.iter_entries_by_dir)
root_ie = self._get_dir_ie(u"", None)
yield u"", root_ie
dir_ids = {u"": root_ie.file_id}
for path, value in self.index.iteritems():
path = path.decode("utf-8")
parent = posixpath.dirname(path).strip("/")
for (dir_path, dir_ie) in self._add_missing_parent_ids(parent,
dir_ids):
yield dir_path, dir_ie
parent_id = self.path2id(parent)
yield path, self._get_file_ie(path, value, parent_id)
@needs_read_lock
def conflicts(self):
# FIXME:
return _mod_conflicts.ConflictList()
def update_basis_by_delta(self, new_revid, delta):
# The index just contains content, which won't have changed.
self._reset_data()
def _walkdirs(self, prefix=""):
if prefix != "":
prefix += "/"
per_dir = defaultdict(list)
for path, value in self.index.iteritems():
if not path.startswith(prefix):
continue
(dirname, child_name) = posixpath.split(path)
dirname = dirname.decode("utf-8")
dir_file_id = self.path2id(dirname)
assert isinstance(value, tuple) and len(value) == 10
(ctime, mtime, dev, ino, mode, uid, gid, size, sha, flags) = value
stat_result = posix.stat_result((mode, ino,
dev, 1, uid, gid, size,
0, mtime, ctime))
per_dir[(dirname, dir_file_id)].append(
(path.decode("utf-8"), child_name.decode("utf-8"),
mode_kind(mode), stat_result,
self.path2id(path.decode("utf-8")),
mode_kind(mode)))
return per_dir.iteritems()
class GitWorkingTreeFormat(workingtree.WorkingTreeFormat):
_tree_class = GitWorkingTree
@property
def _matchingbzrdir(self):
from bzrlib.plugins.git.dir import LocalGitControlDirFormat
return LocalGitControlDirFormat()
def get_format_description(self):
return "Git Working Tree"
def initialize(self, a_bzrdir, revision_id=None, from_branch=None,
accelerator_tree=None, hardlink=False):
"""See WorkingTreeFormat.initialize()."""
if not isinstance(a_bzrdir, LocalGitDir):
raise errors.IncompatibleFormat(self, a_bzrdir)
index = Index(a_bzrdir.root_transport.local_abspath(".git/index"))
index.write()
return GitWorkingTree(a_bzrdir, a_bzrdir.open_repository(),
a_bzrdir.open_branch(), index)
class InterIndexGitTree(tree.InterTree):
"""InterTree that works between a Git revision tree and an index."""
def __init__(self, source, target):
super(InterIndexGitTree, self).__init__(source, target)
self._index = target.index
@classmethod
def is_compatible(cls, source, target):
from bzrlib.plugins.git.repository import GitRevisionTree
return (isinstance(source, GitRevisionTree) and
isinstance(target, GitWorkingTree))
def compare(self, want_unchanged=False, specific_files=None,
extra_trees=None, require_versioned=False, include_root=False,
want_unversioned=False):
changes = self._index.changes_from_tree(
self.source.store, self.source.tree,
want_unchanged=want_unchanged)
source_fileid_map = self.source.mapping.get_fileid_map(
self.source.store.__getitem__,
self.source.tree)
if self.target.mapping.BZR_FILE_IDS_FILE is not None:
file_id = self.target.path2id(
self.target.mapping.BZR_FILE_IDS_FILE)
if file_id is None:
target_fileid_map = {}
else:
target_fileid_map = self.target.mapping.import_fileid_map(
Blob.from_string(self.target.get_file_text(file_id)))
else:
target_fileid_map = {}
target_fileid_map = GitFileIdMap(target_fileid_map,
self.target.mapping)
ret = tree_delta_from_git_changes(changes, self.target.mapping,
(source_fileid_map, target_fileid_map),
specific_file=specific_files, require_versioned=require_versioned)
if want_unversioned:
for e in self.target.extras():
ret.unversioned.append((e, None,
osutils.file_kind(self.target.abspath(e))))
return ret
def iter_changes(self, include_unchanged=False, specific_files=None,
pb=None, extra_trees=[], require_versioned=True,
want_unversioned=False):
changes = self._index.changes_from_tree(
self.source.store, self.source.tree,
want_unchanged=include_unchanged)
# FIXME: Handle want_unversioned
return changes_from_git_changes(changes, self.target.mapping,
specific_file=specific_files)
tree.InterTree.register_optimiser(InterIndexGitTree)
|