/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.200.202 by Jelmer Vernooij
Add git: revision specifier.
1
# Copyright (C) 2009 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
0.200.1635 by Jelmer Vernooij
Change revspec.py license back to GPLv2+.
5
# the Free Software Foundation; either version 2 of the License, or
0.200.202 by Jelmer Vernooij
Add git: revision specifier.
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
"""Custom revision specifier for Subversion."""
17
0.200.1594 by Jelmer Vernooij
Use absolute_import everywhere.
18
from __future__ import absolute_import
19
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
20
# Please note that imports are delayed as much as possible here since
0.200.645 by Jelmer Vernooij
Support DWIM git: revspecs.
21
# if DWIM revspecs are supported this module is imported by __init__.py.
22
0.200.1646 by Jelmer Vernooij
Rename bzrlib to breezy.
23
from ... import version_info as breezy_version
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
24
from ...errors import (
0.200.322 by Jelmer Vernooij
Support short git revision specifiers.
25
    InvalidRevisionId,
0.200.292 by Jelmer Vernooij
Fix formatting.
26
    InvalidRevisionSpec,
27
    )
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
28
from ...revision import (
0.200.979 by Jelmer Vernooij
Special-case NULL_REVISION when looking for Git shas.
29
    NULL_REVISION,
30
)
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
31
from ...revisionspec import (
0.200.292 by Jelmer Vernooij
Fix formatting.
32
    RevisionInfo,
33
    RevisionSpec,
34
    )
0.200.202 by Jelmer Vernooij
Add git: revision specifier.
35
0.200.645 by Jelmer Vernooij
Support DWIM git: revspecs.
36
37
def valid_git_sha1(hex):
0.200.813 by Jelmer Vernooij
Add tests for revspec.
38
    """Check if `hex` is a validly formatted Git SHA1.
0.200.1636 by Jelmer Vernooij
Some formatting fixes.
39
0.200.813 by Jelmer Vernooij
Add tests for revspec.
40
    :param hex: Hex string to validate
41
    :return: Boolean
42
    """
0.200.645 by Jelmer Vernooij
Support DWIM git: revspecs.
43
    import binascii
44
    try:
45
        binascii.unhexlify(hex)
46
    except TypeError:
47
        return False
48
    else:
49
        return True
50
0.200.202 by Jelmer Vernooij
Add git: revision specifier.
51
52
class RevisionSpec_git(RevisionSpec):
53
    """Selects a revision using a Subversion revision number."""
54
55
    help_txt = """Selects a revision using a Git revision sha1.
56
    """
0.200.695 by Jelmer Vernooij
Clean up trailing whitespace.
57
0.200.202 by Jelmer Vernooij
Add git: revision specifier.
58
    prefix = 'git:'
0.200.415 by Jelmer Vernooij
make 'bzr pull --revision' work for remote repositories.
59
    wants_revision_history = False
0.200.202 by Jelmer Vernooij
Add git: revision specifier.
60
0.200.322 by Jelmer Vernooij
Support short git revision specifiers.
61
    def _lookup_git_sha1(self, branch, sha1):
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
62
        from .errors import (
0.200.645 by Jelmer Vernooij
Support DWIM git: revspecs.
63
            GitSmartRemoteNotSupported,
64
            )
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
65
        from .mapping import (
0.200.716 by Jelmer Vernooij
Fix use in non-git branches.
66
            default_mapping,
67
            )
0.200.645 by Jelmer Vernooij
Support DWIM git: revspecs.
68
0.200.716 by Jelmer Vernooij
Fix use in non-git branches.
69
        bzr_revid = getattr(branch.repository, "lookup_foreign_revision_id",
70
                              default_mapping.revision_id_foreign_to_bzr)(sha1)
0.200.415 by Jelmer Vernooij
make 'bzr pull --revision' work for remote repositories.
71
        try:
72
            if branch.repository.has_revision(bzr_revid):
