git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: worley@alum.mit.edu (Dale R. Worley)
To: git@vger.kernel.org
Subject: [git-users] Problem using detached worktrees with commands implemented in scripts
Date: Wed, 16 Oct 2013 16:03:30 -0400	[thread overview]
Message-ID: <201310162003.r9GK3UYj014414@freeze.ariadne.com> (raw)

In Git, one can set up a repository with a "detached worktree", where
the .git directory is not a subdirectory of the top directory of the
work tree.

In general, Git commands on a repository with a detached worktree can
be executed by cd'ing into the directory containing the .git
directory, and executing the Git command there.  E.g., "git add" and
"git commit" execute as one would expect.  (I think they can also be
executed by cd'ing to the worktree and setting GIT_DIR.)

However, this approach does not work with "git filter-branch", which
objects with "You need to run this command from the toplevel of the
working tree."

I suspect that it does not work with other Git commands that are
implemented with shell scripts.  The problem appears to be in the
git-sh-setup script, which is called by the Git shell scripts to set
up the environment and do preliminary tests.

It seems to me that this inconsistency between the script commands and
the binary commands can be fixed by updating git-sh-setup in this way:

--- git-sh-setup.Custom.orig	2013-06-20 12:59:45.000000000 -0400
+++ git-sh-setup	2013-10-07 22:34:06.719946134 -0400
@@ -297,14 +297,18 @@
 # if we require to be in a git repository.
 if test -z "$NONGIT_OK"
 then
-	GIT_DIR=$(git rev-parse --git-dir) || exit
+	export GIT_DIR=$(git rev-parse --git-dir) || exit
 	if [ -z "$SUBDIRECTORY_OK" ]
 	then
-		test -z "$(git rev-parse --show-cdup)" || {
-			exit=$?
-			echo >&2 "You need to run this command from the toplevel of the working tree."
-			exit $exit
-		}
+		cdup="$(git rev-parse --show-cdup)"
+		if [ -n "$cdup" ]
+		then
+			# Current directory is not the toplevel.
+			# Set GIT_DIR to the absolute path of the repository.
+			GIT_DIR=$(cd "$GIT_DIR" && pwd)
+			# cd to the toplevel.
+			cd $cdup
+		fi
 	fi
 	test -n "$GIT_DIR" && GIT_DIR=$(cd "$GIT_DIR" && pwd) || {
 		echo >&2 "Unable to determine absolute path of git directory"

What this change does is, when a command is invoked from a directory
containing a repository with a detached worktree, is to set GIT_DIR to
the directory of the repository, then cd to the top of the worktree.
After that, the script command should work as expected.

I am far from being an expert in Git internals, so I don't know
whether this is the correct approach to take to this problem or not.

Does anyone have any feedback on this?

Dale

             reply	other threads:[~2013-10-16 20:10 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-10-16 20:03 Dale R. Worley [this message]
2013-10-16 21:42 ` [git-users] Problem using detached worktrees with commands implemented in scripts Junio C Hamano
2013-10-16 22:39   ` Philip Oakley
2013-10-16 23:08     ` Junio C Hamano
2013-10-17 20:11       ` Philip Oakley
2013-10-17 20:50         ` Junio C Hamano
2013-10-17 21:14           ` Philip Oakley
2013-10-17 22:38             ` Philip Oakley
2013-10-17 22:48               ` Jonathan Nieder
2013-10-18 20:40                 ` Philip Oakley
2013-10-18 22:54           ` Dale R. Worley
2013-10-17 19:09   ` Dale R. Worley
2013-10-17 20:08     ` Junio C Hamano
2013-10-18 22:25       ` Dale R. Worley
2013-10-18 22:43         ` Junio C Hamano
2013-10-21 18:51           ` Dale R. Worley
2013-10-18 22:50       ` Dale R. Worley

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=201310162003.r9GK3UYj014414@freeze.ariadne.com \
    --to=worley@alum.mit.edu \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).