@@ -2218,6 +2218,62 @@ int handle_long_path(wchar_t *path, int len, int max_path, int expand)
22182218 }
22192219}
22202220
2221+ static void setup_windows_environment ()
2222+ {
2223+ char * tmp ;
2224+
2225+ /* on Windows it is TMP and TEMP */
2226+ if (!getenv ("TMPDIR" )) {
2227+ if (!(tmp = getenv ("TMP" )))
2228+ tmp = getenv ("TEMP" );
2229+ if (tmp )
2230+ setenv ("TMPDIR" , tmp , 1 );
2231+ }
2232+
2233+ if ((tmp = getenv ("TMPDIR" ))) {
2234+ /*
2235+ * Convert all dir separators to forward slashes,
2236+ * to help shell commands called from the Git
2237+ * executable (by not mistaking the dir separators
2238+ * for escape characters).
2239+ */
2240+ for (; * tmp ; tmp ++ )
2241+ if (* tmp == '\\' )
2242+ * tmp = '/' ;
2243+ }
2244+
2245+ if (!getenv ("TZ" ) && (tmp = getenv ("MSYS2_TZ" )))
2246+ setenv ("TZ" , tmp , 1 );
2247+
2248+ /* simulate TERM to enable auto-color (see color.c) */
2249+ if (!getenv ("TERM" ))
2250+ setenv ("TERM" , "cygwin" , 1 );
2251+
2252+ /* calculate HOME if not set */
2253+ if (!getenv ("HOME" )) {
2254+ /*
2255+ * try $HOMEDRIVE$HOMEPATH - the home share may be a network
2256+ * location, thus also check if the path exists (i.e. is not
2257+ * disconnected)
2258+ */
2259+ if ((tmp = getenv ("HOMEDRIVE" ))) {
2260+ struct strbuf buf = STRBUF_INIT ;
2261+ strbuf_addstr (& buf , tmp );
2262+ if ((tmp = getenv ("HOMEPATH" ))) {
2263+ strbuf_addstr (& buf , tmp );
2264+ if (is_directory (buf .buf ))
2265+ setenv ("HOME" , buf .buf , 1 );
2266+ else
2267+ tmp = NULL ; /* use $USERPROFILE */
2268+ }
2269+ strbuf_release (& buf );
2270+ }
2271+ /* use $USERPROFILE if the home share is not available */
2272+ if (!tmp && (tmp = getenv ("USERPROFILE" )))
2273+ setenv ("HOME" , tmp , 1 );
2274+ }
2275+ }
2276+
22212277/*
22222278 * Disable MSVCRT command line wildcard expansion (__getmainargs called from
22232279 * mingw startup code, see init.c in mingw runtime).
@@ -2287,46 +2343,16 @@ void mingw_startup()
22872343 __argv [0 ] = wcstoutfdup_startup (buffer , _wpgmptr , maxlen );
22882344 for (i = 1 ; i < argc ; i ++ )
22892345 __argv [i ] = wcstoutfdup_startup (buffer , wargv [i ], maxlen );
2290- for (i = 0 ; wenv [i ]; i ++ ) {
2346+ for (i = 0 ; wenv [i ]; i ++ )
22912347 environ [i ] = wcstoutfdup_startup (buffer , wenv [i ], maxlen );
2292- if (!strncasecmp (environ [i ], "MSYS2_TZ=" , 9 )) {
2293- char * to_free = environ [i ];
2294- environ [i ] = xstrdup (to_free + 6 );
2295- free (to_free );
2296- }
2297- if (!strncasecmp (environ [i ], "TMP=" , 4 )) {
2298- /*
2299- * Convert all dir separators to forward slashes,
2300- * to help shell commands called from the Git
2301- * executable (by not mistaking the dir separators
2302- * for escape characters).
2303- */
2304- char * p ;
2305- for (p = environ [i ]; * p ; p ++ )
2306- if (* p == '\\' )
2307- * p = '/' ;
2308- }
2309- }
23102348 environ [i ] = NULL ;
23112349 free (buffer );
23122350
23132351 /* sort environment for O(log n) getenv / putenv */
23142352 qsort (environ , i , sizeof (char * ), compareenv );
23152353
23162354 /* fix Windows specific environment settings */
2317-
2318- /* on Windows it is TMP and TEMP */
2319- if (!mingw_getenv ("TMPDIR" )) {
2320- const char * tmp = mingw_getenv ("TMP" );
2321- if (!tmp )
2322- tmp = mingw_getenv ("TEMP" );
2323- if (tmp )
2324- setenv ("TMPDIR" , tmp , 1 );
2325- }
2326-
2327- /* simulate TERM to enable auto-color (see color.c) */
2328- if (!getenv ("TERM" ))
2329- setenv ("TERM" , "cygwin" , 1 );
2355+ setup_windows_environment ();
23302356
23312357 /*
23322358 * Avoid a segmentation fault when cURL tries to set the CHARSET
0 commit comments