/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 dir.py

Don't fetch revision already present.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007 Canonical Ltd
 
2
# Copyright (C) 2010 Jelmer Vernooij
 
3
#
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
#
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program; if not, write to the Free Software
 
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
 
 
18
"""An adapter between a Git control dir and a Bazaar ControlDir."""
 
19
 
 
20
import urllib
 
21
 
 
22
from bzrlib import (
 
23
    errors as bzr_errors,
 
24
    lockable_files,
 
25
    trace,
 
26
    osutils,
 
27
    urlutils,
 
28
    )
 
29
from bzrlib.bzrdir import CreateRepository
 
30
from bzrlib.transport import do_catching_redirections
 
31
 
 
32
LockWarner = getattr(lockable_files, "_LockWarner", None)
 
33
 
 
34
from bzrlib.controldir import (
 
35
    ControlDir,
 
36
    ControlDirFormat,
 
37
    format_registry,
 
38
    )
 
39
 
 
40
 
 
41
class GitLock(object):
 
42
    """A lock that thunks through to Git."""
 
43
 
 
44
    def __init__(self):
 
45
        self.lock_name = "git lock"
 
46
 
 
47
    def lock_write(self, token=None):
 
48
        pass
 
49
 
 
50
    def lock_read(self):
 
51
        pass
 
52
 
 
53
    def unlock(self):
 
54
        pass
 
55
 
 
56
    def peek(self):
 
57
        pass
 
58
 
 
59
    def validate_token(self, token):
 
60
        pass
 
61
 
 
62
    def break_lock(self):
 
63
        raise NotImplementedError(self.break_lock)
 
64
 
 
65
    def dont_leave_in_place(self):
 
66
        raise NotImplementedError(self.dont_leave_in_place)
 
67
 
 
68
    def leave_in_place(self):
 
69
        raise NotImplementedError(self.leave_in_place)
 
70
 
 
71
 
 
72
class GitLockableFiles(lockable_files.LockableFiles):
 
73
    """Git specific lockable files abstraction."""
 
74
 
 
75
    def __init__(self, transport, lock):
 
76
        self._lock = lock
 
77
        self._transaction = None
 
78
        self._lock_mode = None
 
79
        self._transport = transport
 
80
        if LockWarner is None:
 
81
            # Bzr 1.13
 
82
            self._lock_count = 0
 
83
        else:
 
84
            self._lock_warner = LockWarner(repr(self))
 
85
 
 
86
 
 
87
class GitDirConfig(object):
 
88
 
 
89
    def get_default_stack_on(self):
 
90
        return None
 
91
 
 
92
    def set_default_stack_on(self, value):
 
93
        raise bzr_errors.BzrError("Cannot set configuration")
 
94
 
 
95
 
 
96
class GitControlDirFormat(ControlDirFormat):
 
97
 
 
98
    _lock_class = lockable_files.TransportLock
 
99
 
 
100
    colocated_branches = True
 
101
    fixed_components = True
 
102
 
 
103
    def __eq__(self, other):
 
104
        return type(self) == type(other)
 
105
 
 
106
    def is_supported(self):
 
107
        return True
 
108
 
 
109
    def network_name(self):
 
110
        return "git"
 
111
 
 
112
 
 
113
class GitDir(ControlDir):
 
114
    """An adapter to the '.git' dir used by git."""
 
115
 
 
116
    def is_supported(self):
 
117
        return True
 
118
 
 
119
    def can_convert_format(self):
 
120
        return False
 
121
 
 
122
    def break_lock(self):
 
123
        pass
 
124
 
 
125
    def cloning_metadir(self, stacked=False):
 
126
        return format_registry.make_bzrdir("default")
 
127
 
 
128
    def checkout_metadir(self, stacked=False):
 
129
        return format_registry.make_bzrdir("default")
 
130
 
 
131
    def _get_selected_ref(self, branch):
 
132
        if branch is None and getattr(self, "_get_selected_branch", False):
 
133
            branch = self._get_selected_branch()
 
134
        if branch is not None:
 
135
            from bzrlib.plugins.git.refs import branch_name_to_ref
 
136
            return branch_name_to_ref(branch, None)
 
137
        segment_parameters = getattr(
 
138
            self.user_transport, "get_segment_parameters", lambda: {})()
 
139
        ref = segment_parameters.get("ref")
 
140
        if ref is not None:
 
141
            ref = urlutils.unescape(ref)
 
142
        return ref
 
143
 
 
144
    def get_config(self):
 
145
        return GitDirConfig()
 
146
 
 
147
    def _available_backup_name(self, base):
 
148
        return osutils.available_backup_name(base, self.root_transport.has)
 
