libgit2 / libgit2sharp

Git + .NET = ❤
http://libgit2.github.com
MIT License
3.18k stars 887 forks source link

On MacOS, `Repository.Init` yields different `WorkingDirectory` than provided in constructor #1945

Closed leonardochaia closed 2 years ago

leonardochaia commented 2 years ago

Hi all, we are using lib2gitsharp in dotnet-affected, a tool to detect which projects are affected by a set of changes.

I am not sure if this is a MacOS thing, or expected behavior. I couldn't find any related Issue or StackOverflow post.

Reproduction steps

Creating a new repository in MacOS using Repository.Init is prefixing a /private folder.

public void TestRepoPaths()
{
    using var directory = new TempWorkingDirectory();

    Repository.Init(directory.Path);
    using var repo = new Repository(directory.Path);

    var workDir = Path.TrimEndingDirectorySeparator(repo.Info.WorkingDirectory);

    Assert.Equal(directory.Path, workDir);
}

logs:

Request create repo at /var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T/Temporary5b072d08b1104c0cbf3ef3cf5c804bfa
Output Git Init /private/var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T/Temporary5b072d08b1104c0cbf3ef3cf5c804bfa/.git/
Repo Path: /private/var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T/Temporary5b072d08b1104c0cbf3ef3cf5c804bfa/.git/
Repo Workdir: /private/var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T/Temporary5b072d08b1104c0cbf3ef3cf5c804bfa/

Link to complete reproduction example

Link to build results passing on Windows and ubuntu, but not MacOS

Expected behavior

When using var repo = new Repository(workingDirectoryPath) on MacOs, I would expect repo.Info.WorkingDirectory to equal the received workingDirectoryPath.

Actual behavior

When using var repo = new Repository(workingDirectoryPath) on MacOS, repo.Info.WorkingDirectory does not equal the provided workingDirectoryPath.

Under Windows and Linux the same path is returned.

Version of LibGit2Sharp (release number or SHA1)

0.27.0-preview-0175

Operating system(s) tested; .NET runtime tested

All tests performed against netcoreapp3.1, net5.0 and net6.0.

MacOS 11.6.4

Ubuntu 20.04.3

Microsoft Windows Server 2022 10.0.203

ethomson commented 2 years ago

On macOS, $TMPDIR is somewhere in /var/folders (with a per-user obscure name), but /var is a symlink to /private/var.

libgit2 is returning the canonicalized path. I honestly don't remember why we do that, offhand, but those are two paths that point to the same place.

leonardochaia commented 2 years ago

Thanks @ethomson , I understand what's going on.

I guess I have a bigger problem than I thought: symlinks :sob: