/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

Merge changes to avoid inventories.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2008 Jelmer Vernooij <jelmer@samba.org>
 
1
# Copyright (C) 2008-2011 Jelmer Vernooij <jelmer@samba.org>
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
21
21
from cStringIO import (
22
22
    StringIO,
23
23
    )
 
24
from collections import defaultdict
24
25
import errno
 
26
from dulwich.errors import NotGitRepository
 
27
from dulwich.index import (
 
28
    Index,
 
29
    changes_from_tree,
 
30
    cleanup_mode,
 
31
    index_entry_from_stat,
 
32
    )
 
33
from dulwich.object_store import (
 
34
    tree_lookup_path,
 
35
    )
25
36
from dulwich.objects import (
26
37
    Blob,
27
 
    )
28
 
from dulwich.protocol import (
 
38
    S_IFGITLINK,
29
39
    ZERO_SHA,
30
40
    )
 
41
from dulwich.repo import Repo
31
42
import os
 
43
import posixpath
32
44
import stat
 
45
import sys
33
46
 
34
47
from bzrlib import (
35
48
    errors,
 
49
    conflicts as _mod_conflicts,
36
50
    ignores,
37
 
    lockable_files,
38
 
    lockdir,
 
51
    inventory,
 
52
    lock,
39
53
    osutils,
40
 
    transport,
 
54
    trace,
41
55
    tree,
42
56
    workingtree,
43
57
    )
44
58
from bzrlib.decorators import (
45
59
    needs_read_lock,
46
60
    )
47
 
 
48
 
 
49
 
