diff --git a/dulwich/reflog.py b/dulwich/reflog.py index 3c15a4c3..750697f3 100644 --- a/dulwich/reflog.py +++ b/dulwich/reflog.py @@ -1,42 +1,57 @@ # reflog.py -- Parsing and writing reflog files # Copyright (C) 2015 Jelmer Vernooij and others. # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; version 2 # of the License or (at your option) a later version of the License. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. """Utilities for reading and generating reflogs. """ from dulwich.objects import ( format_timezone, + parse_timezone, ZERO_SHA, ) def format_reflog_line(old_sha, new_sha, committer, timestamp, timezone, message): """Generate a single reflog line. :param old_sha: Old Commit SHA :param new_sha: New Commit SHA :param committer: Committer name and e-mail :param timestamp: Timestamp :param timezone: Timezone :param message: Message """ if old_sha is None: old_sha = ZERO_SHA return (old_sha + b' ' + new_sha + b' ' + committer + b' ' + str(timestamp).encode('ascii') + b' ' + format_timezone(timezone).encode('ascii') + b'\t' + message) + + +def parse_reflog_line(line): + """Parse a reflog line. + + :param line: Line to parse + :return: Tuple of (old_sha, new_sha, committer, timestamp, timezone, + message) + """ + (begin, message) = line.split('\t', 1) + (old_sha, new_sha, rest) = begin.split(' ', 2) + (committer, timestamp_str, timezone_str) = rest.rsplit(' ', 2) + return (old_sha, new_sha, committer, int(timestamp_str), + parse_timezone(timezone_str)[0], message) diff --git a/dulwich/tests/test_reflog.py b/dulwich/tests/test_reflog.py index 4747499c..25348e1c 100644 --- a/dulwich/tests/test_reflog.py +++ b/dulwich/tests/test_reflog.py @@ -1,55 +1,69 @@ # test_reflog.py -- tests for reflog.py # encoding: utf-8 # Copyright (C) 2015 Jelmer Vernooij # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; version 2 # of the License or (at your option) any later version of # the License. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. """Tests for dulwich.reflog.""" -from dulwich.reflog import format_reflog_line +from dulwich.reflog import ( + format_reflog_line, + parse_reflog_line, + ) from dulwich.tests import ( TestCase, ) -class FormatReflogLineTests(TestCase): +class ReflogLineTests(TestCase): - def test_valid(self): + def test_format(self): self.assertEqual( b'0000000000000000000000000000000000000000 ' b'49030649db3dfec5a9bc03e5dde4255a14499f16 Jelmer Vernooij ' b' 1446552482 +0000 ' b'clone: from git://jelmer.uk/samba', format_reflog_line( b'0000000000000000000000000000000000000000', b'49030649db3dfec5a9bc03e5dde4255a14499f16', b'Jelmer Vernooij ', 1446552482, 0, b'clone: from git://jelmer.uk/samba')) self.assertEqual( b'0000000000000000000000000000000000000000 ' b'49030649db3dfec5a9bc03e5dde4255a14499f16 Jelmer Vernooij ' b' 1446552482 +0000 ' b'clone: from git://jelmer.uk/samba', format_reflog_line( None, b'49030649db3dfec5a9bc03e5dde4255a14499f16', b'Jelmer Vernooij ', 1446552482, 0, b'clone: from git://jelmer.uk/samba')) + def test_parse(self): + self.assertEqual( + (b'0000000000000000000000000000000000000000', + b'49030649db3dfec5a9bc03e5dde4255a14499f16', + b'Jelmer Vernooij ', + 1446552482, 0, b'clone: from git://jelmer.uk/samba'), + parse_reflog_line( + b'0000000000000000000000000000000000000000 ' + b'49030649db3dfec5a9bc03e5dde4255a14499f16 Jelmer Vernooij ' + b' 1446552482 +0000 ' + b'clone: from git://jelmer.uk/samba'))