@@ -27,19 +27,36 @@ $loc = $($env:BUILD_SOURCESDIRECTORY)
27
27
$branch = $ ($env: BUILD_SOURCEBRANCH )
28
28
$commitId = $ ($env: BUILD_SOURCEVERSION )
29
29
30
- $localBranch = $branch.Trim ().Replace(" refs/heads/" , " " )
30
+ $localBranch = $branch.Trim ().Replace(" refs/heads/" , " " ).Replace( " refs/ " , " " )
31
31
32
32
Set-Location $loc
33
33
34
+ $haveLocalBranch = $false
34
35
# VSO checks out the commit as a detached HEAD
35
36
# loop through and checkout all branches and the one we want
36
- # then reset hard to get to the desired commit id
37
+ # then reset merge to get to the desired commit id
37
38
foreach ($remoteBranch in . git branch - r) {
38
- . git checkout $remoteBranch.Trim ().Replace(" origin/" , " " ) 2>&1 | write-host
39
+
40
+ $lb = $remoteBranch.Trim ().Replace(" origin/" , " " )
41
+ . git checkout $lb 2>&1 | write-host
39
42
43
+ # keep track of if we have a matching local branch
44
+ # pull requests will not show up here
45
+ if ($lb -eq $localBranch ) {
46
+ $haveLocalBranch = $true
47
+ }
40
48
}
41
- . git checkout $localBranch 2>&1 | write-host
42
- . git reset -- hard $commitId 2>&1 | write-host
49
+
50
+ # if we have a local branch, check that out and set it at the right commit
51
+ if ($haveLocalBranch ){
52
+ . git checkout $localBranch 2>&1 | write-host
53
+ . git reset -- merge $commitId 2>&1 | write-host
54
+ } else {
55
+ # try to create a local branch from this commit - likely a PR
56
+ . git checkout - b $localBranch $commitId 2>&1 | write-host
57
+ }
58
+
59
+
43
60
44
61
# Call GitVersion.exe
45
62
$gvPath = Get-PathToGitVersionExe
0 commit comments