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

Support DWIM git: revspecs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
"""Custom revision specifier for Subversion."""
17
17
 
 
18
# Please note that imports are delayed as much as possible here since 
 
19
# if DWIM revspecs are supported this module is imported by __init__.py.
 
20
 
18
21
from bzrlib.errors import (
19
22
    InvalidRevisionId,
20
23
    InvalidRevisionSpec,
21
24
    )
22
 
from bzrlib.revision import (
23
 
    NULL_REVISION,
24
 
    )
25
25
from bzrlib.revisionspec import (
26
26
    RevisionInfo,
27
27
    RevisionSpec,
28
28
    )
29
29
 
30
 
from bzrlib.plugins.git import (
31
 
    lazy_check_versions,
32
 
    )
33
 
from bzrlib.plugins.git.errors import (
34
 
    GitSmartRemoteNotSupported,
35
 
    )
36
 
from bzrlib.plugins.git.mapping import (
37
 
    mapping_registry,
38
 
    )
 
30
 
 
31
def valid_git_sha1(hex):
 
32
    import binascii
 
33
    try:
 
34
        binascii.unhexlify(hex)
 
35
    except TypeError:
 
36
        return False
 
37
    else:
 
38
        return True
 
39
 
39
40
 
40
41
class RevisionSpec_git(RevisionSpec):
41
42
    """Selects a revision using a Subversion revision number."""
47
48
    wants_revision_history = False
48
49
 
49
50
    def _lookup_git_sha1(self, branch, sha1):
 
51
        from bzrlib.plugins.git.errors import (
 
52
            GitSmartRemoteNotSupported,
 
53
            )
 
54
 
50
55
        bzr_revid = branch.mapping.revision_id_foreign_to_bzr(sha1)
51
56
        try:
52
57
            if branch.repository.has_revision(bzr_revid):
62
67
        return history
63
68
 
64
69
    def __nonzero__(self):
 
70
        from bzrlib.revision import (
 
71
            NULL_REVISION,
 
72
            )
65
73
        # The default implementation uses branch.repository.has_revision()
66
74
        if self.rev_id is None:
67
75
            return False
70
78
        return True
71
79
 
72
80
    def _find_short_git_sha1(self, branch, sha1):
 
81
        from bzrlib.plugins.git.mapping import (
 
82
            mapping_registry,
 
83
            )
73
84
        parse_revid = getattr(branch.repository, "lookup_git_revid",
74
85
                              mapping_registry.parse_revision_id)
75
86
        branch.repository.lock_read()
88
99
            branch.repository.unlock()
89
100
 
90
101
    def _match_on(self, branch, revs):
91
 
        lazy_check_versions()
92
102
        loc = self.spec.find(':')
93
103
        git_sha1 = self.spec[loc+1:].encode("utf-8")
 
104
        if len(git_sha1) > 40 or not valid_git_sha1(git_sha1):
 
105
            raise InvalidRevisionSpec(self.user_spec, branch)
 
106
        from bzrlib.plugins.git import (
 
107
            lazy_check_versions,
 
108
            )
 
109
        lazy_check_versions()
94
110
        if len(git_sha1) == 40:
95
111
            return self._lookup_git_sha1(branch, git_sha1)
96
 
        elif len(git_sha1) > 40:
97
 
            raise InvalidRevisionSpec(self.user_spec, branch)
98
112
        else:
99
113
            return self._find_short_git_sha1(branch, git_sha1)
100
114