Improved configure-git.ps1

This commit is contained in:
Markus Fleschutz 2024-09-04 12:00:13 +02:00
parent a41d122e28
commit 57bc358ec1

View File

@ -2,7 +2,7 @@
.SYNOPSIS .SYNOPSIS
Configures Git Configures Git
.DESCRIPTION .DESCRIPTION
This PowerShell script configures the Git user settings. This PowerShell script configures your Git user settings.
.PARAMETER fullName .PARAMETER fullName
Specifies the user's full name Specifies the user's full name
.PARAMETER emailAddress .PARAMETER emailAddress
@ -11,12 +11,13 @@
Specifies the user's favorite text editor Specifies the user's favorite text editor
.EXAMPLE .EXAMPLE
PS> ./configure-git.ps1 "Joe Doe" joe@doe.com vim PS> ./configure-git.ps1 "Joe Doe" joe@doe.com vim
(1/6) Searching for Git executable... git version 2.42.0.windows.1 (1/5) Searching for Git executable... git version 2.42.0.windows.1
(2/6) Query user settings... (2/5) Asking for user details...
(3/6) Saving basic settings (autocrlf,symlinks,longpaths,etc.)... (3/5) Saving basic settings (autocrlf,symlinks,longpaths,etc.)...
(4/6) Saving user settings (name,email,editor)... (4/5) Saving user settings (name,email,editor)...
(5/6) Saving user shortcuts ('git br', 'git ls', 'git st', etc.)... (5/5) Saving user shortcuts ('git br', 'git ls', 'git st', etc.)...
(6/6) Listing your current settings... Saved your Git configuration to ~/.gitconfig in 11s.
.LINK .LINK
https://github.com/fleschutz/PowerShell https://github.com/fleschutz/PowerShell
.NOTES .NOTES
@ -26,17 +27,17 @@
param([string]$fullName = "", [string]$emailAddress = "", [string]$favoriteEditor = "") param([string]$fullName = "", [string]$emailAddress = "", [string]$favoriteEditor = "")
try { try {
Write-Host "⏳ (1/6) Searching for Git executable... " -noNewline Write-Host "⏳ (1/5) Searching for Git executable... " -noNewline
& git --version & git --version
if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" } if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" }
"⏳ (2/6) Query user settings..." "⏳ (2/5) Asking for user details..."
if ($fullName -eq "") { $fullName = Read-Host "Enter your full name" } if ($fullName -eq "") { $fullName = Read-Host "Enter your full name" }
if ($emailAddress -eq "") { $emailAddress = Read-Host "Enter your e-mail address"} if ($emailAddress -eq "") { $emailAddress = Read-Host "Enter your e-mail address"}
if ($favoriteEditor -eq "") { $favoriteEditor = Read-Host "Enter your favorite text editor (atom,code,emacs,nano,notepad,subl,vi,vim,...)" } if ($favoriteEditor -eq "") { $favoriteEditor = Read-Host "Enter your favorite text editor, e.g. atom,code,emacs,nano,notepad,subl,vi,vim" }
$stopWatch = [system.diagnostics.stopwatch]::startNew() $stopWatch = [system.diagnostics.stopwatch]::startNew()
"⏳ (3/6) Saving basic settings (autocrlf,symlinks,longpaths,etc.)..." "⏳ (3/5) Saving basic settings (autocrlf,symlinks,longpaths,etc.)..."
& git config --global core.autocrlf false # don't change newlines & git config --global core.autocrlf false # don't change newlines
& git config --global core.symlinks true # enable support for symbolic link files & git config --global core.symlinks true # enable support for symbolic link files
& git config --global core.longpaths true # enable support for long file paths & git config --global core.longpaths true # enable support for long file paths
@ -46,13 +47,13 @@ try {
& git config --global fetch.parallel 0 # enable parallel fetching to improve the speed & git config --global fetch.parallel 0 # enable parallel fetching to improve the speed
if ($lastExitCode -ne "0") { throw "'git config' failed with exit code $lastExitCode" } if ($lastExitCode -ne "0") { throw "'git config' failed with exit code $lastExitCode" }
"⏳ (4/6) Saving user settings (name,email,editor)..." "⏳ (4/5) Saving user settings (name,email,editor)..."
& git config --global user.name $fullName & git config --global user.name $fullName
& git config --global user.email $emailAddress & git config --global user.email $emailAddress
& git config --global core.editor $favoriteEditor & git config --global core.editor $favoriteEditor
if ($lastExitCode -ne "0") { throw "'git config' failed with exit code $lastExitCode" } if ($lastExitCode -ne "0") { throw "'git config' failed with exit code $lastExitCode" }
"⏳ (5/6) Saving user shortcuts ('git br', 'git ls', 'git st', etc.)..." "⏳ (5/5) Saving user shortcuts ('git br', 'git ls', 'git st', etc.)..."
& git config --global alias.br "branch" & git config --global alias.br "branch"
& git config --global alias.chp "cherry-pick --no-commit" & git config --global alias.chp "cherry-pick --no-commit"
& git config --global alias.ci "commit" & git config --global alias.ci "commit"
@ -65,12 +66,8 @@ try {
& git config --global alias.st "status" & git config --global alias.st "status"
if ($lastExitCode -ne "0") { throw "'git config' failed" } if ($lastExitCode -ne "0") { throw "'git config' failed" }
"⏳ (6/6) Listing your current settings..."
& git config --list
if ($lastExitCode -ne "0") { throw "'git config --list' failed with exit code $lastExitCode" }
[int]$elapsed = $stopWatch.Elapsed.TotalSeconds [int]$elapsed = $stopWatch.Elapsed.TotalSeconds
"✔️ Saved your Git configuration in $elapsed sec" "✔️ Saved your Git configuration to ~/.gitconfig in $($elapsed)s."
exit 0 # success exit 0 # success
} catch { } catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber)): $($Error[0])" "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber)): $($Error[0])"