From 4c85e8b2f12842212c6f1d2dcdd2ab9e6d466827 Mon Sep 17 00:00:00 2001 From: Andrey Luzhin <62383996+xpam-net@users.noreply.github.com> Date: Fri, 29 Oct 2021 09:13:35 +0200 Subject: [PATCH 1/4] Fix a problem with an ampersand character in path If the path variable contained values with an ampersand character (such as in the case of MySQL), the string splits by this character, and tries to execute what follows as a separate command. All occurrences of the set command containing %PATH% should be wrapped in quotation marks --- vendor/lib/lib_path.cmd | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/vendor/lib/lib_path.cmd b/vendor/lib/lib_path.cmd index 725b549..3a807ad 100644 --- a/vendor/lib/lib_path.cmd +++ b/vendor/lib/lib_path.cmd @@ -74,7 +74,7 @@ exit /b set "find_query=%add_to_path%" set "find_query=%find_query:\=\\%" set "find_query=%find_query: =\ %" - set OLD_PATH=%PATH% + set "OLD_PATH=%PATH%" setlocal enabledelayedexpansion if "!found!" == "0" ( @@ -121,8 +121,8 @@ exit /b exit /b :toolong - echo %OLD_PATH%>tempfileA - echo %PATH%>tempfileB + echo "%OLD_PATH%">tempfileA + echo "%PATH%">tempfileB fc /b tempfileA tempfileB 2>nul 1>nul if errorlevel 1 ( del tempfileA & del tempfileB & goto :changed ) del tempfileA & del tempfileB From addcefdd47a9584cc78646e5b1087c31b4a27542 Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Sun, 7 Nov 2021 15:16:32 +0100 Subject: [PATCH 2/4] Speed up git status / conflict status by calling git only once. --- vendor/clink.lua | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/vendor/clink.lua b/vendor/clink.lua index f7931ad..ca8d5dc 100644 --- a/vendor/clink.lua +++ b/vendor/clink.lua @@ -339,32 +339,27 @@ local function get_svn_branch(svn_dir) end --- --- Get the status of working dir --- @return {bool} +-- Get the status and conflict status of working dir +-- @return {bool , bool } --- local function get_git_status() local file = io_popenyield("git --no-optional-locks status --porcelain 2>nul") + local conflict_found = false + local is_status = true for line in file:lines() do - file:close() - return false + local code = line:sub(1, 2) + -- print (string.format("code: %s, line: %s", code, line)) + if code == "DD" or code == "AU" or code == "UD" or code == "UA" or code == "DU" or code == "AA" or code == "UU" then + is_status = false + conflict_found = true + break + -- unversioned files are ignored, comment out 'code ~= "!!"' to unignore them + elseif code ~= "!!" and code ~= "??" then + is_status = false + end end file:close() - - return true -end - ---- --- Gets the conflict status --- @return {bool} indicating true for conflict, false for no conflicts ---- -function get_git_conflict() - local file = io_popenyield("git diff --name-only --diff-filter=U 2>nul") - for line in file:lines() do - file:close() - return true; - end - file:close() - return false + return { status = is_status, conflict = conflict_found } end @@ -404,7 +399,7 @@ end --- local function get_git_info_table() local info = clink_promptcoroutine(function () - return { status=get_git_status(), conflict=get_git_conflict() } + return get_git_status() end) if not info then info = cached_info.git_info or {} From 59dfa9c46f8ef06a3c0327042fcd139f4ff7578d Mon Sep 17 00:00:00 2001 From: Dax T Games Date: Thu, 11 Nov 2021 07:35:04 -0500 Subject: [PATCH 3/4] Revert "Fix a problem with an ampersand character in path" --- vendor/lib/lib_path.cmd | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/vendor/lib/lib_path.cmd b/vendor/lib/lib_path.cmd index 3a807ad..725b549 100644 --- a/vendor/lib/lib_path.cmd +++ b/vendor/lib/lib_path.cmd @@ -74,7 +74,7 @@ exit /b set "find_query=%add_to_path%" set "find_query=%find_query:\=\\%" set "find_query=%find_query: =\ %" - set "OLD_PATH=%PATH%" + set OLD_PATH=%PATH% setlocal enabledelayedexpansion if "!found!" == "0" ( @@ -121,8 +121,8 @@ exit /b exit /b :toolong - echo "%OLD_PATH%">tempfileA - echo "%PATH%">tempfileB + echo %OLD_PATH%>tempfileA + echo %PATH%>tempfileB fc /b tempfileA tempfileB 2>nul 1>nul if errorlevel 1 ( del tempfileA & del tempfileB & goto :changed ) del tempfileA & del tempfileB From 9f065a63e8e9c134e35d4f12ecce23f24c34793c Mon Sep 17 00:00:00 2001 From: Dax T Games Date: Tue, 16 Nov 2021 09:22:17 -0500 Subject: [PATCH 4/4] 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