1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
|
# test_reflog.py -- tests for reflog.py
# Copyright (C) 2015 Jelmer Vernooij <jelmer@jelmer.uk>
#
# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
# Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
# General Public License as published by the Free Software Foundation; version 2.0
# or (at your option) any later version. You can redistribute it and/or
# modify it under the terms of either of these two licenses.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# You should have received a copy of the licenses; if not, see
# <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
# and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
# License, Version 2.0.
#
"""Tests for dulwich.reflog."""
import tempfile
from io import BytesIO
from dulwich.objects import ZERO_SHA, Blob, Commit, Tree
from dulwich.reflog import (
drop_reflog_entry,
expire_reflog,
format_reflog_line,
iter_reflogs,
parse_reflog_line,
parse_reflog_spec,
read_reflog,
)
from dulwich.repo import Repo
from . import TestCase
class ReflogSpecTests(TestCase):
def test_parse_reflog_spec_basic(self) -> None:
# Test basic reflog spec
ref, index = parse_reflog_spec("HEAD@{1}")
self.assertEqual(b"HEAD", ref)
self.assertEqual(1, index)
def test_parse_reflog_spec_with_full_ref(self) -> None:
# Test with full ref name
ref, index = parse_reflog_spec("refs/heads/master@{5}")
self.assertEqual(b"refs/heads/master", ref)
self.assertEqual(5, index)
def test_parse_reflog_spec_bytes(self) -> None:
# Test with bytes input
ref, index = parse_reflog_spec(b"develop@{0}")
self.assertEqual(b"develop", ref)
self.assertEqual(0, index)
def test_parse_reflog_spec_no_ref(self) -> None:
# Test with no ref (defaults to HEAD)
ref, index = parse_reflog_spec("@{2}")
self.assertEqual(b"HEAD", ref)
self.assertEqual(2, index)
def test_parse_reflog_spec_invalid_no_brace(self) -> None:
# Test invalid spec without @{
with self.assertRaises(ValueError) as cm:
parse_reflog_spec("HEAD")
self.assertIn("Expected format: ref@{n}", str(cm.exception))
def test_parse_reflog_spec_invalid_no_closing_brace(self) -> None:
# Test invalid spec without closing brace
with self.assertRaises(ValueError) as cm:
parse_reflog_spec("HEAD@{1")
self.assertIn("Expected format: ref@{n}", str(cm.exception))
def test_parse_reflog_spec_invalid_non_numeric(self) -> None:
# Test invalid spec with non-numeric index
with self.assertRaises(ValueError) as cm:
parse_reflog_spec("HEAD@{foo}")
self.assertIn("Expected integer", str(cm.exception))
class ReflogLineTests(TestCase):
def test_format(self) -> None:
self.assertEqual(
b"0000000000000000000000000000000000000000 "
b"49030649db3dfec5a9bc03e5dde4255a14499f16 Jelmer Vernooij "
b"<jelmer@jelmer.uk> 1446552482 +0000 "
b"clone: from git://jelmer.uk/samba",
format_reflog_line(
b"0000000000000000000000000000000000000000",
b"49030649db3dfec5a9bc03e5dde4255a14499f16",
b"Jelmer Vernooij <jelmer@jelmer.uk>",
1446552482,
0,
b"clone: from git://jelmer.uk/samba",
),
)
self.assertEqual(
b"0000000000000000000000000000000000000000 "
b"49030649db3dfec5a9bc03e5dde4255a14499f16 Jelmer Vernooij "
b"<jelmer@jelmer.uk> 1446552482 +0000 "
b"clone: from git://jelmer.uk/samba",
format_reflog_line(
None,
b"49030649db3dfec5a9bc03e5dde4255a14499f16",
b"Jelmer Vernooij <jelmer@jelmer.uk>",
1446552482,
0,
b"clone: from git://jelmer.uk/samba",
),
)
def test_parse(self) -> None:
reflog_line = (
b"0000000000000000000000000000000000000000 "
b"49030649db3dfec5a9bc03e5dde4255a14499f16 Jelmer Vernooij "
b"<jelmer@jelmer.uk> 1446552482 +0000 "
b"clone: from git://jelmer.uk/samba"
)
self.assertEqual(
(
b"0000000000000000000000000000000000000000",
b"49030649db3dfec5a9bc03e5dde4255a14499f16",
b"Jelmer Vernooij <jelmer@jelmer.uk>",
1446552482,
0,
b"clone: from git://jelmer.uk/samba",
),
parse_reflog_line(reflog_line),
)
_TEST_REFLOG = (
b"0000000000000000000000000000000000000000 "
b"49030649db3dfec5a9bc03e5dde4255a14499f16 Jelmer Vernooij "
b"<jelmer@jelmer.uk> 1446552482 +0000 "
b"clone: from git://jelmer.uk/samba\n"
b"49030649db3dfec5a9bc03e5dde4255a14499f16 "
b"42d06bd4b77fed026b154d16493e5deab78f02ec Jelmer Vernooij "
b"<jelmer@jelmer.uk> 1446552483 +0000 "
b"clone: from git://jelmer.uk/samba\n"
b"42d06bd4b77fed026b154d16493e5deab78f02ec "
b"df6800012397fb85c56e7418dd4eb9405dee075c Jelmer Vernooij "
b"<jelmer@jelmer.uk> 1446552484 +0000 "
b"clone: from git://jelmer.uk/samba\n"
)
class ReflogDropTests(TestCase):
def setUp(self) -> None:
TestCase.setUp(self)
self.f = BytesIO(_TEST_REFLOG)
self.original_log = list(read_reflog(self.f))
self.f.seek(0)
def _read_log(self):
self.f.seek(0)
return list(read_reflog(self.f))
def test_invalid(self) -> None:
self.assertRaises(ValueError, drop_reflog_entry, self.f, -1)
def test_drop_entry(self) -> None:
drop_reflog_entry(self.f, 0)
log = self._read_log()
self.assertEqual(len(log), 2)
self.assertEqual(self.original_log[0:2], log)
self.f.seek(0)
drop_reflog_entry(self.f, 1)
log = self._read_log()
self.assertEqual(len(log), 1)
self.assertEqual(self.original_log[1], log[0])
def test_drop_entry_with_rewrite(self) -> None:
drop_reflog_entry(self.f, 1, True)
log = self._read_log()
self.assertEqual(len(log), 2)
self.assertEqual(self.original_log[0], log[0])
self.assertEqual(self.original_log[0].new_sha, log[1].old_sha)
self.assertEqual(self.original_log[2].new_sha, log[1].new_sha)
self.f.seek(0)
drop_reflog_entry(self.f, 1, True)
log = self._read_log()
self.assertEqual(len(log), 1)
self.assertEqual(ZERO_SHA, log[0].old_sha)
self.assertEqual(self.original_log[2].new_sha, log[0].new_sha)
class RepoReflogTests(TestCase):
def setUp(self) -> None:
TestCase.setUp(self)
self.test_dir = tempfile.mkdtemp()
self.repo = Repo.init(self.test_dir)
def tearDown(self) -> None:
TestCase.tearDown(self)
import shutil
self.repo.close()
shutil.rmtree(self.test_dir)
def test_read_reflog_nonexistent(self) -> None:
# Reading a reflog that doesn't exist should return empty
entries = list(self.repo.read_reflog(b"refs/heads/nonexistent"))
self.assertEqual([], entries)
def test_read_reflog_head(self) -> None:
# Create a commit to generate a reflog entry
blob = Blob.from_string(b"test content")
self.repo.object_store.add_object(blob)
tree = Tree()
tree.add(b"test", 0o100644, blob.id)
self.repo.object_store.add_object(tree)
commit = Commit()
commit.tree = tree.id
commit.author = b"Test Author <test@example.com>"
commit.committer = b"Test Author <test@example.com>"
commit.commit_time = 1234567890
commit.commit_timezone = 0
commit.author_time = 1234567890
commit.author_timezone = 0
commit.message = b"Initial commit"
self.repo.object_store.add_object(commit)
# Manually write a reflog entry
self.repo._write_reflog(
b"HEAD",
ZERO_SHA,
commit.id,
b"Test Author <test@example.com>",
1234567890,
0,
b"commit (initial): Initial commit",
)
# Read the reflog
entries = list(self.repo.read_reflog(b"HEAD"))
self.assertEqual(1, len(entries))
self.assertEqual(ZERO_SHA, entries[0].old_sha)
self.assertEqual(commit.id, entries[0].new_sha)
self.assertEqual(b"Test Author <test@example.com>", entries[0].committer)
self.assertEqual(1234567890, entries[0].timestamp)
self.assertEqual(0, entries[0].timezone)
self.assertEqual(b"commit (initial): Initial commit", entries[0].message)
def test_iter_reflogs(self) -> None:
# Create commits and reflog entries
blob = Blob.from_string(b"test content")
self.repo.object_store.add_object(blob)
tree = Tree()
tree.add(b"test", 0o100644, blob.id)
self.repo.object_store.add_object(tree)
commit = Commit()
commit.tree = tree.id
commit.author = b"Test Author <test@example.com>"
commit.committer = b"Test Author <test@example.com>"
commit.commit_time = 1234567890
commit.commit_timezone = 0
commit.author_time = 1234567890
commit.author_timezone = 0
commit.message = b"Initial commit"
self.repo.object_store.add_object(commit)
# Write reflog entries for multiple refs
self.repo._write_reflog(
b"HEAD",
ZERO_SHA,
commit.id,
b"Test Author <test@example.com>",
1234567890,
0,
b"commit (initial): Initial commit",
)
self.repo._write_reflog(
b"refs/heads/master",
ZERO_SHA,
commit.id,
b"Test Author <test@example.com>",
1234567891,
0,
b"branch: Created from HEAD",
)
self.repo._write_reflog(
b"refs/heads/develop",
ZERO_SHA,
commit.id,
b"Test Author <test@example.com>",
1234567892,
0,
b"branch: Created from HEAD",
)
# Use iter_reflogs to get all reflogs
import os
logs_dir = os.path.join(self.repo.controldir(), "logs")
reflogs = list(iter_reflogs(logs_dir))
# Should have at least HEAD, refs/heads/master, and refs/heads/develop
self.assertIn(b"HEAD", reflogs)
self.assertIn(b"refs/heads/master", reflogs)
self.assertIn(b"refs/heads/develop", reflogs)
class ReflogExpireTests(TestCase):
def setUp(self) -> None:
TestCase.setUp(self)
# Create a reflog with entries at different timestamps
self.f = BytesIO()
# Old entry (timestamp: 1000000000)
self.f.write(
b"0000000000000000000000000000000000000000 "
b"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
b"Test <test@example.com> 1000000000 +0000\told entry\n"
)
# Medium entry (timestamp: 1500000000)
self.f.write(
b"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa "
b"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb "
b"Test <test@example.com> 1500000000 +0000\tmedium entry\n"
)
# Recent entry (timestamp: 2000000000)
self.f.write(
b"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb "
b"cccccccccccccccccccccccccccccccccccccccc "
b"Test <test@example.com> 2000000000 +0000\trecent entry\n"
)
self.f.seek(0)
def _read_log(self):
self.f.seek(0)
return list(read_reflog(self.f))
def test_expire_no_criteria(self) -> None:
# If no expiration criteria, nothing should be expired
count = expire_reflog(self.f)
self.assertEqual(0, count)
log = self._read_log()
self.assertEqual(3, len(log))
def test_expire_by_time(self) -> None:
# Expire entries older than timestamp 1600000000
# Should remove the first two entries
count = expire_reflog(self.f, expire_time=1600000000)
self.assertEqual(2, count)
log = self._read_log()
self.assertEqual(1, len(log))
self.assertEqual(b"recent entry", log[0].message)
def test_expire_unreachable(self) -> None:
# Test expiring unreachable entries
# Mark the middle entry as unreachable
def reachable_checker(sha):
return sha != b"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
count = expire_reflog(
self.f,
expire_unreachable_time=1600000000,
reachable_checker=reachable_checker,
)
self.assertEqual(1, count)
log = self._read_log()
self.assertEqual(2, len(log))
# First and third entries should remain
self.assertEqual(b"old entry", log[0].message)
self.assertEqual(b"recent entry", log[1].message)
def test_expire_mixed(self) -> None:
# Test with both expire_time and expire_unreachable_time
def reachable_checker(sha):
# Only the most recent entry is reachable
return sha == b"cccccccccccccccccccccccccccccccccccccccc"
count = expire_reflog(
self.f,
expire_time=1800000000, # Would expire first two if reachable
expire_unreachable_time=1200000000, # Would expire first if unreachable
reachable_checker=reachable_checker,
)
# First entry is unreachable and old enough -> expired
# Second entry is unreachable but not old enough -> kept
# Third entry is reachable and recent -> kept
self.assertEqual(1, count)
log = self._read_log()
self.assertEqual(2, len(log))
self.assertEqual(b"medium entry", log[0].message)
self.assertEqual(b"recent entry", log[1].message)
def test_expire_all_entries(self) -> None:
# Expire all entries
count = expire_reflog(self.f, expire_time=3000000000)
self.assertEqual(3, count)
log = self._read_log()
self.assertEqual(0, len(log))
|