Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TemporaryFileSwap regression where file_path could not be Path #1776

Merged
merged 4 commits into from
Dec 21, 2023

Commits on Dec 21, 2023

  1. Add a test for git.index.util.TemporaryFileSwap

    This is a general test for TemporaryFileSwap, but by being
    parametrized by the type of file_path, it reveals a regression
    introduced in 9e86053 (gitpython-developers#1770). TemporaryFileSwap still works when
    file_path is a string, but is now broken when it is a Path. That
    worked before, and the type annotations document that it should
    be able to work. This is at least a bug because TemporaryFileSwap
    is public. (I am unsure whether, in practice, GitPython itself uses
    it in a way that sometimes passes a Path object as file_path. But
    code that uses GitPython may call it directly and pass Path.)
    EliahKagan committed Dec 21, 2023
    Configuration menu
    Copy the full SHA
    487a4fd View commit details
    Browse the repository at this point in the history
  2. Fix TemporaryFileSwap bug when file_path is a Path

    This fixes the regression introduced in 9e86053 (gitpython-developers#1770) where the
    file_path argument to TemporaryFileSwap.__init__ could no longer be
    a Path object.
    
    The change also makes this truer to the code from before gitpython-developers#1770,
    still without the race condition fixed there, in that str was
    called on file_path then as well. However, it is not clear that
    this is a good thing, because this is not an idiomatic use of
    mkstemp. The reason the `prefix` cannot be a Path is that it is
    expected to be a filename prefix, with leading directories given in
    the `dir` argument.
    EliahKagan committed Dec 21, 2023
    Configuration menu
    Copy the full SHA
    1ddf953 View commit details
    Browse the repository at this point in the history
  3. Refactor TemporaryFileSwap.__init__ for clarity

    This changes the mkstemp call TemporaryFileSwap uses to atomically
    create (and thus reserve) the temporary file, so that it passes
    only a filename (i.e., basename) prefix as `prefix`, and passes all
    other path components (i.e., the directory) as `dir`. (If the
    original path has no directory, then `dir` is "" as before.)
    
    This makes the mkstemp call *slightly* more idiomatic. This also
    makes it clearer, as it is no longer using `prefix` for something
    that feels like it should be possible to pass as a Path object.
    
    Although mkstemp does not accept a Path as `prefix`, it does (as
    expected) accept one as `dir`. However, to keep the code simple,
    I'm passing str for both. The os.path.split function accepts both
    str and Path (since Python 3.6), and returns str objects, which are
    now used for the `dir` and `prefix` arguments to mkstemp.
    
    For unusual cases, this may technically not be a refactoring. For
    example, a file_path of "a/b//c" will be split into "a/b" and "c".
    If the automatically generated temporary file suffix is "xyz", then
    that results in a tmp_file_path of "a/b/cxyz" where "a/b//cxyz"
    would have been used before. The tmp_file_path attribute of a
    TemporaryFileSwap object is public (and used in filesystem calls).
    
    However, no guarantee has ever been given that the temporary file
    path have the original path as an exact string prefix. I believe
    the slightly weaker relationship I expressed in the recently
    introduced test_temporary_file_swap -- another file in the same
    directory, named with the original filename with more characters,
    consisting of equivalent path components in the same order -- has
    always been the intended one.
    
    Note that this slight possible variation does not apply to the
    file_path attribute. That attribute is always kept exactly as it
    was, both in its type and its value, and it always used unmodified
    in calls that access the filesystem.
    EliahKagan committed Dec 21, 2023
    Configuration menu
    Copy the full SHA
    b438c45 View commit details
    Browse the repository at this point in the history
  4. Tweak formatting for @pytest.mark.parametrize

    This removes the "fmt: off" / "fmt: on" directives around the
    `@pytest.mark.parametrize` decoration on test_blob_filter, and
    reformats it with black, for consistency with other such
    decorations.
    
    The style used there, *if* it could be specified as a rule and thus
    used without "fmt:" directives, may be nicer than how black formats
    multi-line mark decorations. However, since that decoration was
    written, there have been a number of other such decorations, which
    have been in black style.
    
    This also removes the only (or only remaining?) "fmt:" directive in
    the codebase. As such, it should possibly have been done in gitpython-developers#1760.
    EliahKagan committed Dec 21, 2023
    Configuration menu
    Copy the full SHA
    4e91a6c View commit details
    Browse the repository at this point in the history