@@ -2880,6 +2880,66 @@ int xwcstoutf(char *utf, const wchar_t *wcs, size_t utflen)
2880
2880
return -1 ;
2881
2881
}
2882
2882
2883
+ #ifdef ENSURE_MSYSTEM_IS_SET
2884
+ #if !defined(RUNTIME_PREFIX ) || !defined(HAVE_WPGMPTR )
2885
+ static size_t append_system_bin_dirs (char * path UNUSED , size_t size UNUSED )
2886
+ {
2887
+ return 0 ;
2888
+ }
2889
+ #else
2890
+ static size_t append_system_bin_dirs (char * path , size_t size )
2891
+ {
2892
+ char prefix [32768 ];
2893
+ const char * slash ;
2894
+ size_t len = xwcstoutf (prefix , _wpgmptr , sizeof (prefix )), off = 0 ;
2895
+
2896
+ if (len == 0 || len >= sizeof (prefix ) ||
2897
+ !(slash = find_last_dir_sep (prefix )))
2898
+ return 0 ;
2899
+ /* strip trailing `git.exe` */
2900
+ len = slash - prefix ;
2901
+
2902
+ /* strip trailing `cmd` or `mingw64\bin` or `mingw32\bin` or `bin` or `libexec\git-core` */
2903
+ if (strip_suffix_mem (prefix , & len , "\\mingw64\\libexec\\git-core" ) ||
2904
+ strip_suffix_mem (prefix , & len , "\\mingw64\\bin" ))
2905
+ off += xsnprintf (path + off , size - off ,
2906
+ "%.*s\\mingw64\\bin;" , (int )len , prefix );
2907
+ else if (strip_suffix_mem (prefix , & len , "\\clangarm64\\libexec\\git-core" ) ||
2908
+ strip_suffix_mem (prefix , & len , "\\clangarm64\\bin" ))
2909
+ off += xsnprintf (path + off , size - off ,
2910
+ "%.*s\\clangarm64\\bin;" , (int )len , prefix );
2911
+ else if (strip_suffix_mem (prefix , & len , "\\mingw32\\libexec\\git-core" ) ||
2912
+ strip_suffix_mem (prefix , & len , "\\mingw32\\bin" ))
2913
+ off += xsnprintf (path + off , size - off ,
2914
+ "%.*s\\mingw32\\bin;" , (int )len , prefix );
2915
+ else if (strip_suffix_mem (prefix , & len , "\\cmd" ) ||
2916
+ strip_suffix_mem (prefix , & len , "\\bin" ) ||
2917
+ strip_suffix_mem (prefix , & len , "\\libexec\\git-core" ))
2918
+ off += xsnprintf (path + off , size - off ,
2919
+ "%.*s\\mingw%d\\bin;" , (int )len , prefix ,
2920
+ (int )(sizeof (void * ) * 8 ));
2921
+ else
2922
+ return 0 ;
2923
+
2924
+ off += xsnprintf (path + off , size - off ,
2925
+ "%.*s\\usr\\bin;" , (int )len , prefix );
2926
+ return off ;
2927
+ }
2928
+ #endif
2929
+ #endif
2930
+
2931
+ static int is_system32_path (const char * path )
2932
+ {
2933
+ WCHAR system32 [MAX_PATH ], wpath [MAX_PATH ];
2934
+
2935
+ if (xutftowcs_path (wpath , path ) < 0 ||
2936
+ !GetSystemDirectoryW (system32 , ARRAY_SIZE (system32 )) ||
2937
+ _wcsicmp (system32 , wpath ))
2938
+ return 0 ;
2939
+
2940
+ return 1 ;
2941
+ }
2942
+
2883
2943
static void setup_windows_environment (void )
2884
2944
{
2885
2945
char * tmp = getenv ("TMPDIR" );
@@ -2920,7 +2980,8 @@ static void setup_windows_environment(void)
2920
2980
strbuf_addstr (& buf , tmp );
2921
2981
if ((tmp = getenv ("HOMEPATH" ))) {
2922
2982
strbuf_addstr (& buf , tmp );
2923
- if (is_directory (buf .buf ))
2983
+ if (!is_system32_path (buf .buf ) &&
2984
+ is_directory (buf .buf ))
2924
2985
setenv ("HOME" , buf .buf , 1 );
2925
2986
else
2926
2987
tmp = NULL ; /* use $USERPROFILE */
@@ -2931,6 +2992,41 @@ static void setup_windows_environment(void)
2931
2992
if (!tmp && (tmp = getenv ("USERPROFILE" )))
2932
2993
setenv ("HOME" , tmp , 1 );
2933
2994
}
2995
+
2996
+ if (!getenv ("PLINK_PROTOCOL" ))
2997
+ setenv ("PLINK_PROTOCOL" , "ssh" , 0 );
2998
+
2999
+ #ifdef ENSURE_MSYSTEM_IS_SET
3000
+ if (!(tmp = getenv ("MSYSTEM" )) || !tmp [0 ]) {
3001
+ const char * home = getenv ("HOME" ), * path = getenv ("PATH" );
3002
+ char buf [32768 ];
3003
+ size_t off = 0 ;
3004
+
3005
+ #if defined(__aarch64__ ) || defined(_M_ARM64 ) || defined(_M_ARM64EC )
3006
+ setenv ("MSYSTEM" , "CLANGARM64" , 1 );
3007
+ #elif defined(__MINGW64__ ) || defined(_M_AMD64 )
3008
+ setenv ("MSYSTEM" , "MINGW64" , 1 );
3009
+ #else
3010
+ setenv ("MSYSTEM" , "MINGW32" , 1 );
3011
+ #endif
3012
+
3013
+ if (home )
3014
+ off += xsnprintf (buf + off , sizeof (buf ) - off ,
3015
+ "%s\\bin;" , home );
3016
+ off += append_system_bin_dirs (buf + off , sizeof (buf ) - off );
3017
+ if (path )
3018
+ off += xsnprintf (buf + off , sizeof (buf ) - off ,
3019
+ "%s" , path );
3020
+ else if (off > 0 )
3021
+ buf [off - 1 ] = '\0' ;
3022
+ else
3023
+ buf [0 ] = '\0' ;
3024
+ setenv ("PATH" , buf , 1 );
3025
+ }
3026
+ #endif
3027
+
3028
+ if (!getenv ("LC_ALL" ) && !getenv ("LC_CTYPE" ) && !getenv ("LANG" ))
3029
+ setenv ("LC_CTYPE" , "C.UTF-8" , 1 );
2934
3030
}
2935
3031
2936
3032
static PSID get_current_user_sid (void )
0 commit comments