from bzrlib.plugins.git.inventory import (
50
 
    GitIndexInventory,
 
61
from bzrlib.mutabletree import needs_tree_write_lock
 
62
 
 
63
 
 
64
from bzrlib.plugins.git.dir import (
 
65
    LocalGitDir,
51
66
    )
52
67
from bzrlib.plugins.git.tree import (
53
68
    changes_from_git_changes,
55
70
    )
56
71
from bzrlib.plugins.git.mapping import (
57
72
    GitFileIdMap,
 
73
    mode_kind,
58
74
    )
59
75
 
60
76
IGNORE_FILENAME = ".gitignore"
67
83
        self.basedir = bzrdir.root_transport.local_abspath('.')
68
84
        self.bzrdir = bzrdir
69
85
        self.repository = repo
 
86
        self.store = self.repository._git.object_store
70
87
        self.mapping = self.repository.get_mapping()
71
88
        self._branch = branch
72
89
        self._transport = bzrdir.transport
73
 
 
74
 
        self.controldir = self.bzrdir.transport.local_abspath('bzr')
75
 
 
76
 
        try:
77
 
            os.makedirs(self.controldir)
78
 
            os.makedirs(os.path.join(self.controldir, 'lock'))
79
 
        except OSError:
80
 
            pass
81
 
 
82
 
        self._control_files = lockable_files.LockableFiles(
83
 
            transport.get_transport(self.controldir), 'lock', lockdir.LockDir)
84
90
        self._format = GitWorkingTreeFormat()
85
91
        self.index = index
 
92
        self._versioned_dirs = None
86
93
        self.views = self._make_views()
 
94
        self._rules_searcher = None
87
95
        self._detect_case_handling()
88
 
 
 
96
        self._reset_data()
 
97
        self._fileid_map = self._basis_fileid_map.copy()
 
98
        self._lock_mode = None
 
99
        self._lock_count = 0
 
100
 
 
101
    def lock_read(self):
 
102
        """Lock the repository for read operations.
 
103
 
 
104
        :return: A bzrlib.lock.LogicalLockResult.
 
105
        """
 
106
        if not self._lock_mode:
 
107
            self._lock_mode = 'r'
 
108
            self._lock_count = 1
 
109
            self.index.read()
 
110
        else:
 
111
            self._lock_count += 1
 
112
        self.branch.lock_read()
 
113
        return lock.LogicalLockResult(self.unlock)
 
114
 
 
115
    def lock_tree_write(self):
 
116
        if not self._lock_mode:
 
117
            self._lock_mode = 'w'
 
118
            self._lock_count = 1
 
119
            self.index.read()
 
120
        elif self._lock_mode == 'r':
 
121
            raise errors.ReadOnlyError(self)
 
122
        else:
 
123
            self._lock_count +=1
 
124
        self.branch.lock_read()
 
125
        return lock.LogicalLockResult(self.unlock)
 
126
 
 
127
    def lock_write(self, token=None):
 
128
        if not self._lock_mode:
 
129
            self._lock_mode = 'w'
 
130
            self._lock_count = 1
 
131
            self.index.read()
 
132
        elif self._lock_mode == 'r':
 
133
            raise errors.ReadOnlyError(self)
 
134
        else:
 
135
            self._lock_count +=1
 
136
        self.branch.lock_write()
 
137
        return lock.LogicalLockResult(self.unlock)
 
138
 
 
139
    def is_locked(self):
 
140
        return self._lock_count >= 1
 
141
 
 
142
    def get_physical_lock_status(self):
 
143
        return False
 
144
 
 
145
    def unlock(self):
 
146
        if not self._lock_count:
 
147
            return lock.cant_unlock_not_held(self)
 
148
        self.branch.unlock()
 
149
        self._cleanup()
 
150
        self._lock_count -= 1
 
151
        if self._lock_count > 0:
 
152
            return
 
153
        self._lock_mode = None
 
154
 
 
155
    def _detect_case_handling(self):
 
156
        try:
 
157
            self._transport.stat(".git/cOnFiG")
 
158
        except errors.NoSuchFile:
 
159
            self.case_sensitive = True
 
160
        else:
 
161
            self.case_sensitive = False
 
162
 
 
163
    def merge_modified(self):
 
164
        return {}
 
165
 
 
166
    def set_parent_trees(self, parents_list, allow_leftmost_as_ghost=False):
 
167
        self.set_parent_ids([p for p, t in parents_list])
 
168
 
 
169
    def _index_add_entry(self, path, file_id, kind):
 
170
        assert self._lock_mode is not None
 
171
        assert isinstance(path, basestring)
 
172
        assert type(file_id) == str or file_id is None
 
173
        if kind == "directory":
 
174
            # Git indexes don't contain directories
 
175
            return
 
176
        if kind == "file":
 
177
            blob = Blob()
 
178
            try:
 
179
                file, stat_val = self.get_file_with_stat(file_id, path)
 
180
            except (errors.NoSuchFile, IOError):
 
181
                # TODO: Rather than come up with something here, use the old index
 
182
                file = StringIO()
 
183
                stat_val = os.stat_result(
 
184
                    (stat.S_IFREG | 0644, 0, 0, 0, 0, 0, 0, 0, 0, 0))
 
185
            blob.set_raw_string(file.read())
 
186
        elif kind == "symlink":
 
187
            blob = Blob()
 
188
            try:
 
189
                stat_val = os.lstat(self.abspath(path))
 
190
            except (errors.NoSuchFile, OSError):
 
191
                # TODO: Rather than come up with something here, use the 
 
192
                # old index
 
193
                stat_val = os.stat_result(
 
194
                    (stat.S_IFLNK, 0, 0, 0, 0, 0, 0, 0, 0, 0))
 
195
            blob.set_raw_string(
 
196
                self.get_symlink_target(file_id, path).encode("utf-8"))
 
197
        else:
 
198
            raise AssertionError("unknown kind '%s'" % kind)
 
199
        # Add object to the repository if it didn't exist yet
 
200
        if not blob.id in self.store:
 
201
            self.store.add_object(blob)
 
202
        # Add an entry to the index or update the existing entry
 
203
        flags = 0 # FIXME
 
204
        encoded_path = path.encode("utf-8")
 
205
        self.index[encoded_path] = index_entry_from_stat(
 
206
            stat_val, blob.id, flags)
 
207
        if self._versioned_dirs is not None:
 
208
            self._ensure_versioned_dir(encoded_path)
 
209
 
 
210
    def _ensure_versioned_dir(self, dirname):
 
211
        if dirname in self._versioned_dirs:
 
212
            return
 
213
        if dirname != "":
 
214
            self._ensure_versioned_dir(posixpath.dirname(dirname))
 
215
        self._versioned_dirs.add(dirname)
 
216
 
 
217
    def _load_dirs(self):
 
218
        assert self._lock_mode is not None
 
219
        self._versioned_dirs = set()
 
220
        for p in self.index:
 
221
            self._ensure_versioned_dir(posixpath.dirname(p))
 
222
 
 
223
    def _unversion_path(self, path):
 
224
        assert self._lock_mode is not None
 
225
        encoded_path = path.encode("utf-8")
 
226
        try:
 
227
            del self.index[encoded_path]
 
228
        except KeyError:
 
229
            # A directory, perhaps?
 
230
            for p in list(self.index):
 
231
                if p.startswith(encoded_path+"/"):
 
232
                    del self.index[p]
 
233
        # FIXME: remove empty directories
 
234
 
 
235
    @needs_tree_write_lock
 
236
    def unversion(self, file_ids):
 
237
        for file_id in file_ids:
 
238
            path = self.id2path(file_id)
 
239
            self._unversion_path(path)
 
240
        self.flush()
 
241
 
 
242
    def check_state(self):
 
243
        """Check that the working state is/isn't valid."""
 
244
        pass
 
245
 
 
246
    @needs_tree_write_lock
 
247
    def remove(self, files, verbose=False, to_file=None, keep_files=True,
 
248
        force=False):
 
249
        """Remove nominated files from the working tree metadata.
 
250
 
 
251
        :param files: File paths relative to the basedir.
 
252
        :param keep_files: If true, the files will also be kept.
 
253
        :param force: Delete files and directories, even if they are changed
 
254
            and even if the directories are not empty.
 
255
        """
 
256
        all_files = set() # specified and nested files 
 
257
 
 
258
        if isinstance(files, basestring):
 
259
            files = [files]
 
260
 
 
261
        if to_file is None:
 
262
            to_file = sys.stdout
 
263
 
 
264
        files = list(all_files)
 
265
 
 
266
        if len(files) == 0:
 
267
            return # nothing to do
 
268
 
 
269
        # Sort needed to first handle directory content before the directory
 
270
        files.sort(reverse=True)
 
271
 
 
272
        def backup(file_to_backup):
 
273
            abs_path = self.abspath(file_to_backup)
 
274
            backup_name = self.bzrdir._available_backup_name(file_to_backup)
 
275
            osutils.rename(abs_path, self.abspath(backup_name))
 
276
            return "removed %s (but kept a copy: %s)" % (
 
277
                file_to_backup, backup_name)
 
278
 
 
279
        for f in files:
 
280
            fid = self.path2id(f)
 
281
            if not fid:
 
282
                message = "%s is not versioned." % (f,)
 
283
            else:
 
284
                abs_path = self.abspath(f)
 
285
                if verbose:
 
286
                    # having removed it, it must be either ignored or unknown
 
287
                    if self.is_ignored(f):
 
288
                        new_status = 'I'
 
289
                    else:
 
290
                        new_status = '?'
 
291
                    # XXX: Really should be a more abstract reporter interface
 
292
                    kind_ch = osutils.kind_marker(self.kind(fid))
 
293
                    to_file.write(new_status + '       ' + f + kind_ch + '\n')
 
294
                # Unversion file
 
295
                # FIXME: _unversion_path() is O(size-of-index) for directories
 
296
                self._unversion_path(f)
 
297
                message = "removed %s" % (f,)
 
298
                if osutils.lexists(abs_path):
 
299
                    if (osutils.isdir(abs_path) and
 
300
                        len(os.listdir(abs_path)) > 0):
 
301
                        if force:
 
302
                            osutils.rmtree(abs_path)
 
303
                            message = "deleted %s" % (f,)
 
304
                        else:
 
305
                            message = backup(f)
 
306
                    else:
 
307
                        if not keep_files:
 
308
                            osutils.delete_any(abs_path)
 
309
                            message = "deleted %s" % (f,)
 
310
 
 
311
            # print only one message (if any) per file.
 
312
            if message is not None:
 
313
                trace.note(message)
 
314
        self.flush()
 
315
 
 
316
    def _add(self, files, ids, kinds):
 
317
        for (path, file_id, kind) in zip(files, ids, kinds):
 
318
            if file_id is not None:
 
319
                self._fileid_map.set_file_id(path.encode("utf-8"), file_id)
 
320
            else:
 
321
                file_id = self._fileid_map.lookup_file_id(path.encode("utf-8"))
 
322
            self._index_add_entry(path, file_id, kind)
 
323
 
 
324
    @needs_tree_write_lock
 
325
    def smart_add(self, file_list, recurse=True, action=None, save=True):
 
326
        added = []
 
327
        ignored = {}
 
328
        user_dirs = []
 
329
        for filepath in osutils.canonical_relpaths(self.basedir, file_list):
 
330
            abspath = self.abspath(filepath)
 
331
            kind = osutils.file_kind(abspath)
 
332
            if action is not None:
 
333
                file_id = action(self, None, filepath, kind)
 
334
            else:
 
335
                file_id = None
 
336
            if kind in ("file", "symlink"):
 
337
                if save:
 
338
                    self._index_add_entry(filepath, file_id, kind)
 
339
                added.append(filepath)
 
340
            elif kind == "directory":
 
341
                if recurse:
 
342
                    user_dirs.append(filepath)
 
343
            else:
 
344
                raise errors.BadFileKindError(filename=abspath, kind=kind)
 
345
        for user_dir in user_dirs:
 
346
            abs_user_dir = self.abspath(user_dir)
 
347
            for name in os.listdir(abs_user_dir):
 
348
                subp = os.path.join(user_dir, name)
 
349
                if self.is_control_filename(subp) or self.mapping.is_special_file(subp):
 
350
                    continue
 
351
                ignore_glob = self.is_ignored(subp)
 
352
                if ignore_glob is not None:
 
353
                    ignored.setdefault(ignore_glob, []).append(subp)
 
354
                    continue
 
355
                abspath = self.abspath(subp)
 
356
                kind = osutils.file_kind(abspath)
 
357
                if kind == "directory":
 
358
                    user_dirs.append(subp)
 
359
                else:
 
360
                    if action is not None:
 
361
                        file_id = action(self, None, filepath, kind)
 
362
                    else:
 
363
                        file_id = None
 
364
                    if save:
 
365
                        self._index_add_entry(subp, file_id, kind)
 
366
        if added and save:
 
367
            self.flush()
 
368
        return added, ignored
 
369
 
 
370
    def _set_root_id(self, file_id):
 
371
        self._fileid_map.set_file_id("", file_id)
 
372
 
 
373
    @needs_tree_write_lock
 
374
    def move(self, from_paths, to_dir=None, after=False):
 
375
        rename_tuples = []
 
376
        to_abs = self.abspath(to_dir)
 
377
        if not os.path.isdir(to_abs):
 
378
            raise errors.BzrMoveFailedError('', to_dir,
 
379
                errors.NotADirectory(to_abs))
 
380
 
 
381
        for from_rel in from_paths:
 
382
            from_tail = os.path.split(from_rel)[-1]
 
383
            to_rel = os.path.join(to_dir, from_tail)
 
384
            self.rename_one(from_rel, to_rel, after=after)
 
385
            rename_tuples.append((from_rel, to_rel))
 
386
        self.flush()
 
387
        return rename_tuples
 
388
 
 
389
    @needs_tree_write_lock
 
390
    def rename_one(self, from_rel, to_rel, after=False):
 
391
        from_path = from_rel.encode("utf-8")
 
392
        to_path = to_rel.encode("utf-8")
 
393
        if not self.has_filename(to_rel):
 
394
            raise errors.BzrMoveFailedError(from_rel, to_rel,
 
395
                errors.NoSuchFile(to_rel))
 
396
        if not from_path in self.index:
 
397
            raise errors.BzrMoveFailedError(from_rel, to_rel,
 
398
                errors.NotVersionedError(path=from_rel))
 
399
        if not after:
 
400
            os.rename(self.abspath(from_rel), self.abspath(to_rel))
 
401
        self.index[to_path] = self.index[from_path]
 
402
        del self.index[from_path]
 
403
        self.flush()
 
404
 
 
405
    def get_root_id(self):
 
406
        return self.path2id("")
 
407
 
 
408
    def _has_dir(self, path):
 
409
        if path == "":
 
410
            return True
 
411
        if self._versioned_dirs is None:
 
412
            self._load_dirs()
 
413
        return path in self._versioned_dirs
 
414
 
 
415
    @needs_read_lock
 
416
    def path2id(self, path):
 
417
        encoded_path = path.encode("utf-8")
 
418
        if self._is_versioned(encoded_path):
 
419
            return self._fileid_map.lookup_file_id(encoded_path)
 
420
        return None
 
421
 
 
422
    def _iter_files_recursive(self, from_dir=None):
 
423
        if from_dir is None:
 
424
            from_dir = ""
 
425
        for (dirpath, dirnames, filenames) in os.walk(self.abspath(from_dir)):
 
426
            dir_relpath = dirpath[len(self.basedir):].strip("/")
 
427
            if self.bzrdir.is_control_filename(dir_relpath):
 
428
                continue
 
429
            for filename in filenames:
 
430
                if not self.mapping.is_special_file(filename):
 
431
                    yield os.path.join(dir_relpath, filename)
 
432
 
 
433
    @needs_read_lock
89
434
    def extras(self):
90
435
        """Yield all unversioned files in this WorkingTree.
91
436
        """
92
 
        for (dirpath, dirnames, filenames) in os.walk(self.basedir):
93
 
            if self.bzrdir.is_control_filename(dirpath[len(self.basedir):].strip("/")):
94
 
                continue
95
 
            for filename in filenames:
96
 
                relpath = os.path.join(dirpath[len(self.basedir):].strip("/"), filename)
97
 
                if not relpath in self.index:
98
 
                    yield relpath
99
 
 
100
 
 
101
 
    def unlock(self):
102
 
        # non-implementation specific cleanup
103
 
        self._cleanup()
104
 
 
105
 
        # reverse order of locking.
106
 
        try:
107
 
            return self._control_files.unlock()
108
 
        finally:
109
 
            self.branch.unlock()
110
 
 
111
 
    def is_control_filename(self, path):
112
 
        return os.path.basename(path) == ".git"
113
 
 
114
 
    def _rewrite_index(self):
115
 
        self.index.clear()
116
 
        for path, entry in self._inventory.iter_entries():
117
 
            if entry.kind == "directory":
118
 
                # Git indexes don't contain directories
119
 
                continue
120
 
            if entry.kind == "file":
121
 
                blob = Blob()
122
 
                try:
123
 
                    file, stat_val = self.get_file_with_stat(entry.file_id, path)
124
 
                except (errors.NoSuchFile, IOError):
125
 
                    # TODO: Rather than come up with something here, use the old index
126
 
                    file = StringIO()
127
 
                    from posix import stat_result
128
 
                    stat_val = stat_result((stat.S_IFREG | 0644, 0, 0, 0, 0, 0, 0, 0, 0, 0))
129
 
                blob.set_raw_string(file.read())
130
 
            elif entry.kind == "symlink":
131
 
                blob = Blob()
132
 
                try:
133
 
                    stat_val = os.lstat(self.abspath(path))
134
 
                except (errors.NoSuchFile, OSError):
135
 
                    # TODO: Rather than come up with something here, use the 
136
 
                    # old index
137
 
                    from posix import stat_result
138
 
                    stat_val = stat_result((stat.S_IFLNK, 0, 0, 0, 0, 0, 0, 0, 0, 0))
139
 
                blob.set_raw_string(entry.symlink_target)
140
 
            else:
141
 
                raise AssertionError("unknown kind '%s'" % entry.kind)
142
 
            # Add object to the repository if it didn't exist yet
143
 
            if not blob.id in self.repository._git.object_store:
144
 
                self.repository._git.object_store.add_object(blob)
145
 
            # Add an entry to the index or update the existing entry
146
 
            flags = 0 # FIXME
147
 
            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)
