From 9f065a63e8e9c134e35d4f12ecce23f24c34793c Mon Sep 17 00:00:00 2001 From: Dax T Games Date: Tue, 16 Nov 2021 09:22:17 -0500 Subject: [PATCH] Ps1 git discovery (#2438) * Fix start errors in path with () * Fix start errors in path with () * Fix start errors in path with () * Fix start errors in path with () * remove quotes from debug_output messages * remove quotes from debug_output messages * remove quotes from debug_output messages * discovery newer git version * new git path config for powershell * xxx * add isGitShim * fix no git found in path * debug * revert not ps1 changes * cleanup * final * spacing * fixes * cleanup * cleanup * timer for Powershell * fixes * fixes for git path settings * grab vendor/bin/alias.cmd from master Co-authored-by: Dax T. Games Co-authored-by: dgames --- vendor/profile.ps1 | 51 +++++++++++++-- vendor/psmodules/Cmder.ps1 | 127 +++++++++++++++++++++++++++++++++---- 2 files changed, 158 insertions(+), 20 deletions(-) diff --git a/vendor/profile.ps1 b/vendor/profile.ps1 index ad9182a..ea3df01 100644 --- a/vendor/profile.ps1 +++ b/vendor/profile.ps1 @@ -3,6 +3,7 @@ # !!! THIS FILE IS OVERWRITTEN WHEN CMDER IS UPDATED # !!! Use "%CMDER_ROOT%\config\user_profile.ps1" to add your own startup commands +$CMDER_INIT_START=$(Get-Date -UFormat %s) # Compatibility with PS major versions <= 2 if(!$PSScriptRoot) { @@ -39,13 +40,48 @@ if(-not $moduleInstallerAvailable -and -not $env:PSModulePath.Contains($CmderMod $env:PSModulePath = $env:PSModulePath.Insert(0, "$CmderModulePath;") } -try { - # Check if git is on PATH, i.e. Git already installed on system - Get-command -Name "git" -ErrorAction Stop >$null -} catch { - if (test-path "$env:CMDER_ROOT\vendor\git-for-windows") { - Configure-Git "$env:CMDER_ROOT\vendor\git-for-windows" +$gitVersionVendor = (readVersion -gitPath "$ENV:CMDER_ROOT\vendor\git-for-windows\cmd") +# write-host "GIT VENDOR: ${gitVersionVendor}" + +# Get user installed Git Version[s] and Compare with vendored if found. +foreach ($git in (get-command -ErrorAction SilentlyContinue -all 'git')) { + # write-host "GIT Path: " + $git.Path + $gitDir = Split-Path -Path $git.Path + $gitDir = isGitShim -gitPath $gitDir + $gitVersionUser = (readVersion -gitPath $gitDir) + # write-host "GIT USER: ${gitVersionUser}" + + $useGitVersion = compare_git_versions -userVersion $gitVersionUser -vendorVersion $gitVersionVendor + # write-host "Using GIT Version: ${useGitVersion}" + + # Use user installed Git + if ($gitPathUser -eq $null) { + if ($gitDir -match '\\mingw32\\bin' -or $gitDir -match '\\mingw64\\bin') { + $gitPathUser = ($gitDir.subString(0,$gitDir.Length - 12)) + } else { + $gitPathUser = ($gitDir.subString(0,$gitDir.Length - 4)) + } } + + if ($useGitVersion -eq $gitVersionUser) { + # write-host "Using GIT Dir: ${gitDir}" + $ENV:GIT_INSTALL_ROOT = $gitPathUser + $ENV:GIT_INSTALL_TYPE = 'USER' + break + } +} + +# User vendored Git. +if ($ENV:GIT_INSTALL_ROOT -eq $null -and $gitVersionVendor -ne $null) { + $ENV:GIT_INSTALL_ROOT = "$ENV:CMDER_ROOT\vendor\git-for-windows" + $ENV:GIT_INSTALL_TYPE = 'VENDOR' +} + +# write-host "GIT_INSTALL_ROOT: ${ENV:GIT_INSTALL_ROOT}" +# write-host "GIT_INSTALL_TYPE: ${ENV:GIT_INSTALL_TYPE}" + +if (-not($ENV:GIT_INSTALL_ROOT -eq $null)) { + $env:Path = Configure-Git -gitRoot "$ENV:GIT_INSTALL_ROOT" -gitType $ENV:GIT_INSTALL_TYPE -gitPathUser $gitPathUser } if ( Get-command -Name "vim" -ErrorAction silentlycontinue) { @@ -177,3 +213,6 @@ if ( $(get-command prompt).Definition -match 'PS \$\(\$executionContext.SessionS # if (!$(get-command Prompt).Options -match 'ReadOnly') {Set-Item -Path function:\prompt -Value $Prompt -Options ReadOnly} Set-Item -Path function:\prompt -Value $Prompt -Options ReadOnly } + +$CMDER_INIT_END=$(Get-Date -UFormat %s) +# write-host "Elapsed Time: $(get-Date) `($($CMDER_INIT_END - $CMDER_INIT_START) total`)" diff --git a/vendor/psmodules/Cmder.ps1 b/vendor/psmodules/Cmder.ps1 index 59eae1e..eb782e5 100644 --- a/vendor/psmodules/Cmder.ps1 +++ b/vendor/psmodules/Cmder.ps1 @@ -1,18 +1,117 @@ -function Configure-Git($GIT_INSTALL_ROOT){ - $env:Path += $(";" + $GIT_INSTALL_ROOT + "\cmd") +function readVersion($gitPath) { + $gitExecutable = "${gitPath}\git.exe" - # Add "$GIT_INSTALL_ROOT\usr\bin" to the path if exists and not done already - $GIT_INSTALL_ROOT_ESC=$GIT_INSTALL_ROOT.replace('\','\\') - if ((test-path "$GIT_INSTALL_ROOT\usr\bin") -and -not ($env:path -match "$GIT_INSTALL_ROOT_ESC\\usr\\bin")) { - $env:path = "$env:path;$GIT_INSTALL_ROOT\usr\bin" - } + if (!(test-path "$gitExecutable")) { + return $null + } - # Add "$GIT_INSTALL_ROOT\mingw[32|64]\bin" to the path if exists and not done already - if ((test-path "$GIT_INSTALL_ROOT\mingw32\bin") -and -not ($env:path -match "$GIT_INSTALL_ROOT_ESC\\mingw32\\bin")) { - $env:path = "$env:path;$GIT_INSTALL_ROOT\mingw32\bin" - } elseif ((test-path "$GIT_INSTALL_ROOT\mingw64\bin") -and -not ($env:path -match "$GIT_INSTALL_ROOT_ESC\\mingw64\\bin")) { - $env:path = "$env:path;$GIT_INSTALL_ROOT\mingw64\bin" - } + $gitVersion = (cmd /c "${gitExecutable}" --version) + + if ($gitVersion -match 'git version') { + ($trash1, $trash2, $gitVersion) = $gitVersion.split(' ', 3) + } else { + pause + return $null + } + + return $gitVersion.toString() +} + +function isGitShim($gitPath) { + # check if there's shim - and if yes follow the path + + if (test-path "${gitPath}\git.shim") { + $shim = (get-content "${gitPath}\git.shim") + ($trash, $gitPath) = $shim.replace(' ','').split('=') + + $gitPath=$gitPath.replace('\git.exe','') + } + + return $gitPath.toString() +} + +function compareVersions($userVersion, $vendorVersion) { + if (-not($userVersion -eq $null)) { + ($userMajor, $userMinor, $userPatch, $userBuild) = $userVersion.split('.', 4) + } else { + return -1 + } + + if (-not($vendorVersion -eq $null)) { + ($vendorMajor, $vendorMinor, $vendorPatch, $vendorBuild) = $vendorVersion.split('.', 4) + } else { + 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 ($userMinor -gt $vendorMinor) {return 1} + if ($userMinor -lt $vendorMinor) {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} + + return 0 +} + +function compare_git_versions($userVersion, $vendorVersion) { + $result = compareVersions -userVersion $userVersion -vendorVersion $vendorVersion + + # write-host "Compare Versions Result: ${result}" + if ($result -ge 0) { + return $userVersion + } else { + return $vendorVersion + } +} + +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 older match its path config adding paths + # in the same path positions allowing a user to configure Cmder Git path + # using locally installed Git Path Config. + if ($gitType -eq 'VENDOR') { + # If User Git is installed replace its path config with Newer Vendored Git Path + if ($gitPathUser -ne '' -and $gitPathUser -ne $null) { + write-host -foregroundcolor yellow "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) + } else { + if (!($env:Path -match [regex]::Escape("$gitRoot\cmd"))) { + # write-host "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"))) { + # write-host "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-host "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"))) { + # write-host "Adding $gitRoot\usr\bin to the path" + $newPath = "$newPath;$gitRoot\usr\bin" + } + } + + return $newPath + } + + return $env:path } function Import-Git(){ @@ -32,7 +131,7 @@ function Import-Git(){ return $true } -function checkGit($Path) { +function checkGit($Path) { if (Test-Path -Path (Join-Path $Path '.git') ) { if($env:gitLoaded -eq 'false') { $env:gitLoaded = Import-Git