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