From ab4a332e7f30799bbee61e6308573151a64f7ef9 Mon Sep 17 00:00:00 2001 From: 5hif7y <44300833+5hif7y@users.noreply.github.com> Date: Sat, 14 Nov 2026 14:34:52 -0300 Subject: [PATCH] Find Git Bash through git --exec-path in mirai.cmd and the tool alike The tool looked only in %ProgramFiles%\Git, so bench compare and mirai measure failed with Git installed elsewhere, and mirai.cmd probed four folders before asking git. Both now take one rule: git --exec-path names /mingw64/libexec/git-core, and Git Bash is /bin/bash.exe. It also finds a Git reached through a shim, such as Scoop's. Co-Authored-By: Claude Opus 5.5 --- mirai.cmd | 28 +++++++--------------------- tools/mirai/shell.odin | 16 +++++++++++----- tools/mirai/shell_test.odin | 12 ++++++++++++ 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/mirai.cmd b/mirai.cmd index 588f63359..04f993a10 100644 --- a/mirai.cmd +++ b/mirai.cmd @@ -2,32 +2,18 @@ rem Lets PowerShell and cmd find ./mirai: runs the bash launcher with Git Bash. The path goes with rem forward slashes because the launcher finds the engine root from the slashes of $0. setlocal -set "bash=%ProgramFiles%\Git\bin\bash.exe" -if exist "%bash%" goto run -set "bash=%ProgramFiles(x86)%\Git\bin\bash.exe" -if exist "%bash%" goto run -set "bash=%LocalAppData%\Programs\Git\bin\bash.exe" -if exist "%bash%" goto run -if defined GIT_INSTALL_ROOT set "bash=%GIT_INSTALL_ROOT%\bin\bash.exe" -if exist "%bash%" goto run -rem Git for Windows installed elsewhere: git.exe in PATH names its root. +rem Git for Windows is found by its git, as tools/mirai finds it: git --exec-path names +rem \mingw64\libexec\git-core, and its bash is \bin\bash.exe. set "bash=" -for /f "delims=" %%i in ('git --exec-path 2^>nul') do call :from_exec "%%i" -if defined bash goto run -goto missing - -:from_exec -for %%j in ("%~1\..\..\..") do ( - if exist "%%~fj\bin\bash.exe" set "bash=%%~fj\bin\bash.exe" - if not defined bash if exist "%%~fj\usr\bin\bash.exe" set "bash=%%~fj\usr\bin\bash.exe" & set "bashopt=-l" -) -goto :eof +for /f "delims=" %%i in ('git --exec-path 2^>nul') do set "bash=%%~fi\..\..\..\bin\bash.exe" +if not defined bash goto missing +if not exist "%bash%" goto missing :run set "launcher=%~dp0mirai" -"%bash%" %bashopt% "%launcher:\=/%" %* +"%bash%" "%launcher:\=/%" %* exit /b %errorlevel% :missing -echo mirai: Git Bash is missing; install Git for Windows or run ./mirai from bash 1>&2 +echo mirai: Git Bash is missing; install Git for Windows with git on the PATH, or run ./mirai from bash 1>&2 exit /b 1 diff --git a/tools/mirai/shell.odin b/tools/mirai/shell.odin index 767996c0f..3d86dee6c 100644 --- a/tools/mirai/shell.odin +++ b/tools/mirai/shell.odin @@ -3,19 +3,25 @@ package main import "core:fmt" import "core:os" import "core:strings" +import "mirai:std/subprocess" -// The shell that runs the scripts of the repository: Git Bash on Windows, by the path mirai.cmd -// uses (the bash of the PATH is usually the one of WSL, which has no odin), sh elsewhere. +// The shell that runs the scripts of the repository: Git Bash on Windows, found as mirai.cmd finds +// it, through git --exec-path (the bash of the PATH is usually the one of WSL, which has no odin); +// sh elsewhere. shell_executable :: proc() -> (path:string,found:bool) { when ODIN_OS==.Windows { - program_files,_:=os.lookup_env("ProgramFiles",context.allocator) - bash:=join(program_files,"Git","bin","bash.exe") + state,exec,_,error:=subprocess.exec({command={"git","--exec-path"}},context.allocator) + if error!=nil || state.exit_code!=0 {return "git --exec-path",false} + bash:=git_bash(strings.trim_space(string(exec))) return bash,os.is_file(bash) } else { return "sh",true } } +// The bash of the Git for Windows whose git --exec-path is exec: /mingw64/libexec/git-core. +git_bash :: proc(exec:string) -> string {return join(exec,"..","..","..","bin","bash.exe")} + // command with the script it starts run by shell_executable, where the system cannot start one // itself: on Windows, a file named with a path that begins with #! (./mirai). // ok is false, after saying why, when the script needs a shell that is not there. @@ -24,7 +30,7 @@ script_command :: proc(command:[]string) -> (resolved:[]string,ok:bool) { if len(command)==0 || !strings.contains_any(command[0],"/\\") || !shebang(command[0]) {return command,true} shell,found:=shell_executable() if !found { - fmt.eprintfln("mirai: %s is an sh script and Git Bash is missing in %s; install Git for Windows",command[0],shell) + fmt.eprintfln("mirai: %s is an sh script and no Git Bash was found by %s; install Git for Windows with git on the PATH",command[0],shell) return nil,false } list:=make([dynamic]string,0,len(command)+1) diff --git a/tools/mirai/shell_test.odin b/tools/mirai/shell_test.odin index 907d50a16..724081706 100644 --- a/tools/mirai/shell_test.odin +++ b/tools/mirai/shell_test.odin @@ -47,3 +47,15 @@ a_content_tool_runs_with_its_collections_and_its_arguments :: proc(t:^testing.T) testing.expect(t,!slice.contains(alone[:],"--"),"nothing after it") testing.expect_value(t,alone[2],"C:/game/tools/content") } + +// Git Bash is found from the root git --exec-path names, also through a shim such as Scoop's, +// whose git reports the folder it was installed in. +@(test) +git_bash_is_found_from_the_exec_path_of_git :: proc(t:^testing.T) { + context.allocator=context.temp_allocator + cases:=[?]struct{exec:string,root:string}{ + {"C:/Program Files/Git/mingw64/libexec/git-core","C:/Program Files/Git"}, + {"C:/Users/u/scoop/apps/git/current/mingw64/libexec/git-core","C:/Users/u/scoop/apps/git/current"}, + } + for c in cases {testing.expect_value(t,git_bash(c.exec),join(c.root,"bin","bash.exe"))} +}