-
-
Notifications
You must be signed in to change notification settings - Fork 803
/
Shared.ps1
165 lines (140 loc) · 5.16 KB
/
Shared.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# Define these variables since they are not defined in WinPS 5.x
if ($PSVersionTable.PSVersion.Major -lt 6) {
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '')]
$IsWindows = $true
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '')]
$IsLinux = $false
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '')]
$IsMacOS = $false
}
$modulePath = Convert-Path $PSScriptRoot\..\src
$moduleManifestPath = "$modulePath\posh-git.psd1"
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '')]
$csi = [char]0x1b + "["
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '')]
$expectedEncoding = if ($PSVersionTable.PSVersion.Major -le 5) { "utf8" } else { "ascii" }
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssigments', '')]
$originalTitle = $Host.UI.RawUI.WindowTitle
if (!(Get-Variable -Name gitbin -Scope global -ErrorAction SilentlyContinue)) {
if (($PSVersionTable.PSVersion.Major -le 5) -or $IsWindows) {
# On Windows, we can access the git binary via git.exe
$global:gitbin = Get-Command -Name git -CommandType Application -TotalCount 1
}
else {
# On Linux/macOS, we can access the git binary via its path /usr/bin/git
$global:gitbin = (Get-Command -Name git -CommandType Application -TotalCount 1).Path
}
}
# We need this or the Git mocks don't work
# This must global in order to be accessible in posh-git module scope
function global:git {
$OFS = ' '
$cmdline = "$args"
# Write-Warning "in global git func with: $cmdline"
switch ($cmdline) {
'--version' { 'git version 2.16.2.windows.1' }
'help' { Get-Content $PSScriptRoot\git-help.txt }
default {
$res = Invoke-Expression "&$gitbin $cmdline"
$res
}
}
}
# This must global in order to be accessible in posh-git module scope
function global:Convert-NativeLineEnding([string]$content, [switch]$SplitLines) {
$tmp = $content -split "`n" | ForEach-Object { $_.TrimEnd("`r")}
if ($SplitLines) {
$tmp
}
else {
$content = $tmp -join [System.Environment]::NewLine
$content
}
}
function GetHomePath() {
if ($GitPromptSettings.DefaultPromptAbbreviateHomeDirectory) {
"~"
}
else {
$Home
}
}
function GetHomeRelPath([string]$Path) {
if (!$Path.StartsWith($Home)) {
# Path not under $Home
return $Path
}
if ($GitPromptSettings.DefaultPromptAbbreviateHomeDirectory) {
"~$($Path.Substring($Home.Length))"
}
else {
$Path
}
}
function GetGitRelPath([string]$Path) {
$gitPath = Get-GitDirectory
if (!$gitPath) {
throw "GetGitRelPath Should -be called inside a git repository"
}
# Up one level from `.git`
$gitPath = Split-Path $gitPath -Parent
if (!$Path.StartsWith($gitPath)) {
# Path not under $gitPath
return $Path
}
if ($GitPromptSettings.DefaultPromptAbbreviateGitDirectory) {
$gitName = Split-Path $gitPath -Leaf
$relPath = if ($Path -eq $gitPath) { "" } else { $Path.Substring($gitPath.Length + 1) }
"$gitName`:$relPath"
}
else {
# Otherwise, honor Home path abbreviation
GetHomeRelPath $Path
}
}
function GetMacOSAdjustedTempPath($Path) {
if (($PSVersionTable.PSVersion.Major -ge 6) -and $IsMacOS) {
# Mac OS's temp folder has a symlink in its path - /var is linked to /private/var
return "/private${Path}"
}
$Path
}
function MakeNativePath([string]$Path) {
$Path -replace '\\|/', [System.IO.Path]::DirectorySeparatorChar
}
function MakeGitPath([string]$Path) {
$Path -replace '\\', '/'
}
function NewGitTempRepo([switch]$MakeInitialCommit) {
Push-Location
$temp = [System.IO.Path]::GetTempPath()
$repoPath = Join-Path $temp ([IO.Path]::GetRandomFileName())
&$gitbin init $repoPath *>$null
Set-Location $repoPath
if ($MakeInitialCommit) {
&$gitbin config user.email "[email protected]"
&$gitbin config user.name "Spaceman Spiff"
'readme' | Out-File ./README.md -Encoding ascii
&$gitbin add ./README.md *>$null
&$gitbin commit -m "initial commit." *>$null
}
$repoPath
}
function RemoveGitTempRepo($RepoPath) {
Pop-Location
if ($repoPath -and (Test-Path $repoPath)) {
Remove-Item $repoPath -Recurse -Force
}
}
function ResetGitTempRepoWorkingDir($RepoPath, $Branch = 'master') {
Set-Location $repoPath
&$gitbin checkout -fq $Branch *>$null
&$gitbin clean -xdfq *>$null
}
Remove-Item Function:\prompt
Remove-Module posh-git -Force *>$null
# For Pester testing, enable strict mode inside the posh-git module
$env:POSHGIT_ENABLE_STRICTMODE = 1
# Force the posh-git prompt to be installed. Could be runnng on dev system where user has customized the prompt.
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')]
$module = Import-Module $moduleManifestPath -ArgumentList $true,$false -Force -PassThru