2 rem this gets the current branch from either the branchname (if we attached) or
3 rem by using scientific branch fetching algorithms [tm] git is in detached HEAD state
4 rem result will be in env var %BRANCH%
7 :: detect detached head
8 git symbolic-ref HEAD >nul 2>&1
12 rem find the branchname - if current branch is a pr we have to take this into account aswell
13 rem (would be refs/pulls/pr/number/head then)
14 rem normal branch would be refs/heads/branchname
16 FOR /f "tokens=3,4 delims=/" %%a IN ('git symbolic-ref HEAD') DO (
26 :: when building with jenkins there's no branch. First git command gets the branch even there
27 :: it ignores all branches in the pr/ scope (pull requests) and finds the first non pr branch
28 :: (this mimics what the linux side does via sed and head -n1
29 :: but is empty in a normal build environment. Second git command gets the branch there.
30 FOR /f "tokens=2,3 delims=/" %%a IN ('git branch -r --contains HEAD') DO (
31 :: ignore pull requests
33 rem our branch could be like origin/Frodo (we need %%a here)
34 rem or our branch could be like origin/HEAD -> origin/master (we need %%b here)
35 rem if we have %%b - use it - else use %%a
37 :: we found the first non-pullrequest branch - gotcha
46 FOR /f "tokens=* delims= " %%a IN ('git rev-parse --abbrev-ref HEAD') DO SET BRANCH=%%a