Skip to contentSkip to conversation

setup.c: set workdir when gitdir is not default

PATCH In review Max Kirillov 3 participants 5 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 setup.c: set workdir when gitdir is not default

When gitfile is used, git sets GIT_DIR environment variable for subsequent commands, and that commands start working in mode "GIT_DIR set, workdir current", which is incorrect for the case when git runs from subdirectory of repository. This can be observed at least for running aliases - git fails with message "internal error: work tree has already been set"

Fix by setting GIT_WORK_TREE environment also.

Add test which demonstrates problem with alias.

---
 setup.c            | 4 +++-
 t/t0002-gitfile.sh | 7 +++++++
 2 files changed, 10 insertions(+), 1 deletion(-)
setup.c +3 -1
index 0a22f8b..bcf4e31 100644
@@ -508,8 +508,10 @@ static const char *setup_discovered_git_dir(const char *gitdir,
 
 	/* #0, #1, #5, #8, #9, #12, #13 */
 	set_git_work_tree(".");
-	if (strcmp(gitdir, DEFAULT_GIT_DIR_ENVIRONMENT))
+	if (strcmp(gitdir, DEFAULT_GIT_DIR_ENVIRONMENT)) {
 		set_git_dir(gitdir);
+		setenv(GIT_WORK_TREE_ENVIRONMENT, get_git_work_tree(), 1);
+	}
 	inside_git_dir = 0;
 	inside_work_tree = 1;
 	if (offset == len)
t/t0002-gitfile.sh +7 -0
index 37e9396..428cfdc 100755
@@ -99,4 +99,11 @@ test_expect_success 'check rev-list' '
 	test "$SHA" = "$(git rev-list HEAD)"
 '
 
+test_expect_success 'check alias call from subdirectory' '
+	git config alias.testalias "rev-parse HEAD" &&
+	mkdir -p subdir &&
+	cd subdir &&
+	git testalias
+'
+
 test_done
Signature · 1 line
2.0.1.1697.g73c6810
#1 lore raw

setup.c static const char *setup_discovered_git_dir(const char *gitdir, · line 510

Quoted 8 lines · Max Kirillov
setup.c +0 -0
index 0a22f8b..bcf4e31 100644
@@ -508,8 +508,10 @@ static const char *setup_discovered_git_dir(const char *gitdir,
 
        /* #0, #1, #5, #8, #9, #12, #13 */
        set_git_work_tree(".");

I wonder if we should setenv(GIT_WORK_TREE_) from inside this function instead. A quick glance over 'git grep set_git_work_tree' gives me the impression that it's safe to do so, and could cover future bugs similar to this.

Quoted 8 lines
setup.c +3 -1
-       if (strcmp(gitdir, DEFAULT_GIT_DIR_ENVIRONMENT))
+       if (strcmp(gitdir, DEFAULT_GIT_DIR_ENVIRONMENT)) {
                set_git_dir(gitdir);
+               setenv(GIT_WORK_TREE_ENVIRONMENT, get_git_work_tree(), 1);
+       }
        inside_git_dir = 0;
        inside_work_tree = 1;
        if (offset == len)
Signature · 1 line
Duy
#2 lore raw
Quoted 8 lines · Duy Nguyen
Quoted 2 lines · Max Kirillov
        /* #0, #1, #5, #8, #9, #12, #13 */
        set_git_work_tree(".");

I wonder if we should setenv(GIT_WORK_TREE_) from inside this function instead. A quick glance over 'git grep set_git_work_tree' gives me the impression that it's safe to do so, and could cover future bugs similar to this.

Actually, I've been thinking about just the oppozite: remove the setenv(GIT_DIR) from the discovery code at all. Because backend performs the same discovery, and it would find the same git directory itself. And leave environment variables to be only set by user.

Currently environment variables are used for both purposes:
user can use them for hacky stuff, and git itself use them for passing internal information. But, as we can see, setting the variables can have bigger effect than expected. When they are set by user, it's ok, let him try, fail and set some else variable to adapt for his needs. But for git code there is the discovery routines, lets just use them always, it will cause a couple of extra (very few) IO calls but should always give same (correct) result

Signature · 1 line
Max
#3 lore raw
In reply to Max Kirillov
Quoted 38 lines · Max Kirillov

When gitfile is used, git sets GIT_DIR environment variable for subsequent commands, and that commands start working in mode "GIT_DIR set, workdir current", which is incorrect for the case when git runs from subdirectory of repository. This can be observed at least for running aliases - git fails with message "internal error: work tree has already been set"

Fix by setting GIT_WORK_TREE environment also.

Add test which demonstrates problem with alias.

---
 setup.c            | 4 +++-
 t/t0002-gitfile.sh | 7 +++++++
 2 files changed, 10 insertions(+), 1 deletion(-)
setup.c +3 -1
index 0a22f8b..bcf4e31 100644
@@ -508,8 +508,10 @@ static const char *setup_discovered_git_dir(const char *gitdir,
 
        /* #0, #1, #5, #8, #9, #12, #13 */
        set_git_work_tree(".");
-       if (strcmp(gitdir, DEFAULT_GIT_DIR_ENVIRONMENT))
+       if (strcmp(gitdir, DEFAULT_GIT_DIR_ENVIRONMENT)) {
                set_git_dir(gitdir);
+               setenv(GIT_WORK_TREE_ENVIRONMENT, get_git_work_tree(), 1);
+       }
        inside_git_dir = 0;
        inside_work_tree = 1;
        if (offset == len)
t/t0002-gitfile.sh +0 -0
index 37e9396..428cfdc 100755

t/t0002-gitfile.sh test_expect_success 'check rev-list' ' · line 103

Quoted 5 lines
t/t0002-gitfile.sh +2 -0
@@ -99,4 +99,11 @@ test_expect_success 'check rev-list' '
        test "$SHA" = "$(git rev-list HEAD)"
 '
 
+test_expect_success 'check alias call from subdirectory' '
+       git config alias.testalias "rev-parse HEAD" &&

Perhaps use test_config here?

t/t0002-gitfile.sh test_expect_success 'check rev-list' ' · line 106

Quoted 3 lines
t/t0002-gitfile.sh +3 -0
+       mkdir -p subdir &&
+       cd subdir &&
+       git testalias

If a new test is added following this one, it will be run from within 'subdir', which might come as a surprise as the author of the new test. Wrapping the 'cd' and following code in a subshell is a good way to avoid the problem.

    mkdir -p subdir &&
    (
        cd subdir &&
        git testalias
    )
Quoted 5 lines
t/t0002-gitfile.sh +2 -1
+'
+
 test_done
--

2.0.1.1697.g73c6810

#4 lore raw
Quoted 8 lines · Eric Sunshine

t/t0002-gitfile.sh test_expect_success 'check rev-list' ' · line 106

Quoted 3 lines
t/t0002-gitfile.sh +3 -0
+       mkdir -p subdir &&
+       cd subdir &&
+       git testalias

If a new test is added following this one, it will be run from within 'subdir', which might come as a surprise as the author of the new test. Wrapping the 'cd' and following code in a subshell is a good way to avoid the problem.

Sure, my mistake. Will fix this and the other issue you mentioned. Thank you.

Signature · 1 line
Max