git-filter-branch.sh: Fix broken setting of GIT_DIR
PATCH 1/1 git-filter-branch.sh: Fix broken setting of GIT_DIR
If filter-branch is entered with an unset GIT_DIR, things are rather fragile. The GIT_DIR variable setting then points to something like $(pwd)/../.. which is neither guaranteed to be a git directory (depends on where filter-branch is started), nor will it continue to work once the temporary directory (for which the pwd is output) ceases to exist.
So we just call git-name-rev in order to get the correct setting here for exporting.
--- git-filter-branch.sh | 12 ++++-------- 1 files changed, 4 insertions(+), 8 deletions(-)
git-filter-branch.sh +4 -8
@@ -170,14 +170,10 @@ do esac done < "$tempdir"/backup-refs -case "$GIT_DIR" in -/*) - ;; -*) - GIT_DIR="$(pwd)/../../$GIT_DIR" - ;; -esac -export GIT_DIR GIT_WORK_TREE=. +GIT_DIR=$(cd ../..;git-name-rev --git-dir) +GIT_WORK_TREE=. + +export GIT_DIR GIT_WORK_TREE # These refs should be updated if their heads were rewritten
Signature · 1 line
1.5.3.rc4.43.gaf14
PATCH 1/1 git-filter-branch.sh: Fix broken setting of GIT_DIR
If filter-branch is entered with an unset GIT_DIR, things are rather fragile. The GIT_DIR variable setting then points to something like $(pwd)/../.. which is neither guaranteed to be a git directory (depends on where filter-branch is started), nor will it continue to work once the temporary directory (for which the pwd is output) ceases to exist.
So we just call git-rev-parse in order to get the correct setting here for exporting.
--- git-filter-branch.sh | 13 +++++-------- 1 files changed, 5 insertions(+), 8 deletions(-)
git-filter-branch.sh +5 -8
@@ -170,14 +170,11 @@ do esac done < "$tempdir"/backup-refs -case "$GIT_DIR" in -/*) - ;; -*) - GIT_DIR="$(pwd)/../../$GIT_DIR" - ;; -esac -export GIT_DIR GIT_WORK_TREE=. +GIT_DIR=$(cd ../..;cd "./$(git-rev-parse --git-dir)";pwd) + +GIT_WORK_TREE=. + +export GIT_DIR GIT_WORK_TREE # These refs should be updated if their heads were rewritten
Signature · 1 line
1.5.3.rc4.74.g3739cb17
Quoted 3 lines · David Kastrup
If filter-branch is entered with an unset
GIT_DIR, things are rather fragile. TheGIT_DIRvariable setting then points to something like $(pwd)/../.. which is neither guaranteed to be a git directory
I think this comment refers to this part, ...
git-filter-branch.sh do · line 179
Quoted 7 lines
git-filter-branch.sh +0
-7-case "$GIT_DIR" in -/*) - ;; -*) - GIT_DIR="$(pwd)/../../$GIT_DIR" - ;; -esac
... however, at the beginning of the script, it dot-includes git-sh-setup, which sets (but not export) GIT_DIR for the rest of the script to use (see the last if..then..else).
If you got an unset GIT_DIR when you reached that case statement you are removing here, I suspect that there is something else going on, but I do not see what it is...
Puzzled...
Quoted 8 lines · Junio C Hamano
Quoted 3 lines · David Kastrup
If filter-branch is entered with an unset
GIT_DIR, things are rather fragile. TheGIT_DIRvariable setting then points to something like $(pwd)/../.. which is neither guaranteed to be a git directoryI think this comment refers to this part, ...
Quoted 11 lines
git-filter-branch.sh do · line 179
Quoted 7 lines
git-filter-branch.sh +0
-7-case "$GIT_DIR" in -/*) - ;; -*) - GIT_DIR="$(pwd)/../../$GIT_DIR" - ;; -esac... however, at the beginning of the script, it dot-includes git-sh-setup, which sets (but not export)
GIT_DIRfor the rest of the script to use (see the last if..then..else).
Ah, ok. In that case my patch is overengineered. However, one could replace the whole case with
GITDIR=$(cd ../..;cd $GIT_DIR;pwd)
This would work with Windows absolute paths, too.
Quoted 5 lines
If you got an unset
GIT_DIRwhen you reached that case statement you are removing here, I suspect that there is something else going on, but I do not see what it is...Puzzled...
The problem I saw was that "$(pwd)/../../$GIT_DIR" does no longer exist by the time the end of the script it reached, because what was "$(pwd)" has already been removed. So it is less onerous than I thought, but still a nuisance when the script ends.
Anyway, if $GIT_DIR is a relative path, things would go wrong in some lines earlier, where the path is already being changed.
If git-sh-setup sets GIT_DIR, perhaps it would be sanest if it also made it absolute?
Otherwise any script that does "cd" will lose track of GIT_DIR, right?
Signature · 1 line
David Kastrup, Kriemhildstr. 15, 44793 Bochum
Quoted 2 lines · David Kastrup
Anyway, if $GIT_DIR is a relative path, things would go wrong in some lines earlier,
and if GIT_DIR were used or exported which it isn't, so we are lucky...
Quoted 6 lines
where the path is already being changed.
If git-sh-setup sets
GIT_DIR, perhaps it would be sanest if it also made it absolute?Otherwise any script that does "cd" will lose track of
GIT_DIR, right?
Actually, wouldn't by far the most straightforward thing be if git-rev-parse --git-dir always returned an absolute path (even when being passed a relative path in GIT_DIR)?
No need for postprocessing, no need to keep track of changed directories.
Signature · 1 line
David Kastrup, Kriemhildstr. 15, 44793 Bochum
Quoted 4 lines · David Kastrup
Actually, wouldn't by far the most straightforward thing be if git-rev-parse --git-dir always returned an absolute path (even when being passed a relative path in
GIT_DIR)?
That might also help get rid of an annoying failure mode where some git commands (the shell script ones) fail if you cd into a git repository via a symlink, while others (the builtins) work just fine.
-Steve
Quoted 9 lines · Steven Grimm
Quoted 4 lines · David Kastrup
Actually, wouldn't by far the most straightforward thing be if git-rev-parse --git-dir always returned an absolute path (even when being passed a relative path in
GIT_DIR)?That might also help get rid of an annoying failure mode where some git commands (the shell script ones) fail if you cd into a git repository via a symlink, while others (the builtins) work just fine.
It might be the easiest workaround for this issue to unset PWD in git-sh-setup.sh (and git-clone.sh) where we unset CDPATH. Anybody wants to try to come up with additional test scripts to expose the problem and a patch to these two files?
Quoted 12 lines · David Kastrup
Quoted 2 lines · David Kastrup
Anyway, if $GIT_DIR is a relative path, things would go wrong in some lines earlier,
and if
GIT_DIRwere used or exported which it isn't, so we are lucky...Quoted 5 lines
where the path is already being changed.
If git-sh-setup sets
GIT_DIR, perhaps it would be sanest if it also made it absolute?
Quoted 8 lines
Quoted 1 line
Otherwise any script that does "cd" will lose track of
GIT_DIR, right?Actually, wouldn't by far the most straightforward thing be if git-rev-parse --git-dir always returned an absolute path (even when being passed a relative path in
GIT_DIR)?No need for postprocessing, no need to keep track of changed directories.
To illustrate the potential for trouble:
git-commit.sh
has the lines
647:
cd_to_toplevel
git rerere
if test "$ret" = 0
then
if test -x "$GIT_DIR"/hooks/post-commit then
"$GIT_DIR"/hooks/post-commit
fi
Obviously, this won't work if GIT_DIR has a relative path and the command has not been started from the top level already.
In a similar vein, git-checkout.sh has
263:
GIT_DIR="$GIT_DIR" git symbolic-ref -m "checkout: moving from $old_branch_name to $branch" HEAD "refs/heads/$branch"
while we have previously cd_to_toplevel in line 154.
And so on. Rather than fixing all those scripts, it would seem safer to let git-rev-parse --git-dir (I don't understand the code in builtin-rev-parse, by the way: why does it look at "prefix"?), or at the very least git-sh-setup.sh return an absolute path.
Signature · 1 line
David Kastrup, Kriemhildstr. 15, 44793 Bochum
I think your other patch to always give full path to the shell scripts from git-sh-setup makes sense. Could you please make test scripts to expose the problems you described in the message I am replying to and add it as say t/t2050-git-dir-relative.sh?
Quoted 4 lines · Junio C Hamano
I think your other patch to always give full path to the shell scripts from git-sh-setup makes sense. Could you please make test scripts to expose the problems you described in the message I am replying to and add it as say
t/t2050-git-dir-relative.sh?
It is actually a bit harder to trigger than I expected: if GIT_DIR is not a direct descendant of the current directory, git-rev-parse does output an absolute path. So at least for the test failure in git-commit, I have to revert to a non-run-of-the-mill setup.
Signature · 1 line
David Kastrup, Kriemhildstr. 15, 44793 Bochum
PATCH 1/1 Add a test for git-commit being confused by relative GIT_DIR
In reply to Junio C Hamano---
It takes an inordinarily large amount of time to create these tests, I am afraid. There is little sense in making a more extensive audit, cranking out more cases which fail with relative paths, when the fix, namely making GIT_DIR absolute, is so simple.
t/t2050-git-dir-relative.sh | 54 +++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 54 insertions(+), 0 deletions(-) create mode 100755 t/t2050-git-dir-relative.sh
t/t2050-git-dir-relative.sh new file +54 -0
@@ -0,0 +1,54 @@ +#!/bin/sh + +test_description='check problems with relative GIT_DIR + +This test creates a working tree state with a file and subdir: + + top (committed several times) + subdir (a subdirectory) + +It creates a commit-hook and tests it, then moves .git +into the subdir while keeping the worktree location, +and tries commits from the top and the subdir, checking +that the commit-hook still gets called.' + +. ./test-lib.sh + +test_expect_success 'Setting up post-commit hook' ' +mkdir -p .git/hooks && +cat <<EOF >.git/hooks/post-commit && +#!/bin/sh +touch $(pwd)/output +echo "Post commit hook was called." +EOF +chmod +x .git/hooks/post-commit' + +test_expect_success 'post-commit hook used ordinarily' ' +echo initial >top && +git-add top +git-commit -m initial && +test -r output +' + +rm -rf output +mkdir subdir +mv .git subdir + +test_expect_success 'post-commit-hook created and used from top dir' ' +echo changed >top && +git --git-dir subdir/.git add top && +git --git-dir subdir/.git commit -m topcommit && +test -r output +' + +rm -rf output + +test_expect_success 'post-commit-hook from sub dir' ' +echo changed again >top +cd subdir && +git --git-dir .git --work-tree .. add ../top && +git --git-dir .git --work-tree .. commit -m subcommit && +test -r ../output +' + +test_done
Signature · 1 line
1.5.3.rc2.187.g9a1d2-dirty
Re: [PATCH] Add a test for git-commit being confused by relative GIT_DIR
t/t2050-git-dir-relative.sh line 24
Quoted 8 lines · David Kastrup
t/t2050-git-dir-relative.sh new file +8
-0+test_expect_success 'Setting up post-commit hook' ' +mkdir -p .git/hooks && +cat <<EOF >.git/hooks/post-commit && +#!/bin/sh +touch $(pwd)/output +echo "Post commit hook was called." +EOF +chmod +x .git/hooks/post-commit'
We have avoided to use here text inside test_expect_success, as there have been reports that some otherwise reasonably usable shells do not grok it. Although I prefer to do everything, including the set-up part, inside test_expect_success, please move this code outside.
Also I do not think you would want to say touch $(pwd)/output there inside the here text that begins with <<EOF not <<\EOF.
Re: [PATCH] Add a test for git-commit being confused by relative GIT_DIR
Quoted 14 lines · Junio C Hamano
t/t2050-git-dir-relative.sh line 24
Quoted 8 lines · David Kastrup
t/t2050-git-dir-relative.sh new file +8
-0+test_expect_success 'Setting up post-commit hook' ' +mkdir -p .git/hooks && +cat <<EOF >.git/hooks/post-commit && +#!/bin/sh +touch $(pwd)/output +echo "Post commit hook was called." +EOF +chmod +x .git/hooks/post-commit'We have avoided to use here text inside
test_expect_success, as there have been reports that some otherwise reasonably usable shells do not grok it.
Ok.
Quoted 2 lines
Although I prefer to do everything, including the set-up part, inside
test_expect_success, please move this code outside.
I can easily enough make it an echo.
Quoted 2 lines
Also I do not think you would want to say touch $(pwd)/output there inside the here text that begins with <<EOF not <<\EOF.
But I most certainly do! The same file should be touched regardless of what the cwd at the time of calling the hook is. Otherwise, I would not need the $(pwd) in the first place. The whole point is that it is expanded at the time of the hook creation.
Signature · 1 line
David Kastrup, Kriemhildstr. 15, 44793 Bochum
Re: [PATCH] Add a test for git-commit being confused by relative GIT_DIR
Quoted 4 lines · David Kastrup
Quoted 2 lines
Also I do not think you would want to say touch $(pwd)/output there inside the here text that begins with <<EOF not <<\EOF.
But I most certainly do!
Then you would need to shell-quote $(pwd) so that you can have your git.git checked out somewhere under "/My Documents/source" directory, wouldn't you?
Re: [PATCH] Add a test for git-commit being confused by relative GIT_DIR
Quoted 10 lines · Junio C Hamano
Quoted 4 lines · David Kastrup
Quoted 2 lines
Also I do not think you would want to say touch $(pwd)/output there inside the here text that begins with <<EOF not <<\EOF.
But I most certainly do!
Then you would need to shell-quote $(pwd) so that you can have your git.git checked out somewhere under "/My Documents/source" directory, wouldn't you?
Ok, that's a tough one.
Signature · 1 line
David Kastrup, Kriemhildstr. 15, 44793 Bochum