@@ -2841,6 +2841,59 @@ int xwcstoutf(char *utf, const wchar_t *wcs, size_t utflen)
2841
2841
return -1 ;
2842
2842
}
2843
2843
2844
+ #ifdef ENSURE_MSYSTEM_IS_SET
2845
+ static size_t append_system_bin_dirs (char * path , size_t size )
2846
+ {
2847
+ #if !defined(RUNTIME_PREFIX ) || !defined(HAVE_WPGMPTR )
2848
+ return 0 ;
2849
+ #else
2850
+ char prefix [32768 ];
2851
+ const char * slash ;
2852
+ size_t len = xwcstoutf (prefix , _wpgmptr , sizeof (prefix )), off = 0 ;
2853
+
2854
+ if (len == 0 || len >= sizeof (prefix ) ||
2855
+ !(slash = find_last_dir_sep (prefix )))
2856
+ return 0 ;
2857
+ /* strip trailing `git.exe` */
2858
+ len = slash - prefix ;
2859
+
2860
+ /* strip trailing `cmd` or `mingw64\bin` or `mingw32\bin` or `bin` or `libexec\git-core` */
2861
+ if (strip_suffix_mem (prefix , & len , "\\mingw64\\libexec\\git-core" ) ||
2862
+ strip_suffix_mem (prefix , & len , "\\mingw64\\bin" ))
2863
+ off += xsnprintf (path + off , size - off ,
2864
+ "%.*s\\mingw64\\bin;" , (int )len , prefix );
2865
+ else if (strip_suffix_mem (prefix , & len , "\\mingw32\\libexec\\git-core" ) ||
2866
+ strip_suffix_mem (prefix , & len , "\\mingw32\\bin" ))
2867
+ off += xsnprintf (path + off , size - off ,
2868
+ "%.*s\\mingw32\\bin;" , (int )len , prefix );
2869
+ else if (strip_suffix_mem (prefix , & len , "\\cmd" ) ||
2870
+ strip_suffix_mem (prefix , & len , "\\bin" ) ||
2871
+ strip_suffix_mem (prefix , & len , "\\libexec\\git-core" ))
2872
+ off += xsnprintf (path + off , size - off ,
2873
+ "%.*s\\mingw%d\\bin;" , (int )len , prefix ,
2874
+ (int )(sizeof (void * ) * 8 ));
2875
+ else
2876
+ return 0 ;
2877
+
2878
+ off += xsnprintf (path + off , size - off ,
2879
+ "%.*s\\usr\\bin;" , (int )len , prefix );
2880
+ return off ;
2881
+ #endif
2882
+ }
2883
+ #endif
2884
+
2885
+ static int is_system32_path (const char * path )
2886
+ {
2887
+ WCHAR system32 [MAX_PATH ], wpath [MAX_PATH ];
2888
+
2889
+ if (xutftowcs_path (wpath , path ) < 0 ||
2890
+ !GetSystemDirectoryW (system32 , ARRAY_SIZE (system32 )) ||
2891
+ _wcsicmp (system32 , wpath ))
2892
+ return 0 ;
2893
+
2894
+ return 1 ;
2895
+ }
2896
+
2844
2897
static void setup_windows_environment (void )
2845
2898
{
2846
2899
char * tmp = getenv ("TMPDIR" );
@@ -2881,7 +2934,8 @@ static void setup_windows_environment(void)
2881
2934
strbuf_addstr (& buf , tmp );
2882
2935
if ((tmp = getenv ("HOMEPATH" ))) {
2883
2936
strbuf_addstr (& buf , tmp );
2884
- if (is_directory (buf .buf ))
2937
+ if (!is_system32_path (buf .buf ) &&
2938
+ is_directory (buf .buf ))
2885
2939
setenv ("HOME" , buf .buf , 1 );
2886
2940
else
2887
2941
tmp = NULL ; /* use $USERPROFILE */
@@ -2892,6 +2946,37 @@ static void setup_windows_environment(void)
2892
2946
if (!tmp && (tmp = getenv ("USERPROFILE" )))
2893
2947
setenv ("HOME" , tmp , 1 );
2894
2948
}
2949
+
2950
+ if (!getenv ("PLINK_PROTOCOL" ))
2951
+ setenv ("PLINK_PROTOCOL" , "ssh" , 0 );
2952
+
2953
+ #ifdef ENSURE_MSYSTEM_IS_SET
2954
+ if (!(tmp = getenv ("MSYSTEM" )) || !tmp [0 ]) {
2955
+ const char * home = getenv ("HOME" ), * path = getenv ("PATH" );
2956
+ char buf [32768 ];
2957
+ size_t off = 0 ;
2958
+
2959
+ xsnprintf (buf , sizeof (buf ),
2960
+ "MINGW%d" , (int )(sizeof (void * ) * 8 ));
2961
+ setenv ("MSYSTEM" , buf , 1 );
2962
+
2963
+ if (home )
2964
+ off += xsnprintf (buf + off , sizeof (buf ) - off ,
2965
+ "%s\\bin;" , home );
2966
+ off += append_system_bin_dirs (buf + off , sizeof (buf ) - off );
2967
+ if (path )
2968
+ off += xsnprintf (buf + off , sizeof (buf ) - off ,
2969
+ "%s" , path );
2970
+ else if (off > 0 )
2971
+ buf [off - 1 ] = '\0' ;
2972
+ else
2973
+ buf [0 ] = '\0' ;
2974
+ setenv ("PATH" , buf , 1 );
2975
+ }
2976
+ #endif
2977
+
2978
+ if (!getenv ("LC_ALL" ) && !getenv ("LC_CTYPE" ) && !getenv ("LANG" ))
2979
+ setenv ("LC_CTYPE" , "C.UTF-8" , 1 );
2895
2980
}
2896
2981
2897
2982
static PSID get_current_user_sid (void )
0 commit comments