Skip to content

Commit 3fa372c

Browse files
author
Philippe Elsass
committed
Add option to abbreviate based path based on the git root
1 parent 1e9587f commit 3fa372c

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/PoshGitTypes.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ class PoshGitPromptSettings {
283283
[PoshGitTextSpan]$DefaultPromptSuffix = '$(">" * ($nestedPromptLevel + 1)) '
284284

285285
[bool]$DefaultPromptAbbreviateHomeDirectory = $true
286+
[bool]$DefaultPromptAbbreviateGitDirectory = $false
286287
[bool]$DefaultPromptWriteStatusFirst = $false
287288
[bool]$DefaultPromptEnableTiming = $false
288289
[PoshGitTextSpan]$DefaultPromptTimingFormat = ' {0}ms'

src/Utils.ps1

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ function Get-PathStringComparison {
281281
function Get-PromptPath {
282282
$settings = $global:GitPromptSettings
283283
$abbrevHomeDir = $settings -and $settings.DefaultPromptAbbreviateHomeDirectory
284+
$abbrevGitDir = $settings -and $settings.DefaultPromptAbbreviateGitDirectory
284285

285286
# A UNC path has no drive so it's better to use the ProviderPath e.g. "\\server\share".
286287
# However for any path with a drive defined, it's better to use the Path property.
@@ -289,10 +290,22 @@ function Get-PromptPath {
289290
$pathInfo = $ExecutionContext.SessionState.Path.CurrentLocation
290291
$currentPath = if ($pathInfo.Drive) { $pathInfo.Path } else { $pathInfo.ProviderPath }
291292

293+
# Look up the git root
294+
if ($abbrevGitDir) {
295+
$gitPath = $(Get-GitDirectory)
296+
if ($gitPath) { $gitPath = $(Split-Path $gitPath -Parent) }
297+
}
298+
292299
$stringComparison = Get-PathStringComparison
293300

301+
# Abbreviate path by replacing beginning of path with - *iff* the path is under a git repository
302+
if ($abbrevGitDir -and $currentPath -and $gitPath -and
303+
$currentPath.StartsWith($gitPath, $stringComparison)) {
304+
$removePath = $(Split-Path $gitPath -Parent)
305+
$currentPath = "-" + $currentPath.SubString($removePath.Length)
306+
}
294307
# Abbreviate path by replacing beginning of path with ~ *iff* the path is under the user's home dir
295-
if ($abbrevHomeDir -and $currentPath -and !$currentPath.Equals($Home, $stringComparison) -and
308+
elseif ($abbrevHomeDir -and $currentPath -and !$currentPath.Equals($Home, $stringComparison) -and
296309
$currentPath.StartsWith($Home, $stringComparison)) {
297310

298311
$currentPath = "~" + $currentPath.SubString($Home.Length)

0 commit comments

Comments
 (0)