{"thread":{"id":"33377","subject":"Two potential bugs in aliases that expand to shell commands","startedAt":"2013-04-04T02:05:33Z","lastAt":"2013-04-04T06:40:07Z","messageCount":2,"participants":["Han","Jeff King"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"213083","messageId":"CACE=nd1dwhCyrKdzFq7O4UgD1TH8-c5R+4L2rH0NGW-jWqLr_g@mail.gmail.com","threadId":"33377","inReplyTo":null,"subject":"Two potential bugs in aliases that expand to shell commands","fromName":"Han","fromEmail":"laughinghan@gmail.com","sentAt":"2013-04-04T02:05:33Z","receivedAt":"2013-04-04T02:05:33Z","isPatch":false,"sender":{"key":"laughinghan@gmail.com","avatar":null},"body":"These might just be documentation bugs.\n\nAliases that expand to shell commands are only documented fairly\nsuperficially, but `git help config` does say: \"If the alias expansion\nis prefixed with an exclamation point, it will be treated as a shell\ncommand.\"\n\nIt also says, nearer the beginning: \"String values may be entirely or\npartially enclosed in double quotes. You need to enclose variable\nvalues in double quotes if you want to preserve leading or trailing\nwhitespace, or if the variable value contains comment characters (i.e.\nit contains # or ;).\"\n\nThere appears to be another case string values need to be enclosed in\nquotes, which is a shell command where you want to preserve quote\ncharacters (not leading or trailing); a minimal example is\n\n  shortcut = !cd \"\" && pwd\n\nwill act like you ran, in bash,\n\n$ cd  && pwd\n\nand print your homedir path, whereas\n\n  shortcut = !\"cd \\\"\\\" && pwd\"\n\nwill act like you ran, in bash,\n\n$ cd \"\" && pwd\n\nand stay in the top-level directory of the repo (which is where\naliases that expand to shell commands are run from).\n\nThis is problematic precisely because aliases that expand to shell\ncommands are run from the top-level directory of the repo: if you had\nan alias that worked great at the top-level directory, like\n\n  shortcut = !do_something\n\nbut it was doing the wrong thing in subdirectories, my first instinct,\nat least, upon reading \"Note that shell commands will be executed from\nthe top-level directory of a repository, which may not necessarily be\nthe current directory. GIT_PREFIX is set as returned by running git\nrev-parse --show-prefix from the original current directory.\", is to\ndo\n\n  shortcut = !cd \"$GIT_PREFIX\" && do_something\n\nwhich will now do the right thing in subdirectories, but at the\ntop-level directory of the repo, $GIT_PREFIX is undefined/the empty\nstring, and it cds to your homedir.\n\nThe other bug I'm much more confused by. If you have an alias like\n\n  shortcut = !\"echo -n lol; echo wut\"\n\nit will, in fact, print\n\n-n lol\nwut\n\nwhich is, uh, not what bash prints. Is git special-casing echo?\n\n(I discovered these while adding something semi-jokingly to my\n.gitconfig: https://github.com/laughinghan/dotfiles/commit/34f5528825b287ff40acfe57808b32931a87261c\n)\n\nHan\n"},{"id":"213085","messageId":"20130404064007.GA8278@sigill.intra.peff.net","threadId":"33377","inReplyTo":"CACE=nd1dwhCyrKdzFq7O4UgD1TH8-c5R+4L2rH0NGW-jWqLr_g@mail.gmail.com","subject":"Re: Two potential bugs in aliases that expand to shell commands","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2013-04-04T06:40:07Z","receivedAt":"2013-04-04T06:40:07Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Wed, Apr 03, 2013 at 07:05:33PM -0700, Han wrote:\n\n> There appears to be another case string values need to be enclosed in\n> quotes, which is a shell command where you want to preserve quote\n> characters (not leading or trailing); a minimal example is\n> \n>   shortcut = !cd \"\" && pwd\n\nYes. You must escape any double-quotes that are not the beginning or\nend of a quoted string.\n\n>   shortcut = !\"cd \\\"\\\" && pwd\"\n\nThis is fine. Technically so is:\n\n  shortcut = !cd \\\"\\\" && pwd\n\nbut I think it is more readable to put such shell snippets inside a\ndouble-quoted string, to make it more clear what is going on.\n\nDocumentation patches welcome.\n\n> The other bug I'm much more confused by. If you have an alias like\n> \n>   shortcut = !\"echo -n lol; echo wut\"\n> \n> it will, in fact, print\n> \n> -n lol\n> wut\n> \n> which is, uh, not what bash prints. Is git special-casing echo?\n\nNo, git does not special-case echo. But it runs shell commands with\n/bin/sh, which may or may not be bash on your system (and \"-n\" is not\nnecessarily portable to other POSIX shells).\n\nIf you really want to use bash, do:\n\n  shortcut = \"!bash -c 'echo -n lol; echo wut'\"\n\nor just use printf, which is a portable way to spell \"echo -n\".\n\n-Peff\n"}]}