2021-03-28 16:58:51 +02:00
# This script will set the following variables:
# GIT_HASH
# If the git binary is found and the git work tree is intact, GIT_HASH is worked out using the `git rev-parse --short HEAD` command
# The value of GIT_HASH can be overriden by defining the GIT_HASH environment variable
# GIT_COMMIT
# If the git binary is found and the git work tree is intact, GIT_COMMIT is worked out using the `git rev-parse HEAD` command
# The value of GIT_COMMIT can be overriden by defining the GIT_COMMIT environment variable
# GIT_RELEASE
# If the git binary is found and the git work tree is intact, GIT_RELEASE is worked out using the `git describe` command
# The value of GIT_RELEASE can be overriden by defining the GIT_RELEASE environment variable
2022-06-04 21:00:42 +02:00
# GIT_MODIFIED
2023-02-16 14:56:20 +01:00
# If the git binary is found and the git work tree is intact, GIT_MODIFIED is worked out by checking if output of `git status --porcelain -z` command is empty
2022-06-04 21:00:42 +02:00
# The value of GIT_MODIFIED cannot be overriden
2021-03-28 16:58:51 +02:00
find_package ( Git )
set ( GIT_HASH "GIT-REPOSITORY-NOT-FOUND" )
set ( GIT_COMMIT "GIT-REPOSITORY-NOT-FOUND" )
set ( GIT_RELEASE "${PROJECT_VERSION}" )
2022-06-04 21:00:42 +02:00
set ( GIT_MODIFIED 0 )
2021-03-28 16:58:51 +02:00
2023-02-16 14:56:20 +01:00
if ( DEFINED ENV{CHATTERINO_SKIP_GIT_GEN} )
2022-06-05 16:44:20 +02:00
return ( )
2023-02-16 14:56:20 +01:00
endif ( )
2022-06-05 16:44:20 +02:00
2023-02-16 14:56:20 +01:00
if ( GIT_EXECUTABLE )
2021-03-28 16:58:51 +02:00
execute_process (
2023-02-16 14:56:20 +01:00
C O M M A N D $ { G I T _ E X E C U T A B L E } r e v - p a r s e - - i s - i n s i d e - w o r k - t r e e
W O R K I N G _ D I R E C T O R Y $ { C M A K E _ S O U R C E _ D I R }
R E S U L T _ V A R I A B L E G I T _ R E P O S I T O R Y _ N O T _ F O U N D
O U T P U T _ Q U I E T
E R R O R _ Q U I E T
2021-03-28 16:58:51 +02:00
)
2023-02-16 14:56:20 +01:00
if ( GIT_REPOSITORY_NOT_FOUND )
2021-03-28 16:58:51 +02:00
set ( GIT_REPOSITORY_FOUND 0 )
2023-02-16 14:56:20 +01:00
else ( )
2021-03-28 16:58:51 +02:00
set ( GIT_REPOSITORY_FOUND 1 )
endif ( )
2023-02-16 14:56:20 +01:00
if ( GIT_REPOSITORY_FOUND )
2021-03-28 16:58:51 +02:00
execute_process (
2023-02-16 14:56:20 +01:00
C O M M A N D $ { G I T _ E X E C U T A B L E } r e v - p a r s e - - s h o r t H E A D
W O R K I N G _ D I R E C T O R Y $ { C M A K E _ S O U R C E _ D I R }
O U T P U T _ V A R I A B L E G I T _ H A S H
O U T P U T _ S T R I P _ T R A I L I N G _ W H I T E S P A C E
2021-03-28 16:58:51 +02:00
)
execute_process (
2023-02-16 14:56:20 +01:00
C O M M A N D $ { G I T _ E X E C U T A B L E } r e v - p a r s e H E A D
W O R K I N G _ D I R E C T O R Y $ { C M A K E _ S O U R C E _ D I R }
O U T P U T _ V A R I A B L E G I T _ C O M M I T
O U T P U T _ S T R I P _ T R A I L I N G _ W H I T E S P A C E
2021-03-28 16:58:51 +02:00
)
execute_process (
2023-02-16 14:56:20 +01:00
C O M M A N D $ { G I T _ E X E C U T A B L E } d e s c r i b e
W O R K I N G _ D I R E C T O R Y $ { C M A K E _ S O U R C E _ D I R }
O U T P U T _ V A R I A B L E G I T _ R E L E A S E
O U T P U T _ S T R I P _ T R A I L I N G _ W H I T E S P A C E
2021-03-28 16:58:51 +02:00
)
2022-06-04 21:00:42 +02:00
execute_process (
2023-02-16 14:56:20 +01:00
C O M M A N D $ { G I T _ E X E C U T A B L E } s t a t u s - - p o r c e l a i n - z
W O R K I N G _ D I R E C T O R Y $ { C M A K E _ S O U R C E _ D I R }
O U T P U T _ V A R I A B L E G I T _ M O D I F I E D _ O U T P U T
O U T P U T _ S T R I P _ T R A I L I N G _ W H I T E S P A C E
2022-06-04 21:00:42 +02:00
)
2023-02-16 14:56:20 +01:00
endif ( GIT_REPOSITORY_FOUND )
endif ( GIT_EXECUTABLE )
if ( GIT_MODIFIED_OUTPUT )
if ( DEFINED ENV{CHATTERINO_REQUIRE_CLEAN_GIT} )
message ( STATUS "git status --porcelain -z\n${GIT_MODIFIED_OUTPUT}" )
message ( FATAL_ERROR "Git repository was expected to be clean, but modifications were found!" )
endif ( )
2021-03-28 16:58:51 +02:00
2022-06-04 21:00:42 +02:00
set ( GIT_MODIFIED 1 )
2023-02-16 14:56:20 +01:00
endif ( )
2022-06-04 21:00:42 +02:00
2023-02-16 14:56:20 +01:00
if ( DEFINED ENV{GIT_HASH} )
2021-03-28 16:58:51 +02:00
set ( GIT_HASH "$ENV{GIT_HASH}" )
2023-02-16 14:56:20 +01:00
endif ( )
if ( DEFINED ENV{GIT_COMMIT} )
2021-03-28 16:58:51 +02:00
set ( GIT_COMMIT "$ENV{GIT_COMMIT}" )
2023-02-16 14:56:20 +01:00
endif ( )
if ( DEFINED ENV{GIT_RELEASE} )
2021-03-28 16:58:51 +02:00
set ( GIT_RELEASE "$ENV{GIT_RELEASE}" )
2023-02-16 14:56:20 +01:00
endif ( )
2021-03-28 16:58:51 +02:00
2022-06-04 21:00:42 +02:00
message ( STATUS "Injected git values: ${GIT_COMMIT} (${GIT_RELEASE}) modified: ${GIT_MODIFIED}" )