Search code examples
clibgit2

libgit2 - c: how can I tell which version is being used


I am writing an application that uses libgit2:

https://github.com/eantoranz/gitmod

At some point I am using a value from an enum defined in types.h:

https://github.com/libgit2/libgit2/blob/a83fd5107879d18b31ff8173ea062136256321be/include/git2/types.h#L77

But I just noticed that in previous versions it is called GIT_OBJ_TREE:

https://github.com/libgit2/libgit2/blob/45c6187cbfdcfee2bcf59a1ed3a853065142f203/include/git2/types.h#L72

Tnen the queston is, how can I know what version of libgit2 is being used? I though that perhaps there is a defined value that I could use that that I cold then use something like:

#ifdef GIT2_0_28
// use GIT_OBJECT_TREE
#else
// use GIT_OBJ_TREE
#endif

But I don't see something like that. What other trick can I use?


Solution

  • If you want to identify the version of libgit2, then I would not use LIBGIT_SOVERSION, since that's a string that defines the "so-version", which is the ABI version, not the API version. And it's a string, which complicates that test.

    I'd instead look at the major/minor/teeny version numbers which are definitely integers.

    #if LIBGIT2_VER_MAJOR == 0 && LIBGIT2_VER_MINOR < 28
    # ...
    #endif
    

    But in this case, I wouldn't look at the API version at all - you don't need to.

    If you really want to target versions prior to 0.28 and versions later, then just use GIT_OBJ_TREE. Although GIT_OBJ_TREE is deprecated, it's still available as part of libgit2's safe deprecation layer which is enabled by default.

    It won't be removed until libgit2 v2.0, at the earliest.