Skip to content

feat: auto-generate version from git tags#11

Merged
EricBorges2019 merged 2 commits intomainfrom
feat/auto-version
Mar 10, 2026
Merged

feat: auto-generate version from git tags#11
EricBorges2019 merged 2 commits intomainfrom
feat/auto-version

Conversation

@EricBorges2019
Copy link
Owner

@EricBorges2019 EricBorges2019 commented Mar 10, 2026

Summary

  • Replace hardcoded VERSION 1.0.0 in CMakeLists.txt with git describe --tags --always
  • Add version.hpp.in template configured at build time into build/generated/
  • Demo prints version string on startup (e.g. soft_render 0.0.2-17-g9f8a5e9)

How it works

Tag a release (git tag 0.1.0) and the next build picks it up automatically. Between tags, the full git describe output (tag + commit count + hash) is used.

Test plan

  • Local build passes (cmake -B build && cmake --build build)
  • All 3 CTest targets pass
  • Demo prints correct version string
  • CI passes on this PR

🤖 Generated with Claude Code

Replaces hardcoded VERSION 1.0.0 in CMakeLists.txt with git describe
output. Adds version.hpp.in template that CMake configures at build
time. Demo now prints version string on startup.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@EricBorges2019
Copy link
Owner Author

/gemini review

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a great feature to automatically generate the project version from git tags. The overall approach is sound. I've provided a couple of suggestions for the CMakeLists.txt file to improve the robustness of the implementation. Specifically, I recommend using find_package(Git) for more reliable git command execution and adjusting the version parsing regex to handle common tag formats. The other changes look good.

CMakeLists.txt Outdated
set(SR_VERSION "0.0.0-unknown")
endif()

string(REGEX MATCH "^([0-9]+)\\.([0-9]+)\\.([0-9]+)" _ver_match "${SR_VERSION}")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The current regex will fail to parse git tags that have a v prefix (e.g., v1.2.3), which is a common convention. To make the version parsing more robust, the regex should be updated to optionally match the v prefix.

string(REGEX MATCH "^v?([0-9]+)\\.([0-9]+)\\.([0-9]+)" _ver_match "${SR_VERSION}")

