Statistics
| Revision:

gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / resources-plugin / scripting / lib / dulwich / objectspec.py @ 959

History | View | Annotate | Download (4.96 KB)

1
# objectspec.py -- Object specification
2
# Copyright (C) 2014 Jelmer Vernooij <jelmer@samba.org>
3
#
4
# Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
5
# General Public License as public by the Free Software Foundation; version 2.0
6
# or (at your option) any later version. You can redistribute it and/or
7
# modify it under the terms of either of these two licenses.
8
#
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS,
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
# See the License for the specific language governing permissions and
13
# limitations under the License.
14
#
15
# You should have received a copy of the licenses; if not, see
16
# <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
17
# and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
18
# License, Version 2.0.
19
#
20

    
21
"""Object specification."""
22

    
23

    
24
def to_bytes(text):
25
    if getattr(text, "encode", None) is not None:
26
        text = text.encode('ascii')
27
    return text
28

    
29

    
30
def parse_object(repo, objectish):
31
    """Parse a string referring to an object.
32

33
    :param repo: A `Repo` object
34
    :param objectish: A string referring to an object
35
    :return: A git object
36
    :raise KeyError: If the object can not be found
37
    """
38
    objectish = to_bytes(objectish)
39
    return repo[objectish]
40

    
41

    
42
def parse_ref(container, refspec):
43
    """Parse a string referring to a reference.
44

45
    :param container: A RefsContainer object
46
    :param refspec: A string referring to a ref
47
    :return: A ref
48
    :raise KeyError: If the ref can not be found
49
    """
50
    refspec = to_bytes(refspec)
51
    possible_refs = [
52
        refspec,
53
        b"refs/" + refspec,
54
        b"refs/tags/" + refspec,
55
        b"refs/heads/" + refspec,
56
        b"refs/remotes/" + refspec,
57
        b"refs/remotes/" + refspec + b"/HEAD"
58
    ]
59
    for ref in possible_refs:
60
        if ref in container:
61
            return ref
62
    else:
63
        raise KeyError(refspec)
64

    
65

    
66
def parse_reftuple(lh_container, rh_container, refspec):
67
    """Parse a reftuple spec.
68

69
    :param lh_container: A RefsContainer object
70
    :param hh_container: A RefsContainer object
71
    :param refspec: A string
72
    :return: A tuple with left and right ref
73
    :raise KeyError: If one of the refs can not be found
74
    """
75
    if refspec.startswith(b"+"):
76
        force = True
77
        refspec = refspec[1:]
78
    else:
79
        force = False
80
    refspec = to_bytes(refspec)
81
    if b":" in refspec:
82
        (lh, rh) = refspec.split(b":")
83
    else:
84
        lh = rh = refspec
85
    if lh == b"":
86
        lh = None
87
    else:
88
        lh = parse_ref(lh_container, lh)
89
    if rh == b"":
90
        rh = None
91
    else:
92
        try:
93
            rh = parse_ref(rh_container, rh)
94
        except KeyError:
95
            # TODO: check force?
96
            if not b"/" in rh:
97
                rh = b"refs/heads/" + rh
98
    return (lh, rh, force)
99

    
100

    
101
def parse_reftuples(lh_container, rh_container, refspecs):
102
    """Parse a list of reftuple specs to a list of reftuples.
103

104
    :param lh_container: A RefsContainer object
105
    :param hh_container: A RefsContainer object
106
    :param refspecs: A list of refspecs or a string
107
    :return: A list of refs
108
    :raise KeyError: If one of the refs can not be found
109
    """
110
    if not isinstance(refspecs, list):
111
        refspecs = [refspecs]
112
    ret = []
113
    # TODO: Support * in refspecs
114
    for refspec in refspecs:
115
        ret.append(parse_reftuple(lh_container, rh_container, refspec))
116
    return ret
117

    
118

    
119
def parse_refs(container, refspecs):
120
    """Parse a list of refspecs to a list of refs.
121

122
    :param container: A RefsContainer object
123
    :param refspecs: A list of refspecs or a string
124
    :return: A list of refs
125
    :raise KeyError: If one of the refs can not be found
126
    """
127
    # TODO: Support * in refspecs
128
    if not isinstance(refspecs, list):
129
        refspecs = [refspecs]
130
    ret = []
131
    for refspec in refspecs:
132
        ret.append(parse_ref(container, refspec))
133
    return ret
134

    
135

    
136
def parse_commit_range(repo, committishs):
137
    """Parse a string referring to a range of commits.
138

139
    :param repo: A `Repo` object
140
    :param committishs: A string referring to a range of commits.
141
    :return: An iterator over `Commit` objects
142
    :raise KeyError: When the reference commits can not be found
143
    :raise ValueError: If the range can not be parsed
144
    """
145
    committishs = to_bytes(committishs)
146
    # TODO(jelmer): Support more than a single commit..
147
    return iter([parse_commit(repo, committishs)])
148

    
149

    
150
def parse_commit(repo, committish):
151
    """Parse a string referring to a single commit.
152

153
    :param repo: A` Repo` object
154
    :param commitish: A string referring to a single commit.
155
    :return: A Commit object
156
    :raise KeyError: When the reference commits can not be found
157
    :raise ValueError: If the range can not be parsed
158
    """
159
    committish = to_bytes(committish)
160
    return repo[committish] # For now..
161

    
162

    
163
# TODO: parse_path_in_tree(), which handles e.g. v1.0:Documentation