0.200.1601 by Jelmer Vernooij
remove compatibility code for bzr < 2.5.
73
                return RevisionInfo.from_revision_id(branch, bzr_revid)
0.200.415 by Jelmer Vernooij
make 'bzr pull --revision' work for remote repositories.
74
        except GitSmartRemoteNotSupported:
75
            return RevisionInfo(branch, None, bzr_revid)
0.200.322 by Jelmer Vernooij
Support short git revision specifiers.
76
        raise InvalidRevisionSpec(self.user_spec, branch)
77
0.200.418 by Jelmer Vernooij
Override GitRevisionSpec.__nonzero__ as the default implementation uses has_revision(), which is not available on remote git repositories.
78
    def __nonzero__(self):
79
        # The default implementation uses branch.repository.has_revision()
80
        if self.rev_id is None:
81
            return False
82
        if self.rev_id == NULL_REVISION:
83
            return False
84
        return True
85
0.200.322 by Jelmer Vernooij
Support short git revision specifiers.
86
    def _find_short_git_sha1(self, branch, sha1):
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
87
        from .mapping import (
0.200.908 by Jelmer Vernooij
Avoid interpreting bzr-svn revisions in git revspecs.
88
            ForeignGit,
0.200.645 by Jelmer Vernooij
Support DWIM git: revspecs.
89
            mapping_registry,
90
            )
0.200.650 by Jelmer Vernooij
Use standard names for lookup functions.
91
        parse_revid = getattr(branch.repository, "lookup_bzr_revision_id",
0.200.322 by Jelmer Vernooij
Support short git revision specifiers.
92
                              mapping_registry.parse_revision_id)
93
        branch.repository.lock_read()
94
        try:
95
            graph = branch.repository.get_graph()
96
            for revid, _ in graph.iter_ancestry([branch.last_revision()]):
0.200.979 by Jelmer Vernooij
Special-case NULL_REVISION when looking for Git shas.
97
                if revid == NULL_REVISION:
98
                    continue
0.200.322 by Jelmer Vernooij
Support short git revision specifiers.
99
                try:
100
                    foreign_revid, mapping = parse_revid(revid)
101
                except InvalidRevisionId:
102
                    continue
0.200.908 by Jelmer Vernooij
Avoid interpreting bzr-svn revisions in git revspecs.
103
                if not isinstance(mapping.vcs, ForeignGit):
104
                    continue
0.200.322 by Jelmer Vernooij
Support short git revision specifiers.
105
                if foreign_revid.startswith(sha1):
0.200.1601 by Jelmer Vernooij
remove compatibility code for bzr < 2.5.
106
                    return RevisionInfo.from_revision_id(branch, revid)
0.200.322 by Jelmer Vernooij
Support short git revision specifiers.
107
            raise InvalidRevisionSpec(self.user_spec, branch)
108
        finally:
109
            branch.repository.unlock()
110
0.200.202 by Jelmer Vernooij
Add git: revision specifier.
111
    def _match_on(self, branch, revs):
112
        loc = self.spec.find(':')
113
        git_sha1 = self.spec[loc+1:].encode("utf-8")
0.200.645 by Jelmer Vernooij
Support DWIM git: revspecs.
114
        if len(git_sha1) > 40 or not valid_git_sha1(git_sha1):
115
            raise InvalidRevisionSpec(self.user_spec, branch)
0.200.1641 by Jelmer Vernooij
Use relative imports where possible.
116
        from . import (
0.200.645 by Jelmer Vernooij
Support DWIM git: revspecs.
117
            lazy_check_versions,
118
            )
119
        lazy_check_versions()
0.200.322 by Jelmer Vernooij
Support short git revision specifiers.
120
        if len(git_sha1) == 40:
121
            return self._lookup_git_sha1(branch, git_sha1)
122
        else:
123
            return self._find_short_git_sha1(branch, git_sha1)
0.200.202 by Jelmer Vernooij
Add git: revision specifier.
124
125
    def needs_branch(self):
126
        return True
127
128
    def get_branch(self):
129
        return None