148
 
 
 
437
        return set(self._iter_files_recursive()) - set(self.index)
 
438
 
 
439
    @needs_tree_write_lock
149
440
    def flush(self):
150
441
        # TODO: Maybe this should only write on dirty ?
151
 
        if self._control_files._lock_mode != 'w':
 
442
        if self._lock_mode != 'w':
152
443
            raise errors.NotWriteLocked(self)
153
 
        self._rewrite_index()
154
444
        self.index.write()
155
 
        self._inventory_is_modified = False
 
445
 
 
446
    @needs_read_lock
 
447
    def __iter__(self):
 
448
        for path in self.index:
 
449
            yield self.path2id(path)
 
450
        self._load_dirs()
 
451
        for path in self._versioned_dirs:
 
452
            yield self.path2id(path)
 
453
 
 
454
    def has_or_had_id(self, file_id):
 
455
        if self.has_id(file_id):
 
456
            return True
 
457
        if self.had_id(file_id):
 
458
            return True
 
459
        return False
 
460
 
 
461
    def had_id(self, file_id):
 
462
        path = self._basis_fileid_map.lookup_file_id(file_id)
 
463
        try:
 
464
            head = self.repository._git.head()
 
465
        except KeyError:
 
466
            # Assume no if basis is not accessible
 
