1
+ <#
2
+ . SYNOPSIS
3
+ Get current git branch.
4
+ #>
1
5
function Get-Git-CurrentBranch {
2
6
git symbolic- ref -- quiet HEAD * > $null
7
+
3
8
if ($LASTEXITCODE -eq 0 ) {
4
9
return git rev- parse -- abbrev- ref HEAD
5
10
} else {
@@ -12,3 +17,88 @@ function Remove-Alias ([string] $AliasName) {
12
17
Remove-Item Alias:$AliasName - Force 2> $null
13
18
}
14
19
}
20
+
21
+ function Format-AliasDefinition {
22
+ param (
23
+ [Parameter (Mandatory = $true )][string ] $Definition
24
+ )
25
+
26
+ $definitionLines = $Definition.Trim () -split " `n " | ForEach-Object {
27
+ $line = $_.TrimEnd ()
28
+
29
+ # Trim 1 indent
30
+ if ($_ -match " ^`t " ) {
31
+ return $line.Substring (1 )
32
+ }
33
+ elseif ($_ -match ' ^ ' ) {
34
+ return $line.Substring (4 )
35
+ }
36
+
37
+ return $line
38
+
39
+ }
40
+
41
+ return $definitionLines -join " `n "
42
+ }
43
+
44
+ <#
45
+ . SYNOPSIS
46
+ Get git aliases' definition.
47
+ . DESCRIPTION
48
+ Get definition of all git aliases or specific alias.
49
+ . EXAMPLE
50
+ PS C:\> Get-Git-Aliases
51
+ Get definition of all git aliases.
52
+ . EXAMPLE
53
+ PS C:\> Get-Git-Aliases -Alias gst
54
+ Get definition of `gst` alias.
55
+ #>
56
+ function Get-Git-Aliases ([string ] $Alias ) {
57
+ $esc = [char ] 27
58
+ $green = 32
59
+ $magenta = 35
60
+
61
+ $Alias = $Alias.Trim ()
62
+ $aliases = Get-Command - Module git- aliases
63
+
64
+ if (-not ([string ]::IsNullOrEmpty($Alias ))) {
65
+ $foundAliases = $aliases | Where-Object - Property Name - Value $Alias -EQ
66
+
67
+ if ($foundAliases -is [array ]) {
68
+ return Format-AliasDefinition ($foundAliases [0 ].Definition)
69
+ }
70
+ else {
71
+ return Format-AliasDefinition ($foundAliases.Definition )
72
+ }
73
+ }
74
+
75
+ $aliases = $aliases | ForEach-Object {
76
+ $name = $_.Name
77
+ $definition = Format-AliasDefinition ($_.Definition )
78
+ $definition = " $definition `n " # Add 1 line break for some row space
79
+
80
+ return [PSCustomObject ]@ {
81
+ Name = $name
82
+ Definition = $definition
83
+ }
84
+ }
85
+
86
+ $cols = @ (
87
+ @ {
88
+ Name = ' Name'
89
+ Expression = {
90
+ # Print alias name in green
91
+ " $esc [$ ( $green ) m$ ( $_.Name ) $esc [0m"
92
+ }
93
+ },
94
+ @ {
95
+ Name = ' Definition'
96
+ Expression = {
97
+ # Print alias definition in yellow
98
+ " $esc [$ ( $magenta ) m$ ( $_.Definition ) $esc [0m"
99
+ }
100
+ }
101
+ )
102
+
103
+ return Format-Table - InputObject $aliases - AutoSize - Wrap - Property $cols
104
+ }
0 commit comments