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