467
            return False
 
468
        if head == ZERO_SHA:
 
469
            return False
 
470
        root_tree = self.store[head].tree
 
471
        try:
 
472
            tree_lookup_path(self.store.__getitem__, root_tree, path)
 
473
        except KeyError:
 
474
            return False
 
475
        else:
 
476
            return True
 
477
 
 
478
    def has_id(self, file_id):
 
479
        try:
 
480
            self.id2path(file_id)
 
481
        except errors.NoSuchId:
 
482
            return False
 
483
        else:
 
484
            return True
 
485
 
 
486
    @needs_read_lock
 
487
    def id2path(self, file_id):
 
488
        assert type(file_id) is str, "file id not a string: %r" % file_id
 
489
        file_id = osutils.safe_utf8(file_id)
 
490
        path = self._fileid_map.lookup_path(file_id)
 
491
        # FIXME: What about directories?
 
492
        if self._is_versioned(path):
 
493
            return path.decode("utf-8")
 
494
        raise errors.NoSuchId(self, file_id)
 
495
 
 
496
    def get_file_mtime(self, file_id, path=None):
 
497
        """See Tree.get_file_mtime."""
 
498
        if not path:
 
499
            path = self.id2path(file_id)
 
500
        return os.lstat(self.abspath(path)).st_mtime
