@@ -20,6 +20,43 @@ const char git_more_info_string[] =
20
20
21
21
static struct startup_info git_startup_info ;
22
22
static int use_pager = -1 ;
23
+ static char orig_cwd [PATH_MAX ];
24
+ static const char * env_names [] = {
25
+ GIT_DIR_ENVIRONMENT ,
26
+ GIT_WORK_TREE_ENVIRONMENT ,
27
+ GIT_IMPLICIT_WORK_TREE_ENVIRONMENT ,
28
+ GIT_PREFIX_ENVIRONMENT
29
+ };
30
+ static char * orig_env [4 ];
31
+ static int saved_environment ;
32
+
33
+ static void save_env (void )
34
+ {
35
+ int i ;
36
+ if (saved_environment )
37
+ return ;
38
+ saved_environment = 1 ;
39
+ if (!getcwd (orig_cwd , sizeof (orig_cwd )))
40
+ die_errno ("cannot getcwd" );
41
+ for (i = 0 ; i < ARRAY_SIZE (env_names ); i ++ ) {
42
+ orig_env [i ] = getenv (env_names [i ]);
43
+ if (orig_env [i ])
44
+ orig_env [i ] = xstrdup (orig_env [i ]);
45
+ }
46
+ }
47
+
48
+ static void restore_env (void )
49
+ {
50
+ int i ;
51
+ if (* orig_cwd && chdir (orig_cwd ))
52
+ die_errno ("could not move to %s" , orig_cwd );
53
+ for (i = 0 ; i < ARRAY_SIZE (env_names ); i ++ ) {
54
+ if (orig_env [i ])
55
+ setenv (env_names [i ], orig_env [i ], 1 );
56
+ else
57
+ unsetenv (env_names [i ]);
58
+ }
59
+ }
23
60
24
61
static void commit_pager_choice (void ) {
25
62
switch (use_pager ) {
@@ -272,6 +309,7 @@ static int handle_alias(int *argcp, const char ***argv)
272
309
* RUN_SETUP for reading from the configuration file.
273
310
*/
274
311
#define NEED_WORK_TREE (1<<3)
312
+ #define NO_SETUP (1<<4)
275
313
276
314
struct cmd_struct {
277
315
const char * cmd ;
@@ -352,7 +390,7 @@ static struct cmd_struct commands[] = {
352
390
{ "cherry" , cmd_cherry , RUN_SETUP },
353
391
{ "cherry-pick" , cmd_cherry_pick , RUN_SETUP | NEED_WORK_TREE },
354
392
{ "clean" , cmd_clean , RUN_SETUP | NEED_WORK_TREE },
355
- { "clone" , cmd_clone },
393
+ { "clone" , cmd_clone , NO_SETUP },
356
394
{ "column" , cmd_column , RUN_SETUP_GENTLY },
357
395
{ "commit" , cmd_commit , RUN_SETUP | NEED_WORK_TREE },
358
396
{ "commit-tree" , cmd_commit_tree , RUN_SETUP },
@@ -378,8 +416,8 @@ static struct cmd_struct commands[] = {
378
416
{ "hash-object" , cmd_hash_object },
379
417
{ "help" , cmd_help },
380
418
{ "index-pack" , cmd_index_pack , RUN_SETUP_GENTLY },
381
- { "init" , cmd_init_db },
382
- { "init-db" , cmd_init_db },
419
+ { "init" , cmd_init_db , NO_SETUP },
420
+ { "init-db" , cmd_init_db , NO_SETUP },
383
421
{ "log" , cmd_log , RUN_SETUP },
384
422
{ "ls-files" , cmd_ls_files , RUN_SETUP },
385
423
{ "ls-remote" , cmd_ls_remote , RUN_SETUP_GENTLY },
@@ -484,6 +522,10 @@ static void handle_builtin(int argc, const char **argv)
484
522
struct cmd_struct * p = commands + i ;
485
523
if (strcmp (p -> cmd , cmd ))
486
524
continue ;
525
+ if (saved_environment && (p -> option & NO_SETUP )) {
526
+ restore_env ();
527
+ break ;
528
+ }
487
529
exit (run_builtin (p , argc , argv ));
488
530
}
489
531
}
@@ -539,7 +581,10 @@ static int run_argv(int *argcp, const char ***argv)
539
581
* of overriding "git log" with "git show" by having
540
582
* alias.log = show
541
583
*/
542
- if (done_alias || !handle_alias (argcp , argv ))
584
+ if (done_alias )
585
+ break ;
586
+ save_env ();
587
+ if (!handle_alias (argcp , argv ))
543
588
break ;
544
589
done_alias = 1 ;
545
590
}
0 commit comments