libgit2 / pygit2

Python bindings for libgit2
https://www.pygit2.org/
Other
1.58k stars 382 forks source link

Add Repository.hashfile() #1298

Open jorio opened 4 weeks ago

jorio commented 4 weeks ago

This exposes libgit2's git_repository_hashfile().

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

jdavid commented 3 weeks ago

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

jorio commented 3 weeks ago

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.