Last active
November 18, 2019 17:01
-
-
Save depwl9992/03bfff771f972f012fb4123c5795ae11 to your computer and use it in GitHub Desktop.
Git revision generator for C/C++ (Windows Batch file)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
echo Writing to %file% | |
echo /** @file git_sha1.h > %file% | |
echo * Auto-generated git revision. Created by makegitsha1.bat which is run >> %file% | |
echo * as a pre-build event. The #define generated is then referenced >> %file% | |
echo * within the program in something like an About screen. Simply #include "git_sha1.h" >> %file% | |
echo * from another c/cpp/h/hpp file and pull GIT_SHA1 as a string variable. >> %file% | |
echo * Do note, however, that git_sha1.h MUST be ignored within .gitignore to >> %file% | |
echo * avoid recursive weirdnesses. Additionally this script should only be run >> %file% | |
echo * in a directory that has been initialized as a git repo. >> %file% | |
echo * >> %file% | |
echo * Version 1.0 (2015-04-29) - Initial creation >> %file% | |
echo * Version 1.1 (2018-04-03) - Publish gist and optimized Unavailable Hash detection. >> %file% | |
echo * Version 2.0 (2018-04-10) - Add GIT_DATE definition. >> %file% | |
echo * Version 2.1 (2019-10-11) - GIT_DATE not working. Worked with Compo on StackOverflow and got it working (https://stackoverflow.com/questions/58344937). >> %file% | |
echo * @author Daniel Powell >> %file% | |
echo * @copyright CC BY-SA >> %file% | |
echo * @date 2015-%date:~10,4% >> %file% | |
echo * @version 2.1 >> %file% | |
echo */ >> %file% | |
echo. >> %file% | |
@echo off | |
set "file=%~dp0git_sha1.h" | |
set "cmd=git rev-parse HEAD" | |
set "cmd2=git show -s --format^=%%ci HEAD" | |
set "git_sha1_orig=#define GIT_SHA1" | |
set "git_sha1=#define GIT_SHA1" | |
set "git_date_orig=#define GIT_DATE" | |
set "git_date=#define GIT_DATE" | |
for /f "delims=" %%i in ('^"%cmd%^"') do set "git_sha1=%git_sha1% "%%i"" | |
for /f "delims=" %%i in ('^"%cmd2%^"') do set "git_date=%git_date% "%%i"" | |
set git_date_temp=%git_date: =% | |
(echo #ifndef git_sha1H | |
echo #define git_sha1H | |
if not "%git_sha1%"=="%git_sha1_orig%" echo %git_sha1% | |
if "%git_sha1%"=="%git_sha1_orig%" echo %git_sha1% "Git Hash Unavailable" | |
if not "%git_date_temp%"=="%git_date_orig%" echo %git_date% | |
if "%git_date_temp%"=="%git_date_orig%" echo %git_date% "%date%_%time: =0%" | |
echo #endif)>"%file%" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Rev 2: Store date string from the most recent commit in git log and write this to GIT_DATE, for which I have uttered a primal victory scream over the nightmare that is Windows Batch.