-
Notifications
You must be signed in to change notification settings - Fork 28
/
Checkout.psm1
112 lines (96 loc) · 3.3 KB
/
Checkout.psm1
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
function Initialize-Repository {
<#
.Description
The Initialize-Repository function clones and sets up your Git repository. It requires a few environment variables to work:
- SEMAPHORE_GIT_BRANCH
- SEMAPHORE_GIT_URL
- SEMAPHORE_GIT_DIR
- SEMAPHORE_GIT_SHA
if SEMAPHORE_GIT_REF_TYPE is set, a ref-based checkout will be done. If not, a shallow checkout is done.
#>
Get-Command git -ErrorAction 'SilentlyContinue' > $null
if (-not $?) {
throw "git is not available"
}
if (-not (Test-Path env:SEMAPHORE_GIT_BRANCH)) {
throw "SEMAPHORE_GIT_BRANCH is required"
}
if (-not (Test-Path env:SEMAPHORE_GIT_URL)) {
throw "SEMAPHORE_GIT_URL is required"
}
if (-not (Test-Path env:SEMAPHORE_GIT_DIR)) {
throw "SEMAPHORE_GIT_DIR is required"
}
if (-not (Test-Path env:SEMAPHORE_GIT_SHA)) {
throw "SEMAPHORE_GIT_SHA is required"
}
if (Test-Path $env:SEMAPHORE_GIT_DIR) {
Remove-Item $env:SEMAPHORE_GIT_DIR -Recurse -force
}
if (-not (Test-Path env:SEMAPHORE_GIT_DEPTH)) {
$env:SEMAPHORE_GIT_DEPTH = 50
}
switch ($env:SEMAPHORE_GIT_REF_TYPE) {
"pull-request" {
Write-Output "Initializing repository for pull-request..."
git clone --depth $env:SEMAPHORE_GIT_DEPTH $env:SEMAPHORE_GIT_URL $env:SEMAPHORE_GIT_DIR
Set-Location $env:SEMAPHORE_GIT_DIR
git fetch origin +${env:SEMAPHORE_GIT_REF}:
if (-not $?) {
throw "Revision: $env:SEMAPHORE_GIT_SHA not found"
} else {
git checkout -qf FETCH_HEAD
Write-Output "HEAD is now at $env:SEMAPHORE_GIT_SHA"
}
}
"tag" {
Write-Output "Initializing repository for tag..."
git clone --depth $env:SEMAPHORE_GIT_DEPTH -b $env:SEMAPHORE_GIT_TAG_NAME $env:SEMAPHORE_GIT_URL $env:SEMAPHORE_GIT_DIR
if (-not $?) {
throw "Release $env:SEMAPHORE_GIT_TAG_NAME not found"
} else {
Set-Location $env:SEMAPHORE_GIT_DIR
git checkout -qf $env:SEMAPHORE_GIT_TAG_NAME
Write-Output "HEAD is now at $env:SEMAPHORE_GIT_SHA Release $env:SEMAPHORE_GIT_TAG_NAME"
}
}
Default {
Initialize-ShallowRepository
}
}
}
function Initialize-ShallowRepository() {
Write-Output "Performing shallow clone with depth: $env:SEMAPHORE_GIT_DEPTH"
git clone --depth $env:SEMAPHORE_GIT_DEPTH -b $env:SEMAPHORE_GIT_BRANCH $env:SEMAPHORE_GIT_URL $env:SEMAPHORE_GIT_DIR
if (-not $?) {
Write-Output "Branch not found, performing full clone"
git clone $env:SEMAPHORE_GIT_URL $env:SEMAPHORE_GIT_DIR
Set-Location $env:SEMAPHORE_GIT_DIR
if (Test-Revision) {
git reset --hard $env:SEMAPHORE_GIT_SHA
} else {
throw "SHA: $env:SEMAPHORE_GIT_SHA not found"
}
} else {
Set-Location $env:SEMAPHORE_GIT_DIR
git reset --hard $env:SEMAPHORE_GIT_SHA
if (-not $?) {
Write-Output "SHA: $env:SEMAPHORE_GIT_SHA not found, performing full clone"
git fetch --unshallow
if (Test-Revision) {
git reset --hard $env:SEMAPHORE_GIT_SHA
} else {
throw "SHA: $env:SEMAPHORE_GIT_SHA not found"
}
}
}
}
function Test-Revision {
git rev-list HEAD..$env:SEMAPHORE_GIT_SHA
if (-not $?) {
return $false
}
return $true
}
New-Alias -name checkout -value Initialize-Repository
Export-ModuleMember -Function Initialize-Repository -Alias checkout