]> git.ipfire.org Git - thirdparty/git.git/commitdiff
require-work-tree wants more than what its name says
authorJunio C Hamano <gitster@pobox.com>
Thu, 5 May 2011 02:11:18 +0000 (19:11 -0700)
committerJunio C Hamano <gitster@pobox.com>
Tue, 24 May 2011 18:34:40 +0000 (11:34 -0700)
Somebody tried "git pull" from a random place completely outside the work
tree, while exporting GIT_DIR and GIT_WORK_TREE that are set to correct
places, e.g.

    GIT_WORK_TREE=$HOME/git.git
    GIT_DIR=$GIT_WORK_TREE/.git
    export GIT_WORK_TREE GIT_DIR
    cd /tmp
    git pull

At the beginning of git-pull, we check "require-work-tree" and then
"cd-to-toplevel".  I _think_ the original intention when I wrote the
command was "we MUST have a work tree, our $(cwd) might not be at the
top-level directory of it", and no stronger than that.  That check is a
very sensible thing to do before doing cd-to-toplevel.  We check that the
place we would want to go exists, and then go there.

But the implementation of require_work_tree we have today is quite
different.  I don't have energy to dig the history, but currently it says:

    test "$(git rev-parse --is-inside-work-tree 2>/dev/null)" = true ||
    die "fatal: $0 cannot be used without a working tree."

Which is completely bogus.  Even though we may happen to be just outside
of it right now, we may have a working tree that we can cd_to_toplevel
back to.

Add a function "require_work_tree_exists" that implements the check
this function originally intended (this is so that third-party scripts
that rely on the current behaviour do not have to get broken).

For now, update _no_ in-tree scripts, not even "git pull", as nobody on
the list seems to really care about the above corner case workflow that
triggered this. Scripts can be updated after vetting that they do want the
"we want to make sure the place we are going to go actually exists"
semantics.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/git-sh-setup.txt
git-sh-setup.sh

index 3da241304b0d2fdaa376bca740beb024a36b2cb6..1f02c4b6ea3680c01618bbe5ef6a861a14149f41 100644 (file)
@@ -58,9 +58,14 @@ cd_to_toplevel::
        runs chdir to the toplevel of the working tree.
 
 require_work_tree::
-       checks if the repository is a bare repository, and dies
-       if so.  Used by scripts that require working tree
-       (e.g. `checkout`).
+       checks if the current directory is within the working tree
+       of the repository, and otherwise dies.
+
+require_work_tree_exists::
+       checks if the working tree associated with the repository
+       exists, and otherwise dies.  Often done before calling
+       cd_to_toplevel, which is impossible to do if there is no
+       working tree.
 
 get_author_ident_from_commit::
        outputs code for use with eval to set the GIT_AUTHOR_NAME,
index aa16b8356507e4e669ee5c4cb4b5a667942d559e..94e26ed5e8dcf84c4f238c76b6c508dc84d0b7ea 100644 (file)
@@ -140,6 +140,13 @@ cd_to_toplevel () {
        }
 }
 
+require_work_tree_exists () {
+       if test "z$(git rev-parse --is-bare-repository)" != zfalse
+       then
+               die "fatal: $0 cannot be used without a working tree."
+       fi
+}
+
 require_work_tree () {
        test "$(git rev-parse --is-inside-work-tree 2>/dev/null)" = true ||
        die "fatal: $0 cannot be used without a working tree."