Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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> "
Expand Down
57 changes: 57 additions & 0 deletions src/GitPrompt.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 '(?<remotename>\w+)(?<seperator>\/).*' ) {
$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.
Expand Down
1 change: 1 addition & 0 deletions src/PoshGitTypes.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ class PoshGitPromptSettings {

[bool]$EnableStashStatus = $false
[bool]$ShowStatusWhenZero = $true
[bool]$ShowRemoteName = $false
[bool]$AutoRefreshIndex = $true

[UntrackedFilesMode]$UntrackedFilesMode = [UntrackedFilesMode]::Default
Expand Down
1 change: 1 addition & 0 deletions src/posh-git.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ FunctionsToExport = @(
'Write-GitBranchName',
'Write-GitBranchStatus',
'Write-GitIndexStatus',
'Write-GitRemoteName',
'Write-GitStashCount',
'Write-GitWorkingDirStatus',
'Write-GitWorkingDirStatusSummary',
Expand Down
1 change: 1 addition & 0 deletions src/posh-git.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ $exportModuleMemberParams = @{
'Write-GitBranchName',
'Write-GitBranchStatus',
'Write-GitIndexStatus',
'Write-GitRemoteName',
'Write-GitStashCount',
'Write-GitWorkingDirStatus',
'Write-GitWorkingDirStatusSummary',
Expand Down
28 changes: 27 additions & 1 deletion test/Get-GitStatus.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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 = " "
Expand Down
77 changes: 77 additions & 0 deletions test/GitPrompt.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}