Add Feature query support & panic if libgit2 is not thread-aware #354

Merged
carlosmn merged 2 commits from cmn/panic-threading into next 2016-10-31 18:16:27 -05:00
2 changed files with 39 additions and 0 deletions

30
features.go Normal file
View File

@ -0,0 +1,30 @@
package git
/*
#include <git2.h>
*/
import "C"
type Feature int
const (
// libgit2 was built with threading support
FeatureThreads Feature = C.GIT_FEATURE_THREADS
// libgit2 was built with HTTPS support built-in
FeatureHttps Feature = C.GIT_FEATURE_HTTPS
// libgit2 was build with SSH support built-in
FeatureSsh Feature = C.GIT_FEATURE_SSH
// libgit2 was built with nanosecond support for files
FeatureNSec Feature = C.GIT_FEATURE_NSEC
)
// Features returns a bit-flag of Feature values indicating which features the
// loaded libgit2 library has.
func Features() Feature {
features := C.git_libgit2_features()
return Feature(features)
}

9
git.go
View File

@ -124,6 +124,15 @@ func init() {
C.git_libgit2_init()
// Due to the multithreaded nature of Go and its interaction with
// calling C functions, we cannot work with a library that was not built
// with multi-threading support. The most likely outcome is a segfault
// or panic at an incomprehensible time, so let's make it easy by
// panicking right here.
if Features()&FeatureThreads == 0 {
panic("libgit2 was not built with threading support")
}
// This is not something we should be doing, as we may be
// stomping all over someone else's setup. The user should do
// this themselves or use some binding/wrapper which does it