git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <junkio@cox.net>
To: Jon Nelson <jnelson-git@jamponi.net>
Cc: git@vger.kernel.org
Subject: Re: git-checkout and SUBDIRECTORY_OK='Yes'
Date: Thu, 22 Dec 2005 20:58:16 -0800	[thread overview]
Message-ID: <7virtge7fb.fsf@assigned-by-dhcp.cox.net> (raw)
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];

  reply	other threads:[~2005-12-23  4:58 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-12-23  4:20 git-checkout and SUBDIRECTORY_OK='Yes' Jon Nelson
2005-12-23  4:58 ` Junio C Hamano [this message]
2005-12-23 17:56   ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7virtge7fb.fsf@assigned-by-dhcp.cox.net \
    --to=junkio@cox.net \
    --cc=git@vger.kernel.org \
    --cc=jnelson-git@jamponi.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).