156
501
 
157
502
    def get_ignore_list(self):
158
503
        ignoreset = getattr(self, '_ignoreset', None)
165
510
        if self.has_filename(IGNORE_FILENAME):
166
511
            f = self.get_file_byname(IGNORE_FILENAME)
167
512
            try:
 
513
                # FIXME: Parse git file format, rather than assuming it's
 
514
                # the same as for bzr's native formats.
168
515
                ignore_globs.update(ignores.parse_ignore_file(f))
169
516
            finally:
170
517
                f.close()
175
522
        self._change_last_revision(revid)
176
523
 
177
524
    def _reset_data(self):
178
 
        self._inventory_is_modified = False
179
525
        try:
180
526
            head = self.repository._git.head()
181
527
        except KeyError, name:
182
 
            raise errors.NotBranchError("branch %s at %s" % (name, self.repository.base))
183
 
        basis_inv = self.repository.get_inventory(self.branch.lookup_foreign_revision_id(head))
184
 
        store = self.repository._git.object_store
 
528
            raise errors.NotBranchError("branch %s at %s" % (name,
 
529
                self.repository.base))
185
530
        if head == ZERO_SHA:
186
 
            fileid_map = GitFileIdMap({}, self.mapping)
187
 
            basis_inv = None
 
531
            self._basis_fileid_map = GitFileIdMap({}, self.mapping)
188
532
        else:
189
 
            fileid_map = self.mapping.get_fileid_map(store.__getitem__,
190
 
                store[head].tree)
191
 
        result = GitIndexInventory(basis_inv, fileid_map, self.index, store)
192
 
        self._set_inventory(result, dirty=False)
 
533
            self._basis_fileid_map = self.mapping.get_fileid_map(
 
534
                self.store.__getitem__, self.store[head].tree)
 
535
 
 
536
    @needs_read_lock
 
537
    def get_file_verifier(self, file_id, path=None, stat_value=None):
 
538
        if path is None:
 
539
            path = self.id2path(file_id)
 
540
        return ("GIT", self.index[path][-2])
193
541
 
194
542
    @needs_read_lock
195
543
    def get_file_sha1(self, file_id, path=None, stat_value=None):
196
544
        if not path:
197
 
            path = self._inventory.id2path(file_id)
 
545
            path = self.id2path(file_id)
 
546
        abspath = self.abspath(path).encode(osutils._fs_enc)
198
547
        try:
199
 
            return osutils.sha_file_by_name(self.abspath(path).encode(osutils._fs_enc))
 
548
            return osutils.sha_file_by_name(abspath)
200
549
        except OSError, (num, msg):
201
550
            if num in (errno.EISDIR, errno.ENOENT):
202
551
                return None
205
554
    def revision_tree(self, revid):
206
555
        return self.repository.revision_tree(revid)
207
556
 
 
557
    def _is_versioned(self, path):
 
558
        assert self._lock_mode is not None
 
559
        return (path in self.index or self._has_dir(path))
 
560
 
 
561
    def filter_unversioned_files(self, files):
 
562
        return set([p for p in files if not self._is_versioned(p.encode("utf-8"))])
 
563
 
 
564
    def _get_dir_ie(self, path, parent_id):
 
565
        file_id = self.path2id(path)
 
566
        return inventory.InventoryDirectory(file_id,
 
567
            posixpath.basename(path).strip("/"), parent_id)
 
568
 
 
569
    def _add_missing_parent_ids(self, path, dir_ids):
 
570
        if path in dir_ids:
 
571
            return []
 
572
        parent = posixpath.dirname(path).strip("/")
 
573
        ret = self._add_missing_parent_ids(parent, dir_ids)
 
574
        parent_id = dir_ids[parent]
 
