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

Re: git status and commit from subdirectory?

From
Linus Torvalds <torvalds@osdl.org>
Date
Oct 26, 2005, 00:16 UTC
Message-ID
<Pine.LNX.4.64.0510251644090.10477@g5.osdl.org>
In-Reply-To
<Pine.LNX.4.64.0510251735500.25300@iabervon.org>
On Tue, 25 Oct 2005, Daniel Barkalow wrote:
Show 7 quoted lines
>
> It seems like everything that "git status" does can now be done from a 
> subdirectory (giving, of course, the status of the contents of the 
> subdirectory). I think the same might be true of "git commit", but I 
> haven't checked on everything. Is there anything that has to be done to 
> enable this properly other than removing the "|| die" part of the first 
> line?
This patch _may_ work. I've not tested it a lot. Surprise surprise.

NOTE! This has some seriously far-reaching implications. One of them is that a few programs will automagically start working inside some random directories.

And probably others won't. Instead of saying "Not a git archive", they might run and do strange things.

The patch is definitely a big step in the right direction: it makes the shell scripts that include "git-sh-setup" act a lot more like the programs that automatically find the git directory. But everybody that includes git-sh-setup should be verified.

This fixes gitk to also work the same way, btw.
Testing needed. Lots of it.
		Linus
---
diff --git a/git-sh-setup.sh b/git-sh-setup.sh
index dbb9884..044b0b4 100755
--- a/git-sh-setup.sh
+++ b/git-sh-setup.sh
@@ -3,7 +3,7 @@
 # Set up GIT_DIR and GIT_OBJECT_DIRECTORY
 # and return true if everything looks ok
 #
-: ${GIT_DIR=.git}
+: ${GIT_DIR=$(git-rev-parse --git-dir)} || exit
 : ${GIT_OBJECT_DIRECTORY="$GIT_DIR/objects"}
 
 # Having this variable in your environment would break scripts because
diff --git a/gitk b/gitk
index a9d37d9..a934255 100755
--- a/gitk
+++ b/gitk
@@ -12,7 +12,7 @@ proc gitdir {} {
     if {[info exists env(GIT_DIR)]} {
 	return $env(GIT_DIR)
     } else {
-	return ".git"
+	return [exec git-rev-parse --git-dir]
     }
 }
 
diff --git a/setup.c b/setup.c
index c487d7e..96085dd 100644
--- a/setup.c
+++ b/setup.c
@@ -53,11 +53,10 @@ const char **get_pathspec(const char *pr
 	const char **p;
 	int prefixlen;
 
-	if (!prefix && !entry)
-		return NULL;
-
 	if (!entry) {
 		static const char *spec[2];
+		if (!prefix || !*prefix)
+			return NULL;
 		spec[0] = prefix;
 		spec[1] = NULL;
 		return spec;
@@ -120,9 +119,19 @@ const char *setup_git_directory(void)
 
 	if (offset == len)
 		return NULL;
-
 	/* Make "offset" point to past the '/', and add a '/' at the end */
 	offset++;
+
+	/*
+	 * If we're inside the ".git" directory, we have an empty prefix
+	 */
+	if (!strncmp(cwd + offset, ".git", 4)) {
+		switch (cwd[offset+4]) {
+		case '\0': case '/':
+			return "";
+		}
+	}
+
 	cwd[len++] = '/';
 	cwd[len] = 0;
 	return cwd + offset;
Previous: Daniel Barkalow
Message 2 of 2 in “git status and commit from subdirectory?”
  1. Daniel BarkalowOct 25, 2005
  2. Linus TorvaldsOct 26, 2005

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.