Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix TemporaryFileSwap regression where file_path could not be Path#1776

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletiongit/index/util.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -41,7 +41,8 @@ class TemporaryFileSwap:

def __init__(self, file_path: PathLike) -> None:
self.file_path = file_path
fd, self.tmp_file_path = tempfile.mkstemp(prefix=self.file_path, dir="")
dirname, basename = osp.split(file_path)
fd, self.tmp_file_path = tempfile.mkstemp(prefix=basename, dir=dirname)
os.close(fd)
with contextlib.suppress(OSError): # It may be that the source does not exist.
os.replace(self.file_path, self.tmp_file_path)
Expand Down
17 changes: 9 additions & 8 deletionstest/test_blob_filter.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,14 +14,15 @@
from git.types import PathLike


# fmt: off
@pytest.mark.parametrize('paths, path, expected_result', [
((Path("foo"),), Path("foo"), True),
((Path("foo"),), Path("foo/bar"), True),
((Path("foo/bar"),), Path("foo"), False),
((Path("foo"), Path("bar")), Path("foo"), True),
])
# fmt: on
@pytest.mark.parametrize(
"paths, path, expected_result",
[
((Path("foo"),), Path("foo"), True),
((Path("foo"),), Path("foo/bar"), True),
((Path("foo/bar"),), Path("foo"), False),
((Path("foo"), Path("bar")), Path("foo"), True),
],
)
def test_blob_filter(paths: Sequence[PathLike], path: PathLike, expected_result: bool) -> None:
"""Test the blob filter."""
blob_filter = BlobFilter(paths)
Expand Down
25 changes: 24 additions & 1 deletiontest/test_index.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -34,10 +34,11 @@
)
from git.index.fun import hook_path
from git.index.typ import BaseIndexEntry, IndexEntry
from git.index.util import TemporaryFileSwap
from git.objects import Blob
from test.lib import TestBase, fixture, fixture_path, with_rw_directory, with_rw_repo
from git.util import Actor, hex_to_bin, rmtree
from gitdb.base import IStream
from test.lib import TestBase, fixture, fixture_path, with_rw_directory, with_rw_repo

HOOKS_SHEBANG = "#!/usr/bin/env sh\n"

Expand DownExpand Up@@ -1087,3 +1088,25 @@ def test_index_add_pathlike(self, rw_repo):
file.touch()

rw_repo.index.add(file)


class TestIndexUtils:
@pytest.mark.parametrize("file_path_type", [str, Path])
def test_temporary_file_swap(self, tmp_path, file_path_type):
file_path = tmp_path / "foo"
file_path.write_bytes(b"some data")

with TemporaryFileSwap(file_path_type(file_path)) as ctx:
assert Path(ctx.file_path) == file_path
assert not file_path.exists()

# Recreate it with new data, so we can observe that they're really separate.
file_path.write_bytes(b"other data")

temp_file_path = Path(ctx.tmp_file_path)
assert temp_file_path.parent == file_path.parent
assert temp_file_path.name.startswith(file_path.name)
assert temp_file_path.read_bytes() == b"some data"

assert not temp_file_path.exists()
assert file_path.read_bytes() == b"some data" # Not b"other data".

[8]ページ先頭

©2009-2025 Movatter.jp