575
        ie = self._get_dir_ie(path, parent_id)
 
576
        dir_ids[path] = ie.file_id
 
577
        ret.append((path, ie))
 
578
        return ret
 
579
 
 
580
    def _get_file_ie(self, name, path, value, parent_id):
 
581
        assert isinstance(name, unicode)
 
582
        assert isinstance(path, unicode)
 
583
        assert isinstance(value, tuple) and len(value) == 10
 
584
        (ctime, mtime, dev, ino, mode, uid, gid, size, sha, flags) = value
 
585
        file_id = self.path2id(path)
 
586
        if type(file_id) != str:
 
587
            raise AssertionError
 
588
        kind = mode_kind(mode)
 
589
        ie = inventory.entry_factory[kind](file_id, name, parent_id)
 
590
        if kind == 'symlink':
 
591
            ie.symlink_target = self.get_symlink_target(file_id)
 
592
        else:
 
593
            data = self.get_file_text(file_id, path)
 
594
            ie.text_sha1 = osutils.sha_string(data)
 
595
            ie.text_size = len(data)
 
596
            ie.executable = self.is_executable(file_id, path)
 
597
        ie.revision = None
 
598
        return ie
 
599
 
 
600
    def _is_executable_from_path_and_stat_from_stat(self, path, stat_result):
 
601
        mode = stat_result.st_mode
 
602
        return bool(stat.S_ISREG(mode) and stat.S_IEXEC & mode)
 
603
 
 
604
    @needs_read_lock
 
605
    def stored_kind(self, file_id, path=None):
 
606
        if path is None:
 
607
            path = self.id2path(file_id)
 
608
        try:
 
609
            return mode_kind(self.index[path.encode("utf-8")][4])
 
610
        except KeyError:
 
611
            # Maybe it's a directory?
 
612
            if self._has_dir(path):
 
613
                return "directory"
 
614
            raise errors.NoSuchId(self, file_id)
 
615
 
 
616
    def is_executable(self, file_id, path=None):
 
617
        if getattr(self, "_supports_executable", osutils.supports_executable)():
 
618
            if not path:
 
619
                path = self.id2path(file_id)
 
620
            mode = os.lstat(self.abspath(path)).st_mode
 
621
            return bool(stat.S_ISREG(mode) and stat.S_IEXEC & mode)
 
622
        else:
 
623
            basis_tree = self.basis_tree()
 
624
            if file_id in basis_tree:
 
625
                return basis_tree.is_executable(file_id)
 
626
            # Default to not executable
 
627
            return False
 
628
 
 
629
    def _is_executable_from_path_and_stat(self, path, stat_result):
 
630
        if getattr(self, "_supports_executable", osutils.supports_executable)():
 
631
            return self._is_executable_from_path_and_stat_from_stat(path, stat_result)
 
632
        else:
 
633
            return self._is_executable_from_path_and_stat_from_basis(path, stat_result)
 
634
 
 
635
    @needs_read_lock
 
636
    def list_files(self, include_root=False, from_dir=None, recursive=True):
 
637
        # FIXME: Yield non-versioned files
 
638
        if from_dir is None:
 
639
            from_dir = ""
 
640
        dir_ids = {}
 
641
        fk_entries = {'directory': workingtree.TreeDirectory,
 
642
                      'file': workingtree.TreeFile,
 
643
                      'symlink': workingtree.TreeLink}
 
644
        root_ie = self._get_dir_ie(u"", None)
 
645
        if include_root and not from_dir:
 
646
            yield "", "V", root_ie.kind, root_ie.file_id, root_ie
 
647
        dir_ids[u""] = root_ie.file_id
 
648
        if recursive:
 
649
            path_iterator = self._iter_files_recursive(from_dir)
 
650
        else:
 
651
            if from_dir is None:
 
652
                start = self.basedir
 
653
            else:
 
654
                start = os.path.join(self.basedir, from_dir)
 
655
            path_iterator = sorted([os.path.join(from_dir, name) for name in
 
656
                os.listdir(start) if not self.bzrdir.is_control_filename(name)
 
657
                and not self.mapping.is_special_file(name)])
 
658
        for path in path_iterator:
 
659
            try:
 
660
                value = self.index[path]
 
661
            except KeyError:
 
662
                value = None
 
663
            path = path.decode("utf-8")
 
664
            parent, name = posixpath.split(path)
 
665
            for dir_path, dir_ie in self._add_missing_parent_ids(parent, dir_ids):
 
666
                yield dir_path, "V", dir_ie.kind, dir_ie.file_id, dir_ie
 
667
            if value is not None:
 
668
                ie = self._get_file_ie(name, path, value, dir_ids[parent])
 
669
                yield path, "V", ie.kind, ie.file_id, ie
 
670
            else:
 
671
                kind = osutils.file_kind(self.abspath(path))
 
672
                ie = fk_entries[kind]()
 
673
                yield path, "?", kind, None, ie
 
674
 
 
675
    @needs_read_lock
 
676
    def all_file_ids(self):
 
677
        ids = {u"": self.path2id("")}
 
678
        for path in self.index:
 
679
            if self.mapping.is_special_file(path):
 
680
                continue
 
681
            path = path.decode("utf-8")
 
682
            parent = posixpath.dirname(path).strip("/")
 
683
            for e in self._add_missing_parent_ids(parent, ids):
 
684
                pass
 
