-
Notifications
You must be signed in to change notification settings - Fork 887
Add git hash in release string #4774
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,4 +51,54 @@ if(EXISTS ${AUTOCONF_PROJ_CONFIG_H}) | |
| "before CMake's build.") | ||
| endif() | ||
|
|
||
| # Get the git hash to write it in the release message. | ||
| set(PROJ_GIT_HASH "") | ||
| if(DEFINED ENV{PROJ_GITHUB_SHA}) | ||
| string(SUBSTRING "$ENV{PROJ_GITHUB_SHA}" 0 7 PROJ_GIT_HASH) | ||
| else() | ||
| find_package(Git QUIET) | ||
| if(Git_FOUND AND EXISTS "${PROJ_SOURCE_DIR}/.git") | ||
| # Get the short hash | ||
| execute_process( | ||
| COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD | ||
| WORKING_DIRECTORY "${PROJ_SOURCE_DIR}" | ||
| RESULT_VARIABLE GIT_REV_RESULT | ||
| OUTPUT_VARIABLE PROJ_GIT_HASH | ||
| OUTPUT_STRIP_TRAILING_WHITESPACE | ||
| ERROR_QUIET | ||
| ) | ||
|
|
||
| if(GIT_REV_RESULT EQUAL 0) | ||
| # Check if the working directory is dirty | ||
| execute_process( | ||
| COMMAND ${GIT_EXECUTABLE} status --porcelain --untracked-files=no | ||
| WORKING_DIRECTORY "${PROJ_SOURCE_DIR}" | ||
| RESULT_VARIABLE GIT_STATUS_RESULT | ||
| OUTPUT_VARIABLE GIT_STATUS_OUTPUT | ||
| OUTPUT_STRIP_TRAILING_WHITESPACE | ||
| ERROR_QUIET | ||
| ) | ||
|
|
||
| # If the command succeeds and outputs anything, the tree is dirty | ||
| if(GIT_STATUS_RESULT EQUAL 0 AND NOT GIT_STATUS_OUTPUT STREQUAL "") | ||
| string(APPEND PROJ_GIT_HASH "*") | ||
| endif() | ||
|
Comment on lines
+71
to
+85
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This piece of code is to detect a dirty tree (modified files). This is more helpful in the developer's environment, where things can be modified. It makes clear that the code used was not exactly what is in the git hash. I think it is not that helpful in the CI environment, where source code should not be modified. |
||
| else() | ||
| set(PROJ_GIT_HASH "") | ||
| endif() | ||
| endif() | ||
| endif() | ||
| if(PROJ_GIT_HASH) | ||
| add_compile_definitions(PROJ_GIT_HASH=\"${PROJ_GIT_HASH}\") | ||
| endif() | ||
| message(PROJ_GIT_HASH=\"${PROJ_GIT_HASH}\") | ||
|
|
||
| # Tell CMake to re-configure if the Git HEAD or index changes | ||
| if(Git_FOUND AND EXISTS "${PROJ_SOURCE_DIR}/.git") | ||
| set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS | ||
| "${PROJ_SOURCE_DIR}/.git/HEAD" # for the hash | ||
| "${PROJ_SOURCE_DIR}/.git/index" # for the * | ||
| ) | ||
| endif() | ||
|
Comment on lines
+96
to
+102
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In case something is modified in git, proj is reconfigured. This is helpful in the developer's environment. I don't know if it is too much overhead.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that should be fine |
||
|
|
||
| configure_file(cmake/proj_config.cmake.in src/proj_config.h) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,8 +6,15 @@ | |
| #define STR_HELPER(x) #x | ||
| #define STR(x) STR_HELPER(x) | ||
|
|
||
| char const pj_release[] = "Rel. " STR(PROJ_VERSION_MAJOR) "." STR( | ||
| PROJ_VERSION_MINOR) "." STR(PROJ_VERSION_PATCH) ", " | ||
| "September 15th, 2026"; | ||
| #ifdef PROJ_GIT_HASH | ||
| #define PROJ_GIT_REV_STR " (" PROJ_GIT_HASH ")" | ||
| #else | ||
| #define PROJ_GIT_REV_STR "" | ||
| #endif | ||
|
|
||
| char const pj_release[] = | ||
| "Rel. " STR(PROJ_VERSION_MAJOR) "." STR(PROJ_VERSION_MINOR) "." STR( | ||
| PROJ_VERSION_PATCH) PROJ_GIT_REV_STR ", " | ||
| "September 15th, 2026"; | ||
|
Comment on lines
+16
to
+18
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This string is the simplest modification to the previous one: it just adds the hash in parenthesis . I am open to other options. |
||
|
|
||
| const char *pj_get_release() { return pj_release; } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be put above the
ifblock, sinceGIT_FOUNDis a variable used on L97.Furthermore, looking at CMake docs for FindGit, I see that
GIT_FOUNDis deprecated since version 4.2, so these instances should be set toGit_FOUND(which is valid since older versions).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @mwtoews . It is now Git_FOUND.
About the location of the
find_package, in casePROJ_GITHUB_SHAis defined, the second part later in the file is not needed. It can be used to disable the automatic check of the status of the repo.Do you think it is enough that way? Would you prefer something else?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps the automatic check of the status of the repo (L98-L101) could instead be moved up to this block (L60-L86) instead. (The original point I was making is that the same condition appears twice, except the second check at L97 may not always have
Git_FOUNDdefined, which is actually fine with CMake, since undefined vars evaluate false).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it worth it? I separated in two because it is doing two different things (but based on the same if statement, I know).