-
Notifications
You must be signed in to change notification settings - Fork 1
/
hook-it.ps1
101 lines (94 loc) · 2.96 KB
/
hook-it.ps1
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
<#
.SYNOPSIS
Installs git flow (AVH Edition) hooks to a git repository
.DESCRIPTION
Installs git flow hooks to a git repository.
git flow AVH Edition only is suppported.
The target repository must have "git init" and "git flow init" already executed.
.PARAMETER Path
The Path to the target repository
.EXAMPLE
.\hook-it.ps1 -Path /path/to/repo
#>
[CmdletBinding(SupportsShouldProcess=$true)]
Param(
[string] $Path
)
if (! (Test-Path $Path) ) {
Write-Error "Folder $Path does not exist"
exit 1
}
if (! (Test-Path $Path/.git) ) {
Write-Error "Folder $Path is not a git repository"
exit 1
}
# 1/ make sure git flow is initialized
if ( ! (git -C $Path config gitflow.branch.master) ) {
Write-Verbose "Initializing git flow in repository $Path"
if ($PSCmdlet.ShouldProcess("gitflow", "Initialize")) {
git -C $Path flow init -d --tag v
if ( $LASTEXITCODE -ne 0 ) {
Write-Error "git flow init failed"
exit 1
}
}
}
$hooksPath=git -C $Path config gitflow.path.hooks
if ( ! $hooksPath ) {
Write-Error "git flow AVH edition is needed for this to work"
exit 1
}
# 2/ Configure git flow
if ($PSCmdlet.ShouldProcess("gitflow", "Configure Messages")) {
git -C $Path config gitflow.prefix.versiontag "v"
if ( $LASTEXITCODE -ne 0 ) {
Write-Error "git flow Prefix Version Tag configuration failed"
exit 1
}
git -C $Path config gitflow.hotfix.finish.message "Hotfix %tag%"
if ( $LASTEXITCODE -ne 0 ) {
Write-Error "git flow Hotfix Finish Message configuration failed"
exit 1
}
git -C $Path config gitflow.release.finish.message "Release %tag%"
if ( $LASTEXITCODE -ne 0 ) {
Write-Error "git flow Release Finish Message configuration failed"
exit 1
}
$value = (git -C $Path config --get gitflow.allow-master-commit)
if ( $value -eq "" ) {
git -C $Path config --bool gitflow.allow-master-commit false
if ( $LASTEXITCODE -ne 0 ) {
Write-Error "git flow Allow Master Commit configuration failed"
exit 1
}
}
$value = (git -C $Path config --get gitflow.allow-config-commit)
if ( $value -eq "" ) {
git -C $Path config --bool gitflow.allow-config-commit false
if ( $LASTEXITCODE -ne 0 ) {
Write-Error "git flow Allow Conflict Commit configuration failed"
exit 1
}
}
}
# 3/ copy hooks
if (! (Test-Path $hooksPath)) {
Write-Verbose "Creating folder $hooksPath"
New-Item -Path $hooksPath -ItemType Directory -WhatIf:$WhatIfPreference
}
$language = ""
if (Test-Path $Path/package.json) {
$language = "node"
} elseif (Test-Path $Path/go.mod) {
$language = "go"
} elseif (Test-Path $Path/pom.xml) {
$language = "java"
} else {
Write-Error "Could not determine language of repository $Path"
exit 1
}
Write-Verbose "$hooksPath is a $language repository"
Write-Verbose "Copying hooks to folder $hooksPath"
Copy-Item -Path ./hooks/common/* -Destination $hooksPath -Force -WhatIf:$WhatIfPreference
Copy-Item -Path ./hooks/$language/* -Destination $hooksPath -Force -WhatIf:$WhatIfPreference