149
 
 
150
    def sprout(self, url, revision_id=None, force_new_repo=False,
 
151
               recurse='down', possible_transports=None,
 
152
               accelerator_tree=None, hardlink=False, stacked=False,
 
153
               source_branch=None, create_tree_if_local=True):
 
154
        from bzrlib.repository import InterRepository
 
155
        from bzrlib.transport.local import LocalTransport
 
156
        from bzrlib.transport import get_transport
 
157
        target_transport = get_transport(url, possible_transports)
 
158
        target_transport.ensure_base()
 
159
        cloning_format = self.cloning_metadir()
 
160
        # Create/update the result branch
 
161
        result = cloning_format.initialize_on_transport(target_transport)
 
162
        source_branch = self.open_branch()
 
163
        source_repository = self.find_repository()
 
164
        try:
 
165
            result_repo = result.find_repository()
 
166
        except bzr_errors.NoRepositoryPresent:
 
167
            result_repo = result.create_repository()
 
168
            target_is_empty = True
 
169
        else:
 
170
            target_is_empty = None # Unknown
 
171
        if stacked:
 
172
            raise bzr_errors.IncompatibleRepositories(source_repository, result_repo)
 
173
        interrepo = InterRepository.get(source_repository, result_repo)
 
174
 
 
175
        if revision_id is not None:
 
176
            determine_wants = interrepo.get_determine_wants_revids(
 
177
                [revision_id], include_tags=True)
 
178
        else:
 
179
            determine_wants = interrepo.determine_wants_all
 
180
        interrepo.fetch_objects(determine_wants=determine_wants,
 
181
            mapping=source_branch.mapping)
 
182
        result_branch = source_branch.sprout(result,
 
183
            revision_id=revision_id, repository=result_repo)
 
184
        if (create_tree_if_local
 
185
            and isinstance(target_transport, LocalTransport)
 
186
            and (result_repo is None or result_repo.make_working_trees())):
 
187
            wt = result.create_workingtree(accelerator_tree=accelerator_tree,
 
188
                hardlink=hardlink, from_branch=result_branch)
 
189
            wt.lock_write()
 
190
            try:
 
191
                if wt.path2id('') is None:
 
192
                    try:
 
193
                        wt.set_root_id(self.open_workingtree.get_root_id())
 
194
                    except bzr_errors.NoWorkingTree:
 
195
                        pass
 
196
            finally:
 
197
                wt.unlock()
 
198
        return result
 
199
 
 
200
    def clone_on_transport(self, transport, revision_id=None,
 
201
        force_new_repo=False, preserve_stacking=False, stacked_on=None,
 
202
        create_prefix=False, use_existing_dir=True, no_tree=False):
 
203
        """See ControlDir.clone_on_transport."""
 
204
        from bzrlib.repository import InterRepository
 
205
        from bzrlib.plugins.git.mapping import default_mapping
 
206
        if no_tree:
 
207
            format = BareLocalGitControlDirFormat()
 
208
        else:
 
209
            format = LocalGitControlDirFormat()
 
210
        (target_repo, target_controldir, stacking, repo_policy) = format.initialize_on_transport_ex(transport, use_existing_dir=use_existing_dir, create_prefix=create_prefix, force_new_repo=force_new_repo)
 
211
        target_git_repo = target_repo._git
 
212
        source_repo = self.open_repository()
 
213
        source_git_repo = source_repo._git
 
214
        interrepo = InterRepository.get(source_repo, target_repo)
 
215
        if revision_id is not None:
 
216
            determine_wants = interrepo.get_determine_wants_revids([revision_id], include_tags=True)
 
217
        else:
 
218
            determine_wants = interrepo.determine_wants_all
 
219
        (pack_hint, _, refs) = interrepo.fetch_objects(determine_wants,
 
220
            mapping=default_mapping)
 
221
        for name, val in refs.iteritems():
 
222
            target_git_repo.refs[name] = val
 
223
        lockfiles = GitLockableFiles(transport, GitLock())
 
224
        return self.__class__(transport, lockfiles, target_git_repo, format)
 
225
 
 
226
    def find_repository(self):
 
227
        """Find the repository that should be used.
 
228
 
 
229
        This does not require a branch as we use it to find the repo for
 
230
        new branches as well as to hook existing branches up to their
 
231
        repository.
 
232
        """
 
233
        return self.open_repository()
 
234
 
 
235
 
 
236
class LocalGitControlDirFormat(GitControlDirFormat):
 
237
    """The .git directory control format."""
 
238
 
 
239
    bare = False
 
240
 
 
241
    @classmethod
 
