-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathMakeBuildVer.bat
More file actions
145 lines (120 loc) · 4.95 KB
/
MakeBuildVer.bat
File metadata and controls
145 lines (120 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
@echo off
setlocal
set TARGET_FILE=%1
if "%TARGET_FILE%"=="" GOTO usage
set GIT_TAG=%2
if "%GIT_TAG%"=="" GOTO usage
set DEFINE_PREFIX=%3
if "%DEFINE_PREFIX%"=="" GOTO usage
goto get_parts
:usage
echo Usage: MakeBuildVer [TargetFile] [GitTag] [DefinePrefix]
exit
:get_parts
rem === Get the current branch ===
git rev-parse --abbrev-ref HEAD > Temp.txt
set /p ACTIVE_BRANCH=<Temp.txt
rem === Get the current commit hash ===
git rev-parse HEAD > Temp.txt
set /p FULLHASH=<Temp.txt
rem === Get the most recent tag matching our prefix ===
if "%GIT_TAG%" == """" (
git describe --tags > Temp.txt 2>&1
) else (
git describe --tags --match "%GIT_TAG%.*" > Temp.txt 2>&1
)
set /p ACTIVE_TAG=<Temp.txt
if "%ACTIVE_TAG:~0,5%" == "fatal" goto no_tag
if "%GIT_TAG%" == """" set ACTIVE_TAG=X.%ACTIVE_TAG%
rem === Get the number of commits since the tag and remove the hash PREFIX-COMMITS-HASH ===
for /f "tokens=1,2 delims=-" %%a in ("%ACTIVE_TAG%") do set ACTIVE_TAG=%%a&set VERSION_REVISION=%%b
if "%VERSION_REVISION%" == "" set VERSION_REVISION=0
rem === Extract the major/minor/patch version from the tag (append 0s if necessary) ===
for /f "tokens=1,2,3,4 delims=." %%a in ("%ACTIVE_TAG%.0.0") do set VERSION_MAJOR=%%b&set VERSION_MINOR=%%c&set VERSION_PATCH=%%d
for /f "tokens=1,* delims=." %%a in ("%ACTIVE_TAG%") do set VERSION_TAG=%%b
goto have_parts
:no_tag
set VERSION_TAG=NO TAG
set VERSION_MAJOR=0
set VERSION_MINOR=0
set VERSION_PATCH=0
set VERSION_REVISION=0
:have_parts
set BRANCH_INFO=%ACTIVE_BRANCH%
rem === Treat develop branch like master branch for dirty detection ===
if "%ACTIVE_BRANCH%" == "develop" set ACTIVE_BRANCH=master
rem === Build the product version. If on a branch, include the branch name ===
set VERSION_PRODUCT=%VERSION_TAG%
if "%ACTIVE_BRANCH:~0,5%" == "alpha" (
set /A "PRERELEASE_VERSION_MINOR=%VERSION_MINOR%+1"
) else if "%ACTIVE_BRANCH:~0,4%" == "beta" (
set /A "PRERELEASE_VERSION_MINOR=%VERSION_MINOR%+1"
)
rem === If there are any local modifications, set branch name to "dirty" ===
git diff HEAD > Temp.txt
for /F "usebackq" %%A in ('"Temp.txt"') do set DIFF_FILE_SIZE=%%~zA
if %DIFF_FILE_SIZE% GTR 0 (
set BRANCH_INFO=%BRANCH_INFO% [modified]
if "%ACTIVE_BRANCH%" == "master" (
set ACTIVE_BRANCH=dirty
)
)
rem === If we're on master and there any local commits, set branch name to "dirty" ===
if "%ACTIVE_BRANCH%" == "master" (
rem === Get the upstream branch ===
setlocal enabledelayedexpansion
set UPSTREAM_BRANCH=origin/master
for /f "tokens=* USEBACKQ" %%a IN (`git rev-parse --abbrev-ref @{upstream}`) do set UPSTREAM_BRANCH=%%a
rem === Determine how many local commits exist ===
set AHEAD_COUNT=0
for /f "tokens=* USEBACKQ" %%a in (`git rev-list --count !UPSTREAM_BRANCH!..%ACTIVE_BRANCH%`) do set AHEAD_COUNT=%%a
if not "!AHEAD_COUNT!" == "0" (
set /A VERSION_REVISION=%VERSION_REVISION%-!AHEAD_COUNT!
set ACTIVE_BRANCH=dirty
)
setlocal
)
rem === If not on a clean master branch, capture the branch name/dirty state ===
if not "%ACTIVE_BRANCH%" == "master" (
if not "%PRERELEASE_VERSION_MINOR%" == "" (
set VERSION_PRODUCT=%VERSION_MAJOR%.%PRERELEASE_VERSION_MINOR%-%ACTIVE_BRANCH%
) else if not "%ACTIVE_BRANCH:~-6%" == "-fixes" (
set VERSION_PRODUCT=%VERSION_PRODUCT%-%ACTIVE_BRANCH%
)
)
set VERSION_FULL=%VERSION_TAG%
if not "%VERSION_REVISION%" == "0" (
setlocal enabledelayedexpansion
if "%VERSION_PATCH%" == "0" set VERSION_FULL=%VERSION_FULL%.0
set VERSION_FULL=!VERSION_FULL!.%VERSION_REVISION%
setlocal
)
if not "%ACTIVE_BRANCH%" == "master" set VERSION_FULL=%VERSION_FULL%-%ACTIVE_BRANCH%
rem === Generate a new version file ===
@echo Branch: %BRANCH_INFO% (%VERSION_TAG%)
echo #define %DEFINE_PREFIX%_VERSION "%VERSION_MAJOR%.%VERSION_MINOR%.%VERSION_PATCH%.%VERSION_REVISION%" > Temp.txt
echo #define %DEFINE_PREFIX%_VERSION_SHORT "%VERSION_TAG%" >> Temp.txt
echo #define %DEFINE_PREFIX%_VERSION_MAJOR %VERSION_MAJOR% >> Temp.txt
echo #define %DEFINE_PREFIX%_VERSION_MINOR %VERSION_MINOR% >> Temp.txt
echo #define %DEFINE_PREFIX%_VERSION_PATCH %VERSION_PATCH% >> Temp.txt
echo #define %DEFINE_PREFIX%_VERSION_REVISION %VERSION_REVISION% >> Temp.txt
echo #define %DEFINE_PREFIX%_VERSION_PRODUCT "%VERSION_PRODUCT%" >> Temp.txt
echo #define %DEFINE_PREFIX%_VERSION_FULL "%VERSION_FULL%" >> Temp.txt
echo #define %DEFINE_PREFIX%_VERSION_COMMIT_HASH "%FULLHASH%" >> Temp.txt
echo #define %DEFINE_PREFIX%_VERSION_COMMIT_HASH_SHORT "%FULLHASH:~0,8%" >> Temp.txt
rem === Update the existing file only if the new file differs (fc requires backslashes) ===
set TARGET_FILE=%TARGET_FILE:/=\%
if not exist "%TARGET_FILE%" (
rem === File doesn't exist ===
move Temp.txt "%TARGET_FILE%" > nul
) else (
fc "%TARGET_FILE%" Temp.txt > nul
if errorlevel 1 (
rem === File has changed ===
del "%TARGET_FILE%"
move Temp.txt "%TARGET_FILE%" > nul
) else (
rem === File has not changed ===
del Temp.txt
)
)