@@ -3178,6 +3178,66 @@ int xwcstoutf(char *utf, const wchar_t *wcs, size_t utflen)
31783178 return -1 ;
31793179}
31803180
3181+ #ifdef ENSURE_MSYSTEM_IS_SET
3182+ #if !defined(RUNTIME_PREFIX ) || !defined(HAVE_WPGMPTR )
3183+ static size_t append_system_bin_dirs (char * path UNUSED , size_t size UNUSED )
3184+ {
3185+ return 0 ;
3186+ }
3187+ #else
3188+ static size_t append_system_bin_dirs (char * path , size_t size )
3189+ {
3190+ char prefix [32768 ];
3191+ const char * slash ;
3192+ size_t len = xwcstoutf (prefix , _wpgmptr , sizeof (prefix )), off = 0 ;
3193+
3194+ if (len == 0 || len >= sizeof (prefix ) ||
3195+ !(slash = find_last_dir_sep (prefix )))
3196+ return 0 ;
3197+ /* strip trailing `git.exe` */
3198+ len = slash - prefix ;
3199+
3200+ /* strip trailing `cmd` or `mingw64\bin` or `mingw32\bin` or `bin` or `libexec\git-core` */
3201+ if (strip_suffix_mem (prefix , & len , "\\mingw64\\libexec\\git-core" ) ||
3202+ strip_suffix_mem (prefix , & len , "\\mingw64\\bin" ))
3203+ off += xsnprintf (path + off , size - off ,
3204+ "%.*s\\mingw64\\bin;" , (int )len , prefix );
3205+ else if (strip_suffix_mem (prefix , & len , "\\clangarm64\\libexec\\git-core" ) ||
3206+ strip_suffix_mem (prefix , & len , "\\clangarm64\\bin" ))
3207+ off += xsnprintf (path + off , size - off ,
3208+ "%.*s\\clangarm64\\bin;" , (int )len , prefix );
3209+ else if (strip_suffix_mem (prefix , & len , "\\mingw32\\libexec\\git-core" ) ||
3210+ strip_suffix_mem (prefix , & len , "\\mingw32\\bin" ))
3211+ off += xsnprintf (path + off , size - off ,
3212+ "%.*s\\mingw32\\bin;" , (int )len , prefix );
3213+ else if (strip_suffix_mem (prefix , & len , "\\cmd" ) ||
3214+ strip_suffix_mem (prefix , & len , "\\bin" ) ||
3215+ strip_suffix_mem (prefix , & len , "\\libexec\\git-core" ))
3216+ off += xsnprintf (path + off , size - off ,
3217+ "%.*s\\mingw%d\\bin;" , (int )len , prefix ,
3218+ (int )(sizeof (void * ) * 8 ));
3219+ else
3220+ return 0 ;
3221+
3222+ off += xsnprintf (path + off , size - off ,
3223+ "%.*s\\usr\\bin;" , (int )len , prefix );
3224+ return off ;
3225+ }
3226+ #endif
3227+ #endif
3228+
3229+ static int is_system32_path (const char * path )
3230+ {
3231+ WCHAR system32 [MAX_PATH ], wpath [MAX_PATH ];
3232+
3233+ if (xutftowcs_path (wpath , path ) < 0 ||
3234+ !GetSystemDirectoryW (system32 , ARRAY_SIZE (system32 )) ||
3235+ _wcsicmp (system32 , wpath ))
3236+ return 0 ;
3237+
3238+ return 1 ;
3239+ }
3240+
31813241static void setup_windows_environment (void )
31823242{
31833243 char * tmp = getenv ("TMPDIR" );
@@ -3218,7 +3278,8 @@ static void setup_windows_environment(void)
32183278 strbuf_addstr (& buf , tmp );
32193279 if ((tmp = getenv ("HOMEPATH" ))) {
32203280 strbuf_addstr (& buf , tmp );
3221- if (is_directory (buf .buf ))
3281+ if (!is_system32_path (buf .buf ) &&
3282+ is_directory (buf .buf ))
32223283 setenv ("HOME" , buf .buf , 1 );
32233284 else
32243285 tmp = NULL ; /* use $USERPROFILE */
@@ -3230,6 +3291,41 @@ static void setup_windows_environment(void)
32303291 setenv ("HOME" , tmp , 1 );
32313292 }
32323293
3294+ if (!getenv ("PLINK_PROTOCOL" ))
3295+ setenv ("PLINK_PROTOCOL" , "ssh" , 0 );
3296+
3297+ #ifdef ENSURE_MSYSTEM_IS_SET
3298+ if (!(tmp = getenv ("MSYSTEM" )) || !tmp [0 ]) {
3299+ const char * home = getenv ("HOME" ), * path = getenv ("PATH" );
3300+ char buf [32768 ];
3301+ size_t off = 0 ;
3302+
3303+ #if defined(__aarch64__ ) || defined(_M_ARM64 ) || defined(_M_ARM64EC )
3304+ setenv ("MSYSTEM" , "CLANGARM64" , 1 );
3305+ #elif defined(__MINGW64__ ) || defined(_M_AMD64 )
3306+ setenv ("MSYSTEM" , "MINGW64" , 1 );
3307+ #else
3308+ setenv ("MSYSTEM" , "MINGW32" , 1 );
3309+ #endif
3310+
3311+ if (home )
3312+ off += xsnprintf (buf + off , sizeof (buf ) - off ,
3313+ "%s\\bin;" , home );
3314+ off += append_system_bin_dirs (buf + off , sizeof (buf ) - off );
3315+ if (path )
3316+ off += xsnprintf (buf + off , sizeof (buf ) - off ,
3317+ "%s" , path );
3318+ else if (off > 0 )
3319+ buf [off - 1 ] = '\0' ;
3320+ else
3321+ buf [0 ] = '\0' ;
3322+ setenv ("PATH" , buf , 1 );
3323+ }
3324+ #endif
3325+
3326+ if (!getenv ("LC_ALL" ) && !getenv ("LC_CTYPE" ) && !getenv ("LANG" ))
3327+ setenv ("LC_CTYPE" , "C.UTF-8" , 1 );
3328+
32333329 /*
32343330 * Change 'core.symlinks' default to false, unless native symlinks are
32353331 * enabled in MSys2 (via 'MSYS=winsymlinks:nativestrict'). Thus we can
0 commit comments