-
Notifications
You must be signed in to change notification settings - Fork 9
/
Git.ps1
155 lines (128 loc) · 4.53 KB
/
Git.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
function Initialize-GitConfig
{
<#
.SYNOPSIS
Configure git before the first use; assigns name and
email for the current user and sets up some useful defaults
#>
[CmdletBinding()]
param
(
[switch] $Force
)
$gitName = git config --global user.name
if( $gitName -and (-not $Force) )
{
Write-Warning "Looks like git is already configured. If you want to overwrite git config settings anyway, use -Force switch."
return
}
# Git name and email (required)
if( $env:USERDOMAIN -eq "Redmond" )
{
# Figure out name of the current user from Active Directory
$ntAccount = New-Object Security.Principal.NTAccount($env:USERDOMAIN, $env:USERNAME)
$sid = $ntAccount.Translate([Security.Principal.SecurityIdentifier])
$ldap = [adsi] "LDAP://<SID=$sid>"
git config --global user.name $ldap.cn
git config --global user.email "$ENV:USERNAME@microsoft.com"
}
else
{
$name = Read-Host "User name"
git config --global user.name $name
$email = Read-Host "User email"
git config --global user.email $email
}
Write-Output "Git user name and email are configured"
git config --global --replace-all color.grep auto
git config --global --replace-all color.grep.filename "green"
git config --global --replace-all color.grep.linenumber "cyan"
git config --global --replace-all color.grep.match "magenta"
git config --global --replace-all color.grep.separator "black"
git config --global --replace-all grep.lineNumber true
git config --global --replace-all grep.extendedRegexp true
git config --global --replace-all color.diff.meta "yellow"
git config --global --replace-all color.diff.frag "cyan"
git config --global --replace-all color.diff.func "cyan bold"
git config --global --replace-all color.diff.commit "yellow bold"
Write-Output "Git defaults are configured"
# Aliases for the most used commands
git config --global alias.co checkout
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.br branch
git config --global alias.lg "log --graph --pretty=format:'%C(reset)%C(yellow)%h%C(reset) -%C(bold yellow)%d%C(reset) %s %C(green)(%cr) %C(cyan)<%an>%C(reset)' --abbrev-commit --date=relative -n 10"
git config --global alias.gr "grep --break --heading --line-number -iIE"
Write-Output "Git aliases are configured"
}
function Open-GitExtensions
{
<#
.SYNOPSIS
Open GitExtensions GUI frontend
By default the browse window in the current folder would be opened
.PARAMETER Args
Any arguments that should be passed to the git extensions
.PARAMETER NewEnvironment
Use new environment for the process.
This is a workaround for CoreXT that redefines the available dot net runtimes
and this messes up with the .NET 8 runtime lookup done by the latest GitExtensions
.EXAMPLE
gite commit
Open git extension commit dialog for the repo in the current folder
#>
param
(
[Parameter( Mandatory = $false )]
[string[]] $args,
[switch] $NewEnvironment
)
if( -not (Get-Command GitExtensions.exe -ea Ignore) )
{
throw "GitExtensions.exe must be discoverable via PATH environment variable"
}
$param = $args
if( -not $param ) { $param = @("browse") }
if( $NewEnvironment )
{
pwsh -nop -c "Start-Process GitExtensions.exe -UseNewEnvironment -WorkingDirectory $pwd -ArgumentList $($param -join ' ')"
}
else
{
& GitExtensions.exe $param
}
}
function Get-CommitAuthorName( [string] $commit )
{
<#
.SYNOPSIS
Get author name from a git commit
#>
git log -1 --pretty=format:'%aN' $commit
}
function Get-CommitAuthorEmail( [string] $commit )
{
<#
.SYNOPSIS
Get author email from a git commit
#>
git log -1 --pretty=format:'%aE' $commit
}
function Get-CommitAuthorDate( [string] $commit )
{
<#
.SYNOPSIS
Get author commit date from a git commit
#>
git log -1 --pretty=format:'%ai' $commit
}
function Get-CommitMessage( [string] $commit )
{
<#
.SYNOPSIS
Get commit message from a git commit
#>
git log -1 --pretty=format:'%B' $commit
}