242
    def _known_formats(self):
 
243
        return set([LocalGitControlDirFormat()])
 
244
 
 
245
    @property
 
246
    def repository_format(self):
 
247
        from bzrlib.plugins.git.repository import GitRepositoryFormat
 
248
        return GitRepositoryFormat()
 
249
 
 
250
    def get_branch_format(self):
 
251
        from bzrlib.plugins.git.branch import GitBranchFormat
 
252
        return GitBranchFormat()
 
253
 
 
254
    def open(self, transport, _found=None):
 
255
        """Open this directory.
 
256
 
 
257
        """
 
258
        from bzrlib.plugins.git.transportgit import TransportRepo
 
259
        gitrepo = TransportRepo(transport, self.bare)
 
260
        lockfiles = GitLockableFiles(transport, GitLock())
 
261
        return LocalGitDir(transport, lockfiles, gitrepo, self)
 
262
 
 
263
    def get_format_description(self):
 
264
        return "Local Git Repository"
 
265
 
 
266
    def initialize_on_transport(self, transport):
 
267
        from bzrlib.plugins.git.transportgit import TransportRepo
 
268
        TransportRepo.init(transport, bare=self.bare)
 
269
        return self.open(transport)
 
270
 
 
271
    def initialize_on_transport_ex(self, transport, use_existing_dir=False,
 
272
        create_prefix=False, force_new_repo=False, stacked_on=None,
 
273
        stack_on_pwd=None, repo_format_name=None, make_working_trees=None,
 
274
        shared_repo=False, vfs_only=False):
 
275
        def make_directory(transport):
 
276
            transport.mkdir('.')
 
277
            return transport
 
278
        def redirected(transport, e, redirection_notice):
 
279
            trace.note(redirection_notice)
 
280
            return transport._redirected_to(e.source, e.target)
 
281
        try:
 
282
            transport = do_catching_redirections(make_directory, transport,
 
283
                redirected)
 
284
        except bzr_errors.FileExists:
 
285
            if not use_existing_dir:
 
286
                raise
 
287
        except bzr_errors.NoSuchFile:
 
288
            if not create_prefix:
 
289
                raise
 
290
            transport.create_prefix()
 
291
        controldir = self.initialize_on_transport(transport)
 
292
        repository = controldir.open_repository()
 
293
        repository.lock_write()
 
294
        return (repository, controldir, False, CreateRepository(controldir))
 
295
 
 
296
    def is_supported(self):
 
297
        return True
 
298
 
 
299
 
 
300
class BareLocalGitControlDirFormat(LocalGitControlDirFormat):
 
301
 
 
302
    bare = True
 
303
    supports_workingtrees = False
 
304
 
 
305
    def get_format_description(self):
 
306
        return "Local Git Repository (bare)"
 
307
 
 
308
 
 
309
class LocalGitDir(GitDir):
 
310
    """An adapter to the '.git' dir used by git."""
 
311
 
 
312
    def _get_gitrepository_class(self):
 
313
        from bzrlib.plugins.git.repository import LocalGitRepository
 
314
        return LocalGitRepository
 
315
 
 
316
    def __repr__(self):
 
317
        return "<%s at %r>" % (
 
318
            self.__class__.__name__, self.root_transport.base)
 
319
 
 
320
    _gitrepository_class = property(_get_gitrepository_class)
 
321
 
 
322
    @property
 
323
    def user_transport(self):
 
324
        return self.root_transport
 
325
 
 
326
    @property
 
327
    def control_transport(self):
 
328
        return self.transport
 
329
 
 
330
    def __init__(self, transport, lockfiles, gitrepo, format):
 
331
        self._format = format
 
332
        self.root_transport = transport
 
333
        self._mode_check_done = False
 
334
        self._git = gitrepo
 
335
        if gitrepo.bare:
 
336
            self.transport = transport
 
337
        else:
 
338
            self.transport = transport.clone('.git')
 
339
        self._lockfiles = lockfiles
 
340
        self._mode_check_done = None
 
341
 
 
342
    def is_control_filename(self, filename):
 
343
        return (filename == '.git' or filename.startswith('.git/'))
 
344
 
 
345
    def _get_symref(self, ref):
 
346
        from dulwich.repo import SYMREF
 
347
        refcontents = self._git.refs.read_ref(ref)
 
348
        if refcontents is None: # no such ref
 
349
            return None
 
350
        if refcontents.startswith(SYMREF):
 
351
            return refcontents[len(SYMREF):].rstrip("\n")
 
352
        return None
 
353
 
 
354
    def set_branch_reference(self, name, target):
 
355
        ref = self._get_selected_ref(name)
 
