/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: Andrew Bennetts
  • Date: 2009-10-21 11:13:40 UTC
  • mto: This revision was merged to the branch mainline in revision 4762.
  • Revision ID: andrew.bennetts@canonical.com-20091021111340-w7x4d5yf83qwjncc
Add test that WSGI glue allows request handlers to access paths above that request's. backing transport, so long as it is within the WSGI app's backing transport.

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
 
    ignores,
36
 
    inventory,
37
 
    lockable_files,
38
 
    lockdir,
39
 
    osutils,
40
 
    transport,
41
 
    urlutils,
42
 
    workingtree,
43
 
    )
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
 
 
57
 
 
58
 
class GitWorkingTree(workingtree.WorkingTree):
59
 
    """A Git working tree."""
60
 
 
61
 
    def __init__(self, bzrdir, repo, branch):
62
 
        self.basedir = bzrdir.root_transport.local_abspath('.')
63
 
        self.bzrdir = bzrdir
64
 
        self.repository = repo
65
 
        self.mapping = self.repository.get_mapping()
66
 
        self._branch = branch
67
 
        self._transport = bzrdir.transport
68
 
 
69
 
        self.controldir = urlutils.join(self.repository._git._controldir, 'bzr')
70
 
 
71
 
        try:
72
 
            os.makedirs(self.controldir)
73
 
            os.makedirs(os.path.join(self.controldir, 'lock'))
74
 
        except OSError:
75
 
            pass
76
 
 
77
 
        self._control_files = lockable_files.LockableFiles(
78
 
            transport.get_transport(self.controldir), 'lock', lockdir.LockDir)
79
 
        self._format = GitWorkingTreeFormat()
80
 
        self.index = self.repository._git.open_index()
81
 
        self.views = self._make_views()
82
 
        self._detect_case_handling()
83
 
 
84
 
    def unlock(self):
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()
93
 
 
94
 
    def is_control_filename(self, path):
95
 
        return os.path.basename(path) == ".git"
96
 
 
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 get_ignore_list(self):
133
 
        ignoreset = getattr(self, '_ignoreset', None)
134
 
        if ignoreset is not None:
135
 
            return ignoreset
136
 
 
137
 
        ignore_globs = set()
138
 
        ignore_globs.update(ignores.get_runtime_ignores())
139
 
        ignore_globs.update(ignores.get_user_ignores())
140
 
        if self.has_filename(IGNORE_FILENAME):
141
 
            f = self.get_file_byname(IGNORE_FILENAME)
142
 
            try:
143
 
                ignore_globs.update(ignores.parse_ignore_file(f))
144
 
            finally:
145
 
                f.close()
146
 
        self._ignoreset = ignore_globs
147
 
        return ignore_globs
148
 
 
149
 
    def _reset_data(self):
150
 
        self._inventory_is_modified = False
151
 
        basis_inv = self.repository.get_inventory(self.mapping.revision_id_foreign_to_bzr(self.repository._git.head()))
152
 
        result = GitIndexInventory(basis_inv, self.mapping, self.index)
153
 
        self._set_inventory(result, dirty=False)
154
 
 
155
 
    @needs_read_lock
156
 
    def get_file_sha1(self, file_id, path=None, stat_value=None):
157
 
        if not path:
158
 
            path = self._inventory.id2path(file_id)
159
 
        return osutils.sha_file_by_name(self.abspath(path).encode(osutils._fs_enc))
160
 
 
161
 
 
162
 
class GitWorkingTreeFormat(workingtree.WorkingTreeFormat):
163
 
 
164
 
    def get_format_description(self):
165
 
        return "Git Working Tree"