-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathSetup-Git.psm1
More file actions
255 lines (220 loc) · 7.32 KB
/
Setup-Git.psm1
File metadata and controls
255 lines (220 loc) · 7.32 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
function Setup-Git {
[CmdletBinding()]
param()
if (-not (Get-Command git -ErrorAction SilentlyContinue)) {
Write-Error "Git is not installed or not in PATH"
return
}
git config --global core.eol lf
git config --global core.autocrlf input
git config --global core.editor "'$(get-editor)' -w"
git config --global mergetool.p4merge.trustexitcode false
git config --global merge.tool p4merge
git config --global diff.tool p4merge
git config --global difftool.prompt false
git config --global mergetool.prompt false
git config --global mergetool.keepbackup false
git config --global fetch.prune true
git config --global push.default current
git config --global push.autoSetupRemote true
# rebase on pull instead of merge
git config --global branch.autosetuprebase always
# unset all aliases
git config --get-regexp 'alias.*' | foreach-object { -split $_ | select-object -first 1 } | % { . git config --global --unset "$_" }
git config --global alias.alias 'config --get-regexp \"alias.*\"'
git config --global alias.co 'checkout'
git config --global alias.cb 'checkout -b'
git config --global alias.ci 'commit -m'
git config --global alias.ca '!git add -A . && git status -s && git commit -m'
git config --global alias.s 'status -s'
# Branching Aliases
git config --global alias.br branch
git config --global alias.ct 'checkout --track origin/$1'
git config --global alias.db '!f(){ cmd=\"git branch -D $1; git push --delete origin $1\"; echo $cmd; $cmd; }; f'
git config --global alias.dlb 'branch -D $1'
git config --global alias.drb 'push --delete origin $1'
git config --global alias.track '!f(){ branch=$(git name-rev --name-only HEAD); cmd=\"git branch --track $branch ${1:-origin}/${2:-$branch}\"; echo $cmd; $cmd; }; f'
git config --global alias.unstage 'reset .'
git config --global alias.aa "!git add -A . && git status -s"
git config --global alias.pushall '!git push --all; git push --tags'
git config --global alias.ls '!git --no-pager log -10 --date=short --pretty=tformat:\"%C(yellow)%h%Creset - %C(yellow)%an%Creset %C(white)%ad%Creset%C(yellow)%d%Creset %Cgreen%s%Creset\"'
git config --global alias.ll '!git log --date=short --pretty=tformat:\"%C(yellow)%h%Creset - %C(yellow)%an%Creset %C(white)%ad%Creset%C(yellow)%d%Creset %Cgreen%s%Creset\"'
git config --global alias.lg 'log --graph --abbrev-commit --date=relative --pretty=format:\"%C(yellow)%h%Creset - %C(yellow)%an%Creset%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset\"'
# show files in commit
git config --global alias.lf 'show --pretty="format:" --name-only'
git config --global alias.wtf 'reflog'
git config --global alias.rs 'remote show origin'
$username = invoke-expression "git config --get user.name"
if($username -eq "") {
$username = "Not Set"
}
$username = Read-Host "Enter Fullname($username)"
if($username) {
git config --global user.name "'$username'"
}
$email = invoke-expression "git config --get user.email"
if($email -eq "") {
$email = "Not Set"
}
$email = Read-Host "Enter Email($email)"
if($email) {
git config --global user.email "'$email'"
}
if (Get-Command -CommandType Cmdlet Get-Editor -errorAction SilentlyContinue) {
$editor = Get-Editor
if($editor -ne $null) {
git config --global core.editor "'$editor' -multiInst -notabbar -nosession -noPlugin"
}
}
}
###########################
#
# Check-RemoteRepository
# Checks if the local working head needs to be updated with a pull
#
###########################
function Check-RemoteRepository {
[CmdletBinding()]
param(
[string]$Path = $(pwd)
)
if (-not (Test-Path $Path)) {
Write-Error "Path does not exist: $Path"
return
}
pushd $Path
try {
if(Test-GitRepository) {
if((git diff-index --name-only HEAD).length -gt 0) {
Write-Warning "Local Powershell profile repository has uncommited changes"
}
$remote=$(git ls-remote origin HEAD | %{ ($_ -split "[\s]")[0] })
$local=$(git rev-parse HEAD)
Write-Verbose "Local: $local => Remote: $remote"
if($remote -ne $local) {
if(git branch --contains $remote) {
Write-Warning "Local Powershell profile repository has changes that have not been pushed upstream"
}
else {
Write-Warning "Remote Powershell profile repository has changes that are not present locally"
}
}
else {
Write-Verbose "Local Powershell profile repository is up to date"
}
}
}
finally {
popd
}
}
# Git functions
# Mark Embling (http://www.markembling.info/)
# See http://gist.github.com/180853 for gitutils.ps1.
###########################
#
# Test-GitDirectory
# Is the current directory a git repository/working copy?
#
###########################
function Test-GitRepository {
Get-GitDirectory -ne $null;
}
###########################
#
# Get-GitDirectory
#
###########################
function Get-GitDirectory {
Get-LocalOrParentPath .git
}
###########################
#
# Enable-GitColors
#
###########################
function Enable-GitColors {
$env:TERM = 'cygwin'
}
###########################
#
# Get-GitAliasPattern
#
###########################
function Get-GitAliasPattern {
$aliases = @('git') + (Get-Alias | where {$_.definition -eq 'git' } | select -Exp Name) -join '|'
"(" + $aliases + ")"
}
###########################
#
# Get-GitBranch
#
###########################
function Get-GitBranch($gitDir = $(Get-GitDirectory), [Diagnostics.Stopwatch]$sw) {
if ($gitDir) {
dbg 'Finding branch' $sw
dbg "$gitDir"
$r = ''; $b = ''; $c = ''
if (Test-Path $gitDir\rebase-merge\interactive) {
dbg 'Found rebase-merge\interactive' $sw
$r = '|REBASE-i'
$b = "$(Get-Content $gitDir\rebase-merge\head-name)"
} elseif (Test-Path $gitDir\rebase-merge) {
dbg 'Found rebase-merge' $sw
$r = '|REBASE-m'
$b = "$(Get-Content $gitDir\rebase-merge\head-name)"
} else {
if (Test-Path $gitDir\rebase-apply) {
dbg 'Found rebase-apply' $sw
if (Test-Path $gitDir\rebase-apply\rebasing) {
dbg 'Found rebase-apply\rebasing' $sw
$r = '|REBASE'
} elseif (Test-Path $gitDir\rebase-apply\applying) {
dbg 'Found rebase-apply\applying' $sw
$r = '|AM'
} else {
dbg 'Found rebase-apply' $sw
$r = '|AM/REBASE'
}
} elseif (Test-Path $gitDir\MERGE_HEAD) {
dbg 'Found MERGE_HEAD' $sw
$r = '|MERGING'
} elseif (Test-Path $gitDir\BISECT_LOG) {
dbg 'Found BISECT_LOG' $sw
$r = '|BISECTING'
}
dbg 'Falling back on parsing HEAD' $sw
$ref = Get-Content $gitDir\HEAD 2>$null
dbg "Head => $ref"
if ($ref -match 'ref: (?<ref>.+)') {
$b = $Matches['ref']
} elseif ($ref -and $ref.Length -ge 7) {
$b = $ref.Substring(0,7)+'...'
} else {
$b = 'unknown'
}
}
if ('true' -eq $(git rev-parse --is-inside-git-dir 2>$null)) {
dbg 'Inside git directory' $sw
if ('true' -eq $(git rev-parse --is-bare-repository 2>$null)) {
$c = 'BARE:'
} else {
$b = 'GIT_DIR!'
}
}
"$c$($b -replace 'refs/heads/','')$r"
}
}
function Get-GitAliases {
$aliases = (git config --get-regexp 'alias.*' | foreach-object {
$array = -split ($_ -replace 'alias\.');
$output = New-Object PsObject;
Add-Member -InputObject $output NoteProperty Alias ($array[0]);
Add-Member -InputObject $output NoteProperty Command ([string]::join(" ", $array[1..($array.length-1)])); return $output
})
return $aliases
}
function Display-GitAliases {
Get-GitAliases | ft -autosize
}
Export-ModuleMember -Function * -Alias *