diff --git a/.gitattributes b/.gitattributes index b483dfe97..2cfdf930c 100644 --- a/.gitattributes +++ b/.gitattributes @@ -5,3 +5,4 @@ *.ps1 text eol=crlf *.md text eol=lf *.sh text eol=lf +/vendor/cmder_exinit text eol=lf diff --git a/CHANGELOG.md b/CHANGELOG.md index cf2a60265..8811c6503 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,24 @@ ## Unreleased +### Adds + +- Offer to configure Cmder startup for external Git Bash installations without replacing existing hooks. Answering No skips one launch; answering `never` disables future offers for that installation. + +### Changes + +- Bundled and external Cmder hooks load the same `vendor\cmder.sh` startup script. +- Enable Bash and Mintty startup debug messages with a positive `debug_output` value, matching the command-script debug setting. +- Use a shared shell console helper for Bash startup debug messages, including the external Git Bash hook. +- Store external Git Bash setup opt-outs by computer name and Git path in the portable `config/user_cmder_state.txt` file. +- Automatically migrate an existing external `cmder_exinit.sh` hook to `zz-cmder_exinit.sh`, preserving the old hook in a dated `.bak` file without an installation-confirmation prompt. UAC approval may still be required if the Git profile directory is not writable. + ### Fixes +- Remove the legacy bundled `cmder.sh` hook at shell startup only when `zz-cmder.sh` exists, rather than rewriting it during package builds. +- Make the Git Bash/MinTTY launchers use the same bundled and external Git discovery, PATH setup, MSYSTEM selection, and Cmder startup hook as the running `bash.exe`. +- Fix no Git in path when launching `Cmder - Bash` from the unreleased `main` branch. +- Preserve Git Bash's normal startup PATH, including native tools and entries with spaces, with Cmder's bin directories at the front. - Support Git for Windows' `ucrt64` directory in fresh and cached command startup, PowerShell Git discovery, and Git Bash, while retaining older `mingw64` and `mingw32` layouts. This prepares the Git security update in [#3093](https://github.com/cmderdev/cmder/pull/3093). - Show vendor build badges for the requested release ref, with a verified check-runs fallback when the README badge is unavailable. Fix missing README fallback and Azure build-link matching from [#3103](https://github.com/cmderdev/cmder/pull/3103), and link truncated release notes to their full text. diff --git a/README.md b/README.md index 62f9ebc19..be3541b35 100644 --- a/README.md +++ b/README.md @@ -202,6 +202,8 @@ Single user portable configuration is possible using the Cmder specific shell co **Note:** Bash and Mintty sessions will also source the `$HOME/.bashrc` file if it exists after it sources `$CMDER_ROOT/config/user_profile.sh`. +To see Bash or Mintty startup debug messages, set `debug_output` to a positive integer in the environment before launching the shell (for example, `set debug_output=1` in Command Prompt). The bundled and external Git Bash hooks use the same setting. + You can write `*.cmd|*.bat`, `*.ps1`, and `*.sh` scripts and just drop them in the `%CMDER_ROOT%\config\profile.d` folder to add startup config to Cmder. | Shell | Cmder `Profile.d` Scripts | @@ -245,7 +247,7 @@ Bash shells support simple and complex aliases with optional parameters natively To make an alias and/or any other profile settings permanent add it to one of the following: -Note: These are loaded in this order by `$CMDER_ROOT/vendor/git-for-windows/etc/profile.d/cmder.sh`. Anything stored in `$CMDER_ROOT` will be a portable setting and will follow Cmder to another machine. +Bundled Git Bash sets up its normal PATH first, then `zz-cmder.sh` in `etc/profile.d` adds Cmder's bin directories at the front and loads its portable settings. The Git Bash launcher removes the older `cmder.sh` hook only if `zz-cmder.sh` is present. Bash handles personal login files normally; source `~/.bashrc` from your login profile if needed. * `$CMDER_ROOT/config/profile.d/*.sh` * `$CMDER_ROOT/config/user_profile.sh` @@ -287,19 +289,29 @@ You may run bash (the default shell used on Linux, macOS and GNU/Hurd) externall **Recommended Optional Steps:** +When Cmder uses external Git for Windows, the Git Bash launcher offers to install `/etc/profile.d/zz-cmder_exinit.sh` if no Cmder hook exists. If the older `cmder_exinit.sh` hook is present, Cmder automatically renames it to a dated `.bak` file and installs the new hook without prompting. The backup is not a `.sh` file, so Git Bash will not source it. This hook and the bundled `zz-cmder.sh` load [`vendor/cmder.sh`](vendor/cmder.sh), included in full and no-Git packages. For bundled Git, the launcher removes the older `cmder.sh` hook at startup only when `zz-cmder.sh` is present. + +- At the prompt: + - `Y` Integrates `Cmder` into the external `Git` Install. + - `N` Skips integration this launch. + - `never` Suppresses future offers for that Git path on this computer. +- Opt-outs are stored as tab-separated computer name and Git path pairs under `[external-git-bash-opt-outs]` in `config/user_cmder_state.txt`. + - Remove an entry to allow the offer again. + - Repeated section headers let Cmder append entries without rewriting existing state. + Copy the `vendor/cmder_exinit` file to the Cygwin/Babun, MSys2, or Git for Windows SDK environments `/etc/profile.d/` folder to use portable settings in the `$CMDER_ROOT/config` folder. Note: MinGW could work if the init scripts include `profile.d` but this has not been tested. The destination file extension depends on the shell you use in that environment. For example: -* bash - Copy to `/etc/profile.d/cmder_exinit.sh` -* zsh - Copy to `/etc/profile.d/cmder_exinit.zsh` +* bash - Copy to `/etc/profile.d/zz-cmder_exinit.sh` +* zsh - Copy to `/etc/profile.d/zz-cmder_exinit.zsh` Uncomment and edit the line below in the script to use Cmder config even when launched from outside Cmder. ``` -# CMDER_ROOT=${USERPROFILE}/cmder # This is not required if launched from Cmder. +# CMDER_ROOT="${USERPROFILE}/cmder" # Not required when launched from Cmder. ``` ### Customizing user sessions using `init.cmd` custom arguments. diff --git a/packignore b/packignore index 0af0a8c09..671e9e943 100644 --- a/packignore +++ b/packignore @@ -20,7 +20,6 @@ icons\icon_256*.png Cmder.bat vendor\tmp appveyor.yml -vendor\cmder.sh vendor\git-prompt.sh config\user-* config\user_* diff --git a/scripts/build.ps1 b/scripts/build.ps1 index 97af6a467..f308fe227 100644 --- a/scripts/build.ps1 +++ b/scripts/build.ps1 @@ -208,10 +208,18 @@ if (-not $noVendor) { Copy-Item $WinTermSettingsJsonSave $WinTermSettingsJson } - # Put vendor\cmder.sh in /etc/profile.d so it runs when we start bash or mintty + # Run after Git Bash's standard PATH hooks, including env.sh. if ( (Test-Path $($saveTo + "git-for-windows/etc/profile.d") ) ) { - Write-Verbose "Adding cmder.sh /etc/profile.d" - Copy-Item $($saveTo + "cmder.sh") $($saveTo + "git-for-windows/etc/profile.d/cmder.sh") + $bashProfileDir = Join-Path $saveTo 'git-for-windows\etc\profile.d' + Write-Verbose "Installing zz-cmder.sh in $bashProfileDir" + $bundledRootFallback = @' +if [ -z "${CMDER_ROOT:-}" ] && [ -z "${cmder_root:-}" ] && [ -z "${ConEmuDir:-}" ]; then + CMDER_ROOT=$(cd "$(cygpath -am /)/../.." && pwd) || return 1 +fi +'@ + $shim = [IO.File]::ReadAllText((Join-Path $cmder_root 'vendor\cmder_exinit')) + $hook = "$bundledRootFallback`n`n$shim" -replace "`r`n", "`n" + [IO.File]::WriteAllText((Join-Path $bashProfileDir 'zz-cmder.sh'), $hook) } # Replace /etc/profile.d/git-prompt.sh with cmder lambda prompt so it runs when we start bash or mintty diff --git a/scripts/pack.ps1 b/scripts/pack.ps1 index 8519fcc28..376430f42 100644 --- a/scripts/pack.ps1 +++ b/scripts/pack.ps1 @@ -44,6 +44,12 @@ $cmder_root = Resolve-Path $cmderRoot $ErrorActionPreference = "Stop" Ensure-Executable "7z" +foreach ($file in @('vendor\cmder.sh', 'vendor\cmder_exinit', 'vendor\lib\lib_console.sh', 'vendor\setup_external_git_bash.ps1')) { + if (-not (Test-Path -LiteralPath (Join-Path $cmder_root $file) -PathType Leaf)) { + throw "Cannot package Cmder: missing required Bash startup file '$file'." + } +} + function Get-ArchiveFlags { param( [Parameter(Mandatory = $true)] diff --git a/scripts/test-build-helpers.ps1 b/scripts/test-build-helpers.ps1 index caa24ca0b..a83d65921 100644 --- a/scripts/test-build-helpers.ps1 +++ b/scripts/test-build-helpers.ps1 @@ -91,6 +91,57 @@ try { } $cases++ + $packageSource = Join-Path $testRoot 'package source' + $packageVendor = Join-Path $packageSource 'vendor' + $profileDir = Join-Path $packageVendor 'git-for-windows\etc\profile.d' + $null = New-Item -ItemType Directory -Path $profileDir + $legacyBundledHook = Join-Path $profileDir 'cmder.sh' + [IO.File]::WriteAllText($legacyBundledHook, 'legacy bundled hook') + $repo = Split-Path $PSScriptRoot -Parent + $startupFiles = @('cmder.sh', 'cmder_exinit', 'lib\lib_console.sh', 'setup_external_git_bash.ps1') + foreach ($file in $startupFiles) { + $destination = Join-Path $packageVendor $file + $null = New-Item -ItemType Directory -Path (Split-Path $destination -Parent) -Force + Copy-Item -LiteralPath (Join-Path $repo "vendor\$file") -Destination $destination + } + $packageConfig = Join-Path $packageSource 'config' + $null = New-Item -ItemType Directory -Path $packageConfig -Force + [IO.File]::WriteAllText((Join-Path $packageConfig 'user_cmder_state.txt'), 'private Git path') + Copy-Item -LiteralPath (Join-Path $repo 'packignore') -Destination $packageSource + $emptyManifest = Join-Path $testRoot 'empty-sources.json' + Set-Content -LiteralPath $emptyManifest -Value '[]' -Encoding ascii + Set-Content -LiteralPath (Join-Path $profileDir 'git-prompt.sh.bak') -Value '' -Encoding ascii + & (Join-Path $PSScriptRoot 'build.ps1') -SourcesPath $emptyManifest -SaveTo $packageVendor -Config $packageSource + $hook = Get-Content -Raw -LiteralPath (Join-Path $profileDir 'zz-cmder.sh') + if (!$hook.Contains('. "$CMDER_ROOT/vendor/cmder.sh"') -or $hook.Contains('__cmder_find_git_root')) { + throw 'Bundled profile hook must source the shared startup rather than copy it.' + } + if ([IO.File]::ReadAllText($legacyBundledHook) -ne 'legacy bundled hook') { + throw 'Package building must not rewrite the legacy bundled hook.' + } + $packageOutput = Join-Path $testRoot 'archives' + & (Join-Path $PSScriptRoot 'pack.ps1') -CmderRoot $packageSource -SaveTo $packageOutput + foreach ($profile in Get-CmderPackageProfiles) { + foreach ($package in $profile.packages) { + $archive = Join-Path $packageOutput "$($profile.outputFolder)\$($package.name)" + $listing = & 7z l -slt -ba $archive + if ($LASTEXITCODE) { throw "Cannot list $archive." } + if ($listing -contains 'Path = config\user_cmder_state.txt') { + throw "$($package.name) contains user-owned Cmder state." + } + foreach ($file in $startupFiles) { + if ($listing -notcontains "Path = vendor\$file") { + throw "$($package.name) is missing vendor\$file." + } + } + $hasBundledHook = $listing -contains 'Path = vendor\git-for-windows\etc\profile.d\zz-cmder.sh' + if ($hasBundledHook -ne ($package.name -notlike '*_nogit.zip')) { + throw "$($package.name) has incorrect bundled hook inclusion." + } + $cases++ + } + } + Push-Location -LiteralPath $testRoot try { $version = Get-VersionStr diff --git a/scripts/test-git-layout.ps1 b/scripts/test-git-layout.ps1 index 3dfe1058d..08bd78106 100644 --- a/scripts/test-git-layout.ps1 +++ b/scripts/test-git-layout.ps1 @@ -26,13 +26,25 @@ function Assert-Equal($Actual, $Expected, [string]$Label) { $script:assertionCount++ } +$minttyLauncher = [IO.File]::ReadAllText((Join-Path $repoRoot 'vendor\start_git_mintty.cmd')) +$bashLauncher = [IO.File]::ReadAllText((Join-Path $repoRoot 'vendor\start_git_bash.cmd')) +$gitShellLibrary = [IO.File]::ReadAllText((Join-Path $repoRoot 'vendor\lib\lib_git_shell.cmd')) +Assert-Equal $bashLauncher.Contains('lib_git_shell.cmd') $true 'Bash launcher uses shared Git shell setup' +Assert-Equal $minttyLauncher.Contains('lib_git_shell.cmd') $true 'Mintty launcher uses shared Git shell setup' +Assert-Equal $gitShellLibrary.Contains('setup_external_git_bash.ps1') $true 'Shared Git shell setup uses external Git setup' +Assert-Equal $gitShellLibrary.Contains('ucrt64') $true 'Shared setup supports UCRT64' +Assert-Equal $gitShellLibrary.Contains('mingw64') $true 'Shared setup supports MinGW64' +Assert-Equal $gitShellLibrary.Contains('mingw32') $true 'Shared setup supports MinGW32' +Assert-Equal $gitShellLibrary.Contains('conemu-msys2-64.exe') $true 'Bash setup keeps ConEmu integration' +Assert-Equal $minttyLauncher.Contains('"%shellCmd%" /bin/bash -l -i') $true 'Mintty launcher starts an interactive login Bash' + if (!(Test-Path -LiteralPath $BashExecutable -PathType Leaf)) { throw "Git Bash is required; provide -BashExecutable. Not found: $BashExecutable" } $cygpath = Join-Path (Split-Path $BashExecutable -Parent) 'cygpath.exe' if (!(Test-Path -LiteralPath $cygpath -PathType Leaf)) { throw "Not found: $cygpath" } $bashSource = [IO.File]::ReadAllText((Join-Path $repoRoot 'vendor\cmder.sh')) -$bashMatch = [regex]::Match($bashSource, '(?ms)^git_mingw_bin=""\r?\n.*?(?=^PATH="\$\{CMDER_ROOT\}/bin:)') +$bashMatch = [regex]::Match($bashSource, '(?ms)^__cmder_add_git_native_bin\(\) \{\r?\n.*?(?=^PATH="\$\{CMDER_ROOT\}/bin:)') if (!$bashMatch.Success) { throw 'Cannot locate Git native-bin selection in cmder.sh.' } $cmdBlocks = @{} foreach ($source in @('vendor\init.cmd', 'vendor\user_init.cmd.template')) { @@ -50,6 +62,52 @@ $savedArchitecture = $env:PROCESSOR_ARCHITECTURE $savedWow64Architecture = $env:PROCESSOR_ARCHITEW6432 try { $null = New-Item -ItemType Directory -Path $fixtureRoot + foreach ($shell in @('bash', 'mintty')) { + foreach ($debug in @('0', '1')) { + $launcher = Join-Path $repoRoot "vendor\start_git_$shell.cmd" + $cmdFile = Join-Path $fixtureRoot 'test-launcher-debug.cmd' + $cmdText = @( + '@echo off' + "set `"CMDER_ROOT=$fixtureRoot`"" + "set `"ProgramFiles=$fixtureRoot`"" + "set `"ProgramFiles(x86)=$fixtureRoot`"" + 'set "ConEmuDir="' + 'set "DEBUG=1"' + "set `"debug_output=$debug`"" + 'set "fast_init=0"' + 'set "verbose_output=0"' + "call `"$launcher`"" + ) -join "`r`n" + [IO.File]::WriteAllText($cmdFile, $cmdText + "`r`n", [Text.Encoding]::ASCII) + $output = & $env:ComSpec /d /c "`"$cmdFile`"" + Assert-Equal $LASTEXITCODE 0 "$shell launcher parses with debug_output=$debug" + $debugLines = @($output | Where-Object { $_ -match "DEBUG\(start_git_$shell\):" }) + Assert-Equal ($debugLines.Count -gt 0) ($debug -eq '1') "$shell debug messages follow debug_output" + } + } + + $bundledRoot = Join-Path $fixtureRoot 'Cmder (test)' + $bundledGit = Join-Path $bundledRoot 'vendor\git-for-windows' + $null = New-Item -ItemType Directory -Path (Join-Path $bundledGit 'usr\bin'), (Join-Path $bundledGit 'etc\profile.d') -Force + [IO.File]::WriteAllText((Join-Path $bundledGit 'usr\bin\bash.exe'), '') + $legacyHook = Join-Path $bundledGit 'etc\profile.d\cmder.sh' + [IO.File]::WriteAllText($legacyHook, '') + [IO.File]::WriteAllText((Join-Path $bundledGit 'etc\profile.d\zz-cmder.sh'), '') + $helper = Join-Path $repoRoot 'vendor\lib\lib_git_shell.cmd' + $cmdText = @( + '@echo off' + "set `"CMDER_ROOT=$bundledRoot`"" + 'set "debug_output=1"' + 'set "fast_init=0"' + 'set "verbose_output=0"' + "call `"$helper`" initialize bash start_git_bash" + ) -join "`r`n" + [IO.File]::WriteAllText($cmdFile, $cmdText + "`r`n", [Text.Encoding]::ASCII) + $output = & $env:ComSpec /d /c "`"$cmdFile`"" + Assert-Equal $LASTEXITCODE 0 'Bundled shell setup parses with debug output' + Assert-Equal (@($output | Where-Object { $_ -match 'DEBUG\(start_git_bash\): Removing legacy Bash hook:' }).Count) 1 'Bundled hook cleanup reports through console debug output' + Assert-Equal (Test-Path -LiteralPath $legacyHook) $false 'Bundled hook cleanup still runs with debug output' + $cases = @( @{ Name = 'UCRT64 only'; Dirs = @('ucrt64'); Native = 'ucrt64' }, @{ Name = 'MinGW64 only'; Dirs = @('mingw64'); Native = 'mingw64' }, @@ -108,7 +166,15 @@ try { } } $bashFile = Join-Path $fixtureRoot 'select-native.sh' - $bashText = 'GIT_INSTALL_ROOT="$1"' + "`n" + 'PROCESSOR_ARCHITECTURE="$2"' + "`n" + 'PROCESSOR_ARCHITEW6432="$3"' + "`n" + 'PATH="/layout-test-base"' + "`n" + $bashMatch.Value + "`n" + 'printf ''%s\n'' "$PATH"' + "`n" + $bashText = @' +debug_output() { :; } +__cmder_find_git_root() { GIT_INSTALL_ROOT="$fixture_git_root"; } +fixture_git_root="$1" +PROCESSOR_ARCHITECTURE="$2" +PROCESSOR_ARCHITEW6432="$3" +MINGW_PREFIX="/layout-test-missing-prefix" +PATH="/layout-test-base" +'@ + "`n" + $bashMatch.Value + "`n" + 'printf ''%s\n'' "$PATH"' + "`n" [IO.File]::WriteAllText($bashFile, ($bashText -replace "`r`n", "`n"), (New-Object Text.UTF8Encoding($false))) $posixGitRoot = & $cygpath -u $gitRoot $posixScript = & $cygpath -u $bashFile @@ -117,7 +183,8 @@ try { $env:PROCESSOR_ARCHITEW6432 = $architecture.Wow64 $actual = & $BashExecutable --noprofile --norc $posixScript $posixGitRoot $architecture.Process $architecture.Wow64 if ($LASTEXITCODE) { throw 'Bash selection failed.' } - $expectedBash = if ($architecture.Native) { (& $cygpath -u $architecture.Native) + ':/layout-test-base' } else { '/layout-test-base' } + $expectedBash = "${posixGitRoot}/bin:/layout-test-base" + if ($architecture.Native) { $expectedBash = (& $cygpath -u $architecture.Native) + ":$expectedBash" } Assert-Equal $actual $expectedBash "$($case.Name) Bash native bin ($($architecture.Name)) and no empty PATH entry" } } @@ -134,6 +201,198 @@ try { $posixSource = & $cygpath -u (Join-Path $repoRoot 'vendor\cmder.sh') & $BashExecutable -n $posixSource Assert-Equal $LASTEXITCODE 0 'Full cmder.sh Bash syntax' + + $startupRoot = Join-Path $fixtureRoot 'Bash startup' + $null = New-Item -ItemType Directory -Path (Join-Path $startupRoot 'config') -Force + $startupVendor = Join-Path $startupRoot 'vendor' + $startupLib = Join-Path $startupVendor 'lib' + $null = New-Item -ItemType Directory -Path $startupLib -Force + Copy-Item -LiteralPath (Join-Path $repoRoot 'vendor\cmder.sh') -Destination $startupVendor + Copy-Item -LiteralPath (Join-Path $repoRoot 'vendor\lib\lib_console.sh') -Destination $startupLib + $startupProfile = Join-Path $startupRoot 'config\user_profile.sh' + [IO.File]::WriteAllText($startupProfile, '') + [IO.File]::WriteAllText((Join-Path $startupRoot '.bashrc'), "export PATH='/unexpected-bashrc'`n") + $posixStartupRoot = & $cygpath -u $startupRoot + $mountedBins = & $BashExecutable --noprofile --norc -c 'for dir in /ucrt64/bin /mingw64/bin /clangarm64/bin /mingw32/bin; do if [ -d $dir ]; then echo $dir; fi; done' + $mountedBin = @($mountedBins)[0] + if (!$mountedBin) { throw 'No native bin found in the Git Bash runtime.' } + $startupScript = Join-Path $fixtureRoot 'bundled-startup.sh' + [IO.File]::WriteAllText($startupScript, 'export CMDER_ROOT="$1" HOME="$1" MINGW_PREFIX="${3%/bin}" PATH="$4"; unset CMDER_USER_CONFIG DEBUG debug_output; GIT_INSTALL_ROOT=/untouched; . "$2"; [ "$GIT_INSTALL_ROOT" = /untouched ] || exit 1; printf "%s\n" "$PATH"' + "`n") + $posixStartupScript = & $cygpath -u $startupScript + $baseline = "${posixStartupRoot}/bin:${mountedBin}:/usr/bin:/c/Program Files/Custom/bin:/usr/bin/core_perl" + $actual = & $BashExecutable --noprofile --norc $posixStartupScript $posixStartupRoot $posixSource $mountedBin $baseline + Assert-Equal $LASTEXITCODE 0 'Bash startup with the standard Git Bash PATH' + Assert-Equal $actual "${posixStartupRoot}/bin:${posixStartupRoot}/vendor/bin:${baseline}" 'Cmder prepends its bins without changing inherited PATH entries or forcing .bashrc' + + $posixShim = & $cygpath -u (Join-Path $repoRoot 'vendor\cmder_exinit') + $debugScript = Join-Path $fixtureRoot 'test-bash-debug.sh' + $debugLog = Join-Path $fixtureRoot 'bash-debug.log' + [IO.File]::WriteAllText($debugScript, 'export CMDER_ROOT="$1" HOME="$1" MINGW_PREFIX="${3%/bin}" PATH="$4" DEBUG=1; unset CMDER_USER_CONFIG CMDER_EXINIT debug_output; if [ "$5" != unset ]; then export debug_output="$5"; fi; . "$2" 2>"$6"; printf "%s\n" "$PATH"' + "`n") + $posixDebugScript = & $cygpath -u $debugScript + $posixDebugLog = & $cygpath -u $debugLog + foreach ($entry in @($posixSource, $posixShim)) { + foreach ($debug in @('unset', '0', '-1', '1', '2')) { + $actual = & $BashExecutable --noprofile --norc $posixDebugScript $posixStartupRoot $entry $mountedBin $baseline $debug $posixDebugLog + Assert-Equal $LASTEXITCODE 0 "Bash startup via $entry with debug_output=$debug" + Assert-Equal $actual "${posixStartupRoot}/bin:${posixStartupRoot}/vendor/bin:${baseline}" "Debug output preserves startup PATH via $entry" + $debugText = [IO.File]::ReadAllText($debugLog) + Assert-Equal ($debugText -match '(?m)^\d{2}:\d{2}:\d{2} DEBUG\(cmder\.sh\): Cmder root:') ($debug -in @('1', '2')) "Bash debug output follows debug_output=$debug via $entry" + if ($entry -eq $posixShim) { + Assert-Equal ($debugText -match '(?m)^\d{2}:\d{2}:\d{2} DEBUG\(cmder_exinit\): Sourcing startup script:') ($debug -in @('1', '2')) "Shim debug output follows debug_output=$debug" + } + } + } + + $startupConsole = Join-Path $startupLib 'lib_console.sh' + $hiddenConsole = Join-Path $startupLib 'lib_console.hidden' + Rename-Item -LiteralPath $startupConsole -NewName 'lib_console.hidden' + try { + [IO.File]::WriteAllText($debugScript, 'export CMDER_ROOT="$1"; unset CMDER_EXINIT; . "$2" 2>"$3"' + "`n") + foreach ($entry in @($posixSource, $posixShim)) { + & $BashExecutable --noprofile --norc $posixDebugScript $posixStartupRoot $entry $posixDebugLog + Assert-Equal $LASTEXITCODE 1 "Missing console library fails via $entry" + Assert-Equal ([IO.File]::ReadAllText($debugLog).Contains('Cannot read console library:')) $true "Missing console library reports an error via $entry" + } + } finally { + Rename-Item -LiteralPath $hiddenConsole -NewName 'lib_console.sh' + } + + [IO.File]::WriteAllText($startupProfile, "export PATH='/usr/bin:/custom/bin'`n") + $actual = & $BashExecutable --noprofile --norc $posixStartupScript $posixStartupRoot $posixSource $mountedBin $baseline + Assert-Equal $LASTEXITCODE 0 'Bash startup with a Cmder profile PATH reset' + Assert-Equal $actual "${mountedBin}:/usr/bin:/custom/bin" 'Native bin is restored after a Cmder profile resets PATH' + + $installer = Join-Path $repoRoot 'vendor\setup_external_git_bash.ps1' + $installerAst = [System.Management.Automation.Language.Parser]::ParseFile($installer, [ref]$null, [ref]$parseErrors) + if ($parseErrors.Count) { throw $parseErrors[0].Message } + foreach ($definition in $installerAst.EndBlock.Statements) { + if ($definition -is [System.Management.Automation.Language.FunctionDefinitionAst]) { + . ([scriptblock]::Create($definition.Extent.Text)) + } + } + + $externalRoot = Join-Path $fixtureRoot 'External Git' + $externalProfiles = Join-Path $externalRoot 'etc\profile.d' + $externalHook = Join-Path $externalProfiles 'zz-cmder_exinit.sh' + $legacyHook = Join-Path $externalProfiles 'cmder_exinit.sh' + $externalCmder = Join-Path $fixtureRoot "External Cmder's root" + $null = New-Item -ItemType Directory -Path $externalProfiles, (Join-Path $externalCmder 'vendor'), (Join-Path $externalCmder 'config\profile.d') + $sharedStartup = Join-Path $externalCmder 'vendor\cmder.sh' + Copy-Item -LiteralPath (Join-Path $repoRoot 'vendor\cmder_exinit'), (Join-Path $repoRoot 'vendor\cmder.sh') -Destination (Join-Path $externalCmder 'vendor') + $externalLib = Join-Path $externalCmder 'vendor\lib' + $null = New-Item -ItemType Directory -Path $externalLib + Copy-Item -LiteralPath (Join-Path $repoRoot 'vendor\lib\lib_console.sh') -Destination $externalLib + [IO.File]::WriteAllText((Join-Path $externalCmder 'config\profile.d\with spaces.sh'), "printf '%s\n' fragment`n") + [IO.File]::WriteAllText((Join-Path $externalCmder 'config\user_profile.sh'), "printf '%s\n' profile`n") + [IO.File]::WriteAllText($legacyHook, 'existing legacy hook') + $fixtureProfile = "C:\Fixture Users\O'Neil" + $installArgs = @{ GitRoot = $externalRoot; CmderRoot = $externalCmder; UserProfile = $fixtureProfile; ComputerName = 'HOST-A' } + $optOut = Join-Path $externalCmder 'config\user_cmder_state.txt' + & { + function Read-Host { throw 'Migrating an existing hook must not prompt.' } + Install-CmderExternalGitHook @installArgs + } + $legacyBackups = @(Get-ChildItem -LiteralPath $externalProfiles -Filter 'cmder_exinit.sh.*.bak' -File) + Assert-Equal (Test-Path -LiteralPath $legacyHook) $false 'Legacy external hook is removed from the active profile' + Assert-Equal $legacyBackups.Count 1 'Legacy external hook is preserved in a dated backup' + Assert-Equal ([IO.File]::ReadAllText($legacyBackups[0].FullName)) 'existing legacy hook' 'Dated backup preserves the old hook contents' + Assert-Equal ($legacyBackups[0].Name -match '\.sh$') $false 'Dated backup is not sourced as a shell hook' + Assert-Equal (Test-Path -LiteralPath $externalHook) $true 'Legacy external hook is automatically replaced with the late-running hook' + [IO.File]::WriteAllText($legacyHook, 'duplicate legacy hook') + & { + function Read-Host { throw 'Cleaning up a duplicate legacy hook must not prompt.' } + Install-CmderExternalGitHook @installArgs + } + $legacyBackups = @(Get-ChildItem -LiteralPath $externalProfiles -Filter 'cmder_exinit.sh.*.bak' -File) + Assert-Equal (Test-Path -LiteralPath $legacyHook) $false 'Legacy external hook is backed up when the new hook already exists' + Assert-Equal $legacyBackups.Count 2 'Repeated legacy-hook cleanup uses a distinct dated backup' + $posixExternalHook = & $cygpath -u $externalHook + $posixExternalCmder = & $cygpath -u $externalCmder + $externalStartup = Join-Path $fixtureRoot 'external-startup.sh' + [IO.File]::WriteAllText($externalStartup, 'unset CMDER_ROOT CMDER_USER_CONFIG CMDER_EXINIT DEBUG debug_output; export USERPROFILE="$2" PATH="$3" MINGW_PREFIX="${4%/bin}"; . "$1"; . "$1"; printf "%s\n" "$PATH"' + "`n") + $posixExternalStartup = & $cygpath -u $externalStartup + $actual = & $BashExecutable --noprofile --norc $posixExternalStartup $posixExternalHook $fixtureProfile $baseline $mountedBin + Assert-Equal $LASTEXITCODE 0 'Generated external hook is valid Bash' + Assert-Equal ($actual -join "`n") "fragment`nprofile`n${posixExternalCmder}/bin:${posixExternalCmder}/vendor/bin:${baseline}" 'External hook preserves PATH, supports spaces and apostrophes, and runs once' + $actual = & $BashExecutable --noprofile --norc $posixExternalStartup $posixExternalHook 'C:\Other User' $baseline $mountedBin + Assert-Equal $actual $baseline 'Generated hook does not enable Cmder for another Windows profile' + [IO.File]::AppendAllText($sharedStartup, "printf '%s\n' updated`n") + $actual = & $BashExecutable --noprofile --norc $posixExternalStartup $posixExternalHook $fixtureProfile $baseline $mountedBin + Assert-Equal (@($actual)[2]) 'updated' 'Installed external shim sees edits to the shared startup without reinstalling' + Remove-Item -LiteralPath $externalHook + & { + function Read-Host { 'n' } + Install-CmderExternalGitHook @installArgs + } + Assert-Equal (Test-Path -LiteralPath $optOut) $false 'Not now does not save a permanent opt-out' + & { + function Read-Host { 'never' } + Install-CmderExternalGitHook @installArgs + } + & { + function Read-Host { throw 'An explicit opt-out must suppress future setup offers.' } + Install-CmderExternalGitHook @installArgs + } + Assert-Equal (Test-Path -LiteralPath $externalHook) $false 'Skipping setup leaves the external Git installation unchanged' + $optOutContent = [IO.File]::ReadAllText($optOut) + Assert-Equal ($optOutContent.Contains('Remove an entry')) $true 'Portable opt-out explains how to restore the offer' + Assert-Equal ($optOutContent.Contains('[external-git-bash-opt-outs]')) $true 'Portable opt-outs have their own section' + Assert-Equal ($optOutContent.Contains("HOST-A`t$externalRoot")) $true 'Portable opt-out identifies the computer and Git installation' + & { + function Read-Host { 'never' } + Install-CmderExternalGitHook -GitRoot $externalRoot -CmderRoot $externalCmder -UserProfile $fixtureProfile -ComputerName 'HOST-B' + } + Assert-Equal ([IO.File]::ReadAllText($optOut).Contains("HOST-B`t$externalRoot")) $true 'The same Git path on another computer has an independent offer' + & { + function Read-Host { throw 'Matching computer and Git path must suppress the offer.' } + Install-CmderExternalGitHook -GitRoot $externalRoot.ToUpperInvariant() -CmderRoot $externalCmder -UserProfile $fixtureProfile -ComputerName 'host-b' + } + $secondRoot = Join-Path $fixtureRoot 'Second Git' + $null = New-Item -ItemType Directory -Path (Join-Path $secondRoot 'etc\profile.d') -Force + & { + function Read-Host { 'never' } + Install-CmderExternalGitHook -GitRoot $secondRoot -CmderRoot $externalCmder -UserProfile $fixtureProfile -ComputerName 'HOST-A' + } + $optOutContent = [IO.File]::ReadAllText($optOut) + Assert-Equal ($optOutContent.Contains($externalRoot) -and $optOutContent.Contains($secondRoot)) $true 'Opting out another Git preserves existing entries' + Save-CmderExternalGitOptOut -ConfigDirectory (Split-Path $optOut -Parent) -GitRoot $secondRoot.ToUpperInvariant() -ComputerName 'host-a' + Assert-Equal ([IO.File]::ReadAllText($optOut)) $optOutContent 'Saving the same Git path again does not duplicate it' + $defaultHostRoot = Join-Path $fixtureRoot 'Default host Git' + $null = New-Item -ItemType Directory -Path (Join-Path $defaultHostRoot 'etc\profile.d') -Force + & { + function Read-Host { 'never' } + Install-CmderExternalGitHook -GitRoot $defaultHostRoot -CmderRoot $externalCmder -UserProfile $fixtureProfile + } + Assert-Equal ([IO.File]::ReadAllText($optOut).Contains("$([Environment]::MachineName)`t$defaultHostRoot")) $true 'Default opt-out uses the current computer name' + [IO.File]::AppendAllText($optOut, '# User note without a final newline') + $thirdRoot = Join-Path $fixtureRoot 'Third Git' + Save-CmderExternalGitOptOut -ConfigDirectory (Split-Path $optOut -Parent) -GitRoot $thirdRoot -ComputerName 'HOST-A' + Assert-Equal ([IO.File]::ReadAllText($optOut).Contains("# User note without a final newline`r`nHOST-A`t$thirdRoot")) $true 'Appending preserves a user-edited file without a final newline' + $otherEntry = "HOST-C`t$externalRoot" + [IO.File]::AppendAllText($optOut, "[other-state]`r`n$otherEntry`r`nsetting=value`r`n") + $beforeAppend = [IO.File]::ReadAllText($optOut) + Assert-Equal (Test-CmderExternalGitOptOut -Content $beforeAppend -Entry $otherEntry) $false 'A matching line in another section does not suppress the offer' + Save-CmderExternalGitOptOut -ConfigDirectory (Split-Path $optOut -Parent) -GitRoot $externalRoot -ComputerName 'HOST-C' + $afterAppend = [IO.File]::ReadAllText($optOut) + Assert-Equal ($afterAppend.StartsWith($beforeAppend)) $true 'Appending an opt-out preserves other sections and their contents' + Assert-Equal (Test-CmderExternalGitOptOut -Content $afterAppend -Entry $otherEntry) $true 'A later opt-out section is read correctly' + Assert-Equal (Test-CmderExternalGitOptOut -Content $afterAppend -Entry "HOST-A`t$externalRoot") $true 'An earlier opt-out section remains readable' + $movedCmder = Join-Path $fixtureRoot 'Moved Cmder' + $null = New-Item -ItemType Directory -Path (Join-Path $movedCmder 'config') -Force + Copy-Item -LiteralPath $optOut -Destination (Join-Path $movedCmder 'config') + & { + function Read-Host { throw 'A copied portable opt-out must suppress the offer.' } + Install-CmderExternalGitHook -GitRoot $externalRoot.ToUpperInvariant() -CmderRoot $movedCmder -UserProfile $fixtureProfile -ComputerName 'host-a' + } + + $remainingLines = @([IO.File]::ReadAllLines($optOut) | Where-Object { $_ -ne "HOST-A`t$externalRoot" }) + [IO.File]::WriteAllLines($optOut, [string[]]$remainingLines) + Assert-Equal ([IO.File]::ReadAllText($optOut).Contains("HOST-B`t$externalRoot")) $true 'Removing one computer entry preserves another computer opt-out' + & { + function Read-Host { 'yes' } + Install-CmderExternalGitHook @installArgs + } + Assert-Equal (Test-Path -LiteralPath $externalHook) $true 'Removing a Git path re-enables its setup offer' } finally { $env:Path = $savedPath $env:GIT_INSTALL_ROOT = $savedGitRoot diff --git a/vendor/cmder.sh b/vendor/cmder.sh index 27279ffc3..46a4dd1a3 100644 --- a/vendor/cmder.sh +++ b/vendor/cmder.sh @@ -7,66 +7,90 @@ # Add system specific users customizations to $HOME/.bashrc, these # customizations will not follow Cmder to another machine. +if [ -z "${CMDER_ROOT:-}" ] || [ -n "${CMDER_EXINIT:-}" ]; then + return 0 2>/dev/null || exit 0 +fi + +if [ ! -r "$CMDER_ROOT/vendor/lib/lib_console.sh" ]; then + printf '[cmder] Cannot read console library: %s\n' "$CMDER_ROOT/vendor/lib/lib_console.sh" >&2 + return 1 2>/dev/null || exit 1 +fi +. "$CMDER_ROOT/vendor/lib/lib_console.sh" || return 1 +CMDER_EXINIT=1 + function runProfiled { - unset profile_d_scripts - pushd "${1}" >/dev/null - profile_d_scripts=$(ls *.sh 2>/dev/null) - - if [ ! "x${profile_d_scripts}" = "x" ] ; then - for x in ${profile_d_scripts} ; do - # echo Sourcing "${1}/${x}"... - . "${1}/${x}" - done + local script extension=sh + if [ -n "${ZSH_VERSION:-}" ]; then + setopt localoptions nonomatch + extension=zsh fi + pushd "$1" >/dev/null || return + for script in ./*."$extension"; do + [ -f "$script" ] || continue + debug_output cmder.sh "Sourcing profile script: $1/${script#./}" + . "$script" + done popd >/dev/null } -# We do this for bash as admin sessions since $CMDER_ROOT is not being set -if [ -z "$CMDER_ROOT" ] && [ -n "$cmder_root" ] ; then - export CMDER_ROOT=$(cygpath -u $cmder_root) -fi - -if [ -z "$CMDER_ROOT" ] ; then - case "$ConEmuDir" in *\\*) CMDER_ROOT=$( cd "$(cygpath -u "$ConEmuDir")/../.." ; pwd );; esac -else - case "$CMDER_ROOT" in *\\*) CMDER_ROOT="$(cygpath -u "$CMDER_ROOT")";; esac -fi - # Remove any trailing '/' -CMDER_ROOT=$(echo $CMDER_ROOT | sed 's:/*$::') +CMDER_ROOT=$(printf '%s\n' "$CMDER_ROOT" | sed 's:/*$::') export CMDER_ROOT +debug_output cmder.sh "Cmder root: ${CMDER_ROOT}" -if [ -f "/c/Program Files/Git/cmd/git.exe" ] ; then +__cmder_find_git_root() { + if [ -f "/c/Program Files/Git/cmd/git.exe" ] ; then GIT_INSTALL_ROOT="/c/Program Files/Git" -elif [ -f "/c/Program Files(x86)/Git/cmd/git.exe" ] ; then - GIT_INSTALL_ROOT="/c/Program Files(x86)/Git" -elif [ -f "${CMDER_ROOT}/vendor/git-for-windows/cmd/git.exe" ] ; then + elif [ -f "/c/Program Files (x86)/Git/cmd/git.exe" ] ; then + GIT_INSTALL_ROOT="/c/Program Files (x86)/Git" + elif [ -f "${CMDER_ROOT}/vendor/git-for-windows/cmd/git.exe" ] ; then GIT_INSTALL_ROOT=${CMDER_ROOT}/vendor/git-for-windows -fi + fi +} -if [[ ! "$PATH" =~ "${GIT_INSTALL_ROOT}/bin:" ]] ; then - PATH="${GIT_INSTALL_ROOT}/bin:$PATH" -fi +__cmder_add_git_native_bin() { + if [ -n "$git_mingw_bin" ] ; then + case ":$PATH:" in + *":${git_mingw_bin}:"*) + debug_output cmder.sh "Git native bin directory is already on PATH: ${git_mingw_bin}" + ;; + *) + PATH="${git_mingw_bin}${PATH:+:$PATH}" + debug_output cmder.sh "Added Git native bin directory to PATH: ${git_mingw_bin}" + ;; + esac + fi +} git_mingw_bin="" -git_native_dirs="ucrt64 mingw64 mingw32" -if [ "$PROCESSOR_ARCHITECTURE" = "x86" ] && [ -z "$PROCESSOR_ARCHITEW6432" ]; then - git_native_dirs="mingw32" -fi -for git_native_dir in $git_native_dirs; do - if [ -d "$GIT_INSTALL_ROOT/$git_native_dir/bin" ]; then - git_mingw_bin="$GIT_INSTALL_ROOT/$git_native_dir/bin" - break +if [ -n "${MINGW_PREFIX:-}" ] && [ -d "$MINGW_PREFIX/bin" ] ; then + git_mingw_bin="$MINGW_PREFIX/bin" +elif [ -f /cmd/git.exe ] || [ -n "${GIT_INSTALL_ROOT:-}" ]; then + __cmder_find_git_root + debug_output cmder.sh "Fallback Git installation root: ${GIT_INSTALL_ROOT:-not found}" + if [ -n "$GIT_INSTALL_ROOT" ] ; then + case ":$PATH:" in + *":${GIT_INSTALL_ROOT%/}/bin:"*) ;; + *) PATH="${GIT_INSTALL_ROOT%/}/bin${PATH:+:$PATH}" ;; + esac + git_native_dirs="ucrt64 mingw64 mingw32" + if [ "$PROCESSOR_ARCHITECTURE" = "x86" ] && [ -z "$PROCESSOR_ARCHITEW6432" ]; then + git_native_dirs="mingw32" + fi + for git_native_dir in $git_native_dirs; do + if [ -d "${GIT_INSTALL_ROOT%/}/$git_native_dir/bin" ]; then + git_mingw_bin="${GIT_INSTALL_ROOT%/}/$git_native_dir/bin" + break + fi + done + unset git_native_dir git_native_dirs fi -done -unset git_native_dir git_native_dirs - -if [ -n "$git_mingw_bin" ] && [[ ! "$PATH" =~ "${git_mingw_bin}:" ]] ; then - PATH="${git_mingw_bin}:$PATH" fi +debug_output cmder.sh "Git native bin directory: ${git_mingw_bin:-not found}" +__cmder_add_git_native_bin -PATH="${CMDER_ROOT}/bin:${CMDER_ROOT}/vendor/bin:$PATH:${CMDER_ROOT}" +PATH="${CMDER_ROOT}/bin:${CMDER_ROOT}/vendor/bin${PATH:+:$PATH}" export PATH @@ -77,27 +101,33 @@ if [ ! -d "${CMDER_ROOT}/config/profile.d" ] ; then fi if [ -d "${CMDER_ROOT}/config/profile.d" ] ; then + debug_output cmder.sh "Loading Cmder profile directory: ${CMDER_ROOT}/config/profile.d" runProfiled "${CMDER_ROOT}/config/profile.d" fi -if [ -d "${CMDER_USER_CONFIG}/profile.d" ] ; then +case "${CMDER_USER_CONFIG:-}" in + [A-Za-z]:*|*\\*) CMDER_USER_CONFIG=$(cygpath -u "$CMDER_USER_CONFIG") || return 1 ;; +esac +if [ -n "${CMDER_USER_CONFIG:-}" ] && [ -d "${CMDER_USER_CONFIG}/profile.d" ] ; then + debug_output cmder.sh "Loading user profile directory: ${CMDER_USER_CONFIG}/profile.d" runProfiled "${CMDER_USER_CONFIG}/profile.d" fi # Renaming to "config\user_profile.sh" to "user_profile.sh" for consistency. -if [ -f "$CMDER_ROOT/config/user-profile.sh" ] ; then +if [ -f "$CMDER_ROOT/config/user-profile.sh" ] && [ ! -e "$CMDER_ROOT/config/user_profile.sh" ] ; then mv "$CMDER_ROOT/config/user-profile.sh" "$CMDER_ROOT/config/user_profile.sh" fi CmderUserProfilePath="${CMDER_ROOT}/config/user_profile.sh" if [ -f "${CMDER_ROOT}/config/user_profile.sh" ] ; then + debug_output cmder.sh "Sourcing user profile: ${CMDER_ROOT}/config/user_profile.sh" . "${CMDER_ROOT}/config/user_profile.sh" fi -if [ "${CMDER_USER_CONFIG}" != "" ] ; then +if [ -n "${CMDER_USER_CONFIG:-}" ] ; then # Renaming to "config\user_profile.sh" to "user_profile.sh" for consistency. - if [ -f "$CMDER_USER_CONFIG/user-profile.sh" ] ; then + if [ -f "$CMDER_USER_CONFIG/user-profile.sh" ] && [ ! -e "$CMDER_USER_CONFIG/user_profile.sh" ] ; then mv "$CMDER_USER_CONFIG/user-profile.sh" "$CMDER_USER_CONFIG/user_profile.sh" fi @@ -105,16 +135,17 @@ if [ "${CMDER_USER_CONFIG}" != "" ] ; then CmderUserProfilePath="${CMDER_USER_CONFIG}/user_profile.sh" if [ -f "${CMDER_USER_CONFIG}/user_profile.sh" ] ; then + debug_output cmder.sh "Sourcing user profile: ${CMDER_USER_CONFIG}/user_profile.sh" . "${CMDER_USER_CONFIG}/user_profile.sh" fi fi if [ ! -f "${CmderUserProfilePath}" ] ; then + debug_output cmder.sh "Creating user startup file: ${CmderUserProfilePath}" echo Creating user startup file: "${CmderUserProfilePath}" cp "${CMDER_ROOT}/vendor/user_profile.sh.default" "${CmderUserProfilePath}" fi -# Source the users .bashrc file if it exists -if [ -f "${HOME}/.bashrc" ] ; then - . "${HOME}/.bashrc" -fi +# Cmder profiles may have rebuilt PATH. Bash runs personal login files later. +__cmder_add_git_native_bin +debug_output cmder.sh "Final PATH: ${PATH}" diff --git a/vendor/cmder_exinit b/vendor/cmder_exinit index edb12c7ec..2b72562d5 100644 --- a/vendor/cmder_exinit +++ b/vendor/cmder_exinit @@ -1,112 +1,31 @@ -# Copy this file to your non integrated *nix-like environment, -# Cygwin/MSys2/Git for Windows SDK, installs '/etc/profile.d/' -# folder to integrate the externally installed Unix like environment -# into Cmder so it has access to settings stored in Cmder/config -# folder when launched. -# -# The destination file extension depends on the shell you use. For example: -# -# bash - Copy to /etc/profile.d/cmder_exinit.sh -# zsh - Copy to /etc/profile.d/cmder_exinit.zsh -# Add portable user customizations ${CMDER_ROOT}/config/user-profile.sh or -# add whole config scripts to ${CMDER_ROOT}/config/profile.d both will be sourced -# from this file and be applied to the environment at startup. -# -# These customizations will follow Cmder if $CMDER_ROOT is copied -# to another machine. -# -# Add system specific users customizations to $HOME/.bashrc, these -# customizations will not follow Cmder to another machine. +# Source from /etc/profile.d/zz-cmder_exinit.sh (Bash) or .zsh (Zsh). +# Set CMDER_ROOT to enable Cmder settings when launching outside Cmder. +# CMDER_ROOT="${USERPROFILE}/cmder" -# # Uncomment and edit the CMDER_ROOT line to use Cmder/config even when launched -# # from outside Cmder. -# CMDER_ROOT=${USERPROFILE}/cmder # This is not required if launched from Cmder. - -function runProfiled { - unset profile_d_scripts - pushd "${1}" >/dev/null - - if [ -n "${ZSH_VERSION}" ]; then - profile_d_scripts=$(ls *.zsh 2>/dev/null) - elif [ -n "${BASH_VERSION}" ]; then - profile_d_scripts=$(ls *.sh 2>/dev/null) - fi - - if [ -n "${profile_d_scripts}" ] ; then - for x in ${profile_d_scripts} ; do - . "${1}/${x}" - done - fi - popd >/dev/null -} - -# Check that we haven't already been sourced. -[[ -z ${CMDER_EXINIT} ]] && CMDER_EXINIT="1" || return - -if [ -z "$CMDER_ROOT" ] && [ -n "$cmder_root" ] ; then - export CMDER_ROOT=$(cygpath -u $cmder_root) +if [ -z "${CMDER_ROOT:-}" ]; then + CMDER_ROOT="${cmder_root:-}" fi -# We do this for bash as admin sessions since $CMDER_ROOT is not being set -if [ "$CMDER_ROOT" = "" -a "$ConEmuDir" != "" ] ; then - if [ -d "${ConEmuDir}/../../vendor" ] ; then - case "$ConEmuDir" in *\\*) CMDER_ROOT=$( cd "$(cygpath -u "$ConEmuDir")/../.." ; pwd );; esac - fi -elif [ "$CMDER_ROOT" != "" ] ; then - case "$CMDER_ROOT" in *\\*) CMDER_ROOT="$(cygpath -u "$CMDER_ROOT")";; esac +if [ -z "$CMDER_ROOT" ]; then + case "${ConEmuDir:-}" in + [A-Za-z]:*|*\\*) CMDER_ROOT=$(cd "$(cygpath -u "$ConEmuDir")/../.." && pwd) || return 1 ;; + esac fi -if [ -n "$CMDER_ROOT" ] ; then - # Remove any trailing '/' - CMDER_ROOT=$(echo $CMDER_ROOT | sed 's:/*$::') - - export CMDER_ROOT - - PATH=${CMDER_ROOT}/bin:${CMDER_ROOT}/vendor/bin:$PATH:${CMDER_ROOT} - - export PATH - - # Drop *.sh or *.zsh files into "${CMDER_ROOT}\config\profile.d" - # to source them at startup. - if [ ! -d "${CMDER_ROOT}/config/profile.d" ] ; then - mkdir -p "${CMDER_ROOT}/config/profile.d" - fi +[ -n "$CMDER_ROOT" ] || return 0 +case "$CMDER_ROOT" in + [A-Za-z]:*|*\\*) CMDER_ROOT=$(cygpath -u "$CMDER_ROOT") || return 1 ;; +esac - if [ -d "${CMDER_ROOT}/config/profile.d" ] ; then - runProfiled "${CMDER_ROOT}/config/profile.d" - fi - - if [ -d "${CMDER_USER_CONFIG}/profile.d" ] ; then - runProfiled "${CMDER_USER_CONFIG}/profile.d" - fi - - - # Renaming to "config\user_profile.sh" to "user_profile.sh" for consistency. - if [ -f "$CMDER_ROOT/config/user-profile.sh" ] ; then - mv "$CMDER_ROOT/config/user-profile.sh" "$CMDER_ROOT/config/user_profile.sh" - fi - - CmderUserProfilePath="${CMDER_ROOT}/config/user_profile.sh" - if [ -f "${CMDER_ROOT}/config/user_profile.sh" ] ; then - . "${CMDER_ROOT}/config/user_profile.sh" - fi - - if [ "${CMDER_USER_CONFIG}" != "" ] ; then - # Renaming to "config\user_profile.sh" to "user_profile.sh" for consistency. - if [ -f "$CMDER_USER_CONFIG/user-profile.sh" ] ; then - mv "$CMDER_USER_CONFIG/user-profile.sh" "$CMDER_USER_CONFIG/user_profile.sh" - fi - - export PATH=${CMDER_USER_CONFIG}/bin:$PATH - - CmderUserProfilePath="${CMDER_USER_CONFIG}/user_profile.sh" - if [ -f "${CMDER_USER_CONFIG}/user_profile.sh" ] ; then - . "${CMDER_USER_CONFIG}/user_profile.sh" - fi - fi +if [ ! -r "$CMDER_ROOT/vendor/lib/lib_console.sh" ]; then + printf '[cmder] Cannot read console library: %s\n' "$CMDER_ROOT/vendor/lib/lib_console.sh" >&2 + return 1 +fi +. "$CMDER_ROOT/vendor/lib/lib_console.sh" || return 1 - if [ ! -f "${CmderUserProfilePath}" ] ; then - echo Creating user startup file: "${CmderUserProfilePath}" - cp "${CMDER_ROOT}/vendor/user_profile.sh.default" "${CmderUserProfilePath}" - fi +if [ ! -r "$CMDER_ROOT/vendor/cmder.sh" ]; then + printf '[cmder] Cannot read startup script: %s\n' "$CMDER_ROOT/vendor/cmder.sh" >&2 + return 1 fi +debug_output cmder_exinit "Sourcing startup script: $CMDER_ROOT/vendor/cmder.sh" +. "$CMDER_ROOT/vendor/cmder.sh" diff --git a/vendor/lib/lib_console.sh b/vendor/lib/lib_console.sh new file mode 100644 index 000000000..c5e972794 --- /dev/null +++ b/vendor/lib/lib_console.sh @@ -0,0 +1,5 @@ +debug_output() { + if [ "${debug_output:-0}" -gt 0 ]; then + printf '%s DEBUG(%s): %s\n\n' "$(date '+%H:%M:%S')" "$1" "$2" >&2 + fi +} diff --git a/vendor/lib/lib_git_shell.cmd b/vendor/lib/lib_git_shell.cmd new file mode 100644 index 000000000..3dab3a661 --- /dev/null +++ b/vendor/lib/lib_git_shell.cmd @@ -0,0 +1,100 @@ +@echo off + +if /i "%~1"=="initialize" goto initialize +exit /b + +:initialize +set "git_shell_type=%~2" +set "git_shell_label=%~3" +if not defined git_shell_label set "git_shell_label=start_git_shell" + +if not defined fast_init set "fast_init=0" +if not defined verbose_output set "verbose_output=0" +if not defined debug_output set "debug_output=0" +call "%~dp0lib_console.cmd" + +if not defined CMDER_ROOT ( + if defined ConEmuDir ( + for /f "delims=" %%i in ("%ConEmuDir%\..\..") do set "CMDER_ROOT=%%~fi" + ) else ( + for /f "delims=" %%i in ("%~dp0\..\..") do set "CMDER_ROOT=%%~fi" + ) +) +%print_debug% %git_shell_label% "Cmder root: '%CMDER_ROOT%'" + +set "gitCommand=" +if /i "%git_shell_type%"=="bash" if defined ConEmuDir ( + set "gitCommand=--command=%ConEmuBaseDirShort%\conemu-msys2-64.exe" +) +if defined gitCommand %print_debug% %git_shell_label% "ConEmu command: '%gitCommand%'" + +set "cmderExternalGitRoot=" +set "gitCmd=" +set "shellCmd=" + +if exist "%CMDER_ROOT%\vendor\git-for-windows\usr\bin\%git_shell_type%.exe" goto bundled +if exist "%ProgramFiles%\git\usr\bin\%git_shell_type%.exe" goto external_program_files +if exist "%ProgramFiles(x86)%\git\usr\bin\%git_shell_type%.exe" goto external_program_files_x86 +goto set_msystem + +:bundled +set "PATH=%CMDER_ROOT%\vendor\git-for-windows\usr\bin;%PATH%" +set "gitCmd=%CMDER_ROOT%\vendor\git-for-windows\git-cmd.exe" +set "shellCmd=%CMDER_ROOT%\vendor\git-for-windows\usr\bin\%git_shell_type%.exe" +%print_debug% %git_shell_label% "Using bundled Git for Windows." +for %%i in ("%CMDER_ROOT%\vendor\git-for-windows\etc\profile.d") do ( + if exist "%%~i\zz-cmder.sh" if not exist "%%~i\zz-cmder.sh\" ( + if exist "%%~i\cmder.sh" if not exist "%%~i\cmder.sh\" ( + %print_debug% %git_shell_label% "Removing legacy Bash hook: '%%~i\cmder.sh'" + del /q "%%~i\cmder.sh" + ) + ) +) +goto set_msystem + +:external_program_files +set "PATH=%ProgramFiles%\git\usr\bin;%PATH%" +set "gitCmd=%ProgramFiles%\git\git-cmd.exe" +set "shellCmd=%ProgramFiles%\git\usr\bin\%git_shell_type%.exe" +set "cmderExternalGitRoot=%ProgramFiles%\git" +%print_debug% %git_shell_label% "Using Git for Windows under Program Files." +goto setup_external + +:external_program_files_x86 +set "PATH=%ProgramFiles(x86)%\git\usr\bin;%PATH%" +set "gitCmd=%ProgramFiles(x86)%\git\git-cmd.exe" +set "shellCmd=%ProgramFiles(x86)%\git\usr\bin\%git_shell_type%.exe" +set "cmderExternalGitRoot=%ProgramFiles(x86)%\git" +%print_debug% %git_shell_label% "Using Git for Windows under Program Files x86." + +:setup_external +if not defined cmderExternalGitRoot goto set_msystem +if exist "%cmderExternalGitRoot%\etc\profile.d\zz-cmder_exinit.sh" goto existing_external_hook +goto install_external_hook + +:existing_external_hook +if not exist "%cmderExternalGitRoot%\etc\profile.d\cmder_exinit.sh" goto set_msystem + +:install_external_hook +"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -File "%~dp0..\setup_external_git_bash.ps1" -GitRoot "%cmderExternalGitRoot%" +if errorlevel 1 echo [%git_shell_label%] External Git setup did not complete. Continuing to %git_shell_type%. 1>&2 + +:set_msystem +%print_debug% %git_shell_label% "Git command: '%gitCmd%'" +%print_debug% %git_shell_label% "Shell executable: '%shellCmd%'" +if not defined shellCmd %print_debug% %git_shell_label% "%git_shell_type%.exe was not found." +if not defined shellCmd goto cleanup +if defined MSYSTEM goto cleanup + +for %%i in ("%gitCmd%") do ( + if exist "%%~dpiucrt64\bin\git.exe" set "MSYSTEM=UCRT64" +) +for %%i in ("%gitCmd%") do if not defined MSYSTEM if exist "%%~dpimingw64\bin\git.exe" set "MSYSTEM=MINGW64" +for %%i in ("%gitCmd%") do if not defined MSYSTEM if exist "%%~dpiclangarm64\bin\git.exe" set "MSYSTEM=CLANGARM64" +for %%i in ("%gitCmd%") do if not defined MSYSTEM if exist "%%~dpimingw32\bin\git.exe" set "MSYSTEM=MINGW32" + +:cleanup +set "cmderExternalGitRoot=" +set "git_shell_type=" +set "git_shell_label=" +exit /b diff --git a/vendor/setup_external_git_bash.ps1 b/vendor/setup_external_git_bash.ps1 new file mode 100644 index 000000000..27380d01c --- /dev/null +++ b/vendor/setup_external_git_bash.ps1 @@ -0,0 +1,240 @@ +param( + [Parameter(Mandatory = $true)] + [string]$GitRoot +) + +$ErrorActionPreference = 'Stop' + +function Write-CmderExternalGitHook { + param([string]$ProfileDir, [string]$Content) + + $hook = Join-Path $ProfileDir 'zz-cmder_exinit.sh' + $legacyHook = Join-Path $ProfileDir 'cmder_exinit.sh' + $backupPath = $null + if ([IO.File]::Exists($legacyHook)) { + $backupBase = 'cmder_exinit.sh.' + (Get-Date -Format 'yyyyMMdd-HHmmss') + $backupPath = Join-Path $ProfileDir "$backupBase.bak" + for ($suffix = 1; (Test-Path -LiteralPath $backupPath); $suffix++) { + $backupPath = Join-Path $ProfileDir "$backupBase-$suffix.bak" + } + } + + if ([IO.File]::Exists($hook)) { + if ($backupPath) { + [IO.File]::Move($legacyHook, $backupPath) + Write-Host "Backed up the old Cmder hook to '$backupPath'." + } + return + } + foreach ($name in @('zz-cmder.sh', 'cmder.sh')) { + if (Test-Path -LiteralPath (Join-Path $ProfileDir $name)) { return } + } + + $temp = Join-Path $ProfileDir ('.cmder-exinit-' + [guid]::NewGuid().ToString('N') + '.tmp') + $created = $false + $installed = $false + try { + $stream = [IO.File]::Open($temp, [IO.FileMode]::CreateNew, [IO.FileAccess]::Write) + $created = $true + try { + $bytes = [Text.Encoding]::UTF8.GetBytes($Content) + $stream.Write($bytes, 0, $bytes.Length) + } finally { + $stream.Dispose() + } + if ($backupPath) { + [IO.File]::Move($legacyHook, $backupPath) + Write-Host "Backed up the old Cmder hook to '$backupPath'." + } + [IO.File]::Move($temp, $hook) + $installed = $true + } finally { + if (!$installed -and $backupPath -and [IO.File]::Exists($backupPath) -and + ![IO.File]::Exists($legacyHook) -and ![IO.File]::Exists($hook)) { + [IO.File]::Move($backupPath, $legacyHook) + } + if ($created -and [IO.File]::Exists($temp)) { [IO.File]::Delete($temp) } + } +} + +function Request-CmderExternalGitHookElevation { + param([string]$ProfileDir, [string]$Content) + + # Pass the hook as data; the elevated process does not load scripts from the checkout. + $data = @{ ProfileDir = $ProfileDir; Content = $Content } | ConvertTo-Json -Compress + $payload = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($data)) + $writer = ${function:Write-CmderExternalGitHook}.ToString() + $command = @" +`$ErrorActionPreference = 'Stop' +`$data = [Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('$payload')) | ConvertFrom-Json +& { +$writer +} -ProfileDir `$data.ProfileDir -Content `$data.Content +"@ + $encoded = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($command)) + $powershell = Join-Path ([Environment]::SystemDirectory) 'WindowsPowerShell\v1.0\powershell.exe' + $process = Start-Process -FilePath $powershell -ArgumentList '-NoLogo', '-NoProfile', '-NonInteractive', '-EncodedCommand', $encoded ` + -WorkingDirectory ([Environment]::SystemDirectory) -Verb RunAs -Wait -PassThru + if ($process.ExitCode -ne 0) { + throw "Elevated Cmder hook installation failed (exit code $($process.ExitCode))." + } +} + +function Test-CmderExternalGitOptOut { + param([string]$Content, [string]$Entry) + + $section = '' + foreach ($line in ($Content -split '\r?\n')) { + if ($line -match '^[ \t]*\[([^\]]+)\][ \t]*$') { + $section = $Matches[1] + } elseif ($section -eq 'external-git-bash-opt-outs' -and $line -eq $Entry) { + return $true + } + } + return $false +} + +function Save-CmderExternalGitOptOut { + param([string]$ConfigDirectory, [string]$GitRoot, [string]$ComputerName) + + $null = [IO.Directory]::CreateDirectory($ConfigDirectory) + $path = Join-Path $ConfigDirectory 'user_cmder_state.txt' + $entry = "$ComputerName`t$GitRoot" + $stream = [IO.File]::Open($path, [IO.FileMode]::OpenOrCreate, [IO.FileAccess]::ReadWrite, [IO.FileShare]::None) + try { + $reader = New-Object IO.StreamReader($stream, [Text.Encoding]::UTF8, $true, 1024, $true) + try { + $content = $reader.ReadToEnd() + $encoding = $reader.CurrentEncoding + } finally { + $reader.Dispose() + } + if (Test-CmderExternalGitOptOut -Content $content -Entry $entry) { return } + $section = '' + foreach ($line in ($content -split '\r?\n')) { + if ($line -match '^[ \t]*\[([^\]]+)\][ \t]*$') { + $section = $Matches[1] + } + } + $stream.Seek(0, [IO.SeekOrigin]::End) | Out-Null + $writer = New-Object IO.StreamWriter($stream, $encoding, 1024, $true) + try { + if ($content -and !$content.EndsWith("`n")) { + $writer.WriteLine() + } + if ($section -ne 'external-git-bash-opt-outs') { + $writer.WriteLine('# Each entry is a computer name and external Git path, separated by a tab.') + $writer.WriteLine('# Cmder will not offer to configure Git Bash for the listed computer and path.') + $writer.WriteLine('# Remove an entry to allow the offer again.') + $writer.WriteLine('[external-git-bash-opt-outs]') + } + $writer.WriteLine($entry) + } finally { + $writer.Dispose() + } + } finally { + $stream.Dispose() + } +} + +function Get-CmderExternalGitHookContent { + param([string]$CmderRoot, [string]$UserProfile) + + $startup = Join-Path $CmderRoot 'vendor\cmder.sh' + if (!(Test-Path -LiteralPath $startup -PathType Leaf)) { + throw "Cannot install the Git Bash hook: Cmder startup script '$startup' is missing." + } + + $source = Join-Path $CmderRoot 'vendor\cmder_exinit' + $shim = [IO.File]::ReadAllText($source) -replace "`r`n", "`n" + # Do not use the installer's Cmder path by default in another Windows profile. + $quotedRoot = "'" + $CmderRoot.Replace("'", "'\''") + "'" + $quotedProfile = "'" + $UserProfile.Replace("'", "'\''") + "'" + $assignment = @" +if [ -z "`${CMDER_ROOT:-}" ]; then + [ "`${USERPROFILE:-}" = $quotedProfile ] || return 0 + CMDER_ROOT=$quotedRoot +fi +"@ -replace "`r`n", "`n" + return "$assignment`n$shim" +} + +function Install-CmderExternalGitHook { + param([string]$GitRoot, [string]$CmderRoot, [string]$UserProfile, [string]$ComputerName = [Environment]::MachineName) + + $profileDir = Join-Path $GitRoot 'etc\profile.d' + $hook = Join-Path $profileDir 'zz-cmder_exinit.sh' + $legacyHook = Join-Path $profileDir 'cmder_exinit.sh' + if (!(Test-Path -LiteralPath $profileDir -PathType Container)) { + Write-Warning "Git Bash has no profile directory at '$profileDir'. Set up the Cmder hook manually if needed." + return + } + foreach ($name in @('zz-cmder.sh', 'cmder.sh')) { + if (Test-Path -LiteralPath (Join-Path $profileDir $name)) { return } + } + $hasLegacyHook = [IO.File]::Exists($legacyHook) + if ((Test-Path -LiteralPath $legacyHook) -and !$hasLegacyHook) { + Write-Warning "Cannot migrate the old Cmder hook because '$legacyHook' is not a file." + return + } + if ((Test-Path -LiteralPath $hook) -and $hasLegacyHook) { + $content = Get-CmderExternalGitHookContent -CmderRoot $CmderRoot -UserProfile $UserProfile + try { + Write-CmderExternalGitHook -ProfileDir $profileDir -Content $content + } catch [UnauthorizedAccessException] { + Write-Host "Administrator permission is required to back up the old Cmder hook in '$profileDir'." + Request-CmderExternalGitHookElevation -ProfileDir $profileDir -Content $content + } + return + } + if (Test-Path -LiteralPath $hook) { return } + + $configDirectory = Join-Path $CmderRoot 'config' + $optOut = Join-Path $configDirectory 'user_cmder_state.txt' + $entry = "$ComputerName`t$GitRoot" + if (!$hasLegacyHook -and (Test-Path -LiteralPath $optOut)) { + if (Test-CmderExternalGitOptOut -Content ([IO.File]::ReadAllText($optOut)) -Entry $entry) { + if ($env:debug_output -match '^[1-9]') { + Write-Host "[setup_external_git_bash] Skipping the setup offer: '$GitRoot' is opted out in '$optOut'." + } + return + } + } + if (!$hasLegacyHook) { + Write-Host "External Git Bash at '$GitRoot' is not configured to load Cmder's startup files." + Write-Host "Installing '$hook' will also enable Cmder in standalone Git Bash for this Windows profile, using '$CmderRoot' when CMDER_ROOT is unset." + do { + $answer = ([string](Read-Host 'Install the Cmder hook? [y/N/never] (N = not now)')).Trim() + } until ($answer -match '^(y|yes|n|no|never)?$') + + if ($answer -eq 'never') { + Save-CmderExternalGitOptOut -ConfigDirectory $configDirectory -GitRoot $GitRoot -ComputerName $ComputerName + Write-Host "Saved the opt-out in '$optOut'. Remove this computer and Git path entry to allow the offer again." + return + } + if ($answer -notmatch '^y(es)?$') { + return + } + } else { + Write-Host "Migrating the existing Cmder hook in '$profileDir' without prompting." + } + + $content = Get-CmderExternalGitHookContent -CmderRoot $CmderRoot -UserProfile $UserProfile + try { + Write-CmderExternalGitHook -ProfileDir $profileDir -Content $content + } catch [UnauthorizedAccessException] { + Write-Host "Administrator permission is required to update the Cmder hook in '$profileDir'." + Request-CmderExternalGitHookElevation -ProfileDir $profileDir -Content $content + } + if ($hasLegacyHook) { + Write-Host "The old Cmder hook was renamed to a dated .bak file." + } + Write-Host "The Cmder hook is configured in '$profileDir'." +} + +if ([Console]::IsInputRedirected -or [Console]::IsOutputRedirected) { return } + +if (!$env:USERPROFILE -or !$env:CMDER_ROOT) { + throw 'External Git Bash setup requires the Cmder root and a Windows user profile.' +} +Install-CmderExternalGitHook -GitRoot $GitRoot -CmderRoot $env:CMDER_ROOT -UserProfile $env:USERPROFILE diff --git a/vendor/start_git_bash.cmd b/vendor/start_git_bash.cmd index 679c5af9e..a45504232 100644 --- a/vendor/start_git_bash.cmd +++ b/vendor/start_git_bash.cmd @@ -1,51 +1,6 @@ @echo off +call "%~dp0lib\lib_git_shell.cmd" initialize bash start_git_bash -if not defined CMDER_ROOT ( - if defined ConEmuDir ( - for /f "delims=" %%i in ("%ConEmuDir%\..\..") do ( - set "CMDER_ROOT=%%~fi" - ) - ) else ( - for /f "delims=" %%i in ("%~dp0\..") do ( - set "CMDER_ROOT=%%~fi" - ) - ) -) - -if defined ConEmuDir ( - set "gitCommand=--command=%ConEmuBaseDirShort%\conemu-msys2-64.exe" -) - -if exist "%CMDER_ROOT%\vendor\git-for-windows" ( - set "PATH=%CMDER_ROOT%\vendor\git-for-windows\usr\bin;%PATH%" - set "gitCmd=%CMDER_ROOT%\vendor\git-for-windows\git-cmd.exe" - set "bashCmd=%CMDER_ROOT%\vendor\git-for-windows\usr\bin\bash.exe" -) else if exist "%ProgramFiles%\git" ( - set "PATH=%ProgramFiles%\git\usr\bin;%PATH%" - set "gitCmd=%ProgramFiles%\git\git-cmd.exe" - set "bashCmd=%ProgramFiles%\git\usr\bin\bash.exe" - if not exist "%ProgramFiles%\git\etc\profile.d\cmder_exinit.sh" ( - echo Run 'mklink "%ProgramFiles%\git\etc\profile.d\cmder_exinit.sh" "%CMDER_ROOT%\vendor\cmder_exinit"' in 'cmd::Cmder as Admin' to use Cmder with external Git Bash - echo. - echo or - echo. - echo Run 'echo "" ^> "%ProgramFiles%\git\etc\profile.d\cmder_exinit.sh"' in 'cmd::Cmder as Admin' to disable this message. - ) -) else if exist "%ProgramFiles(x86)%\git" ( - set "PATH=%ProgramFiles(x86)%\git\usr\bin;%PATH%" - set "gitCmd=%ProgramFiles(x86)%\git\git-cmd.exe" - set "bashCmd=%ProgramFiles(x86)%\git\usr\bin\bash.exe" - if not exist "%ProgramFiles(x86)%\git\etc\profile.d\cmder_exinit.sh" ( - echo Run 'mklink "%ProgramFiles^(x86^)%\git\etc\profile.d\cmder_exinit.sh" "%CMDER_ROOT%\vendor\cmder_exinit"' in 'cmd::Cmder as Admin' to use Cmder with external Git Bash - echo. - echo or - echo. - echo Run 'echo "" ^> "%ProgramFiles^(x86^)%\git\etc\profile.d\cmder_exinit.sh"' in 'cmd::Cmder as Admin' to disable this message. - ) -) - -if defined ConEmuDir ( - "%gitCmd%" --no-cd %gitCommand% "%bashCmd%" --login -i -) else ( - "%bashCmd%" --login -i -) +%print_debug% start_git_bash "Launching shell." +if defined shellCmd if defined ConEmuDir "%gitCmd%" --no-cd %gitCommand% "%shellCmd%" --login -i +if defined shellCmd if not defined ConEmuDir "%shellCmd%" --login -i diff --git a/vendor/start_git_mintty.cmd b/vendor/start_git_mintty.cmd index ea4f09a9f..afb9edb85 100644 --- a/vendor/start_git_mintty.cmd +++ b/vendor/start_git_mintty.cmd @@ -1,40 +1,5 @@ @echo off +call "%~dp0lib\lib_git_shell.cmd" initialize mintty start_git_mintty -if not defined CMDER_ROOT ( - if defined ConEmuDir ( - for /f "delims=" %%i in ("%ConEmuDir%\..\..") do ( - set "CMDER_ROOT=%%~fi" - ) - ) else ( - for /f "delims=" %%i in ("%~dp0\..") do ( - set "CMDER_ROOT=%%~fi" - ) - ) -) - -if exist "%CMDER_ROOT%\vendor\git-for-windows" ( - set "PATH=%CMDER_ROOT%\vendor\git-for-windows\usr\bin;%PATH%" - set "gitCmd=%CMDER_ROOT%\vendor\git-for-windows\usr\bin\mintty.exe" -) else if exist "%ProgramFiles%\git" ( - set "PATH=%ProgramFiles%\git\usr\bin;%PATH%" - set "gitCmd=%ProgramFiles%\git\usr\bin\mintty.exe" - if not exist "%ProgramFiles%\git\etc\profile.d\cmder_exinit.sh" ( - echo Run 'mklink "%ProgramFiles%\git\etc\profile.d\cmder_exinit.sh" "%CMDER_ROOT%\vendor\cmder_exinit"' in 'cmd::Cmder as Admin' to use Cmder with external Git Bash - echo. - echo or - echo. - echo Run 'echo "" ^> "%ProgramFiles%\git\etc\profile.d\cmder_exinit.sh"' in 'cmd::Cmder as Admin' to disable this message. - ) -) else if exist "%ProgramFiles(x86)%\git" ( - set "PATH=%ProgramFiles(x86)%\git\usr\bin;%PATH%" - set "gitCmd=%ProgramFiles(x86)%\git\usr\bin\mintty.exe" - if not exist "%ProgramFiles(x86)%\git\etc\profile.d\cmder_exinit.sh" ( - echo Run 'mklink "%ProgramFiles^(x86^)%\git\etc\profile.d\cmder_exinit.sh" "%CMDER_ROOT%\vendor\cmder_exinit"' in 'cmd::Cmder as Admin' to use Cmder with external Git Bash - echo. - echo or - echo. - echo Run 'echo "" ^> "%ProgramFiles^(x86^)%\git\etc\profile.d\cmder_exinit.sh"' in 'cmd::Cmder as Admin' to disable this message. - ) -) - -"%gitCmd%" /bin/bash -l +%print_debug% start_git_mintty "Launching Mintty." +if defined shellCmd "%shellCmd%" /bin/bash -l -i