Skip to content

AlexFabre/project-version

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

52 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

project-version

A little POSIX shell script that generates a C/C++ header or a Cmake variable file containing the version information (Major Minor etc...) of a Git based repository.

The script simply parses the git describe command to extract the firmware information, and create the corresponding defines.

Compatibility

C project

The script will generate a C header file.

./project-version.sh -f h -o project/include/version.h

C++ project

The script will generate a C++ header file.

./project-version.sh -f hpp -o project/include/version.hpp

Zephyr project

The script will generate the appropriate VERSION file.

./project-version.sh -f zephyr -o cmake-project/VERSION

Cmake project

The script will generate a CMakeLists variable file.

./project-version.sh -f cmake -o cmake-project/version.txt

Requirements

  • Restricted to git based repository.
  • Git must be available from the PATH. (On windows, run the script with git bash).

Usage

Clone the repo or simply copy the project-version.sh script into your repository and let the magic happen.

All available options can be listed with option -h

$ ./project-version.sh -h
==> project-version.sh 0.7.0
A little POSIX shell script to generate
version information for your C project.
ref: https://github.com/AlexFabre/project-version
Usage:
project-version.sh <options>
    -f <output format>
        -f h : (default) Generate a C header file (.h).
        -f hpp : Generate a C++ header file (.hpp).
        -f zephyr : Generate a zephyr VERSION file.
        -f cmake : Generate a cmake variable file.
    -o <output file name>
    -t <git tag format> (default 'v') (ex. 'v' will match tags 'v0.3.1' and 'v0.2.1-beta' but not tags '1.3.4' nor 'version3.2.3')
    -h <help>
    -v <script version>
    -l <script logs> (default none)

Example of generated header for C/C++ project

/**
 * @file version.h
 * @brief This file declares the firmware revision information
 *
 * Generated with project-version.sh 0.6.0
 * A little POSIX shell script to generate
 * version information for your C project.
 * ref: https://github.com/AlexFabre/project-version
 *
 * Do not edit this file manually. Its content
 * is generated with project-version.sh script.
 */
#ifndef VERSION_H__
#define VERSION_H__

/* Project version */
#define VERSION_MAJOR                     0
#define VERSION_MINOR                     5
#define VERSION_PATCH                     0

/* Git repo info */
#define VERSION_BRANCH_NAME               "main"
#define VERSION_NB_COMMITS_SINCE_LAST_TAG 3
#define VERSION_COMMIT_SHORT_SHA          "b620737"

/* Build date time (UTC) */
#define VERSION_BUILD_DAY                 15
#define VERSION_BUILD_MONTH               11
#define VERSION_BUILD_YEAR                2024
#define VERSION_BUILD_HOUR                9

#endif /* VERSION_H__ */

Example of generated header for Zephyr project

# This file declares the firmware revision information
#
# Generated with project-version.sh 0.6.0
# A little POSIX shell script to generate
# version information for your C project.
# ref: https://github.com/AlexFabre/project-version
#
# Do not edit this file manually. Its content
# is generated with project-version.sh script.

VERSION_MAJOR = 0
VERSION_MINOR = 5
PATCHLEVEL = 0
VERSION_TWEAK = 3
EXTRAVERSION = main

Example of generated file for cmake project

# This file declares the firmware revision information
#
# Generated with project-version.sh 0.7.0
# A little POSIX shell script to generate
# version information for your C project.
# ref: https://github.com/AlexFabre/project-version
#
# Do not edit this file manually. Its content
# is generated with project-version.sh script.

# Project version
set(APP_FW_MAJOR                     0)
set(APP_FW_MINOR                     6)
set(APP_FW_PATCH                     0)
set(PROJECT_VER                      "v0.6.0")

# Git repo info
set(APP_FW_BRANCH_NAME               "feat/improve-cmake-support")
set(APP_FW_NB_COMMITS_SINCE_LAST_TAG 0)
set(APP_FW_COMMIT_SHORT_SHA          "9f31bce")

# Build date time (UTC)
set(APP_FW_BUILD_DAY                 24)
set(APP_FW_BUILD_MONTH               11)
set(APP_FW_BUILD_YEAR                2025)
set(APP_FW_BUILD_HOUR                13)

Code quality

  • Shellcheck
  • Codespell

Improvements

  • Path handling in file_path_checker function: The function currently assumes that the provided path always ends with a filename. If the input path doesn't have a filename or extension, it defaults to version.h. This could lead to incorrect behavior in some cases. Consider validating the input path and handling errors more explicitly.

About

Git based version information for your C/C++ projects

Resources

License

Stars

Watchers

Forks

Languages