find_package(Git QUIET)
if(GIT_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git")
    execute_process(COMMAND ${GIT_EXECUTABLE} log --pretty=format:'%h' -n 1
            OUTPUT_VARIABLE GIT_REV
            ERROR_QUIET)

    # Check whether we got any revision (which isn't
    # always the case, e.g. when someone downloaded a zip
    # file from Github instead of a checkout)
    if ("${GIT_REV}" STREQUAL "")
        set(GIT_REV "N/A")
        set(GIT_DIFF "")
        set(GIT_TAG "N/A")
        set(GIT_BRANCH "N/A")
    else()
        execute_process(
                COMMAND bash -c "${GIT_EXECUTABLE} diff --quiet --exit-code || echo +"
                OUTPUT_VARIABLE GIT_DIFF)
        execute_process(
                COMMAND ${GIT_EXECUTABLE} describe --exact-match --tags
                OUTPUT_VARIABLE GIT_TAG ERROR_QUIET)
        execute_process(
                COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD
                OUTPUT_VARIABLE GIT_BRANCH)

        string(STRIP "${GIT_REV}" GIT_REV)
        string(SUBSTRING "${GIT_REV}" 1 7 GIT_REV)
        string(STRIP "${GIT_DIFF}" GIT_DIFF)
        string(STRIP "${GIT_TAG}" GIT_TAG)
        string(STRIP "${GIT_BRANCH}" GIT_BRANCH)
    endif()

    set(PROJECT_VER_HASH ${GIT_REV}${GIT_DIFF})
    set(PROJECT_VER_TAG ${GIT_TAG})
    set(PROJECT_VER_BRANCH ${GIT_BRANCH})
else()
    set(PROJECT_VER_HASH N/A)
    set(PROJECT_VER_TAG N/A)
    set(PROJECT_VER_BRANCH N/A)
endif()