-->

How to use Libgit2Sharp on Mono/MacOsX?

2019-08-17 05:42发布

问题:

I am using Xamarin Studio 5.7 with Mono 3.12 on OSX 10.9.5. Using NuGet I downloaded the libgit2sharp library. I am able to use the classes when writing the code, there are no compiler errors. However at run time I get the following error:

Unhandled Exception: System.TypeInitializationException: An exception was thrown by the type initializer for LibGit2Sharp.Core.NativeMethods ---> System.DllNotFoundException: git2-91fa31f at (wrapper managed-to-native) LibGit2Sharp.Core.NativeMethods:git_threads_init () at LibGit2Sharp.Core.NativeMethods+LibraryLifetimeObject..ctor () [0x00000] in <filename unknown>:0 at LibGit2Sharp.Core.NativeMethods..cctor () [0x00000] in <filename unknown>:0 --- End of inner exception stack trace --- at LibGit2Sharp.Core.Proxy.git_repository_open (System.String path) [0x00000] in <filename unknown>:0 at LibGit2Sharp.Repository..ctor (System.String path, LibGit2Sharp.RepositoryOptions options) [0x00000] in <filename unknown>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException: An exception was thrown by the type initializer for LibGit2Sharp.Core.NativeMethods ---> System.DllNotFoundException: git2-91fa31f at (wrapper managed-to-native) LibGit2Sharp.Core.NativeMethods:git_threads_init () at LibGit2Sharp.Core.NativeMethods+LibraryLifetimeObject..ctor () [0x00000] in <filename unknown>:0 at LibGit2Sharp.Core.NativeMethods..cctor () [0x00000] in <filename unknown>:0 --- End of inner exception stack trace --- at LibGit2Sharp.Core.Proxy.git_repository_open (System.String path) [0x00000] in <filename unknown>:0 at LibGit2Sharp.Repository..ctor (System.String path, LibGit2Sharp.RepositoryOptions options) [0x00000] in <filename unknown>:0

It seems to be complaining that it cannot find the git2-91fa31f dll but when I look in the packages folder I can see that that dll does exist.

Has anyone seen this before or know what I can do to fix it?

UPDATE

I followed @nulltoken's instructions and downloaded the libgit2 source. Built it using cmake -DCMAKE_BUILD_TYPE:STRING=RelWithDebInfo -DBUILD_CLAR:BOOL=OFF -DUSE_SSH=OFF -DENABLE_TRACE=ON -DLIBGIT2_FILENAME=git2-91fa31f -DCMAKE_OSX_ARCHITECTURES="i386;x86_64" .. then cmake --build .

This produced a file called libgit2-91fa31f.dylib. I placed this file in the bin/Debug directory where the exe of my project is and where LibGit2Sharp.dll is. However now when I run the program I get the following:

Unhandled Exception: System.TypeInitializationException: An exception was thrown by the type initializer for LibGit2Sharp.Core.NativeMethods ---> System.EntryPointNotFoundException: git_threads_init at (wrapper managed-to-native) LibGit2Sharp.Core.NativeMethods:git_threads_init () at LibGit2Sharp.Core.NativeMethods+LibraryLifetimeObject..ctor () [0x00000] in <filename unknown>:0 at LibGit2Sharp.Core.NativeMethods..cctor () [0x00000] in <filename unknown>:0 --- End of inner exception stack trace --- at LibGit2Sharp.Core.Proxy.git_repository_open (System.String path) [0x00000] in <filename unknown>:0 at LibGit2Sharp.Repository..ctor (System.String path, LibGit2Sharp.RepositoryOptions options) [0x00000] in <filename unknown>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException: An exception was thrown by the type initializer for LibGit2Sharp.Core.NativeMethods ---> System.EntryPointNotFoundException: git_threads_init at (wrapper managed-to-native) LibGit2Sharp.Core.NativeMethods:git_threads_init () at LibGit2Sharp.Core.NativeMethods+LibraryLifetimeObject..ctor () [0x00000] in <filename unknown>:0 at LibGit2Sharp.Core.NativeMethods..cctor () [0x00000] in <filename unknown>:0 --- End of inner exception stack trace --- at LibGit2Sharp.Core.Proxy.git_repository_open (System.String path) [0x00000] in <filename unknown>:0 at LibGit2Sharp.Repository..ctor (System.String path, LibGit2Sharp.RepositoryOptions options) [0x00000] in <filename unknown>:0

I have a feeling I messed up the cmake part. Is that correct?

回答1:

LibGit2Sharp is a .Net binding to the native libgit2 library.

The project ensures that all the tests run on Win 32/64, Linux and MacOsX through the use of continuous integration servers (Travis and AppVeyor).

However, currently the LibGit2Sharp NuGet package only contains the Windows native compiled binaries and, as such, won't be usable as-is on Linux/MacOsX. In order to use LibGit2Sharp in those environments, you'd have to build libgit2 by yourself.

In order to make this happen, you'll need CMake.

You can get some inspiration from the build.libgit2sharp.sh script that's used by the CI servers.

In this Issue, Therzork, a Xamariner (and a LibGit2sharp core contributor), shares some guidance regarding building on MacOsX.

Update

I have a feeling I messed up the cmake part. Is that correct?

I think the got the CMake part is just fine. The exception raised is System.EntryPointNotFoundException. This means that LibGit2Sharp tries to call a method from libgit2 which either doesn't exist or whose signature (number of parameters, types, ...) doesn't match what's exposed.

Indeed, each version of LibGit2Sharp works against a specific version of libgit2. The libgit2 abbreviated commit sha is used to exactly which version of libgit2 is expected.

Version v0.20.1 expects to work against a compiled version of libgit2 as it was in commit 91fa3af (For each LibGit2sharp commit, you can check the expected libgit2 version by taking a look at the libgit2 submodule (for instance, v0.20.1)).

My guess would be that you're actually building a different libigt2 commit (probably the latest one). In order to build commit 91fa31f, run the following git command lines in a shell:

$ git clone https://github.com/libgit2/libgit2
$ cd libgit2
$ git checkout 91fa31f

Then run your CMake magic. The produced binary should now match LibGit2Sharp expectations.



回答2:

libgit2sharp requires libgit2 installed. Have you installed that yet?

https://libgit2.github.com