Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ps1_cmd_git_disc > ps1_git_disc #7

Open
wants to merge 6 commits into
base: ps1_git_disc
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions vendor/bin/cmder_sub_git_for_windows_path.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@echo off

setlocal enabledelayedexpansion
set search=%~1
set replace=%~2


echo %search% | findstr -i "git" >nul
if "%errorlevel%" == "1" exit /b

set path=!path:%search%=%replace%!
(endlocal
set "path=%path%")

exit /b
76 changes: 52 additions & 24 deletions vendor/init.bat
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ if not defined fast_init set fast_init=0
:: Use /max_depth 1-5 to set max recurse depth for calls to `enhance_path_recursive`
if not defined max_depth set max_depth=1

:: Add *nix tools to end of path. 0 turns off *nix tools, 2 adds *nix tools to the front of thr path.
:: Control *nix tools path.
:: 0 turns off *nix tools.
:: 1 Add *nix tools tho end of path.
:: 2 Adds *nix tools to the front of the path.
if not defined nix_tools set nix_tools=1

set "CMDER_USER_FLAGS= "
Expand Down Expand Up @@ -205,20 +208,36 @@ if defined GIT_INSTALL_ROOT (
:: check if git is in path...
for /F "delims=" %%F in ('where git.exe 2^>nul') do (
:: get the absolute path to the user provided git binary
%lib_git% set_user_git_path "%%~dpF"
%lib_git% is_git_shim "%%~dpF"
%lib_git% get_user_git_version
%lib_git% compare_git_versions
if ERRORLEVEL 0 (
%lib_git% compare_git_versions
) else (
:: compare the user git version against the vendored version
:: if the user provided git executable is not found
IF ERRORLEVEL -255 IF NOT ERRORLEVEL -254 (
%lib_console% verbose_output "No git at "%git_executable%" found."
set test_dir=
)
)

if defined GIT_INSTALL_ROOT (
set GIT_INSTALL_TYPE=USER
goto :FOUND_GIT
)
)
set git_executable=

REM echo Git User Path XXX: %CMDER_USER_GIT_PATH%

:: our last hope: our own git...
:VENDORED_GIT
if exist "%CMDER_ROOT%\vendor\git-for-windows" (
REM if exist "%CMDER_ROOT%\vendor\git-for-windows" (
if not defined GIT_INSTALL_ROOT if defined GIT_VERSION_VENDORED (
:VENDORED_GIT
set "GIT_INSTALL_ROOT=%CMDER_ROOT%\vendor\git-for-windows"
%lib_console% debug_output "Using vendored Git '%GIT_VERSION_VENDORED%'..."
set GIT_INSTALL_TYPE=VENDOR
%lib_console% debug_output "Newer user Git NOT found using vendored Git '%GIT_VERSION_VENDORED%'..."
goto :CONFIGURE_GIT
) else (
goto :NO_GIT
Expand All @@ -233,28 +252,37 @@ goto :CONFIGURE_GIT
goto :CONFIGURE_GIT

:CONFIGURE_GIT
%lib_console% debug_output "Using Git from '%GIT_INSTALL_ROOT%..."
:: Add git to the path
if exist "%GIT_INSTALL_ROOT%\cmd\git.exe" %lib_path% enhance_path "%GIT_INSTALL_ROOT%\cmd" ""

:: Add the unix commands at the end to not shadow windows commands like more
if %nix_tools% equ 1 (
%lib_console% verbose_output "Preferring Windows commands"
set "path_position=append"
) else (
%lib_console% verbose_output "Preferring *nix commands"
set "path_position="
)
setlocal enabledelayedexpansion
if "%GIT_INSTALL_TYPE%" equ "VENDOR" (
set "GIT_INSTALL_ROOT=%CMDER_ROOT%\vendor\git-for-windows"
if %debug_output% gtr 0 call "%CMDERR_BIN%\cmder_debug_output.cmd" "Using Git from '!GIT_INSTALL_ROOT!..."

:: Add git to the path
%lib_path% enhance_path "!GIT_INSTALL_ROOT!\cmd" ""

:: Add the unix commands at the end to not shadow windows commands like more
if %nix_tools% equ 1 (
%lib_console% verbose_output "Preferring Windows commands"
set "path_position=append"
) else (
%lib_console% verbose_output "Preferring *nix commands"
set "path_position="
)

if %nix_tools% geq 1 (
if exist "%GIT_INSTALL_ROOT%\mingw32" (
%lib_path% enhance_path "%GIT_INSTALL_ROOT%\mingw32\bin" %path_position%
) else if exist "%GIT_INSTALL_ROOT%\mingw64" (
%lib_path% enhance_path "%GIT_INSTALL_ROOT%\mingw64\bin" %path_position%
if %nix_tools% geq 1 (
if exist "!GIT_INSTALL_ROOT!\mingw32" (
%lib_path% enhance_path "!GIT_INSTALL_ROOT!\mingw32\bin" !path_position!
) else if exist "!GIT_INSTALL_ROOT!\mingw64" (
%lib_path% enhance_path "!GIT_INSTALL_ROOT!\mingw64\bin" !path_position!
)
%lib_path% enhance_path "!GIT_INSTALL_ROOT!\usr\bin" !path_position!
)
) else if defined GIT_VERSION_USER (
if %debug_output% gtr 0 call "%CMDERR_BIN%\cmder_debug_output.cmd" "Using Git from '!GIT_INSTALL_ROOT!..."

%lib_path% enhance_path "%GIT_INSTALL_ROOT%\usr\bin" %path_position%
call "%cmder_root%\vendor\bin\cmder_sub_git_for_windows_path.cmd" "%CMDER_USER_GIT_PATH%" "%GIT_INSTALL_ROOT%\"
)
endlocal & set GIT_INSTALL_ROOT=%GIT_INSTALL_ROOT% & set path=%path%

:: define SVN_SSH so we can use git svn with ssh svn repositories
if not defined SVN_SSH set "SVN_SSH=%GIT_INSTALL_ROOT:\=\\%\\bin\\ssh.exe"
Expand All @@ -266,7 +294,7 @@ if not defined git_locale if exist "%GIT_INSTALL_ROOT%\usr\bin\env.exe" set git_
if not defined git_locale for /F "tokens=* delims=" %%F in ('where env.exe 2^>nul') do ( if not defined git_locale set git_locale="%%F" /usr/bin/locale )

if defined git_locale (
%lib_console% debug_output init.bat "Env Var - git_locale=%git_locale%"
rem %lib_console% debug_output init.bat "Env Var - git_locale=%git_locale%"
if not defined LANG (
for /F "delims=" %%F in ('%git_locale% -uU 2') do (
set "LANG=%%F"
Expand Down
50 changes: 28 additions & 22 deletions vendor/lib/lib_git.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -231,31 +231,23 @@ exit /b
:::-------------------------------------------------------------------------------

:compare_git_versions
:: compare the user git version against the vendored version
%lib_git% compare_versions USER VENDORED

if exist "%test_dir:~0,-4%\cmd\git.exe" (
set "gitFolder=%test_dir:~0,-4%"
) else (
set "gitFolder=%test_dir%"
)

:: use the user provided git if its version is greater than, or equal to the vendored git
if ERRORLEVEL 0 (
:: compare the user git version against the vendored version
%lib_git% compare_versions USER VENDORED

:: use the user provided git if its version is greater than, or equal to the vendored git
if ERRORLEVEL 0 (
if exist "%test_dir:~0,-4%\cmd\git.exe" (
set "GIT_INSTALL_ROOT=%test_dir:~0,-4%"
set test_dir=
) else (
set "GIT_INSTALL_ROOT=%test_dir%"
set test_dir=
)
) else (
%lib_console% verbose_output "Found old %GIT_VERSION_USER% in %test_dir%, but not using..."
set test_dir=
)
set "GIT_INSTALL_ROOT=%gitFolder%"
) else (
:: compare the user git version against the vendored version
:: if the user provided git executable is not found
IF ERRORLEVEL -255 IF NOT ERRORLEVEL -254 (
%lib_console% verbose_output "No git at "%git_executable%" found."
set test_dir=
)
%lib_console% verbose_output "Found old %GIT_VERSION_USER% in %test_dir%, but not using..."
)
set gitFolder=
set test_dir=
exit /b

:::===============================================================================
Expand All @@ -276,3 +268,17 @@ exit /b
%lib_git% validate_version USER %GIT_VERSION_USER%
exit /b

:set_user_git_path
setlocal enabledelayedexpansion
set x=%~1
if not defined CMDER_USER_GIT_PATH set "CMDER_USER_GIT_PATH=%x:~0,-4%" && goto set_user_path_end
if defined CMDER_USER_GIT_PATH echo %x% | findstr "!CMDER_USER_GIT_PATH!" >nul
if defined CMDER_USER_GIT_PATH if ERRORLEVEL 1 (
set "CMDER_USER_GIT_PATH=%x:~0,-4%"
)

:set_user_path_end
(endlocal
set "CMDER_USER_GIT_PATH=%CMDER_USER_GIT_PATH%")

exit /b
5 changes: 3 additions & 2 deletions vendor/profile.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ foreach ($git in (get-command -ErrorAction SilentlyContinue 'git')) {
# write-host "Using GIT Version: ${useGitVersion}"

# Use user installed Git
$gitPathUser = ($gitDir.subString(0,$gitDir.Length - 4))
if ($useGitVersion -eq $gitVersionUser) {
$ENV:GIT_INSTALL_ROOT = ($gitDir.subString(0,$gitDir.Length - 4))
$ENV:GIT_INSTALL_ROOT = $gitPathUser
$ENV:GIT_INSTALL_TYPE = 'USER'
break
}
Expand All @@ -70,7 +71,7 @@ if ($ENV:GIT_INSTALL_ROOT -eq $null -and $gitVersionVendor -ne $null) {
# write-host "GIT_INSTALL_TYPE: ${ENV:GIT_INSTALL_TYPE}"

if (-not($ENV:GIT_INSTALL_ROOT -eq $null)) {
Configure-Git -gitRoot "$ENV:GIT_INSTALL_ROOT" -gitType $ENV:GIT_INSTALL_TYPE
$env:Path = Configure-Git -gitRoot "$ENV:GIT_INSTALL_ROOT" -gitType $ENV:GIT_INSTALL_TYPE -gitPathUser $gitPathUser
}

if ( Get-command -Name "vim" -ErrorAction silentlycontinue) {
Expand Down
131 changes: 38 additions & 93 deletions vendor/psmodules/Cmder.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ function readVersion($gitPath) {

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','')
}
$gitPath=$gitPath.replace('\git.exe','')
}

return $gitPath.toString()
}

Expand Down Expand Up @@ -67,98 +67,43 @@ function compare_git_versions($userVersion, $vendorVersion) {
} else {
return $vendorVersion
}

}

function Configure-Git($gitRoot, $gitType){
# # Current Cmder Behavior
# $GIT_INSTALL_ROOT_ESC=$GIT_INSTALL_ROOT.replace('\','\\')
# if (!($env:Path -match "$GIT_INSTALL_ROOT_ESC\\cmd")) {
# $env:Path = $($GIT_INSTALL_ROOT + "\cmd" + ";" + $env:Path)
# }
#
# # Add "$GIT_INSTALL_ROOT\usr\bin" to the path if exists and not done already
# 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"
# }

# $env:Path = $($env:Path + ";" $GIT_INSTALL_ROOT + "\cmd")

# # 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"
# }

# Proposed Behavior
$gitRootEsc = $gitRoot.replace('\','\\')
if (!($env:Path -match "$gitRootEsc\\cmd")) {
$env:Path = $($gitRoot + "\cmd" + ";" + $env:Path)
}

# 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 (isNixCommand -filename 'curl.exe' -all $true) {
if (isNixCommand -filename 'curl.exe') {
$pathPosition = 'start'
} else {
$pathPosition = 'end'
}

if ($pathPosition -eq 'end') {
# 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 "$gitRootEsc\\mingw32\\bin")) {
$env:path = "$env:path;$gitRoot\mingw32\bin"
} elseif ((test-path "$gitRoot\mingw64\bin") -and -not ($env:path -match "$gitRootEsc\\mingw64\\bin")) {
$env:path = "$env:path;$gitRoot\mingw64\bin"
}
} elseif ($pathPosition -eq 'start') {
if ((test-path "$gitRoot\mingw32\bin") -and -not ($env:path -match "$gitRootEsc\\mingw32\\bin")) {
$env:path = "$gitRoot\mingw32\bin;$env:path"
} elseif ((test-path "$gitRoot\mingw64\bin") -and -not ($env:path -match "$gitRootEsc\\mingw64\\bin")) {
$env:path = "$gitRoot\mingw64\bin;$env:path"
}
}
}

if (isNixCommand -filename 'find' -all $true) {
if (isNixCommand -filename 'find') {
$pathPosition = 'start'
} else {
$pathPosition = 'end'
}

if ($pathPosition -eq 'end') {
$env:path = "$env:path;$gitRoot\usr\bin"
} elseif ($pathPosition -eq 'start') {
$env:path = "$gitRoot\usr\bin;$env:path"
}
}
}
}

function isWindowsCommand($filename, $all=$false) {
if ($all) {
$commands = (get-command $filename -All).source
} else {
$commands = (get-command $filename).source
}

return ($commands -match $env:systemroot.replace('\','\\'))
}

function isNixCommand($filename, $all=$false) {
if ($all) {
$commands = (get-command $filename -All).source
} else {
$commands = (get-command $filename).source
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 "gitPathUser: $gitPathUser"
# write-host "Replacing $gitPathUser with $gitRoot in the path"
$gitPathUserEsc = $gitPathUser.replace('\','\\').replace('(','\(').replace(')','\)')

$newPath = ($env:path -ireplace $gitPathUserEsc, $gitRoot)
} else {
$gitRootEsc = $gitRoot.replace('\','\\')
if (!($env:Path -match "$gitRootEsc\\cmd")) {
$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 "$gitRootEsc\\mingw32\\bin")) {
$newPath = "$newPath;$gitRoot\mingw32\bin"
} elseif ((test-path "$gitRoot\mingw64\bin") -and -not ($env:path -match "$gitRootEsc\\mingw64\\bin")) {
$newPath = "$newPath;$gitRoot\mingw64\bin"
}

if ((test-path "$gitRoot\usr\bin") -and -not ($env:path -match "$gitRootEsc\\usr\\bin")) {
$newPath = "$newPath;$gitRoot\usr\bin"
}
}
}

return ($commands -match '\\git' -and $commands.replace('\','\\') -match '\\bin\\')
return $newPath
}

function Import-Git(){
Expand All @@ -174,7 +119,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
Expand Down