356
        if ref is None:
 
357
            ref = "HEAD"
 
358
        if not getattr(target, "ref", None):
 
359
            raise bzr_errors.BzrError("Can only set symrefs to Git refs")
 
360
        self._git.refs.set_symbolic_ref(ref, target.ref)
 
361
 
 
362
    def get_branch_reference(self, name=None):
 
363
        ref = self._get_selected_ref(name)
 
364
        if ref is None:
 
365
            ref = "HEAD"
 
366
        target_ref = self._get_symref(ref)
 
367
        if target_ref is not None:
 
368
            return urlutils.join_segment_parameters(
 
369
                self.user_url.rstrip("/"), {"ref": urllib.quote(target_ref, '')})
 
370
        return None
 
371
 
 
372
    def find_branch_format(self, name=None):
 
373
        from bzrlib.plugins.git.branch import (
 
374
            GitBranchFormat,
 
375
            GitSymrefBranchFormat,
 
376
            )
 
377
        ref = self._get_selected_ref(name)
 
378
        if ref is None:
 
379
            ref = "HEAD"
 
380
        if self._get_symref(ref) is not None:
 
381
            return GitSymrefBranchFormat()
 
382
        else:
 
383
            return GitBranchFormat()
 
384
 
 
385
    def get_branch_transport(self, branch_format, name=None):
 
386
        if branch_format is None:
 
387
            return self.transport
 
388
        if isinstance(branch_format, LocalGitControlDirFormat):
 
389
            return self.transport
 
390
        raise bzr_errors.IncompatibleFormat(branch_format, self._format)
 
391
 
 
392
    def get_repository_transport(self, format):
 
393
        if format is None:
 
394
            return self.transport
 
395
        if isinstance(format, LocalGitControlDirFormat):
 
396
            return self.transport
 
397
        raise bzr_errors.IncompatibleFormat(format, self._format)
 
398
 
 
399
    def get_workingtree_transport(self, format):
 
400
        if format is None:
 
401
            return self.transport
 
402
        if isinstance(format, LocalGitControlDirFormat):
 
403
            return self.transport
 
404
        raise bzr_errors.IncompatibleFormat(format, self._format)
 
405
 
 
406
    def open_branch(self, name=None, unsupported=False, ignore_fallbacks=None):
 
407
        """'create' a branch for this dir."""
 
408
        repo = self.open_repository()
 
409
        from bzrlib.plugins.git.branch import LocalGitBranch
 
410
        ref = self._get_selected_ref(name)
 
411
        if ref is None:
 
412
            ref = "HEAD"
 
413
        ref, sha = self._git.refs._follow(ref)
 
414
        if not ref in self._git.refs:
 
415
            raise bzr_errors.NotBranchError(self.root_transport.base,
 
416
                    bzrdir=self)
 
417
        return LocalGitBranch(self, repo, ref, self._lockfiles)
 
418
 
 
419
    def destroy_branch(self, name=None):
 
420
        refname = self._get_selected_ref(name)
 
421
        if refname is None:
 
422
            refname = "refs/heads/master"
 
423
        try:
 
424
            del self._git.refs[refname]
 
425
        except KeyError:
 
426
            raise bzr_errors.NotBranchError(self.root_transport.base,
 
427
                    bzrdir=self)
 
428
 
 
429
    def destroy_repository(self):
 
430
        raise bzr_errors.UnsupportedOperation(self.destroy_repository, self)
 
431
 
 
432
    def destroy_workingtree(self):
 
433
        raise bzr_errors.UnsupportedOperation(self.destroy_workingtree, self)
 
434
 
 
435
    def needs_format_conversion(self, format=None):
 
436
        return not isinstance(self._format, format.__class__)
 
437
 
 
438
    def list_branches(self):
 
439
        ret = []
 
440
        for name in self._git.refs.keys():
 
441
            if name.startswith("refs/heads/"):
 
442
                ret.append(self.open_branch(name=name))
 
443
        return ret
 
444
 
 
445
    def open_repository(self):
 
446
        """'open' a repository for this dir."""
 
447
        return self._gitrepository_class(self, self._lockfiles)
 
448
 
 
449
    def open_workingtree(self, recommend_upgrade=True):
 
450
        if not self._git.bare:
 
451
            from dulwich.errors import NoIndexPresent
 
452
            repo = self.open_repository()
 
453
            try:
 
454
                index = repo._git.open_index()
 
455
            except NoIndexPresent:
 
456
                pass
 
457
            else:
 
458
                from bzrlib.plugins.git.workingtree import GitWorkingTree
 
459
                try:
 
460
                    branch = self.open_branch()
 
