-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgithash.bat
332 lines (286 loc) · 9.69 KB
/
githash.bat
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
@echo off
set OUT_DIR=%~1
if "%OUT_DIR%" == "" (
set OUT_DIR=.
)
@rem replace '/' with '\'
set OUT_DIR=%OUT_DIR:/=\%
@echo.
@echo ---- Make githash.h ----
call :set_git_variables
call :set_repo_and_pr_variables
call :set_ci_build_url
call :update_output_githash
exit /b 0
:set_git_variables
@rem ensure to be in the proper directory
pushd "%~dp0"
: Git enabled checking
set GIT_ENABLED=1
if not defined CMD_GIT call "%~dp0..\tools\find-tools.bat"
if not defined CMD_GIT (
set GIT_ENABLED=0
@echo NOTE: No git command
)
if not exist ..\.git (
set GIT_ENABLED=0
@echo NOTE: No .git directory
)
@rem clear variable in advance
set GIT_SHORT_COMMIT_HASH=
set GIT_COMMIT_HASH=
set GIT_REMOTE_ORIGIN_URL=
set GIT_TAG_NAME=
: Get git hash if git is enabled
if "%GIT_ENABLED%" == "1" (
for /f "usebackq" %%s in (`"%CMD_GIT%" show -s --format^=%%h`) do (
set GIT_SHORT_COMMIT_HASH=%%s
)
for /f "usebackq" %%s in (`"%CMD_GIT%" show -s --format^=%%H`) do (
set GIT_COMMIT_HASH=%%s
)
for /f "usebackq" %%s in (`"%CMD_GIT%" config --get remote.origin.url`) do (
set GIT_REMOTE_ORIGIN_URL=%%s
)
@rem get tag of 'HEAD'
@rem Ignore errors when `HEAD` is not tagged.
@rem https://superuser.com/questions/743735/suppressing-errors-from-an-embedded-command-in-a-batch-file-for-loop
for /f "usebackq" %%s in (`"%CMD_GIT%" describe --tags --contains 2^>nul`) do (
set GIT_TAG_NAME=%%s
)
) else (
set GIT_SHORT_COMMIT_HASH=
set GIT_COMMIT_HASH=
set GIT_REMOTE_ORIGIN_URL=
set GIT_TAG_NAME=
)
@rem get back to the original directory
popd
exit /b 0
:set_repo_and_pr_variables
if defined APPVEYOR_REPO_NAME (
set CI_REPO_NAME=%APPVEYOR_REPO_NAME%
) else if defined BUILD_REPOSITORY_NAME (
set CI_REPO_NAME=%BUILD_REPOSITORY_NAME%
)
if defined APPVEYOR_ACCOUNT_NAME (
set CI_ACCOUNT_NAME=%APPVEYOR_ACCOUNT_NAME%
) else if defined BUILD_DEFINITIONNAME (
set CI_ACCOUNT_NAME=%BUILD_DEFINITIONNAME%
)
@rem ----------------------------------------------------------------------------------------------------------
@rem Be aware the following rules.
@rem BUILD_BUILDID (at azure pipeline) is the counterpart of APPVEYOR_BUILD_NUMBER (at appveyor).
@rem BUILD_BUILDNUMBER (at azure pipeline) is the counterpart of APPVEYOR_BUILD_VERSION (at appveyor).
@rem ----------------------------------------------------------------------------------------------------------
@rem This is super confusing.
@rem BUILD_BUILDNUMBER (at azure pipeline) and APPVEYOR_BUILD_NUMBER (at appveyor) are different information.
@rem ----------------------------------------------------------------------------------------------------------
if defined APPVEYOR_BUILD_NUMBER (
@rem APPVEYOR_BUILD_NUMBER=1624
set CI_BUILD_NUMBER=%APPVEYOR_BUILD_NUMBER%
) else if defined BUILD_BUILDID (
@rem example BUILD_BUILDID=672
set CI_BUILD_NUMBER=%BUILD_BUILDID%
)
if defined APPVEYOR_BUILD_VERSION (
@rem APPVEYOR_BUILD_VERSION=1.0.1624
set CI_BUILD_VERSION=%APPVEYOR_BUILD_VERSION%
) else if defined BUILD_BUILDNUMBER (
@rem example BUILD_BUILDNUMBER=20200205.4
set CI_BUILD_VERSION=%BUILD_BUILDNUMBER%
)
if defined APPVEYOR_PULL_REQUEST_NUMBER (
set GITHUB_PR_NUMBER=%APPVEYOR_PULL_REQUEST_NUMBER%
) else if defined SYSTEM_PULLREQUEST_PULLREQUESTNUMBER (
set GITHUB_PR_NUMBER=%SYSTEM_PULLREQUEST_PULLREQUESTNUMBER%
)
if defined APPVEYOR_PULL_REQUEST_HEAD_COMMIT (
set GITHUB_PR_HEAD_COMMIT=%APPVEYOR_PULL_REQUEST_HEAD_COMMIT%
) else if defined SYSTEM_PULLREQUEST_SOURCECOMMITID (
set GITHUB_PR_HEAD_COMMIT=%SYSTEM_PULLREQUEST_SOURCECOMMITID%
)
if not "%GITHUB_PR_HEAD_COMMIT%" == "" (
set GITHUB_PR_HEAD_SHORT_COMMIT=%GITHUB_PR_HEAD_COMMIT:~0,8%
) else (
set GITHUB_PR_HEAD_SHORT_COMMIT=
)
if "%BUILD_REPOSITORY_PROVIDER%"=="GitHub" (
set GITHUB_ON=1
)
if "%APPVEYOR_REPO_PROVIDER%"=="gitHub" (
set GITHUB_ON=1
)
set PREFIX_GITHUB=https://github.com
if "%GITHUB_ON%" == "1" (
set "GITHUB_COMMIT_URL=%PREFIX_GITHUB%/%CI_REPO_NAME%/commit/%GIT_COMMIT_HASH%"
@rem Not Pull Request
if "%GITHUB_PR_NUMBER%" == "" (
@rem No PR
) else (
@rem PR URL
set "GITHUB_PR_HEAD_URL=%PREFIX_GITHUB%/%CI_REPO_NAME%/pull/%GITHUB_PR_NUMBER%/commits/%GITHUB_PR_HEAD_COMMIT%"
)
)
exit /b 0
:set_ci_build_url
call :set_ci_build_url_for_appveyor
call :set_ci_build_url_for_azurepipelines
exit /b 0
:set_ci_build_url_for_appveyor
if not defined APPVEYOR exit /b 0
if not defined APPVEYOR_URL exit /b 0
if not defined APPVEYOR_ACCOUNT_NAME exit /b 0
if not defined APPVEYOR_PROJECT_SLUG exit /b 0
if not defined APPVEYOR_BUILD_VERSION exit /b 0
set CI_BUILD_URL=%APPVEYOR_URL%/project/%APPVEYOR_ACCOUNT_NAME%/%APPVEYOR_PROJECT_SLUG%/build/%APPVEYOR_BUILD_VERSION%
exit /b 0
:set_ci_build_url_for_azurepipelines
if not defined SYSTEM_TEAMFOUNDATIONSERVERURI exit /b 0
if not defined SYSTEM_TEAMPROJECT exit /b 0
if not defined BUILD_BUILDID exit /b 0
set CI_BUILD_URL=%SYSTEM_TEAMFOUNDATIONSERVERURI%%SYSTEM_TEAMPROJECT%/_build/results?buildId=%BUILD_BUILDID%
exit /b 0
:update_output_githash
@rem update githash.h if necessary
set GITHASH_H=%OUT_DIR%\githash.h
set GITHASH_H_TMP=%GITHASH_H%.tmp
@rem set SKIP_CREATE_GITHASH=1 to disable creation of githash.h
@rem check if skip creation of %GITHASH_H%
set VALID_CREATE_GITHASH=1
if "%SKIP_CREATE_GITHASH%" == "1" (
set VALID_CREATE_GITHASH=0
)
if not exist "%GITHASH_H%" (
set VALID_CREATE_GITHASH=1
)
if "%VALID_CREATE_GITHASH%" == "0" (
@echo skip creation of %GITHASH_H%
exit /b 0
)
call :output_githash > "%GITHASH_H_TMP%"
fc "%GITHASH_H%" "%GITHASH_H_TMP%" 1>nul 2>&1
if not errorlevel 1 (
del "%GITHASH_H_TMP%"
@echo %GITHASH_H% was not updated.
) else (
@echo GIT_SHORT_COMMIT_HASH : %GIT_SHORT_COMMIT_HASH%
@echo GIT_COMMIT_HASH : %GIT_COMMIT_HASH%
@echo GIT_REMOTE_ORIGIN_URL : %GIT_REMOTE_ORIGIN_URL%
@echo GIT_TAG_NAME : %GIT_TAG_NAME%
@echo.
@echo CI_REPO_NAME : %CI_REPO_NAME%
@echo CI_ACCOUNT_NAME : %CI_ACCOUNT_NAME%
@echo CI_BUILD_VERSION : %CI_BUILD_VERSION%
@echo CI_BUILD_NUMBER : %CI_BUILD_NUMBER%
@echo CI_BUILD_URL : %CI_BUILD_URL%
@echo.
@echo GITHUB_COMMIT_URL : %GITHUB_COMMIT_URL%
@echo GITHUB_PR_HEAD_URL : %GITHUB_PR_HEAD_URL%
@echo GITHUB_PR_HEAD_COMMIT : %GITHUB_PR_HEAD_COMMIT%
@echo GITHUB_PR_HEAD_SHORT_COMMIT : %GITHUB_PR_HEAD_SHORT_COMMIT%
@echo.
@echo APPVEYOR_URL : %APPVEYOR_URL%
@echo APPVEYOR_PROJECT_SLUG : %APPVEYOR_PROJECT_SLUG%
if exist "%GITHASH_H%" del "%GITHASH_H%"
move /y "%GITHASH_H_TMP%" "%GITHASH_H%"
@echo %GITHASH_H% was updated.
)
exit /b 0
:output_githash
echo /*! @file */
echo #pragma once
if "%GIT_SHORT_COMMIT_HASH%" == "" (
echo // GIT_SHORT_COMMIT_HASH is not defined
) else (
echo #define GIT_SHORT_COMMIT_HASH "%GIT_SHORT_COMMIT_HASH%"
)
if "%GIT_COMMIT_HASH%" == "" (
echo // GIT_COMMIT_HASH is not defined
) else (
echo #define GIT_COMMIT_HASH "%GIT_COMMIT_HASH%"
)
if "%GIT_REMOTE_ORIGIN_URL%" == "" (
echo // GIT_REMOTE_ORIGIN_URL is not defined
) else (
echo #define GIT_REMOTE_ORIGIN_URL "%GIT_REMOTE_ORIGIN_URL%"
)
if "%GIT_TAG_NAME%" == "" (
echo // GIT_TAG_NAME is not defined
) else (
echo #define GIT_TAG_NAME "%GIT_TAG_NAME%"
)
if "%CI_REPO_NAME%" == "" (
echo // CI_REPO_NAME is not defined
) else (
echo #define CI_REPO_NAME "%CI_REPO_NAME%"
)
@rem enable 'dev version' macro which will be disabled on release branches
echo #define DEV_VERSION
if "%CI_ACCOUNT_NAME%" == "" (
echo // CI_ACCOUNT_NAME is not defined
) else (
echo #define CI_ACCOUNT_NAME "%CI_ACCOUNT_NAME%"
)
if "%CI_BUILD_VERSION%" == "" (
echo // CI_BUILD_VERSION is not defined
) else (
echo #define CI_BUILD_VERSION "%CI_BUILD_VERSION%"
)
if "%CI_BUILD_NUMBER%" == "" (
echo // CI_BUILD_NUMBER is not defined
echo // CI_BUILD_NUMBER_INT is not defined
echo // CI_BUILD_NUMBER_LABEL is not defined
) else (
echo #define CI_BUILD_NUMBER "%CI_BUILD_NUMBER%"
echo #define CI_BUILD_NUMBER_INT %CI_BUILD_NUMBER%
echo #define CI_BUILD_NUMBER_LABEL "Build %CI_BUILD_NUMBER%"
)
if "%GITHUB_PR_NUMBER%" == "" (
echo // GITHUB_PR_NUMBER is not defined
echo // GITHUB_PR_NUMBER_INT is not defined
echo // GITHUB_PR_NUMBER_LABEL is not defined
) else (
echo #define GITHUB_PR_NUMBER "%GITHUB_PR_NUMBER%"
echo #define GITHUB_PR_NUMBER_INT %GITHUB_PR_NUMBER%
echo #define GITHUB_PR_NUMBER_LABEL "PR %GITHUB_PR_NUMBER%"
)
if "%GITHUB_COMMIT_URL%" == "" (
echo // GITHUB_COMMIT_URL is not defined
) else (
echo #define GITHUB_COMMIT_URL "%GITHUB_COMMIT_URL%"
)
if "%GITHUB_PR_HEAD_URL%" == "" (
echo // GITHUB_PR_HEAD_URL is not defined
) else (
echo #define GITHUB_PR_HEAD_URL "%GITHUB_PR_HEAD_URL%"
)
if "%GITHUB_PR_HEAD_COMMIT%" == "" (
echo // GITHUB_PR_HEAD_COMMIT is not defined
) else (
echo #define GITHUB_PR_HEAD_COMMIT "%GITHUB_PR_HEAD_COMMIT%"
)
if "%GITHUB_PR_HEAD_SHORT_COMMIT%" == "" (
echo // GITHUB_PR_HEAD_SHORT_COMMIT is not defined
) else (
echo #define GITHUB_PR_HEAD_SHORT_COMMIT "%GITHUB_PR_HEAD_SHORT_COMMIT%"
)
if "%CI_BUILD_URL%" == "" (
echo // CI_BUILD_URL is not defined
) else (
echo #define CI_BUILD_URL "%CI_BUILD_URL%"
)
echo // APPVEYOR specific variables
if "%APPVEYOR_URL%" == "" (
echo // APPVEYOR_URL is not defined
) else (
echo #define APPVEYOR_URL "%APPVEYOR_URL%"
)
if "%APPVEYOR_PROJECT_SLUG%" == "" (
echo // APPVEYOR_PROJECT_SLUG is not defined
) else (
echo #define APPVEYOR_PROJECT_SLUG "%APPVEYOR_PROJECT_SLUG%"
)
echo // APPVEYOR specific variables end
echo //
exit /b 0