/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

  • Committer: Invité
  • Date: 2009-05-05 20:43:26 UTC
  • mto: (0.200.441 trunk)
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: guest@mirexpress-20090505204326-n0vcprylu2hyzq4v
Ensure git plugin is loaded in bzr-*-pack

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
 
 
21
18
from bzrlib.errors import (
 
19
    BzrError,
22
20
    InvalidRevisionId,
23
21
    InvalidRevisionSpec,
 
22
    NoSuchRevision,
24
23
    )
25
24
from bzrlib.revision import (
26
25
    NULL_REVISION,
27
 
)
 
26
    )
28
27
from bzrlib.revisionspec import (
29
28
    RevisionInfo,
30
29
    RevisionSpec,
31
30
    )
32
31
 
33
 
 
34
 
def valid_git_sha1(hex):
35
 
    """Check if `hex` is a validly formatted Git SHA1.
36
 
    
37
 
    :param hex: Hex string to validate
38
 
    :return: Boolean
39
 
    """
40
 
    import binascii
41
 
    try:
42
 
        binascii.unhexlify(hex)
43
 
    except TypeError:
44
 
        return False
45
 
    else:
46
 
        return True
47
 
 
 
32
from bzrlib.plugins.git import (
 
33
    lazy_check_versions,
 
34
    )
 
35
from bzrlib.plugins.git.errors import (
 
36
    GitSmartRemoteNotSupported,
 
37
    )
 
38
from bzrlib.plugins.git.mapping import (
 
39
    mapping_registry,
 
40
    )
48
41
 
49
42
class RevisionSpec_git(RevisionSpec):
50
43
    """Selects a revision using a Subversion revision number."""
51
44
 
52
45
    help_txt = """Selects a revision using a Git revision sha1.
53
46
    """
54
 
 
 
47
    
55
48
    prefix = 'git:'
56
49
    wants_revision_history = False
57
50
 
58
51
    def _lookup_git_sha1(self, branch, sha1):
59
 
        from bzrlib.plugins.git.errors import (
60
 
            GitSmartRemoteNotSupported,
61
 
            )
62
 
        from bzrlib.plugins.git.mapping import (
63
 
            default_mapping,
64
 
            )
65
 
 
66
 
        bzr_revid = getattr(branch.repository, "lookup_foreign_revision_id",
67
 
                              default_mapping.revision_id_foreign_to_bzr)(sha1)
 
52
        bzr_revid = branch.mapping.revision_id_foreign_to_bzr(sha1)
68
53
        try:
69
54
            if branch.repository.has_revision(bzr_revid):
70
55
                history = self._history(branch, bzr_revid)
74
59
        raise InvalidRevisionSpec(self.user_spec, branch)
75
60
 
76
61
    def _history(self, branch, revid):
77
 
        branch.lock_read()
78
 
        try:
79
 
            history = list(branch.repository.iter_reverse_revision_history(
80
 
                revid))
81
 
        finally:
82
 
            branch.unlock()
 
62
        history = list(branch.repository.iter_reverse_revision_history(revid))
83
63
        history.reverse()
84
64
        return history
85
65
 
92
72
        return True
93
73
 
94
74
    def _find_short_git_sha1(self, branch, sha1):
95
 
        from bzrlib.plugins.git.mapping import (
96
 
            ForeignGit,
97
 
            mapping_registry,
98
 
            )
99
 
        parse_revid = getattr(branch.repository, "lookup_bzr_revision_id",
 
75
        parse_revid = getattr(branch.repository, "lookup_git_revid",
100
76
                              mapping_registry.parse_revision_id)
101
77
        branch.repository.lock_read()
102
78
        try:
103
79
            graph = branch.repository.get_graph()
104
80
            for revid, _ in graph.iter_ancestry([branch.last_revision()]):
105
 
                if revid == NULL_REVISION:
106
 
                    continue
107
81
                try:
108
82
                    foreign_revid, mapping = parse_revid(revid)
109
83
                except InvalidRevisionId:
110
84
                    continue
111
 
                if not isinstance(mapping.vcs, ForeignGit):
112
 
                    continue
113
85
                if foreign_revid.startswith(sha1):
114
86
                    history = self._history(branch, revid)
115
87
                    return RevisionInfo.from_revision_id(branch, revid, history)
118
90
            branch.repository.unlock()
119
91
 
120
92
    def _match_on(self, branch, revs):
 
93
        lazy_check_versions()
121
94
        loc = self.spec.find(':')
122
95
        git_sha1 = self.spec[loc+1:].encode("utf-8")
123
 
        if len(git_sha1) > 40 or not valid_git_sha1(git_sha1):
124
 
            raise InvalidRevisionSpec(self.user_spec, branch)
125
 
        from bzrlib.plugins.git import (
126
 
            lazy_check_versions,
127
 
            )
128
 
        lazy_check_versions()
129
96
        if len(git_sha1) == 40:
130
97
            return self._lookup_git_sha1(branch, git_sha1)
 
98
        elif len(git_sha1) > 40:
 
99
            raise InvalidRevisionSpec(self.user_spec, branch)
131
100
        else:
132
101
            return self._find_short_git_sha1(branch, git_sha1)
133
102