685
            ids[path] = self.path2id(path)
 
686
        return set(ids.values())
 
687
 
 
688
    def _directory_is_tree_reference(self, path):
 
689
        # FIXME: Check .gitsubmodules for path
 
690
        return False
 
691
 
 
692
    @needs_read_lock
 
693
    def iter_entries_by_dir(self, specific_file_ids=None, yield_parents=False):
 
694
        # FIXME: Is return order correct?
 
695
        if yield_parents:
 
696
            raise NotImplementedError(self.iter_entries_by_dir)
 
697
        if specific_file_ids is not None:
 
698
            specific_paths = [self.id2path(file_id) for file_id in specific_file_ids]
 
699
            if specific_paths in ([u""], []):
 
700
                specific_paths = None
 
701
            else:
 
702
                specific_paths = set(specific_paths)
 
703
        else:
 
704
            specific_paths = None
 
705
        root_ie = self._get_dir_ie(u"", None)
 
706
        if specific_paths is None:
 
707
            yield u"", root_ie
 
708
        dir_ids = {u"": root_ie.file_id}
 
709
        for path, value in self.index.iteritems():
 
710
            if self.mapping.is_special_file(path):
 
711
                continue
 
712
            path = path.decode("utf-8")
 
713
            if specific_paths is not None and not path in specific_paths:
 
714
                continue
 
715
            (parent, name) = posixpath.split(path)
 
716
            try:
 
717
                file_ie = self._get_file_ie(name, path, value, None)
 
718
            except IOError:
 
719
                continue
 
720
            for (dir_path, dir_ie) in self._add_missing_parent_ids(parent,
 
721
                    dir_ids):
 
722
                yield dir_path, dir_ie
 
723
            file_ie.parent_id = self.path2id(parent)
 
724
            yield path, file_ie
 
725
 
208
726
    @needs_read_lock
209
727
    def conflicts(self):
210
728
        # FIXME:
211
 
        return []
 
729
        return _mod_conflicts.ConflictList()
 
730
 
 
731
    def update_basis_by_delta(self, new_revid, delta):
 
732
        # The index just contains content, which won't have changed.
 
733
        self._reset_data()
 
734
 
 
735
    @needs_read_lock
 
736
    def get_canonical_inventory_path(self, path):
 
737
        for p in self.index:
 
738
            if p.lower() == path.lower():
 
739
                return p
 
740
        else:
 
741
            return path
 
742
 
 
743
    @needs_read_lock
 
744
    def _walkdirs(self, prefix=""):
 
745
        if prefix != "":
 
746
            prefix += "/"
 
747
        per_dir = defaultdict(list)
 
748
        for path, value in self.index.iteritems():
 
749
            if self.mapping.is_special_file(path):
 
750
                continue
 
751
            if not path.startswith(prefix):
 
752
                continue
 
753
            (dirname, child_name) = posixpath.split(path)
 
754
            dirname = dirname.decode("utf-8")
 
755
            dir_file_id = self.path2id(dirname)
 
756
            assert isinstance(value, tuple) and len(value) == 10
 
757
            (ctime, mtime, dev, ino, mode, uid, gid, size, sha, flags) = value
 
758
            stat_result = os.stat_result((mode, ino,
 
759
                    dev, 1, uid, gid, size,
 
760
                    0, mtime, ctime))
 
761
            per_dir[(dirname, dir_file_id)].append(
 
762
                (path.decode("utf-8"), child_name.decode("utf-8"),
 
763
                mode_kind(mode), stat_result,
 
764
                self.path2id(path.decode("utf-8")),
 
765
                mode_kind(mode)))
 
766
        return per_dir.iteritems()
 
767
 
 
768
    def _lookup_entry(self, path, update_index=False):
 
769
        assert type(path) == str
 
770
        entry = self.index[path]
 
771
        index_mode = entry[-6]
 
772
        index_sha = entry[-2]
 
773
        disk_path = os.path.join(self.basedir, path)
 
774
        disk_stat = os.lstat(disk_path)
 
775
        disk_mtime = disk_stat.st_mtime
 
776
        if isinstance(entry[1], tuple):
 
777
            index_mtime = entry[1][0]
 
778
        else:
 
779
            index_mtime = int(entry[1])
 
780
        mtime_delta = (disk_mtime - index_mtime)
 
781
        disk_mode = cleanup_mode(disk_stat.st_mode)
 
782
        if mtime_delta > 0 or disk_mode != index_mode:
 
783
            if stat.S_ISDIR(disk_mode):
 
784
                try:
 
785
                    subrepo = Repo(disk_path)
 
786
                except NotGitRepository:
 
787
                    return (None, None)
 
788
                else:
 
789
                    disk_mode = S_IFGITLINK
 
790
                    git_id = subrepo.head()
 
791
            elif stat.S_ISLNK(disk_mode):
 
792
                blob = Blob.from_string(os.readlink(disk_path).encode('utf-8'))
 
793
                git_id = blob.id
 
794
            elif stat.S_ISREG(disk_mode):
 
795
                with open(disk_path, 'r') as f:
 
796
                    blob = Blob.from_string(f.read())
 
797
                git_id = blob.id
 
798
            else:
 
799
                raise AssertionError
 
800
            if update_index:
 
801
                flags = 0 # FIXME
 
802
                self.index[path] = index_entry_from_stat(disk_stat, git_id, flags, disk_mode)
 
