From: Junio C Hamano Date: Fri, 23 Dec 2005 04:58:16 GMT Subject: Re: git-checkout and SUBDIRECTORY_OK='Yes' Message-ID: <7virtge7fb.fsf@assigned-by-dhcp.cox.net> In-Reply-To: Jon Nelson 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] git-checkout [-b ] 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 ... ;# out of tree to index and file git-checkout -- ... ;# 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 ] [] [...]' +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];