git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [git-users] Problem using detached worktrees with commands implemented in scripts
@ 2013-10-16 20:03 Dale R. Worley
  2013-10-16 21:42 ` Junio C Hamano
  0 siblings, 1 reply; 17+ messages in thread
From: Dale R. Worley @ 2013-10-16 20:03 UTC (permalink / raw)
  To: git

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

^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2013-10-21 18:51 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-10-16 20:03 [git-users] Problem using detached worktrees with commands implemented in scripts Dale R. Worley
2013-10-16 21:42 ` 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

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 NNTP newsgroup(s).