git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[git-users] Problem using detached worktrees with commands implemented in scripts

From
Dale R. Worley <worley@alum.mit.edu>
Date
Oct 16, 2013, 20:03 UTC
Message-ID
<201310162003.r9GK3UYj014414@freeze.ariadne.com>

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
Next: Junio C Hamano
Message 1 of 17 in “[git-users] Problem using detached worktrees with commands implemented in scripts”
  1. Dale R. WorleyOct 16, 2013
  2. Junio C HamanoOct 16, 2013
  3. Philip OakleyOct 16, 2013
  4. Junio C HamanoOct 16, 2013
  5. Philip OakleyOct 17, 2013
  6. Junio C HamanoOct 17, 2013
  7. Philip OakleyOct 17, 2013
  8. Philip OakleyOct 17, 2013
  9. Jonathan NiederOct 17, 2013
  10. Philip OakleyOct 18, 2013
  11. Dale R. WorleyOct 18, 2013
  12. Dale R. WorleyOct 17, 2013
  13. Junio C HamanoOct 17, 2013
  14. Dale R. WorleyOct 18, 2013
  15. Junio C HamanoOct 18, 2013
  16. Dale R. WorleyOct 21, 2013
  17. Dale R. WorleyOct 18, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.