{"thread":{"id":"16582","subject":"git alias always chdir to top","startedAt":"2008-12-03T16:08:52Z","lastAt":"2008-12-05T14:09:48Z","messageCount":3,"participants":["Pete Wyckoff","Jeff King"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"97076","messageId":"20081203160852.GA3773@osc.edu","threadId":"16582","inReplyTo":null,"subject":"git alias always chdir to top","fromName":"Pete Wyckoff","fromEmail":"pw@padd.com","sentAt":"2008-12-03T16:08:52Z","receivedAt":"2008-12-03T16:08:52Z","isPatch":false,"sender":{"key":"pw@padd.com","avatar":null},"body":"I have a git alias that takes a relative file name argument,\nand would like to know from where in the tree it was invoked,\nespecially if inside a subdirectory of the git tree.\n\nConsider .git/config:\n\n    [alias]\n\tpwd = !/bin/pwd\n\nThen a git tree:\n\n    /home/me\n\ttopdir/\n\t    .git/\n\t    subdir/\n\t\tsubdir-y.c\n\t    topdir-x.c\n\nThen inside /home/me/topdir, all is well:\n\n    $ pwd\n    /home/me/topdir\n    $ git pwd\n    /home/me/topdir\n\nBut inside /home/me/topdir/subdir, the pwd alias is invoked in the wrong\ndir:\n\n    $ pwd\n    /home/me/topdir/subdir\n    $ git pwd\n    /home/me/topdir\n\nThe implication of this is that I call an alias command like:\n\n    $ pwd\n    /home/me/topdir/subdir\n    $ git myalias subdir-y.c\n    myalias: No such file subdir-y.c\n\nIt looks like handle_alias() uses setup_git_directory_gently() to\nfind the .git, which chdir()s up until it gets there.  Is there a\nway to do this without changing the process current working\ndirectory instead?  I could even handle an environment variable\nsaving the original cwd, but that's ickier.\n\n\t\t-- Pete\n"},{"id":"97143","messageId":"20081204123402.GA23740@coredump.intra.peff.net","threadId":"16582","inReplyTo":"20081203160852.GA3773@osc.edu","subject":"Re: git alias always chdir to top","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2008-12-04T12:34:02Z","receivedAt":"2008-12-04T12:34:02Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Wed, Dec 03, 2008 at 11:08:52AM -0500, Pete Wyckoff wrote:\n\n> It looks like handle_alias() uses setup_git_directory_gently() to\n> find the .git, which chdir()s up until it gets there.  Is there a\n> way to do this without changing the process current working\n> directory instead?  I could even handle an environment variable\n> saving the original cwd, but that's ickier.\n\nThere has been some discussion of refactoring the setup to _not_ do that\nchdir until later, which should fix your problem. And other problems,\ntoo, since aliases can get confused about whether or not we're in a\nworktree (try \"git config alias.st status && cd .git && git st\") as a\nresult of the startup sequence.  Ideally the _only_ thing to happen\nbefore running an alias would be to look at the config to see how to run\nthe alias, and everything else would be \"as if\" you had just run the\nalias manually.\n\nSo no, there's no way to do it correctly right now. The git commands\ninternally do know the original prefix, but I don't think it is exposed\nvia the environment.\n\nI hope this will get fixed eventually, but refactoring this code is\nunpleasant enough and I have been busy enough that it hasn't happened\nyet. You are of course welcome to volunteer. ;)\n\n-Peff\n"},{"id":"97205","messageId":"20081205140948.GB23087@osc.edu","threadId":"16582","inReplyTo":"20081204123402.GA23740@coredump.intra.peff.net","subject":"Re: git alias always chdir to top","fromName":"Pete Wyckoff","fromEmail":"pw@padd.com","sentAt":"2008-12-05T14:09:48Z","receivedAt":"2008-12-05T14:09:48Z","isPatch":false,"sender":{"key":"pw@padd.com","avatar":null},"body":"peff@peff.net wrote on Thu, 04 Dec 2008 07:34 -0500:\n> On Wed, Dec 03, 2008 at 11:08:52AM -0500, Pete Wyckoff wrote:\n> \n> > It looks like handle_alias() uses setup_git_directory_gently() to\n> > find the .git, which chdir()s up until it gets there.  Is there a\n> > way to do this without changing the process current working\n> > directory instead?  I could even handle an environment variable\n> > saving the original cwd, but that's ickier.\n> \n> There has been some discussion of refactoring the setup to _not_ do that\n> chdir until later, which should fix your problem. And other problems,\n> too, since aliases can get confused about whether or not we're in a\n> worktree (try \"git config alias.st status && cd .git && git st\") as a\n> result of the startup sequence.  Ideally the _only_ thing to happen\n> before running an alias would be to look at the config to see how to run\n> the alias, and everything else would be \"as if\" you had just run the\n> alias manually.\n> \n> So no, there's no way to do it correctly right now. The git commands\n> internally do know the original prefix, but I don't think it is exposed\n> via the environment.\n> \n> I hope this will get fixed eventually, but refactoring this code is\n> unpleasant enough and I have been busy enough that it hasn't happened\n> yet. You are of course welcome to volunteer. ;)\n\nThanks for these comments.  I missed the discussion about\nrefactoring to move the chdir around.\n\nIn my particular case, since the only usage of this particular git\nalias is by another script, I can get away with passing the full\npath name and making some assumptions about the caller.\n\nPoint taken that it would be good to clean up the alias code path\nso that this issue doesn't even arise in the first place.\n\n\t\t-- Pete\n"}]}