add notes on what shim actually is

This commit is contained in:
David Refoua 2024-10-17 19:43:14 +03:30 committed by GitHub
parent 540532f126
commit e220d114b2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 185 additions and 179 deletions

View File

@ -187,6 +187,11 @@ exit /b
:::=============================================================================== :::===============================================================================
:::is_git_shim - Check if the directory has a git.shim file :::is_git_shim - Check if the directory has a git.shim file
:::. :::.
:::description:
:::.
::: Shim is a small helper program for Scoop that calls the executable configured in git.shim file
::: See: github.com/ScoopInstaller/Shim and github.com/cmderdev/cmder/pull/1905
:::.
:::include: :::include:
:::. :::.
::: call "$0" ::: call "$0"
@ -202,7 +207,7 @@ exit /b
:is_git_shim :is_git_shim
pushd "%~1" pushd "%~1"
:: check if there's shim - and if yes follow the path :: check if there is a shim file - if yes, read the actual executable path
setlocal enabledelayedexpansion setlocal enabledelayedexpansion
if exist git.shim ( if exist git.shim (
for /F "tokens=2 delims== " %%I in (git.shim) do ( for /F "tokens=2 delims== " %%I in (git.shim) do (

View File

@ -1,178 +1,179 @@
function readVersion($gitPath) { function readVersion($gitPath) {
$gitExecutable = "${gitPath}\git.exe" $gitExecutable = "${gitPath}\git.exe"
if (-not (Test-Path "$gitExecutable")) { if (-not (Test-Path "$gitExecutable")) {
return $null return $null
} }
$gitVersion = (cmd /c "${gitExecutable}" --version) $gitVersion = (cmd /c "${gitExecutable}" --version)
if ($gitVersion -match 'git version') { if ($gitVersion -match 'git version') {
($trash1, $trash2, $gitVersion) = $gitVersion.split(' ', 3) ($trash1, $trash2, $gitVersion) = $gitVersion.split(' ', 3)
} else { } else {
pause pause
return $null return $null
} }
return $gitVersion.toString() return $gitVersion.toString()
} }
function isGitShim($gitPath) { function isGitShim($gitPath) {
# check if there's shim - and if yes follow the path # check if there is a shim file - if yes, read the actual executable path
# See: github.com/ScoopInstaller/Shim
if (Test-Path "${gitPath}\git.shim") {
$shim = (get-content "${gitPath}\git.shim") if (Test-Path "${gitPath}\git.shim") {
($trash, $gitPath) = $shim.replace(' ', '').split('=') $shim = (get-content "${gitPath}\git.shim")
($trash, $gitPath) = $shim.replace(' ', '').split('=')
$gitPath = $gitPath.replace('\git.exe', '')
} $gitPath = $gitPath.replace('\git.exe', '')
}
return $gitPath.toString()
} return $gitPath.toString()
}
function compareVersions($userVersion, $vendorVersion) {
if ($null -ne $userVersion) { function compareVersions($userVersion, $vendorVersion) {
($userMajor, $userMinor, $userPatch, $userBuild) = $userVersion.split('.', 4) if ($null -ne $userVersion) {
} else { ($userMajor, $userMinor, $userPatch, $userBuild) = $userVersion.split('.', 4)
return -1 } else {
} return -1
}
if ($null -ne $vendorVersion) {
($vendorMajor, $vendorMinor, $vendorPatch, $vendorBuild) = $vendorVersion.split('.', 4) if ($null -ne $vendorVersion) {
} else { ($vendorMajor, $vendorMinor, $vendorPatch, $vendorBuild) = $vendorVersion.split('.', 4)
return 1 } else {
} return 1
}
if (($userMajor -eq $vendorMajor) -and ($userMinor -eq $vendorMinor) -and ($userPatch -eq $vendorPatch) -and ($userBuild -eq $vendorBuild)) {
return 1 if (($userMajor -eq $vendorMajor) -and ($userMinor -eq $vendorMinor) -and ($userPatch -eq $vendorPatch) -and ($userBuild -eq $vendorBuild)) {
} return 1
}
if ($userMajor -gt $vendorMajor) { return 1 }
if ($userMajor -lt $vendorMajor) { return -1 } if ($userMajor -gt $vendorMajor) { return 1 }
if ($userMajor -lt $vendorMajor) { return -1 }
if ($userMinor -gt $vendorMinor) { return 1 }
if ($userMinor -lt $vendorMinor) { return -1 } if ($userMinor -gt $vendorMinor) { return 1 }
if ($userMinor -lt $vendorMinor) { return -1 }
if ($userPatch -gt $vendorPatch) { return 1 }
if ($userPatch -lt $vendorPatch) { return -1 } if ($userPatch -gt $vendorPatch) { return 1 }
if ($userPatch -lt $vendorPatch) { return -1 }
if ($userBuild -gt $vendorBuild) { return 1 }
if ($userBuild -lt $vendorBuild) { return -1 } if ($userBuild -gt $vendorBuild) { return 1 }
if ($userBuild -lt $vendorBuild) { return -1 }
return 0
} return 0
}
function compare_git_versions($userVersion, $vendorVersion) {
$result = compareVersions -userVersion $userVersion -vendorVersion $vendorVersion function compare_git_versions($userVersion, $vendorVersion) {
$result = compareVersions -userVersion $userVersion -vendorVersion $vendorVersion
Write-Debug "Compare Versions Result: ${result}"
if ($result -ge 0) { Write-Debug "Compare Versions Result: ${result}"
return $userVersion if ($result -ge 0) {
} return $userVersion
else { }
return $vendorVersion else {
} return $vendorVersion
} }
}
function Configure-Git($gitRoot, $gitType, $gitPathUser) {
# Proposed Behavior function Configure-Git($gitRoot, $gitType, $gitPathUser) {
# Proposed Behavior
# Modify the path if we are using VENDORED Git, do nothing if using USER Git.
# If User Git is installed but is older, match its path config adding paths # Modify the path if we are using VENDORED Git, do nothing if using USER Git.
# in the same path positions allowing a user to configure Cmder Git path # If User Git is installed but is older, match its path config adding paths
# using locally installed Git Path Config. # in the same path positions allowing a user to configure Cmder Git path
if ($gitType -eq 'VENDOR') { # using locally installed Git Path Config.
# If User Git is installed replace its path config with Newer Vendored Git Path if ($gitType -eq 'VENDOR') {
if (($null -ne $gitPathUser) -and ($gitPathUser -ne '')) { # If User Git is installed replace its path config with Newer Vendored Git Path
Write-Verbose "Cmder 'profile.ps1': Replacing older user Git path '$gitPathUser' with newer vendored Git path '$gitRoot' in the system path..." if (($null -ne $gitPathUser) -and ($gitPathUser -ne '')) {
Write-Verbose "Cmder 'profile.ps1': Replacing older user Git path '$gitPathUser' with newer vendored Git path '$gitRoot' in the system path..."
$newPath = ($env:path -ireplace [regex]::Escape($gitPathUser), $gitRoot)
} $newPath = ($env:path -ireplace [regex]::Escape($gitPathUser), $gitRoot)
else { }
if (-not ($env:Path -match [regex]::Escape("$gitRoot\cmd"))) { else {
Write-Debug "Adding $gitRoot\cmd to the path" if (-not ($env:Path -match [regex]::Escape("$gitRoot\cmd"))) {
$newPath = $($gitRoot + "\cmd" + ";" + $env:Path) Write-Debug "Adding $gitRoot\cmd to the path"
} $newPath = $($gitRoot + "\cmd" + ";" + $env:Path)
}
# Add "$gitRoot\mingw[32|64]\bin" to the path if exists and not done already
if ((Test-Path "$gitRoot\mingw32\bin") -and -not ($env:path -match [regex]::Escape("$gitRoot\mingw32\bin"))) { # Add "$gitRoot\mingw[32|64]\bin" to the path if exists and not done already
Write-Debug "Adding $gitRoot\mingw32\bin to the path" if ((Test-Path "$gitRoot\mingw32\bin") -and -not ($env:path -match [regex]::Escape("$gitRoot\mingw32\bin"))) {
$newPath = "$newPath;$gitRoot\mingw32\bin" Write-Debug "Adding $gitRoot\mingw32\bin to the path"
} $newPath = "$newPath;$gitRoot\mingw32\bin"
elseif ((Test-Path "$gitRoot\mingw64\bin") -and -not ($env:path -match [regex]::Escape("$gitRoot\mingw64\bin"))) { }
Write-Debug "Adding $gitRoot\mingw64\bin to the path" elseif ((Test-Path "$gitRoot\mingw64\bin") -and -not ($env:path -match [regex]::Escape("$gitRoot\mingw64\bin"))) {
$newPath = "$newPath;$gitRoot\mingw64\bin" Write-Debug "Adding $gitRoot\mingw64\bin to the path"
} $newPath = "$newPath;$gitRoot\mingw64\bin"
}
# Add "$gitRoot\usr\bin" to the path if exists and not done already
if ((Test-Path "$gitRoot\usr\bin") -and -not ($env:path -match [regex]::Escape("$gitRoot\usr\bin"))) { # Add "$gitRoot\usr\bin" to the path if exists and not done already
Write-Debug "Adding $gitRoot\usr\bin to the path" if ((Test-Path "$gitRoot\usr\bin") -and -not ($env:path -match [regex]::Escape("$gitRoot\usr\bin"))) {
$newPath = "$newPath;$gitRoot\usr\bin" Write-Debug "Adding $gitRoot\usr\bin to the path"
} $newPath = "$newPath;$gitRoot\usr\bin"
} }
}
return $newPath
} return $newPath
}
return $env:path
} return $env:path
}
function Import-Git() {
$GitModule = Get-Module -Name Posh-Git -ListAvailable function Import-Git() {
if ($GitModule | Select-Object version | Where-Object version -le ([version]"0.6.1.20160330")) { $GitModule = Get-Module -Name Posh-Git -ListAvailable
Import-Module Posh-Git > $null if ($GitModule | Select-Object version | Where-Object version -le ([version]"0.6.1.20160330")) {
} Import-Module Posh-Git > $null
if ($GitModule | Select-Object version | Where-Object version -ge ([version]"1.0.0")) { }
Import-Module Posh-Git > $null if ($GitModule | Select-Object version | Where-Object version -ge ([version]"1.0.0")) {
$GitPromptSettings.AnsiConsole = $false Import-Module Posh-Git > $null
} $GitPromptSettings.AnsiConsole = $false
if (-not $GitModule) { }
Write-Host -NoNewline "`r`n" if (-not $GitModule) {
Write-Warning "Missing git support, install posh-git with 'Install-Module posh-git' and restart Cmder." Write-Host -NoNewline "`r`n"
Write-Host -NoNewline "`r$([char]0x1B)[A" Write-Warning "Missing git support, install posh-git with 'Install-Module posh-git' and restart Cmder."
return $false Write-Host -NoNewline "`r$([char]0x1B)[A"
} return $false
# Make sure we only run once by always returning true }
return $true # Make sure we only run once by always returning true
} return $true
}
function checkGit($Path) {
if (-not (Get-Command git -ErrorAction SilentlyContinue)) { function checkGit($Path) {
return if (-not (Get-Command git -ErrorAction SilentlyContinue)) {
} return
if (-not (Test-Path -Path (Join-Path $Path '.git'))) { }
$SplitPath = Split-Path $path if (-not (Test-Path -Path (Join-Path $Path '.git'))) {
if ($SplitPath) { checkGit($SplitPath) } $SplitPath = Split-Path $path
return if ($SplitPath) { checkGit($SplitPath) }
} return
if (getGitStatusSetting -eq $true) { }
if ($null -eq $env:gitLoaded) { if (getGitStatusSetting -eq $true) {
$env:gitLoaded = Import-Git if ($null -eq $env:gitLoaded) {
} $env:gitLoaded = Import-Git
if ($env:gitLoaded -eq $true) { }
Write-VcsStatus if ($env:gitLoaded -eq $true) {
} Write-VcsStatus
} }
else { }
$headContent = Get-Content (Join-Path $Path '.git/HEAD') else {
if ($headContent -like "ref: refs/heads/*") { $headContent = Get-Content (Join-Path $Path '.git/HEAD')
$branchName = $headContent.Substring(16) if ($headContent -like "ref: refs/heads/*") {
} $branchName = $headContent.Substring(16)
else { }
$branchName = "HEAD detached at $($headContent.Substring(0, 7))" else {
} $branchName = "HEAD detached at $($headContent.Substring(0, 7))"
Write-Host " [$branchName]" -NoNewline -ForegroundColor White }
} Write-Host " [$branchName]" -NoNewline -ForegroundColor White
} }
}
function getGitStatusSetting() {
$gitStatus = (git --no-pager config -l) | Out-String function getGitStatusSetting() {
$gitStatus = (git --no-pager config -l) | Out-String
foreach ($line in $($gitStatus -split "`r`n")) {
if (($line -match 'cmder.status=false') -or ($line -match 'cmder.psstatus=false')) { foreach ($line in $($gitStatus -split "`r`n")) {
return $false if (($line -match 'cmder.status=false') -or ($line -match 'cmder.psstatus=false')) {
} return $false
} }
}
return $true
} return $true
}