@@ -25,14 +25,14 @@ static const char *env_names[] = {
25
25
GIT_PREFIX_ENVIRONMENT
26
26
};
27
27
static char * orig_env [4 ];
28
- static int saved_environment ;
28
+ static int saved_env_before_alias ;
29
29
30
- static void save_env (void )
30
+ static void save_env_before_alias (void )
31
31
{
32
32
int i ;
33
- if (saved_environment )
33
+ if (saved_env_before_alias )
34
34
return ;
35
- saved_environment = 1 ;
35
+ saved_env_before_alias = 1 ;
36
36
orig_cwd = xgetcwd ();
37
37
for (i = 0 ; i < ARRAY_SIZE (env_names ); i ++ ) {
38
38
orig_env [i ] = getenv (env_names [i ]);
@@ -41,13 +41,16 @@ static void save_env(void)
41
41
}
42
42
}
43
43
44
- static void restore_env (void )
44
+ static void restore_env (int external_alias )
45
45
{
46
46
int i ;
47
- if (orig_cwd && chdir (orig_cwd ))
47
+ if (! external_alias && orig_cwd && chdir (orig_cwd ))
48
48
die_errno ("could not move to %s" , orig_cwd );
49
49
free (orig_cwd );
50
50
for (i = 0 ; i < ARRAY_SIZE (env_names ); i ++ ) {
51
+ if (external_alias &&
52
+ !strcmp (env_names [i ], GIT_PREFIX_ENVIRONMENT ))
53
+ continue ;
51
54
if (orig_env [i ])
52
55
setenv (env_names [i ], orig_env [i ], 1 );
53
56
else
@@ -226,14 +229,14 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
226
229
static int handle_alias (int * argcp , const char * * * argv )
227
230
{
228
231
int envchanged = 0 , ret = 0 , saved_errno = errno ;
229
- const char * subdir ;
230
232
int count , option_count ;
231
233
const char * * new_argv ;
232
234
const char * alias_command ;
233
235
char * alias_string ;
234
236
int unused_nongit ;
235
237
236
- subdir = setup_git_directory_gently (& unused_nongit );
238
+ save_env_before_alias ();
239
+ setup_git_directory_gently (& unused_nongit );
237
240
238
241
alias_command = (* argv )[0 ];
239
242
alias_string = alias_lookup (alias_command );
@@ -243,6 +246,7 @@ static int handle_alias(int *argcp, const char ***argv)
243
246
int argc = * argcp , i ;
244
247
245
248
commit_pager_choice ();
249
+ restore_env (1 );
246
250
247
251
/* build alias_argv */
248
252
alias_argv = xmalloc (sizeof (* alias_argv ) * (argc + 1 ));
@@ -291,8 +295,7 @@ static int handle_alias(int *argcp, const char ***argv)
291
295
ret = 1 ;
292
296
}
293
297
294
- if (subdir && chdir (subdir ))
295
- die_errno ("Cannot change to '%s'" , subdir );
298
+ restore_env (0 );
296
299
297
300
errno = saved_errno ;
298
301
@@ -307,7 +310,6 @@ static int handle_alias(int *argcp, const char ***argv)
307
310
* RUN_SETUP for reading from the configuration file.
308
311
*/
309
312
#define NEED_WORK_TREE (1<<3)
310
- #define NO_SETUP (1<<4)
311
313
312
314
struct cmd_struct {
313
315
const char * cmd ;
@@ -389,7 +391,7 @@ static struct cmd_struct commands[] = {
389
391
{ "cherry" , cmd_cherry , RUN_SETUP },
390
392
{ "cherry-pick" , cmd_cherry_pick , RUN_SETUP | NEED_WORK_TREE },
391
393
{ "clean" , cmd_clean , RUN_SETUP | NEED_WORK_TREE },
392
- { "clone" , cmd_clone , NO_SETUP },
394
+ { "clone" , cmd_clone },
393
395
{ "column" , cmd_column , RUN_SETUP_GENTLY },
394
396
{ "commit" , cmd_commit , RUN_SETUP | NEED_WORK_TREE },
395
397
{ "commit-tree" , cmd_commit_tree , RUN_SETUP },
@@ -415,8 +417,8 @@ static struct cmd_struct commands[] = {
415
417
{ "hash-object" , cmd_hash_object },
416
418
{ "help" , cmd_help },
417
419
{ "index-pack" , cmd_index_pack , RUN_SETUP_GENTLY },
418
- { "init" , cmd_init_db , NO_SETUP },
419
- { "init-db" , cmd_init_db , NO_SETUP },
420
+ { "init" , cmd_init_db },
421
+ { "init-db" , cmd_init_db },
420
422
{ "interpret-trailers" , cmd_interpret_trailers , RUN_SETUP_GENTLY },
421
423
{ "log" , cmd_log , RUN_SETUP },
422
424
{ "ls-files" , cmd_ls_files , RUN_SETUP },
@@ -530,9 +532,13 @@ static void handle_builtin(int argc, const char **argv)
530
532
531
533
builtin = get_builtin (cmd );
532
534
if (builtin ) {
533
- if (saved_environment && (builtin -> option & NO_SETUP ))
534
- restore_env ();
535
- else
535
+ /*
536
+ * XXX: if we can figure out cases where it is _safe_
537
+ * to do, we can avoid spawning a new process when
538
+ * saved_env_before_alias is true
539
+ * (i.e. setup_git_dir* has been run once)
540
+ */
541
+ if (!saved_env_before_alias )
536
542
exit (run_builtin (builtin , argc , argv ));
537
543
}
538
544
}
@@ -590,7 +596,6 @@ static int run_argv(int *argcp, const char ***argv)
590
596
*/
591
597
if (done_alias )
592
598
break ;
593
- save_env ();
594
599
if (!handle_alias (argcp , argv ))
595
600
break ;
596
601
done_alias = 1 ;
0 commit comments