forked from github/VisualStudio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequire-CleanWorkTree.ps1
More file actions
57 lines (46 loc) · 1.23 KB
/
Require-CleanWorkTree.ps1
File metadata and controls
57 lines (46 loc) · 1.23 KB
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
<#
.SYNOPSIS
Ensures the working tree has no uncommitted changes
.PARAMETER Action
The action that requires a clean work tree. This will appear in error messages.
.PARAMETER WarnOnly
When true, warns rather than dies when uncommitted changes are found.
#>
[CmdletBinding()]
Param(
[ValidateNotNullOrEmpty()]
[string]
$Action
,
[switch]
$WarnOnly = $false
)
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
. $PSScriptRoot\modules.ps1 | out-null
# Based on git-sh-setup.sh:require_clean_work_tree in git.git, but changed not
# to ignore submodules.
Push-Location $rootDirectory
Run-Command -Fatal { & $git rev-parse --verify HEAD | Out-Null }
& $git update-index -q --refresh
& $git diff-files --quiet
$error = ""
if ($LastExitCode -ne 0) {
$error = "You have unstaged changes."
}
& $git diff-index --cached --quiet HEAD --
if ($LastExitCode -ne 0) {
if ($error) {
$error += " Additionally, your index contains uncommitted changes."
} else {
$error = "Your index contains uncommitted changes."
}
}
if ($error) {
if ($WarnOnly) {
Write-Warning "$error Continuing anyway."
} else {
Die 2 ("Cannot $Action" + ": $error")
}
}
Pop-Location