{"thread":{"id":"9809","subject":"how to access working tree from .git dir?","startedAt":"2007-09-06T23:20:00Z","lastAt":"2007-09-08T01:02:41Z","messageCount":9,"participants":["Josh England","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"52816","messageId":"1189120800.6203.23.camel@beauty","threadId":"9809","inReplyTo":null,"subject":"how to access working tree from .git dir?","fromName":"Josh England","fromEmail":"jjengla@sandia.gov","sentAt":"2007-09-06T23:20:00Z","receivedAt":"2007-09-06T23:20:00Z","isPatch":false,"sender":{"key":"jjengla@sandia.gov","avatar":null},"body":"In messsing around with hooks, I've discovered that not all hooks are\nrun in the same environment.  In particular, the current working\ndirectory in the post-receive hook (maybe others as well) is the GIT_DIR\n(.git) directory, instead of the root of the working tree (as in\npre-commit).\n\nThis wouldn't be so bad, but it seems that `git rev-parse --show-cdup`\ndoes not show anything valid if your current working directory is inside\nthe .git dir.  This creates a scenario where there is no consistent way\nto access the root of the working tree from within a hook.  To make\nthings worse, the behavior changes subtly whether $GIT_DIR is defined or\nnot.\n\nI've got a patch that changes the current working directory before\ncalling the post-receive hook, but that's more of a workaround than a\nfix.  I'd like to be able to run `git rev-parse --show-cdup` from with\na .git directory and have it produce a valid result.\n\nSo:  I'm poking around around and trying to find the right answer but\nsome things look weird.  If anyone knows the quick-and-easy answer here\nplease let me know.  Otherwise, I'll continue to poke around some more.\n\nThanks,\n\n-JE\n"},{"id":"52942","messageId":"7v642m436q.fsf@gitster.siamese.dyndns.org","threadId":"9809","inReplyTo":"1189120800.6203.23.camel@beauty","subject":"Re: how to access working tree from .git dir?","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2007-09-07T20:43:09Z","receivedAt":"2007-09-07T20:43:09Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Josh England\" <jjengla@sandia.gov> writes:\n\n> In messsing around with hooks, I've discovered that not all hooks are\n> run in the same environment.  In particular, the current working\n> directory in the post-receive hook (maybe others as well) is the GIT_DIR\n> (.git) directory, instead of the root of the working tree (as in\n> pre-commit).\n\nIt is not even \"instead of\"; that's the only sane thing to do\nfor post-receive, which is in response to git-push and usually\nused for a bare repository, i.e. without any work tree.\n"},{"id":"52949","messageId":"1189200421.12525.8.camel@beauty","threadId":"9809","inReplyTo":"7v642m436q.fsf@gitster.siamese.dyndns.org","subject":"Re: how to access working tree from .git dir?","fromName":"Josh England","fromEmail":"jjengla@comcast.net","sentAt":"2007-09-07T21:27:01Z","receivedAt":"2007-09-07T21:27:01Z","isPatch":false,"sender":{"key":"jjengla@comcast.net","avatar":null},"body":"On Fri, 2007-09-07 at 13:43 -0700, Junio C Hamano wrote:\n> \"Josh England\" <jjengla@sandia.gov> writes:\n> \n> > In messsing around with hooks, I've discovered that not all hooks are\n> > run in the same environment.  In particular, the current working\n> > directory in the post-receive hook (maybe others as well) is the GIT_DIR\n> > (.git) directory, instead of the root of the working tree (as in\n> > pre-commit).\n> \n> It is not even \"instead of\"; that's the only sane thing to do\n> for post-receive, which is in response to git-push and usually\n> used for a bare repository, i.e. without any work tree.\n\nI thought there was probably a sane reason for it.  That is perfectly\nacceptable, but the problem still exists that there doesn't seem to be a\ngood way to access the top of the working tree from within the GIT_DIR.\nSince I now know that post-receive has a CWD in .git, I could just use\n`pwd`/../ , but I was hoping for a better (read: consistent between\nhooks) solution.\n\n-JE\n"},{"id":"52951","messageId":"7vhcm62lru.fsf@gitster.siamese.dyndns.org","threadId":"9809","inReplyTo":"1189200421.12525.8.camel@beauty","subject":"Re: how to access working tree from .git dir?","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2007-09-07T21:44:37Z","receivedAt":"2007-09-07T21:44:37Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Josh England <jjengla@comcast.net> writes:\n\n> ... there doesn't seem to be a\n> good way to access the top of the working tree from within the GIT_DIR.\n> Since I now know that post-receive has a CWD in .git, I could just use\n> `pwd`/../ , but I was hoping for a better (read: consistent between\n> hooks) solution.\n\nI do not think it is a bad thing for _your_ script to have the\nknowledge that in _your_ repositories everywhere, the top of the\nwork tree is $GIT_DIR/.. and there is no repository that lacks a\nwork tree.  Obviously that would not work for people with bare\nrepositories, but they would not be using _your_ script in their\nbare repositories, so that is Ok.\n\nYou can also configure core.worktree in $GIT_DIR/config and use\nthat from the hook script, I presume, although I haven't done it\n(and do not see a need to do so) myself yet.\n"},{"id":"52955","messageId":"1189203064.15140.2.camel@beauty","threadId":"9809","inReplyTo":"7vhcm62lru.fsf@gitster.siamese.dyndns.org","subject":"Re: how to access working tree from .git dir?","fromName":"Josh England","fromEmail":"jjengla@comcast.net","sentAt":"2007-09-07T22:11:04Z","receivedAt":"2007-09-07T22:11:04Z","isPatch":false,"sender":{"key":"jjengla@comcast.net","avatar":null},"body":"On Fri, 2007-09-07 at 14:44 -0700, Junio C Hamano wrote:\n> Josh England <jjengla@comcast.net> writes:\n> \n> > ... there doesn't seem to be a\n> > good way to access the top of the working tree from within the GIT_DIR.\n> > Since I now know that post-receive has a CWD in .git, I could just use\n> > `pwd`/../ , but I was hoping for a better (read: consistent between\n> > hooks) solution.\n> \n> I do not think it is a bad thing for _your_ script to have the\n> knowledge that in _your_ repositories everywhere, the top of the\n> work tree is $GIT_DIR/.. and there is no repository that lacks a\n> work tree.  Obviously that would not work for people with bare\n> repositories, but they would not be using _your_ script in their\n> bare repositories, so that is Ok.\n\nOK. Fair enough.  Maybe it would be good to note in git-sh-setup.sh that\nmany of the supplied functions will not work when called from within\n$GIT_DIR.\n\n-JE\n"},{"id":"52956","messageId":"7vveam15w9.fsf@gitster.siamese.dyndns.org","threadId":"9809","inReplyTo":"1189203064.15140.2.camel@beauty","subject":"Re: how to access working tree from .git dir?","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2007-09-07T22:12:54Z","receivedAt":"2007-09-07T22:12:54Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Josh England <jjengla@comcast.net> writes:\n\n> OK. Fair enough.  Maybe it would be good to note in git-sh-setup.sh that\n> many of the supplied functions will not work when called from within\n> $GIT_DIR.\n\nSorry, \"supplied functions\"?  Care to clarify with a patch?\n"},{"id":"52959","messageId":"1189204498.15140.4.camel@beauty","threadId":"9809","inReplyTo":"7vveam15w9.fsf@gitster.siamese.dyndns.org","subject":"Re: how to access working tree from .git dir?","fromName":"Josh England","fromEmail":"jjengla@comcast.net","sentAt":"2007-09-07T22:34:58Z","receivedAt":"2007-09-07T22:34:58Z","isPatch":false,"sender":{"key":"jjengla@comcast.net","avatar":null},"body":"On Fri, 2007-09-07 at 15:12 -0700, Junio C Hamano wrote:\n> Josh England <jjengla@comcast.net> writes:\n> \n> > OK. Fair enough.  Maybe it would be good to note in git-sh-setup.sh that\n> > many of the supplied functions will not work when called from within\n> > $GIT_DIR.\n> \n> Sorry, \"supplied functions\"?  Care to clarify with a patch?\n\nI guess really just the cd_to_topdir() function.  It will silently fail\nwhen run from within $GIT_DIR.\n\n-JE\n"},{"id":"52963","messageId":"7vr6la11q9.fsf@gitster.siamese.dyndns.org","threadId":"9809","inReplyTo":"1189204498.15140.4.camel@beauty","subject":"Re: how to access working tree from .git dir?","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2007-09-07T23:42:54Z","receivedAt":"2007-09-07T23:42:54Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Josh England <jjengla@comcast.net> writes:\n\n> On Fri, 2007-09-07 at 15:12 -0700, Junio C Hamano wrote:\n>> Josh England <jjengla@comcast.net> writes:\n>> \n>> > OK. Fair enough.  Maybe it would be good to note in git-sh-setup.sh that\n>> > many of the supplied functions will not work when called from within\n>> > $GIT_DIR.\n>> \n>> Sorry, \"supplied functions\"?  Care to clarify with a patch?\n>\n> I guess really just the cd_to_topdir() function.  It will silently fail\n> when run from within $GIT_DIR.\n\nAh, I see what you meant.\n\nI think you probably are supposed to check with is_bare_repository\nor something before calling that, as asking to cd to toplevel\nimplies you know there is such a thing as toplevel ;-)\n"},{"id":"52968","messageId":"1189213361.15140.13.camel@beauty","threadId":"9809","inReplyTo":"7vr6la11q9.fsf@gitster.siamese.dyndns.org","subject":"Re: how to access working tree from .git dir?","fromName":"Josh England","fromEmail":"jjengla@comcast.net","sentAt":"2007-09-08T01:02:41Z","receivedAt":"2007-09-08T01:02:41Z","isPatch":false,"sender":{"key":"jjengla@comcast.net","avatar":null},"body":"On Fri, 2007-09-07 at 16:42 -0700, Junio C Hamano wrote:\n> Josh England <jjengla@comcast.net> writes:\n> \n> > On Fri, 2007-09-07 at 15:12 -0700, Junio C Hamano wrote:\n> >> Josh England <jjengla@comcast.net> writes:\n> >> \n> >> > OK. Fair enough.  Maybe it would be good to note in git-sh-setup.sh that\n> >> > many of the supplied functions will not work when called from within\n> >> > $GIT_DIR.\n> >> \n> >> Sorry, \"supplied functions\"?  Care to clarify with a patch?\n> >\n> > I guess really just the cd_to_topdir() function.  It will silently fail\n> > when run from within $GIT_DIR.\n> \n> Ah, I see what you meant.\n> \n> I think you probably are supposed to check with is_bare_repository\n> or something before calling that, as asking to cd to toplevel\n> implies you know there is such a thing as toplevel ;-)\n\nThat's fine.  I guess using `$GIT_DIR/../` as you mentioned before will\njust have to work.\n\n-JE\n"}]}