if(__add_git_info)
	return()
endif()
set(__add_git_info ON)


execute_process(
  COMMAND ${GIT_EXECUTABLE} log -1 --format=%h
  WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
  OUTPUT_VARIABLE PYJADX_COMMIT_HASH
  OUTPUT_STRIP_TRAILING_WHITESPACE
)

execute_process(
  COMMAND ${GIT_EXECUTABLE} rev-list --count ${PYJADX_COMMIT_HASH}
  WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
  OUTPUT_VARIABLE PYJADX_COMMIT_COUNT
  OUTPUT_STRIP_TRAILING_WHITESPACE
)

execute_process(
  COMMAND ${GIT_EXECUTABLE} describe --abbrev=0 --tags HEAD --always
  WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
  OUTPUT_VARIABLE PYJADX_GIT_TAG
  OUTPUT_STRIP_TRAILING_WHITESPACE
)

execute_process(
  COMMAND ${GIT_EXECUTABLE} tag --list --points-at=HEAD
  WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
  OUTPUT_VARIABLE PYJADX_GIT_COMMIT_TAGGED
  OUTPUT_STRIP_TRAILING_WHITESPACE
)

execute_process(
  COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD
  WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
  OUTPUT_VARIABLE PYJADX_GIT_BRANCH
  OUTPUT_STRIP_TRAILING_WHITESPACE
)

string(COMPARE NOTEQUAL "${PYJADX_GIT_COMMIT_TAGGED}" "" PYJADX_IS_TAGGED)

STRING(REGEX MATCHALL "([0-9]+)" VERSION_STRING "${PYJADX_GIT_TAG}")

message(STATUS "Tagged: ${PYJADX_IS_TAGGED}")
if (${PYJADX_IS_TAGGED})
  message(STATUS "Tag: ${PYJADX_GIT_TAG}")
endif()
message(STATUS "Current branch: ${PYJADX_GIT_BRANCH}")


if (VERSION_STRING)
  list(GET VERSION_STRING 0 PYJADX_VERSION_MAJOR)
  list(GET VERSION_STRING 1 PYJADX_VERSION_MINOR)
  list(GET VERSION_STRING 2 PYJADX_VERSION_PATCH)
else()
  set(PYJADX_VERSION_MAJOR 0)
  set(PYJADX_VERSION_MINOR 0)
  set(PYJADX_VERSION_PATCH 0)
endif()

