Skip to content

Commit 884fd45

Browse files
committed
Add Get-Git-Aliases to get complete aliases list
1 parent c6d9d60 commit 884fd45

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed

GitAliases.Extras.psd1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
'Test-WorkingTreeClean',
1515
'Get-CurrentBranch',
1616
'Test-GitRefExists',
17+
'Get-Git-Aliases',
1718
'UpMerge',
1819
'UpRebase',
1920
'gapt',

GitAliases.Extras.psm1

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,116 @@ function Get-GitLongOptionCompletions {
123123
}
124124
}
125125

126+
function Format-GitAliasDefinitionSafe {
127+
param(
128+
[AllowEmptyString()]
129+
[string]$Definition
130+
)
131+
132+
if ([string]::IsNullOrWhiteSpace($Definition)) {
133+
return ''
134+
}
135+
136+
$definitionLines = $Definition.Trim() -split "`r?`n" | ForEach-Object {
137+
$rawLine = [string]$_
138+
$line = $rawLine.TrimEnd()
139+
if ($rawLine -match "^`t") {
140+
if ($line.Length -ge 1) {
141+
return $line.Substring(1)
142+
}
143+
144+
return ''
145+
}
146+
if ($rawLine -match '^ ') {
147+
if ($line.Length -ge 4) {
148+
return $line.Substring(4)
149+
}
150+
151+
return ''
152+
}
153+
154+
return $line
155+
}
156+
157+
return ($definitionLines -join "`n")
158+
}
159+
160+
function Get-GitAliasEntries {
161+
$blacklist = @(
162+
'Get-Git-CurrentBranch',
163+
'Remove-Alias',
164+
'Format-AliasDefinition',
165+
'Get-Git-Aliases',
166+
'Write-Host-Deprecated',
167+
'Format-GitAliasDefinitionSafe',
168+
'Get-GitAliasEntries'
169+
)
170+
171+
$modulePriority = @{
172+
'GitAliases.Extras' = 0
173+
'git-aliases' = 1
174+
}
175+
176+
$entries = @()
177+
$aliasNamePattern = '^g[0-9A-Za-z!]+$'
178+
foreach ($moduleName in @('GitAliases.Extras', 'git-aliases')) {
179+
$commands = Get-Command -Module $moduleName -ErrorAction SilentlyContinue |
180+
Where-Object {
181+
$_.Name -notin $blacklist -and
182+
$_.CommandType -in @('Function', 'Alias') -and
183+
$_.Name -match $aliasNamePattern
184+
}
185+
186+
foreach ($command in $commands) {
187+
$definition = switch ($command.CommandType) {
188+
'Alias' { "Alias -> $($command.Definition)" }
189+
default { Format-GitAliasDefinitionSafe -Definition $command.Definition }
190+
}
191+
192+
if ([string]::IsNullOrWhiteSpace($definition)) {
193+
continue
194+
}
195+
196+
$entries += [PSCustomObject]@{
197+
Name = $command.Name
198+
Definition = $definition
199+
ModuleName = $moduleName
200+
Priority = $modulePriority[$moduleName]
201+
}
202+
}
203+
}
204+
205+
return $entries |
206+
Sort-Object Priority, Name |
207+
Group-Object Name |
208+
ForEach-Object { $_.Group[0] } |
209+
Sort-Object Name
210+
}
211+
212+
function Get-Git-Aliases {
213+
[CmdletBinding()]
214+
param(
215+
[AllowEmptyString()]
216+
[string]$Alias
217+
)
218+
219+
$Alias = if ($null -eq $Alias) { '' } else { $Alias.Trim() }
220+
$aliases = Get-GitAliasEntries
221+
222+
if (-not ([string]::IsNullOrWhiteSpace($Alias))) {
223+
$foundAlias = $aliases | Where-Object { $_.Name -eq $Alias } | Select-Object -First 1
224+
if ($null -eq $foundAlias) {
225+
Write-Error "Alias '$Alias' not found." -ErrorAction Stop
226+
}
227+
228+
return $foundAlias.Definition
229+
}
230+
231+
return $aliases |
232+
Select-Object Name, ModuleName, Definition |
233+
Format-Table -AutoSize -Wrap
234+
}
235+
126236

127237
# --- Custom Git Command Functions ---
128238
function UpMerge {

tests/GitAliases.Extras.Integration.Tests.ps1

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,25 @@ Describe 'GitAliases.Extras module' {
9292
Get-Command Test-InGitRepo -ErrorAction Stop | Should -Not -BeNullOrEmpty
9393
Get-Command UpMerge -ErrorAction Stop | Should -Not -BeNullOrEmpty
9494
Get-Command UpRebase -ErrorAction Stop | Should -Not -BeNullOrEmpty
95+
Get-Command Get-Git-Aliases -ErrorAction Stop | Should -Not -BeNullOrEmpty
9596
Get-Command gfp -ErrorAction Stop | Should -Not -BeNullOrEmpty
9697
Get-Command gsw -ErrorAction Stop | Should -Not -BeNullOrEmpty
9798
}
9899

99100
It 'Test-InGitRepo returns a boolean value outside or inside a repository' {
100101
(Test-InGitRepo).GetType().Name | Should -Be 'Boolean'
101102
}
103+
104+
It 'Get-Git-Aliases resolves custom alias grsh' {
105+
$definition = Get-Git-Aliases grsh
106+
$definition | Should -Match 'git reset --soft HEAD~1'
107+
}
108+
109+
It 'Get-Git-Aliases lists aliases from GitAliases.Extras' {
110+
$allAliasesText = (Get-Git-Aliases | Out-String)
111+
$allAliasesText | Should -Match '(?im)^\s*grsh\s+'
112+
$allAliasesText | Should -Match '(?im)^\s*gfp\s+'
113+
}
102114
}
103115

104116
Describe 'gfp integration' {

0 commit comments

Comments
 (0)