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

Re: git-checkout and SUBDIRECTORY_OK='Yes'

From
Junio C Hamano <junkio@cox.net>
Date
Dec 23, 2005, 04:58 UTC
Message-ID
<7virtge7fb.fsf@assigned-by-dhcp.cox.net>
In-Reply-To
<Pine.LNX.4.63.0512222217040.6812@gheavc.wnzcbav.cig>
Jon Nelson <jnelson-git@jamponi.net> writes:
> Is it safe to set SUBDIRECTORY_OK='Yes' in git-checkout.sh?

Personally, I do not like this kind of question. If it were known to be safe we would have set it so already.

Ah, I must be tired.  Sorry.  Let's try again.

git-checkout does two very different things, and what they should do when run from subdirectory are probably quite different.

It does not make any sense to run the one that switches the current head from anywhere other than the toplevel:

	git-checkout [-f] <branch>
        git-checkout [-b <branch>] <committish>

We could of course chdir to top and do the whole-tree checkout in git-checkout, but the point is the operation does not make sense on a partial tree.

The other form is to update the index file and working tree file selectively:

	git-checkout <treeish> <file>... ;# out of tree to index and file
        git-checkout -- <file>...	 ;# out of index to file

This form _does_ make sense to run from subdirectory; and I myself often wish we supported this.

Before SUBDIRECTORY_OK was introduced, git-*.sh commands were carefully inspected and the ones that do not make much sense to be run in subdirectories were marked with sh-setup. So if you do not see SUBDIRECTORY_OK, it is very likely that you need some real work (like the analysis above to think what should happen when run from a subdirectory, and actually coding it).

This patch *might* work, or it may not.  Please let us know.
diff --git a/git-checkout.sh b/git-checkout.sh
index 36308d2..1219ea0 100755
--- a/git-checkout.sh
+++ b/git-checkout.sh
@@ -1,6 +1,7 @@
 #!/bin/sh
 
 USAGE='[-f] [-b <new_branch>] [<branch>] [<paths>...]'
+SUBDIRECTORY_OK=Sometimes
 . git-sh-setup
 
 old=$(git-rev-parse HEAD)
@@ -95,6 +96,14 @@ else
 	fi
 fi
 
+# We are switching branches and checking out trees, so
+# we *NEED* to be at the toplevel.
+cdup=$(git-rev-parse --show-cdup)
+if test ! -z "$cdup"
+then
+	cd "$cdup"
+fi
+
 [ -z "$new" ] && new=$old
 
 # If we don't have an old branch that we're switching to,
diff --git a/rev-parse.c b/rev-parse.c
index bb4949a..0c951af 100644
--- a/rev-parse.c
+++ b/rev-parse.c
@@ -216,6 +216,18 @@ int main(int argc, char **argv)
 					puts(prefix);
 				continue;
 			}
+			if (!strcmp(arg, "--show-cdup")) {
+				const char *pfx = prefix;
+				while (pfx) {
+					pfx = strchr(pfx, '/');
+					if (pfx) {
+						pfx++;
+						printf("../");
+					}
+				}
+				putchar('\n');
+				continue;
+			}
 			if (!strcmp(arg, "--git-dir")) {
 				const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
 				static char cwd[PATH_MAX];
Previous: Jon NelsonNext: Junio C Hamano
Message 2 of 3 in “git-checkout and SUBDIRECTORY_OK='Yes'”
  1. Jon NelsonDec 23, 2005
  2. Junio C HamanoDec 23, 2005
  3. Junio C HamanoDec 23, 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.