803
            return (git_id, disk_mode)
 
804
        return (index_sha, index_mode)
212
805
 
213
806
 
214
807
class GitWorkingTreeFormat(workingtree.WorkingTreeFormat):
215
808
 
 
809
    _tree_class = GitWorkingTree
 
810
 
 
811
    supports_versioned_directories = False
 
812
 
216
813
    @property
217
814
    def _matchingbzrdir(self):
218
 
        from bzrlib.plugins.git import LocalGitControlDirFormat
 
815
        from bzrlib.plugins.git.dir import LocalGitControlDirFormat
219
816
        return LocalGitControlDirFormat()
220
817
 
221
818
    def get_format_description(self):
222
819
        return "Git Working Tree"
223
820
 
 
821
    def initialize(self, a_bzrdir, revision_id=None, from_branch=None,
 
822
                   accelerator_tree=None, hardlink=False):
 
823
        """See WorkingTreeFormat.initialize()."""
 
824
        if not isinstance(a_bzrdir, LocalGitDir):
 
825
            raise errors.IncompatibleFormat(self, a_bzrdir)
 
826
        index = Index(a_bzrdir.root_transport.local_abspath(".git/index"))
 
827
        index.write()
 
828
        return GitWorkingTree(a_bzrdir, a_bzrdir.open_repository(),
 
829
            a_bzrdir.open_branch(), index)
 
830
 
224
831
 
225
832
class InterIndexGitTree(tree.InterTree):
226
833
    """InterTree that works between a Git revision tree and an index."""
235
842
        return (isinstance(source, GitRevisionTree) and 
236
843
                isinstance(target, GitWorkingTree))
237
844
 
 
845
    @needs_read_lock
238
846
    def compare(self, want_unchanged=False, specific_files=None,
239
847
                extra_trees=None, require_versioned=False, include_root=False,
240
848
                want_unversioned=False):
241
 
        changes = self._index.changes_from_tree(
242
 
            self.source._repository._git.object_store, self.source.tree, 
243
 
            want_unchanged=want_unchanged)
244
 
        source_fileid_map = self.source.mapping.get_fileid_map(
245
 
            self.source._repository._git.object_store.__getitem__,
246
 
            self.source.tree)
247
 
        if self.target.mapping.BZR_FILE_IDS_FILE is not None:
248
 
            file_id = self.target.path2id(
249
 
                self.target.mapping.BZR_FILE_IDS_FILE)
250
 
            if file_id is None:
251
 
                target_fileid_map = {}
252
 
            else:
253
 
                target_fileid_map = self.target.mapping.import_fileid_map(Blob.from_string(self.target.get_file_text(file_id)))
254
 
        else:
255
 
            target_fileid_map = {}
256
 
        target_fileid_map = GitFileIdMap(target_fileid_map, self.target.mapping)
 
849
        # FIXME: Handle include_root
 
850
        changes = changes_between_git_tree_and_index(
 
851
            self.source.store, self.source.tree, 
 
852
            self.target, want_unchanged=want_unchanged,
 
853
            want_unversioned=want_unversioned)
 
854
        source_fileid_map = self.source._fileid_map
 
855
        target_fileid_map = self.target._fileid_map
257
856
        ret = tree_delta_from_git_changes(changes, self.target.mapping,
258
857
            (source_fileid_map, target_fileid_map),
259
858
            specific_file=specific_files, require_versioned=require_versioned)
260
859
        if want_unversioned:
261
860
            for e in self.target.extras():
262
 
                ret.unversioned.append((e, None, osutils.file_kind(self.target.abspath(e))))
 
861
                ret.unversioned.append((e, None,
 
862
                    osutils.file_kind(self.target.abspath(e))))
263
863
        return ret
264
864
 
 
865
    @needs_read_lock
265
866
    def iter_changes(self, include_unchanged=False, specific_files=None,
266
 
        pb=None, extra_trees=[], require_versioned=True, want_unversioned=False):
267
 
        changes = self._index.changes_from_tree(
268
 
            self.source._repository._git.object_store, self.source.tree, 
269
 
            want_unchanged=include_unchanged)
270
 
        # FIXME: Handle want_unversioned
271
 
        return changes_from_git_changes(changes, self.target.mapping, 
 
867
        pb=None, extra_trees=[], require_versioned=True,
 
868
        want_unversioned=False):
 
869
        changes = changes_between_git_tree_and_index(
 
870
            self.source.store, self.source.tree,
 
871
            self.target, want_unchanged=include_unchanged,
 
872
            want_unversioned=want_unversioned)
 
873
        return changes_from_git_changes(changes, self.target.mapping,
272
874
            specific_file=specific_files)
273
875
 
 
876
 
274
877
tree.InterTree.register_optimiser(InterIndexGitTree)
 
878
 
 
879
 
 
880
def changes_between_git_tree_and_index(object_store, tree, target,
 
881
        want_unchanged=False, want_unversioned=False, update_index=False):
 
882
    """Determine the changes between a git tree and a working tree with index.
 
883
 
 
884
    """
 
885
 
 
886
    names = target.index._byname.keys()
 
887
    for (name, mode, sha) in changes_from_tree(names, target._lookup_entry,
 
888
            object_store, tree, want_unchanged=want_unchanged):
 
889
        if name == (None, None):
 
890
            continue
 
891
        yield (name, mode, sha)