461
                except bzr_errors.NotBranchError:
 
462
                    pass
 
463
                else:
 
464
                    return GitWorkingTree(self, repo, branch, index)
 
465
        loc = urlutils.unescape_for_display(self.root_transport.base, 'ascii')
 
466
        raise bzr_errors.NoWorkingTree(loc)
 
467
 
 
468
    def create_repository(self, shared=False):
 
469
        from bzrlib.plugins.git.repository import GitRepositoryFormat
 
470
        if shared:
 
471
            raise bzr_errors.IncompatibleFormat(GitRepositoryFormat(), self._format)
 
472
        return self.open_repository()
 
473
 
 
474
    def create_branch(self, name=None, repository=None,
 
475
                      append_revisions_only=None):
 
476
        refname = self._get_selected_ref(name)
 
477
        from dulwich.protocol import ZERO_SHA
 
478
        # FIXME: This is a bit awkward. Perhaps we should have a
 
479
        # a separate method for changing the default branch?
 
480
        if refname is None:
 
481
            refname = "refs/heads/master"
 
482
            set_head = True
 
483
        else:
 
484
            set_head = False
 
485
 
 
486
        if refname in self._git.refs:
 
487
            raise bzr_errors.AlreadyBranchError(self.base)
 
488
        self._git.refs[refname] = ZERO_SHA
 
489
        if set_head:
 
490
            self._git.refs.set_symbolic_ref("HEAD", refname)
 
491
        branch = self.open_branch(name)
 
492
        if append_revisions_only:
 
493
            branch.set_append_revisions_only(append_revisions_only)
 
494
        return branch
 
495
 
 
496
    def backup_bzrdir(self):
 
497
        if self._git.bare:
 
498
            self.root_transport.copy_tree(".git", ".git.backup")
 
499
            return (self.root_transport.abspath(".git"),
 
500
                    self.root_transport.abspath(".git.backup"))
 
501
        else:
 
502
            raise bzr_errors.BzrError("Unable to backup bare repositories")
 
503
 
 
504
    def create_workingtree(self, revision_id=None, from_branch=None,
 
505
        accelerator_tree=None, hardlink=False):
 
506
        if self._git.bare:
 
507
            raise bzr_errors.UnsupportedOperation(self.create_workingtree, self)
 
508
        from dulwich.index import write_index
 
509
        from dulwich.pack import SHA1Writer
 
510
        f = open(self.transport.local_abspath("index"), 'w+')
 
511
        try:
 
512
            f = SHA1Writer(f)
 
513
            write_index(f, [])
 
514
        finally:
 
515
            f.close()
 
516
        return self.open_workingtree()
 
517
 
 
518
    def _find_or_create_repository(self, force_new_repo=None):
 
519
        return self.create_repository(shared=False)
 
520
 
 
521
    def _find_creation_modes(self):
 
522
        """Determine the appropriate modes for files and directories.
 
523
 
 
524
        They're always set to be consistent with the base directory,
 
525
        assuming that this transport allows setting modes.
 
526
        """
 
527
        # TODO: Do we need or want an option (maybe a config setting) to turn
 
528
        # this off or override it for particular locations? -- mbp 20080512
 
529
        if self._mode_check_done:
 
530
            return
 
531
        self._mode_check_done = True
 
532
        try:
 
533
            st = self.transport.stat('.')
 
534
        except bzr_errors.TransportNotPossible:
 
535
            self._dir_mode = None
 
536
            self._file_mode = None
 
537
        else:
 
538
            # Check the directory mode, but also make sure the created
 
539
            # directories and files are read-write for this user. This is
 
540
            # mostly a workaround for filesystems which lie about being able to
 
541
            # write to a directory (cygwin & win32)
 
542
            if (st.st_mode & 07777 == 00000):
 
543
                # FTP allows stat but does not return dir/file modes
 
544
                self._dir_mode = None
 
545
                self._file_mode = None
 
546
            else:
 
547
                self._dir_mode = (st.st_mode & 07777) | 00700
 
548
                # Remove the sticky and execute bits for files
 
549
                self._file_mode = self._dir_mode & ~07111
 
550
 
 
551
    def _get_file_mode(self):
 
552
        """Return Unix mode for newly created files, or None.
 
553
        """
 
554
        if not self._mode_check_done:
 
555
            self._find_creation_modes()
 
556
        return self._file_mode
 
557
 
 
558
    def _get_dir_mode(self):
 
559
        """Return Unix mode for newly created directories, or None.
 
560
        """
 
561
        if not self._mode_check_done:
 
562
            self._find_creation_modes()
 
563
        return self._dir_mode
 
564