{"thread":{"id":"25945","subject":"Q about git rev-parse {--is-inside-work-tree, --show-cdup}","startedAt":"2010-12-03T21:06:36Z","lastAt":"2010-12-04T16:52:41Z","messageCount":6,"participants":["Dirk Süsserott","Jeff King","Junio C Hamano","Andreas Schwab"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"157265","messageId":"4CF95BDC.60506@dirk.my1.cc","threadId":"25945","inReplyTo":null,"subject":"Q about git rev-parse {--is-inside-work-tree, --show-cdup}","fromName":"Dirk Süsserott","fromEmail":"newsletter@dirk.my1.cc","sentAt":"2010-12-03T21:06:36Z","receivedAt":"2010-12-03T21:06:36Z","isPatch":false,"sender":{"key":"newsletter@dirk.my1.cc","avatar":null},"body":"Hi together,\n\nI wrote some shell scripts that do sth with my git repositories. I place\nmy scripts in my ~/bin folder (not in the repo). So the first step in my\nscripts is always to check whether they get called from whithin a git\nrepo and bail out if they don't.\n\n\nI do this like so:\n\n---------------------\nif [ \"$(git rev-parse --is-inside-work-tree)\" = \"true\" ] # (1)\nthen\n      here=$(pwd)\n      cdup=$(git rev-parse --show-cdup);    # (2a)\n      cdup=${cdup:-\".\"}                     # (2b)\n      cd $cdup                              # (2c)\n\n      [do sth useful from the topdir]\n\n      cd $here\n      exit 0;\nelse\n      echo \"Not inside a git working tree.\"\n      exit 1;\nfi\n---------------------\n\nI have two questions:\n\n1. Wouldn't it be useful, if \"git rev-parse\" (1) had an option \"-q\" that\nsimply indicates whether \"--is-inside-work-tree\" is true by means of the\nreturn code? Actually it has an option \"-q\" but that doesn't work with\n\"--is-inside-work-tree\".\n\n2. Wouldn't it be useful, if \"git rev-parse --show-cdup\" (2a) would\nreturn a dot \".\" instead of nothing if we are already in the topdir?\nThat would make the steps (2a), (2b), (2c) to a simple \"cd $(git\nrev-parse --show-cdup)\".\n\n(For those who aren't familiar with shell programming: (2b) means: 'if\n$cdup is empty, then set it to \".\"'.)\n\nI know that 2. is a problem, because \"git rev-parse\" is a plumbing tool.\nPeople likely rely on \"--show-dup\" to return an empty string. But what\nabout 1.? I think there's no harm if \"-q\" would work for\n\"--is-inside-work-tree\" as well.\n\nBTW: I'm sorry that all my sentences start with \"I\". ;-)\n\nWhat do you think? If appreciated, I'd like to work on a patch for (1).\n\nCheers,\n     Dirk\n"},{"id":"157267","messageId":"20101203212035.GB2632@sigill.intra.peff.net","threadId":"25945","inReplyTo":"4CF95BDC.60506@dirk.my1.cc","subject":"Re: Q about git rev-parse {--is-inside-work-tree, --show-cdup}","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2010-12-03T21:20:35Z","receivedAt":"2010-12-03T21:20:35Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Fri, Dec 03, 2010 at 10:06:36PM +0100, Dirk Süsserott wrote:\n\n> I wrote some shell scripts that do sth with my git repositories. I place\n> my scripts in my ~/bin folder (not in the repo). So the first step in my\n> scripts is always to check whether they get called from whithin a git\n> repo and bail out if they don't.\n> \n> I do this like so:\n> \n> ---------------------\n> if [ \"$(git rev-parse --is-inside-work-tree)\" = \"true\" ] # (1)\n> then\n>      here=$(pwd)\n>      cdup=$(git rev-parse --show-cdup);    # (2a)\n>      cdup=${cdup:-\".\"}                     # (2b)\n>      cd $cdup                              # (2c)\n> \n>      [do sth useful from the topdir]\n> \n>      cd $here\n>      exit 0;\n> else\n>      echo \"Not inside a git working tree.\"\n>      exit 1;\n> fi\n> ---------------------\n\nSince you are writing shell scripts, you may be interested in doing:\n\n  . git-sh-setup\n  require_work_tree\n  cd_to_toplevel\n\n> 1. Wouldn't it be useful, if \"git rev-parse\" (1) had an option \"-q\" that\n> simply indicates whether \"--is-inside-work-tree\" is true by means of the\n> return code? Actually it has an option \"-q\" but that doesn't work with\n> \"--is-inside-work-tree\".\n\nYeah, in general rev-parse's \"-q\" is annoying. It only works with\nchecking revisions (like \"git rev-parse -q --verify HEAD\"), and it\nprobably should apply to a lot more. Patches welcome.\n\n> 2. Wouldn't it be useful, if \"git rev-parse --show-cdup\" (2a) would\n> return a dot \".\" instead of nothing if we are already in the topdir?\n> That would make the steps (2a), (2b), (2c) to a simple \"cd $(git\n> rev-parse --show-cdup)\".\n\nI'm hesitant to change it, as that is a public interface for shell\nscripts. However, there is also \"--show-toplevel\" which will do what you\nwant in one line (or just cd_to_toplevel as I mentioned above).\n\n> What do you think? If appreciated, I'd like to work on a patch for (1).\n\nPlease do.\n\n-Peff\n"},{"id":"157268","messageId":"7v4oauo8fj.fsf@alter.siamese.dyndns.org","threadId":"25945","inReplyTo":"4CF95BDC.60506@dirk.my1.cc","subject":"Re: Q about git rev-parse {--is-inside-work-tree, --show-cdup}","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2010-12-03T21:25:36Z","receivedAt":"2010-12-03T21:25:36Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Dirk Süsserott <newsletter@dirk.my1.cc> writes:\n\n> Hi together,\n>\n> I wrote some shell scripts that do sth with my git repositories. I place\n> my scripts in my ~/bin folder (not in the repo). So the first step in my\n> scripts is always to check whether they get called from whithin a git\n> repo and bail out if they don't.\n>\n>\n> I do this like so:\n>\n> ---------------------\n> if [ \"$(git rev-parse --is-inside-work-tree)\" = \"true\" ] # (1)\n> then\n>      here=$(pwd)\n>      cdup=$(git rev-parse --show-cdup);    # (2a)\n>      cdup=${cdup:-\".\"}                     # (2b)\n>      cd $cdup                              # (2c)\n>\n>      [do sth useful from the topdir]\n>\n>      cd $here\n>      exit 0;\n> else\n>      echo \"Not inside a git working tree.\"\n>      exit 1;\n> fi\n> ---------------------\n>\n> I have two questions:\n>\n> 1. Wouldn't it be useful, if \"git rev-parse\" (1) had an option \"-q\" that\n> simply indicates whether \"--is-inside-work-tree\" is true by means of the\n> return code? Actually it has an option \"-q\" but that doesn't work with\n> \"--is-inside-work-tree\".\n\nThat would break existing scripts that expect \"-q\" to squelch only the\nerror output, no?  I think the risk of breaking existing scripts that\nother people wrote over time that you (and I) haven't seen outweighs any\nbenefit (i.e. \"if test $(rev-parse...) = true\" vs \"if rev-parse...\") you\nare seeing here.\n\nAnother thing to consider is what the script should do when rev-parse\ndetects an error.  Do we know that all scripts want to behave exactly the\nsame way in two cases: (1) when run outside the work tree; and (2) when\nthey cannot determine if they were run from inside or outside?  I don't\nthink so.\n\nIn your example, you are only interested to work inside work tree, and you\nmay find \"if rev-parse... then do this interesting thing else fail fi\"\nsufficient, but a script by somebody else may want to make sure that it is\nnot run inside any git controlled working tree, and it cannot say \"if\nrev-parse then punt else do this big thing fi\" is not an appropriate way\nto write it.\n\nSo in that sense, \"if rev-parse ...\" is not a huge improvement to begin\nwith, for people who want to write their script strictly.  They would\nprobably need to say something like:\n\n\tans=$(git rev-parse ...) || {\n\t\tdo the error thing\n                exit 1\n\t}\n       \tcase \"$ans\" in\n        true)\n\t\tdo the inside-work-tree thing ;;\n\tfalse)\n\t\tdo the outside-work-tree thing ;;\n\t*)\n        \tdo the oops thing;;\n\tesac\n"},{"id":"157271","messageId":"20101203215032.GB3972@sigill.intra.peff.net","threadId":"25945","inReplyTo":"7v4oauo8fj.fsf@alter.siamese.dyndns.org","subject":"Re: Q about git rev-parse {--is-inside-work-tree, --show-cdup}","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2010-12-03T21:50:32Z","receivedAt":"2010-12-03T21:50:32Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Fri, Dec 03, 2010 at 01:25:36PM -0800, Junio C Hamano wrote:\n\n> > 1. Wouldn't it be useful, if \"git rev-parse\" (1) had an option \"-q\" that\n> > simply indicates whether \"--is-inside-work-tree\" is true by means of the\n> > return code? Actually it has an option \"-q\" but that doesn't work with\n> > \"--is-inside-work-tree\".\n> \n> That would break existing scripts that expect \"-q\" to squelch only the\n> error output, no?  I think the risk of breaking existing scripts that\n> other people wrote over time that you (and I) haven't seen outweighs any\n> benefit (i.e. \"if test $(rev-parse...) = true\" vs \"if rev-parse...\") you\n> are seeing here.\n\nRight now \"-q\" doesn't do _anything_ for --is-inside-work-tree, AFAICT.\nIt is a useless no-op. So I don't know if we are breaking anybody. What\ndoes somebody doing \"git rev-parse -q --is-inside-work-tree\" expect to\nhappen?\n\nI don't see why they would expect it to suppress error output. Usually\n\"-q\" is about \"suppress non-essential output, but keep errors coming\".\nIf you wanted to suppress errors, you would use \"2>/dev/null\".\n\nThat being said, in my original reply I only half-thought about Dirk's\nproblem, and considered more the number of times \"git rev-parse -q\" has\nannoyed me in the past by doing nothing[1], and just assumed this was\nanother such case. It really isn't that hard to just check $(git\nrev-parse) in this instance.\n\n-Peff\n\n[1] I wish I could remember my exact case. It's something that I\nremember coming up no more than once every month or two, but that annoys\nme every time, because it doesn't do what I expect.\n\n-Peff\n"},{"id":"157303","messageId":"4CFA5A67.2020108@dirk.my1.cc","threadId":"25945","inReplyTo":"20101203215032.GB3972@sigill.intra.peff.net","subject":"Re: Q about git rev-parse {--is-inside-work-tree, --show-cdup}","fromName":"Dirk Süsserott","fromEmail":"newsletter@dirk.my1.cc","sentAt":"2010-12-04T15:12:39Z","receivedAt":"2010-12-04T15:12:39Z","isPatch":false,"sender":{"key":"newsletter@dirk.my1.cc","avatar":null},"body":"Am 03.12.2010 22:50 schrieb Jeff King:\n> On Fri, Dec 03, 2010 at 01:25:36PM -0800, Junio C Hamano wrote:\n>\n>> That would break existing scripts that expect \"-q\" to squelch only the\n>> error output, no?  I think the risk of breaking existing scripts that\n>> other people wrote over time that you (and I) haven't seen outweighs any\n>> benefit (i.e. \"if test $(rev-parse...) = true\" vs \"if rev-parse...\") you\n>> are seeing here.\n>\n> Right now \"-q\" doesn't do _anything_ for --is-inside-work-tree, AFAICT.\n> It is a useless no-op. So I don't know if we are breaking anybody. What\n> does somebody doing \"git rev-parse -q --is-inside-work-tree\" expect to\n> happen?\n>\n\nPeff, Junio, thanks for your answers.\n\nI already had a suspicion that changing plumbing tools like\nrev-parse was a bad idea. You confirmed that. However, I still\nthink it's a little bug that \"--show-dup\" returns an empty string\ninstead of a dot when already in topdir. But it's too late to\nchange that, I guess. I didn't know about Peff's suggestions\n\"git-sh-setup\" and \"--show-toplevel\". They may help me.\n\n@Peff, right, it's annoying that -q sometimes works and\nsometimes doesn't. To my opinion switches like --quiet and\n--verbose should _always_ work.\n\n     Dirk\n"},{"id":"157305","messageId":"m24oatcwfa.fsf@igel.home","threadId":"25945","inReplyTo":"4CF95BDC.60506@dirk.my1.cc","subject":"Re: Q about git rev-parse {--is-inside-work-tree, --show-cdup}","fromName":"Andreas Schwab","fromEmail":"schwab@linux-m68k.org","sentAt":"2010-12-04T16:52:41Z","receivedAt":"2010-12-04T16:52:41Z","isPatch":false,"sender":{"key":"schwab@linux-m68k.org","avatar":"https://avatars.githubusercontent.com/u/2175493?v=4"},"body":"Dirk Süsserott <newsletter@dirk.my1.cc> writes:\n\n> 2. Wouldn't it be useful, if \"git rev-parse --show-cdup\" (2a) would\n> return a dot \".\" instead of nothing if we are already in the topdir?\n> That would make the steps (2a), (2b), (2c) to a simple \"cd $(git\n> rev-parse --show-cdup)\".\n\nThe string returned by --show-cdup is to be used as prefix for a\nrelative path.  If you want to use it standalone just append \".\":\n\ncd $(git rev-parse --show-cdup).\n\nAndreas.\n\n-- \nAndreas Schwab, schwab@linux-m68k.org\nGPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5\n\"And now for something completely different.\"\n"}]}