CMakeLists.txt Outdated
Comment on lines +9 to +18
execute_process(
COMMAND git describe --tags --always
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE SR_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
if(NOT SR_VERSION)
set(SR_VERSION "0.0.0-unknown")
endif()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For better robustness, it's recommended to use find_package(Git) to locate the git executable and ensure the project is within a Git repository before attempting to run git commands. This avoids potential errors or unexpected behavior when building from a source distribution that doesn't include the .git directory or on systems where git is not in the default PATH.

find_package(Git QUIET)
if(GIT_FOUND AND EXISTS "${CMAKE_SOURCE_DIR}/.git")
    execute_process(
        COMMAND ${GIT_EXECUTABLE} describe --tags --always
        WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
        OUTPUT_VARIABLE SR_VERSION
        OUTPUT_STRIP_TRAILING_WHITESPACE
        ERROR_QUIET
    )
endif()

if(NOT SR_VERSION)
    set(SR_VERSION "0.0.0-unknown")
endif()

@gemini-code-assist
Copy link

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces an automated versioning system for the soft_render project. By leveraging Git tags, the build process now dynamically determines and embeds the project's version, eliminating the need for manual updates. This enhancement streamlines release management and ensures that compiled binaries accurately reflect their source control state.

Highlights

  • Dynamic Versioning: The project's version is now automatically determined from Git tags using git describe --tags --always, replacing the previously hardcoded version in CMakeLists.txt.
  • Version Header Generation: A new version.hpp.in template is configured at build time, generating build/generated/soft_render/version.hpp with version macros.
  • Demo Application Update: The demo application now prints the dynamically generated version string on startup, showcasing the new versioning system.
Changelog
  • CMakeLists.txt
    • Removed the hardcoded VERSION 1.0.0 from the project command.
    • Added execute_process to run git describe --tags --always to fetch the current version from Git.
    • Implemented logic to parse the Git version string into major, minor, and patch components, defaulting to "0.0.0-unknown" if no Git version is found.
    • Added a message(STATUS) call to display the determined version during CMake configuration.
    • Configured include/soft_render/version.hpp.in to generate build/generated/soft_render/version.hpp.
    • Added ${CMAKE_BINARY_DIR}/generated to the public include directories for the soft_render library.
  • examples/demo/main.cpp
    • Included the newly generated soft_render/version.hpp header.
    • Added a std::printf statement to output the SR_VERSION macro on application startup.
  • include/soft_render/version.hpp.in
    • Created a new template file defining SR_VERSION, SR_VERSION_MAJOR, SR_VERSION_MINOR, and SR_VERSION_PATCH macros, which will be replaced by CMake during configuration.
Activity
  • The author reported that the local build passes.
  • All 3 CTest targets pass.
  • The demo application prints the correct version string.
  • CI passing on this PR is a pending item.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Use find_package(Git) with .git dir check for robustness in non-git
builds. Handle v-prefixed tags (e.g. v1.2.3) in version regex.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@EricBorges2019
Copy link
Owner Author

/gemini review

@EricBorges2019 EricBorges2019 merged commit fba1ae3 into main Mar 10, 2026
1 check passed
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a great feature to automatically generate the version number from git tags. The implementation is clean and follows a common pattern for CMake projects. I've made one suggestion in CMakeLists.txt to improve the robustness of the git command execution by using find_package(Git). Otherwise, the changes look good and are well-implemented.

Comment on lines +9 to +18
find_package(Git QUIET)
if(GIT_FOUND AND EXISTS "${CMAKE_SOURCE_DIR}/.git")
execute_process(
COMMAND ${GIT_EXECUTABLE} describe --tags --always
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE SR_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
endif()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For better robustness and portability, it's good practice to use CMake's find_package to locate Git before attempting to execute it. This ensures that the build system doesn't fail unexpectedly in environments where git is not in the default PATH, and uses the executable found by CMake's search logic.

find_package(Git QUIET)
if(Git_FOUND)
    execute_process(
        COMMAND ${GIT_EXECUTABLE} describe --tags --always
        WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
        OUTPUT_VARIABLE SR_VERSION
        OUTPUT_STRIP_TRAILING_WHITESPACE
        ERROR_QUIET
    )
endif()
if(NOT SR_VERSION)
    set(SR_VERSION "0.0.0-unknown")
endif()

@EricBorges2019 EricBorges2019 deleted the feat/auto-version branch March 10, 2026 19:11
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request effectively implements automatic version generation from Git tags, significantly improving upon hardcoded versions. The security review found no vulnerabilities. The approach using git describe and configure_file is appropriate, and the fallback to "0.0.0-unknown" along with the parsing of version components is well-considered.

Comment on lines +23 to +32
string(REGEX MATCH "^v?([0-9]+)\\.([0-9]+)\\.([0-9]+)" _ver_match "${SR_VERSION}")
if(_ver_match)
set(SR_VERSION_MAJOR ${CMAKE_MATCH_1})
set(SR_VERSION_MINOR ${CMAKE_MATCH_2})
set(SR_VERSION_PATCH ${CMAKE_MATCH_3})
else()
set(SR_VERSION_MAJOR 0)
set(SR_VERSION_MINOR 0)
set(SR_VERSION_PATCH 0)
endif()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current regex for parsing major, minor, and patch versions (^v?([0-9]+)\.([0-9]+)\.([0-9]+)) strictly requires all three components (major.minor.patch). If a Git tag is in a format like v1.0 or 1.0 (missing the patch component), the regex will not match, causing the major, minor, and patch versions to incorrectly default to 0 instead of 1, 0, 0. This can lead to misleading version information in the generated header. Consider making the minor and patch components optional in the regex to handle such cases more robustly.

string(REGEX MATCH "^v?([0-9]+)\\.?([0-9]*)\\.?([0-9]*)" _ver_match "${SR_VERSION}")
if(_ver_match)
    set(SR_VERSION_MAJOR ${CMAKE_MATCH_1})
    if(CMAKE_MATCH_2)
        set(SR_VERSION_MINOR ${CMAKE_MATCH_2})
    else()
        set(SR_VERSION_MINOR 0)
    endif()
    if(CMAKE_MATCH_3)
        set(SR_VERSION_PATCH ${CMAKE_MATCH_3})
    else()
        set(SR_VERSION_PATCH 0)
    endif()
else()
    set(SR_VERSION_MAJOR 0)
    set(SR_VERSION_MINOR 0)
    set(SR_VERSION_PATCH 0)
endif()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant