@@ -3268,6 +3268,57 @@ int xwcstoutf(char *utf, const wchar_t *wcs, size_t utflen)
32683268 return -1 ;
32693269}
32703270
3271+ #ifdef ENSURE_MSYSTEM_IS_SET
3272+ #if !defined(RUNTIME_PREFIX ) || !defined(HAVE_WPGMPTR ) || !defined(MINGW_PREFIX )
3273+ static size_t append_system_bin_dirs (char * path UNUSED , size_t size UNUSED )
3274+ {
3275+ return 0 ;
3276+ }
3277+ #else
3278+ static size_t append_system_bin_dirs (char * path , size_t size )
3279+ {
3280+ char prefix [32768 ];
3281+ const char * slash ;
3282+ size_t len = xwcstoutf (prefix , _wpgmptr , sizeof (prefix )), off = 0 ;
3283+
3284+ if (len == 0 || len >= sizeof (prefix ) ||
3285+ !(slash = find_last_dir_sep (prefix )))
3286+ return 0 ;
3287+ /* strip trailing `git.exe` */
3288+ len = slash - prefix ;
3289+
3290+ /* strip trailing `cmd` or `<mingw-prefix>\bin` or `bin` or `libexec\git-core` */
3291+ if (strip_suffix_mem (prefix , & len , "\\" MINGW_PREFIX "\\libexec\\git-core" ) ||
3292+ strip_suffix_mem (prefix , & len , "\\" MINGW_PREFIX "\\bin" ))
3293+ off += xsnprintf (path + off , size - off ,
3294+ "%.*s\\" MINGW_PREFIX "\\bin;" , (int )len , prefix );
3295+ else if (strip_suffix_mem (prefix , & len , "\\cmd" ) ||
3296+ strip_suffix_mem (prefix , & len , "\\bin" ) ||
3297+ strip_suffix_mem (prefix , & len , "\\libexec\\git-core" ))
3298+ off += xsnprintf (path + off , size - off ,
3299+ "%.*s\\" MINGW_PREFIX "\\bin;" , (int )len , prefix );
3300+ else
3301+ return 0 ;
3302+
3303+ off += xsnprintf (path + off , size - off ,
3304+ "%.*s\\usr\\bin;" , (int )len , prefix );
3305+ return off ;
3306+ }
3307+ #endif
3308+ #endif
3309+
3310+ static int is_system32_path (const char * path )
3311+ {
3312+ WCHAR system32 [MAX_PATH ], wpath [MAX_PATH ];
3313+
3314+ if (xutftowcs_path (wpath , path ) < 0 ||
3315+ !GetSystemDirectoryW (system32 , ARRAY_SIZE (system32 )) ||
3316+ _wcsicmp (system32 , wpath ))
3317+ return 0 ;
3318+
3319+ return 1 ;
3320+ }
3321+
32713322static void setup_windows_environment (void )
32723323{
32733324 char * tmp = getenv ("TMPDIR" );
@@ -3319,7 +3370,8 @@ static void setup_windows_environment(void)
33193370 strbuf_addstr (& buf , tmp );
33203371 if ((tmp = getenv ("HOMEPATH" ))) {
33213372 strbuf_addstr (& buf , tmp );
3322- if (is_directory (buf .buf ))
3373+ if (!is_system32_path (buf .buf ) &&
3374+ is_directory (buf .buf ))
33233375 setenv ("HOME" , buf .buf , 1 );
33243376 else
33253377 tmp = NULL ; /* use $USERPROFILE */
@@ -3331,6 +3383,35 @@ static void setup_windows_environment(void)
33313383 setenv ("HOME" , tmp , 1 );
33323384 }
33333385
3386+ if (!getenv ("PLINK_PROTOCOL" ))
3387+ setenv ("PLINK_PROTOCOL" , "ssh" , 0 );
3388+
3389+ #ifdef ENSURE_MSYSTEM_IS_SET
3390+ if (!(tmp = getenv ("MSYSTEM" )) || !tmp [0 ]) {
3391+ const char * home = getenv ("HOME" ), * path = getenv ("PATH" );
3392+ char buf [32768 ];
3393+ size_t off = 0 ;
3394+
3395+ setenv ("MSYSTEM" , ENSURE_MSYSTEM_IS_SET , 1 );
3396+
3397+ if (home )
3398+ off += xsnprintf (buf + off , sizeof (buf ) - off ,
3399+ "%s\\bin;" , home );
3400+ off += append_system_bin_dirs (buf + off , sizeof (buf ) - off );
3401+ if (path )
3402+ off += xsnprintf (buf + off , sizeof (buf ) - off ,
3403+ "%s" , path );
3404+ else if (off > 0 )
3405+ buf [off - 1 ] = '\0' ;
3406+ else
3407+ buf [0 ] = '\0' ;
3408+ setenv ("PATH" , buf , 1 );
3409+ }
3410+ #endif
3411+
3412+ if (!getenv ("LC_ALL" ) && !getenv ("LC_CTYPE" ) && !getenv ("LANG" ))
3413+ setenv ("LC_CTYPE" , "C.UTF-8" , 1 );
3414+
33343415 /*
33353416 * Change 'core.symlinks' default to false, unless native symlinks are
33363417 * enabled in MSys2 (via 'MSYS=winsymlinks:nativestrict'). Thus we can
0 commit comments