Skip to content

Commit 1f94d29

Browse files
committed
Add Remove-PoshGitFromProfile
1 parent 91f0196 commit 1f94d29

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

src/Utils.ps1

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,62 @@ function Get-FileEncoding($Path) {
160160
}
161161
}
162162

163+
<#
164+
.SYNOPSIS
165+
Removes posh-git from all PowerShell profile (startup) scripts.
166+
.DESCRIPTION
167+
Checks if your PowerShell profile scripts are importing posh-git
168+
and if so, removes lines related to posh-git from the script.
169+
.EXAMPLE
170+
PS C:\> Remove-PoshGitFromProfile
171+
Updates your profile script for all PowerShell hosts to not import the
172+
posh-git module when the current PowerShell host starts.
173+
.INPUTS
174+
None.
175+
.OUTPUTS
176+
None.
177+
#>
178+
function Remove-PoshGitFromProfile([switch]$WhatIf) {
179+
$underTest = $false
180+
181+
$profilePath = $null
182+
183+
# Under test, we override some variables using $args as a backdoor.
184+
if ($args.Count -gt 0) {
185+
$profilePath = [string]$args[0]
186+
$underTest = $true
187+
if ($args.Count -gt 1) {
188+
$ModuleBasePath = [string]$args[1]
189+
}
190+
}
191+
192+
@(
193+
$profilePath,
194+
$PROFILE.CurrentUserCurrentHost,
195+
$PROFILE.CurrentUserAllHosts,
196+
$PROFILE.AllUsersCurrentHost,
197+
$PROFILE.AllUsersAllHosts
198+
) | Where-Object { $_ -and (Test-PoshGitImportedInScript $_) } |
199+
ForEach-Object {
200+
$path = $_
201+
$oldProfile = @(Get-Content $path)
202+
$newProfile = New-Object System.Collections.Generic.List[string]
203+
204+
switch -Wildcard ($oldProfile) {
205+
'*PoshGitPrompt*' { if ($WhatIf) { Write-Host "What if: Removing line '$_' from '$path'." } continue; }
206+
'*Load posh-git example profile*' { if ($WhatIf) { Write-Host "What if: Removing line '$_' from '$path'." } continue; }
207+
'*posh-git*profile.example.ps1*' { if ($WhatIf) { Write-Host "What if: Removing line '$_' from '$path'." } continue; }
208+
'Import-Module *posh-git*' { if ($WhatIf) { Write-Host "What if: Removing line '$_' from '$path'." } continue; }
209+
default { $newProfile.Add($_) }
210+
}
211+
212+
if ($newProfile.Count -lt $oldProfile.Length) {
213+
$encoding = Get-FileEncoding $path
214+
Set-Content -Path $path -Value $newProfile -WhatIf:$WhatIf -Encoding $encoding
215+
}
216+
}
217+
}
218+
163219
<#
164220
.SYNOPSIS
165221
Gets a StringComparison enum value appropriate for comparing paths on the OS platform.

src/posh-git.psd1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ PowerShellVersion = '2.0'
2525
FunctionsToExport = @(
2626
'Invoke-NullCoalescing',
2727
'Add-PoshGitToProfile',
28+
'Remove-PoshGitFromProfile',
2829
'Write-GitStatus',
2930
'Write-Prompt',
3031
'Write-VcsStatus',

src/posh-git.psm1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ $exportModuleMemberParams = @{
122122
Function = @(
123123
'Invoke-NullCoalescing',
124124
'Add-PoshGitToProfile',
125+
'Remove-PoshGitFromProfile',
125126
'Write-GitStatus',
126127
'Write-Prompt',
127128
'Write-VcsStatus',

0 commit comments

Comments
 (0)