diff --git a/README.md b/README.md index d368d9614..c3a24160e 100644 --- a/README.md +++ b/README.md @@ -354,6 +354,16 @@ This will change the prompt to: ![~\GitHub\posh-git [main ≡] > ][prompt-two-line] +If you prefer to show the remote repository name for the branch, use this setting: + +```text +$GitPromptSettings.ShowRemoteName = $true +``` + +This will change the prompt to: + +![~\GitHub\posh-git [origin/master ≡]> ][prompt-show-remote-name] + You can swap the order of the path and the Git status summary with the following setting: ```text @@ -522,6 +532,7 @@ function prompt { [prompt-no-abbr]: https://github.com/dahlbyk/posh-git/wiki/images/PromptNoAbbrevHome.png "C:\Users\Keith\GitHub\posh-git [main ≡]> " [prompt-path]: https://github.com/dahlbyk/posh-git/wiki/images/PromptOrangePath.png "~\GitHub\posh-git [main ≡]> " [prompt-swap]: https://github.com/dahlbyk/posh-git/wiki/images/PromptStatusFirst.png "[main ≡] ~\GitHub\posh-git> " +[prompt-show-remote-name]: https://github.com/dahlbyk/posh-git/wiki/images/PromptShowRemoteName.png "~\GitHub\posh-git> [origin/main ≡] " [prompt-two-line]: https://github.com/dahlbyk/posh-git/wiki/images/PromptTwoLine.png "~\GitHub\posh-git [main ≡] > " [prompt-custom]: https://github.com/dahlbyk/posh-git/wiki/images/PromptCustom.png "[main ≡] ~\GitHub\posh-git 02-18 14:04:35 38> " [prompt-custom-wpathdelim]: https://github.com/dahlbyk/posh-git/wiki/images/PromptCustomDelim.png "[main ≡] {~\GitHub\posh-git} 02-18 14:04:35 38> " diff --git a/src/GitPrompt.ps1 b/src/GitPrompt.ps1 index 2a752b93c..5b9345f1c 100644 --- a/src/GitPrompt.ps1 +++ b/src/GitPrompt.ps1 @@ -220,6 +220,7 @@ function Write-GitStatus { } $sb | Write-Prompt $s.BeforeStatus > $null + $sb | Write-GitRemoteName $Status > $null $sb | Write-GitBranchName $Status -NoLeadingSpace > $null $sb | Write-GitBranchStatus $Status > $null @@ -350,6 +351,62 @@ function Get-GitBranchStatusColor { $branchStatusTextSpan } +<# +.SYNOPSIS + Writes the remote (Upstream) name with seperator. +.DESCRIPTION + Writes the remote name and seperator given the current Git status which can retrieved + via the Get-GitStatus command. Remote name (Upstream) can be affected by the + $GitPromptSettings: ShowRemoteName. +.EXAMPLE + PS C:\> Write-GitRemoteName (Get-GitStatus) + + Writes the name of the remote name for branch, followed by seperator ('/') or empty line + when no Upstream is set. +.INPUTS + System.Management.Automation.PSCustomObject + This is PSCustomObject returned by Get-GitStatus +.OUTPUTS + System.String, System.Text.StringBuilder + This command returns a System.String object unless the -StringBuilder parameter + is supplied. In this case, it returns a System.Text.StringBuilder. +#> +function Write-GitRemoteName { + param( + # The Git status object that provides the status information to be written. + # This object is retrieved via the Get-GitStatus command. + [Parameter(Position = 0)] + $Status, + + # If specified the branch name is written into the provided StringBuilder object. + [Parameter(ValueFromPipeline = $true)] + [System.Text.StringBuilder] + $StringBuilder + ) + + $s = $global:GitPromptSettings + if (!$Status -or !$s -or !$s.ShowRemoteName) { + return $(if ($StringBuilder) { $StringBuilder } else { "" }) + } + + $str = "" + + # Expecting similar format as 'origin/master' or 'origin/development/master' + if($Status.Upstream -match '(?\w+)(?\/).*' ) { + $remoteNameTextSpan = [PoshGitTextSpan]::new($s.DefaultColor) + $remoteNameTextSpan.Text = $Matches.remotename + '/' + + if ($StringBuilder) { + $StringBuilder | Write-Prompt $remoteNameTextSpan > $null + } + else { + $str = Write-Prompt $remoteNameTextSpan + } + } + + return $(if ($StringBuilder) { $StringBuilder } else { $str }) +} + <# .SYNOPSIS Writes the branch name given the current Git status. diff --git a/src/PoshGitTypes.ps1 b/src/PoshGitTypes.ps1 index bd33a7d6c..6c6dd26aa 100644 --- a/src/PoshGitTypes.ps1 +++ b/src/PoshGitTypes.ps1 @@ -266,6 +266,7 @@ class PoshGitPromptSettings { [bool]$EnableStashStatus = $false [bool]$ShowStatusWhenZero = $true + [bool]$ShowRemoteName = $false [bool]$AutoRefreshIndex = $true [UntrackedFilesMode]$UntrackedFilesMode = [UntrackedFilesMode]::Default diff --git a/src/posh-git.psd1 b/src/posh-git.psd1 index b203727c7..528f85eab 100644 --- a/src/posh-git.psd1 +++ b/src/posh-git.psd1 @@ -39,6 +39,7 @@ FunctionsToExport = @( 'Write-GitBranchName', 'Write-GitBranchStatus', 'Write-GitIndexStatus', + 'Write-GitRemoteName', 'Write-GitStashCount', 'Write-GitWorkingDirStatus', 'Write-GitWorkingDirStatusSummary', diff --git a/src/posh-git.psm1 b/src/posh-git.psm1 index 06e45159a..c68c606dc 100644 --- a/src/posh-git.psm1 +++ b/src/posh-git.psm1 @@ -177,6 +177,7 @@ $exportModuleMemberParams = @{ 'Write-GitBranchName', 'Write-GitBranchStatus', 'Write-GitIndexStatus', + 'Write-GitRemoteName', 'Write-GitStashCount', 'Write-GitWorkingDirStatus', 'Write-GitWorkingDirStatusSummary', diff --git a/test/Get-GitStatus.Tests.ps1 b/test/Get-GitStatus.Tests.ps1 index 418a55a7d..696e43435 100644 --- a/test/Get-GitStatus.Tests.ps1 +++ b/test/Get-GitStatus.Tests.ps1 @@ -36,8 +36,34 @@ Describe 'Get-GitStatus Tests' { $status.Index.Modified.Count | Should -Be 0 $status.Index.Unmerged.Count | Should -Be 0 } + It 'Returns the correct remote repository name' { + Mock -ModuleName posh-git git { + $OFS = " " + if ($args -contains 'rev-parse') { + $res = Invoke-Expression "&$gitbin $args" + return $res + } + Convert-NativeLineEnding @' +## rkeithill/more-status-tests...origin/rkeithill/more-status-tests +'@ + } - + $status = Get-GitStatus + Should -Invoke -ModuleName posh-git -CommandName git -Exactly 1 + $status.Branch | Should -Be "rkeithill/more-status-tests" + $status.Upstream | Should -Be "origin/rkeithill/more-status-tests" + $status.HasIndex | Should -Be $false + $status.HasUntracked | Should -Be $false + $status.HasWorking | Should -Be $false + $status.Working.Added.Count | Should -Be 0 + $status.Working.Deleted.Count | Should -Be 0 + $status.Working.Modified.Count | Should -Be 0 + $status.Working.Unmerged.Count | Should -Be 0 + $status.Index.Added.Count | Should -Be 0 + $status.Index.Deleted.Count | Should -Be 0 + $status.Index.Modified.Count | Should -Be 0 + $status.Index.Unmerged.Count | Should -Be 0 + } It 'Returns the correct number of added untracked working files' { Mock -ModuleName posh-git git { $OFS = " " diff --git a/test/GitPrompt.Tests.ps1 b/test/GitPrompt.Tests.ps1 index 022a19b16..60e5688e8 100644 --- a/test/GitPrompt.Tests.ps1 +++ b/test/GitPrompt.Tests.ps1 @@ -54,3 +54,80 @@ M test/Baz.Tests.ps1 } } } + +Describe 'Write-GitRemoteName Tests' { + Context 'ShowRemoteName enabled with single segment branch name' { + BeforeAll { + # Ensure these settings start out set to the default values + $global:GitPromptSettings = New-GitPromptSettings + $GitPromptSettings.ShowRemoteName = $true + + Mock -ModuleName posh-git -CommandName git { + $OFS = " " + if ($args -contains 'rev-parse') { + $res = Invoke-Expression "&$gitbin $args" + return $res + } + Convert-NativeLineEnding -SplitLines @' +## master...origin/master +'@ + } + } + + It 'Should return status containing repository name with seperator' { + $res = Write-GitRemoteName (Get-GitStatus) + Should -Invoke -ModuleName posh-git -CommandName git -Exactly 1 + $res | Should -BeExactly "origin/" + } + } + + Context 'ShowRemoteName enabled with multi segment branch name' { + BeforeAll { + # Ensure these settings start out set to the default values + $global:GitPromptSettings = New-GitPromptSettings + $GitPromptSettings.ShowRemoteName = $true + + Mock -ModuleName posh-git -CommandName git { + $OFS = " " + if ($args -contains 'rev-parse') { + $res = Invoke-Expression "&$gitbin $args" + return $res + } + Convert-NativeLineEnding -SplitLines @' +## development/master...origin/development/master +'@ + } + } + + It 'Should return status containing repository name with seperator' { + $res = Write-GitRemoteName (Get-GitStatus) + Should -Invoke -ModuleName posh-git -CommandName git -Exactly 1 + $res | Should -BeExactly "origin/" + } + } + + Context 'ShowRemoteName disabled should not output' { + BeforeAll { + # Ensure these settings start out set to the default values + $global:GitPromptSettings = New-GitPromptSettings + $GitPromptSettings.ShowRemoteName = $false + + Mock -ModuleName posh-git -CommandName git { + $OFS = " " + if ($args -contains 'rev-parse') { + $res = Invoke-Expression "&$gitbin $args" + return $res + } + Convert-NativeLineEnding -SplitLines @' +## master...origin/master +'@ + } + } + + It 'Should return status containing repository name with seperator' { + $res = Write-GitRemoteName (Get-GitStatus) + Should -Invoke -ModuleName posh-git -CommandName git -Exactly 1 + $res | Should -BeNullOrEmpty + } + } +}