PowerShell/Scripts/configure-git.ps1

69 lines
2.7 KiB
PowerShell
Raw Normal View History

2022-08-29 11:34:30 +02:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
2022-04-26 15:08:48 +02:00
Configures Git
2021-07-13 21:10:02 +02:00
.DESCRIPTION
2022-01-29 12:47:46 +01:00
This PowerShell script configures the user settings for Git.
2021-10-16 16:50:10 +02:00
.PARAMETER FullName
Specifies the user's full name
.PARAMETER EmailAddress
Specifies the user's email address
.PARAMETER FavoriteEditor
Specifies the user's favorite text editor
2021-07-13 21:10:02 +02:00
.EXAMPLE
2021-09-24 17:19:49 +02:00
PS> ./configure-git
2021-07-13 21:10:02 +02:00
.LINK
https://github.com/fleschutz/PowerShell
2022-01-29 12:47:46 +01:00
.NOTES
2022-04-26 15:08:48 +02:00
Author: Markus Fleschutz | License: CC0
2020-12-29 15:14:21 +01:00
#>
2020-09-29 20:05:52 +02:00
2021-07-15 15:51:22 +02:00
param([string]$FullName = "", [string]$EmailAddress = "", [string]$FavoriteEditor = "")
2021-02-18 20:17:55 +01:00
2021-02-02 10:27:54 +01:00
try {
2022-04-26 15:08:48 +02:00
$StopWatch = [system.diagnostics.stopwatch]::startNew()
2022-08-29 11:34:30 +02:00
"⏳ Step 1/4 - Searching for Git executable..."
2021-09-10 08:29:14 +02:00
& git --version
2021-04-17 11:29:32 +02:00
if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" }
2021-02-02 10:27:54 +01:00
2022-08-29 11:34:30 +02:00
"⏳ Step 2/4 - Input details..."
if ($FullName -eq "") { $FullName = read-host "Enter your full name" }
if ($EmailAddress -eq "") { $EmailAddress = read-host "Enter your e-mail address"}
if ($FavoriteEditor -eq "") { $FavoriteEditor = read-host "Enter your favorite text editor (emacs,nano,vi,vim,...)" }
"⏳ Step 3/4 - Saving basic settings (name,email,editor,symlinks,etc.)..."
2021-02-13 10:26:14 +01:00
& git config --global user.name $FullName
& git config --global user.email $EmailAddress
& git config --global core.editor $FavoriteEditor
& git config --global core.autocrlf false
& git config --global core.symlinks true
2021-02-24 14:13:12 +01:00
& git config --global core.longpaths true
2021-08-23 14:32:20 +02:00
& git config --global http.sslVerify false
2021-02-13 10:26:14 +01:00
& git config --global init.defaultBranch main
2021-06-28 08:42:13 +02:00
& git config --global merge.renamelimit 99999
2021-08-23 14:32:20 +02:00
& git config --global pull.rebase false
2022-04-26 15:08:48 +02:00
if ($lastExitCode -ne "0") { throw "'git config' failed with exit code $lastExitCode" }
2022-08-29 11:34:30 +02:00
"⏳ Step 4/4 - Saving basic shortcuts (co,br,ci,st,pl,ps,mrg,chp,ls,smu)..."
& git config --global alias.co "checkout"
& git config --global alias.br "branch"
& git config --global alias.ci "commit"
& git config --global alias.st "status"
& git config --global alias.pl "pull --recurse-submodules"
& git config --global alias.ps "push"
& git config --global alias.mrg "merge --no-commit --no-ff"
& git config --global alias.chp "cherry-pick --no-commit"
& git config --global alias.ls "log -n20 --pretty=format:'%Cred%h%Creset%C(yellow)%d%Creset %s %C(bold blue)by %an%Creset %C(green)%cr%Creset' --abbrev-commit"
& git config --global alias.smu "submodule update --init"
2021-09-10 08:29:14 +02:00
if ($lastExitCode -ne "0") { throw "'git config' failed" }
2022-04-26 15:08:48 +02:00
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
2022-08-29 11:34:30 +02:00
"✔️ saved your Git configuration in $Elapsed sec, it's now:"
2022-04-26 15:08:48 +02:00
2021-02-13 10:26:14 +01:00
& git config --list
2022-04-26 15:08:48 +02:00
if ($lastExitCode -ne "0") { throw "'git config --list' failed with exit code $lastExitCode" }
2021-09-27 10:09:45 +02:00
exit 0 # success
2020-12-09 10:30:55 +01:00
} catch {
2022-04-26 15:08:48 +02:00
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber)): $($Error[0])"
2020-12-09 10:30:55 +01:00
exit 1
2022-04-26 15:08:48 +02:00
}