@@ -20,6 +20,43 @@ const char git_more_info_string[] =
2020
2121static struct startup_info git_startup_info ;
2222static 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+ }
2360
2461static void commit_pager_choice (void ) {
2562 switch (use_pager ) {
@@ -272,6 +309,7 @@ static int handle_alias(int *argcp, const char ***argv)
272309 * RUN_SETUP for reading from the configuration file.
273310 */
274311#define NEED_WORK_TREE (1<<3)
312+ #define NO_SETUP (1<<4)
275313
276314struct cmd_struct {
277315 const char * cmd ;
@@ -352,7 +390,7 @@ static struct cmd_struct commands[] = {
352390 { "cherry" , cmd_cherry , RUN_SETUP },
353391 { "cherry-pick" , cmd_cherry_pick , RUN_SETUP | NEED_WORK_TREE },
354392 { "clean" , cmd_clean , RUN_SETUP | NEED_WORK_TREE },
355- { "clone" , cmd_clone },
393+ { "clone" , cmd_clone , NO_SETUP },
356394 { "column" , cmd_column , RUN_SETUP_GENTLY },
357395 { "commit" , cmd_commit , RUN_SETUP | NEED_WORK_TREE },
358396 { "commit-tree" , cmd_commit_tree , RUN_SETUP },
@@ -378,8 +416,8 @@ static struct cmd_struct commands[] = {
378416 { "hash-object" , cmd_hash_object },
379417 { "help" , cmd_help },
380418 { "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 },
383421 { "log" , cmd_log , RUN_SETUP },
384422 { "ls-files" , cmd_ls_files , RUN_SETUP },
385423 { "ls-remote" , cmd_ls_remote , RUN_SETUP_GENTLY },
@@ -484,6 +522,10 @@ static void handle_builtin(int argc, const char **argv)
484522 struct cmd_struct * p = commands + i ;
485523 if (strcmp (p -> cmd , cmd ))
486524 continue ;
525+ if (saved_environment && (p -> option & NO_SETUP )) {
526+ restore_env ();
527+ break ;
528+ }
487529 exit (run_builtin (p , argc , argv ));
488530 }
489531}
@@ -539,7 +581,10 @@ static int run_argv(int *argcp, const char ***argv)
539581 * of overriding "git log" with "git show" by having
540582 * alias.log = show
541583 */
542- if (done_alias || !handle_alias (argcp , argv ))
584+ if (done_alias )
585+ break ;
586+ save_env ();
587+ if (!handle_alias (argcp , argv ))
543588 break ;
544589 done_alias = 1 ;
545590 }
0 commit comments