Merge pull request #4896 from sportica/mac_fix_space
[vuplus_xbmc] / project / Win32BuildSetup / getbranch.bat
1 @echo off
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%
5 SET BRANCH=na
6 SET DETACHED=1
7 :: detect detached head
8 git symbolic-ref HEAD >nul 2>&1
9 IF %ERRORLEVEL%==0 (
10   SET DETACHED=0
11 )
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
15 IF %DETACHED%==0 (
16   FOR /f "tokens=3,4 delims=/" %%a IN ('git symbolic-ref HEAD') DO (
17     IF %%a==pr (
18       SET BRANCH=PR%%b
19     ) ELSE (
20       SET BRANCH=%%a
21     )
22   )
23   GOTO branchfound
24 )
25
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
32   IF NOT %%a==pr (
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
36     IF NOT "%%b"=="" (
37       :: we found the first non-pullrequest branch - gotcha
38       SET BRANCH=%%b
39     ) ELSE (
40       SET BRANCH=%%a
41     )
42     GOTO branchfound
43   )
44 )
45 IF "%BRANCH%"=="na" (
46   FOR /f "tokens=* delims= " %%a IN ('git rev-parse --abbrev-ref HEAD') DO SET BRANCH=%%a
47 )
48 :branchfound