pygit2 icon indicating copy to clipboard operation
pygit2 copied to clipboard

Add Repository.hashfile()

Open jorio opened this issue 1 year ago • 2 comments

This exposes libgit2's git_repository_hashfile().

pygit2.hashfile() already exists, but this lets you hash files using the repository's filters.

jorio avatar Jun 02 '24 12:06 jorio

It looks good, but the tests don't pass on Windows: https://ci.appveyor.com/project/jdavid/pygit2/builds/49936765/job/72q19bou1q95mxy9

jdavid avatar Jun 04 '24 11:06 jdavid

Hmm, this looks like a libgit2 issue on Windows.

str(Path(...)) returns a "native" path that uses \ as the directory separator. But git_repository_hashfile doesn't recognize this path as being part of the workdir – which is required for the test to pass – unless you replace the backslashes with forward slashes.

In other words, editing my test as follows will make it pass on Windows:

    # Treat absolute path with filters
    absolute_path = str(Path(testrepo.workdir, 'hellocrlf.txt'))
    absolute_path = absolute_path.replace('\\', '/')  # <--------------- yikes!
    h = testrepo.hashfile(absolute_path)
    assert h == original_hash

But in this case, I would argue that libgit2 should canonicalize the input path to its preferred form internally.

jorio avatar Jun 04 '24 19:06 jorio

Thanks for the excellent report in libgit2

I think we can just skip the test on Windows, with a reference to the libgit2 issue, and then merge the PR.

jdavid avatar Jul 13 '24 08:07 jdavid

If we want to keep the test on Windows, pygit2 could sanitize the path before passing it to libgit2 on Windows, and perhaps issue a RuntimeWarning with a link to the issue if the path contains any backslashes.

That would make hashfile work in the "least surprising way" on Windows. I can add this to the PR soon if we think that's a good compromise. What do you think?

jorio avatar Jul 13 '24 08:07 jorio

The PR is straightforward and the error is clearly in the libgit2 side. What you propose is in the best interest of users, yet I'm not sure we should workaround a libgit 2 bug. Do as you see fit, I leave it to your good judgement.

jdavid avatar Jul 13 '24 19:07 jdavid

By the way you have been the largest contributor to pygit2 since you started few years ago, about half the contributions no less. Also your contributions have been of an outstanding quality, making my reviews easy. So I would like to give you a big thanks!

jdavid avatar Jul 13 '24 19:07 jdavid

Glad to be of service! And thank you for steering this project in the right direction :)

I have a vested interest in improving pygit2 because I’ve been working on a GUI that hits almost all of pygit2’s API surface area. I’m planning to make the first public release soon-ish. Here’s what it looks like:

Untitled2

Anyway, regarding this PR - let’s leave the implementation as it is, but the unit test could demonstrate how to make it work on Windows (call Path.as_posix() before passing the path to hashfile). I rebased my branch and fixed the test, hopefully it passes on Windows now.

jorio avatar Jul 21 '24 15:07 jorio