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 / reflog.py @ 959

History | View | Annotate | Download (2.4 KB)

1
# reflog.py -- Parsing and writing reflog files
2
# Copyright (C) 2015 Jelmer Vernooij and others.
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
"""Utilities for reading and generating reflogs.
22
"""
23

    
24
import collections
25

    
26
from dulwich.objects import (
27
    format_timezone,
28
    parse_timezone,
29
    ZERO_SHA,
30
    )
31

    
32
Entry = collections.namedtuple('Entry', ['old_sha', 'new_sha', 'committer',
33
    'timestamp', 'timezone', 'message'])
34

    
35

    
36
def format_reflog_line(old_sha, new_sha, committer, timestamp, timezone, message):
37
    """Generate a single reflog line.
38

39
    :param old_sha: Old Commit SHA
40
    :param new_sha: New Commit SHA
41
    :param committer: Committer name and e-mail
42
    :param timestamp: Timestamp
43
    :param timezone: Timezone
44
    :param message: Message
45
    """
46
    if old_sha is None:
47
        old_sha = ZERO_SHA
48
    return (old_sha + b' ' + new_sha + b' ' + committer + b' ' +
49
            str(timestamp).encode('ascii') + b' ' +
50
            format_timezone(timezone) + b'\t' + message)
51

    
52

    
53
def parse_reflog_line(line):
54
    """Parse a reflog line.
55

56
    :param line: Line to parse
57
    :return: Tuple of (old_sha, new_sha, committer, timestamp, timezone,
58
        message)
59
    """
60
    (begin, message) = line.split(b'\t', 1)
61
    (old_sha, new_sha, rest) = begin.split(b' ', 2)
62
    (committer, timestamp_str, timezone_str) = rest.rsplit(b' ', 2)
63
    return Entry(old_sha, new_sha, committer, int(timestamp_str),
64
                 parse_timezone(timezone_str)[0], message)
65

    
66

    
67
def read_reflog(f):
68
    """Read reflog.
69

70
    :param f: File-like object
71
    :returns: Iterator over Entry objects
72
    """
73
    for l in f:
74
        yield parse_reflog_line(l)