Skip to content

Commit b30adaa

Browse files
committed
Merge branch 'nd/init-restore-env'
Some subcommands do not want to be aliased because of the side effects that happens while the definitions of the aliases are looked up from configuration system. * nd/init-restore-env: git potty: restore environments after alias expansion
2 parents b7ce583 + c056261 commit b30adaa

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed

git.c

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,43 @@ const char git_more_info_string[] =
2020

2121
static struct startup_info git_startup_info;
2222
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+
}
2360

2461
static 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

276314
struct 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
}

t/t0001-init.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ test_expect_success 'plain through aliased command, outside any git repo' '
5656
check_config plain-aliased/.git false unset
5757
'
5858

59-
test_expect_failure 'plain nested through aliased command' '
59+
test_expect_success 'plain nested through aliased command' '
6060
(
6161
git init plain-ancestor-aliased &&
6262
cd plain-ancestor-aliased &&
@@ -68,7 +68,7 @@ test_expect_failure 'plain nested through aliased command' '
6868
check_config plain-ancestor-aliased/plain-nested/.git false unset
6969
'
7070

71-
test_expect_failure 'plain nested in bare through aliased command' '
71+
test_expect_success 'plain nested in bare through aliased command' '
7272
(
7373
git init --bare bare-ancestor-aliased.git &&
7474
cd bare-ancestor-aliased.git &&

0 commit comments

Comments
 (0)