{"thread":{"id":"40955","subject":"git branch in Bash subshell \"$(git branch -a)\" including ls output as part of return?","startedAt":"2015-12-07T16:52:28Z","lastAt":"2015-12-07T17:57:30Z","messageCount":4,"participants":["Alex Jones","Charles Bailey"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"274113","messageId":"CAJGk2Wx4B=AHFxkJep=yYVKU9UyJieazoNaC44W5yBXoMi+gdg@mail.gmail.com","threadId":"40955","inReplyTo":null,"subject":"git branch in Bash subshell \"$(git branch -a)\" including ls output as part of return?","fromName":"Alex Jones","fromEmail":"ajones@netsertive.com","sentAt":"2015-12-07T16:52:28Z","receivedAt":"2015-12-07T16:52:28Z","isPatch":false,"sender":{"key":"ajones@netsertive.com","avatar":null},"body":"Hello Folks,\n\nI am Running OSX 10.10.5 Yosemite along with git 2.6.3 installed via\nhomebrew package manager.\n\nI recently stumbled across the following bug in some scripting I was\ndoing. \"git branch -a --list\" and \"git branch -a\" seem to include the\noutput of an \"ls\" command if executed as part of a subshell in a bash\nscript. I can't speculate to the reason.\n\nScript goal: Delete all branches aside from master in the repository\nExample outputs from several commands  repo:\n\nls output:\n\najonespro:Deploy_Script ajones$ ls -l\ntotal 0\ndrwxr-xr-x  11 ajones  wheel  374 Dec  7 10:50 AppDeploy\ndrwxr-xr-x  11 ajones  wheel  374 Dec  7 11:15 WebDeploy\n\n\ngit branch -a output:\n\najonespro:Deploy_Script ajones$ git branch -a\n\n* DWH_concurrent_api\n  Email_No_Error_If_No_Old_Version\n  IT/configs_in_app_support\n  PHP_Build_Repo\n  master\n  remotes/origin/DWH_concurrent_api\n  remotes/origin/Email_No_Error_If_No_Old_Version\n  remotes/origin/IT/configs_in_app_support\n  remotes/origin/PHP_Build_Repo\n  remotes/origin/master\n\necho $(git branch -a) output:\n\najonespro:Deploy_Script ajones$ echo $(git branch -a)\nAppDeploy WebDeploy DWH_concurrent_api\nEmail_No_Error_If_No_Old_Version IT/configs_in_app_support\nPHP_Build_Repo master remotes/origin/DWH_concurrent_api\nremotes/origin/Email_No_Error_If_No_Old_Version\nremotes/origin/IT/configs_in_app_support remotes/origin/PHP_Build_Repo\nremotes/origin/master\n\nWhile it might be hard to see from that output, The first two\n\"branches\" in the subshell's output are actually the directories\ncontained within the repo. If I place a file at the root it includes\nthat in the branch list as well. Since my test file \"test.txt\" (not\nshown in example) and \"WebDeploy\" are sorted before all the branches,\nI suspect some output buffer is being accidentally being written to.\n\nHas any experienced this before, and can anyone reproduce it on a\ndifferent configuration? I wouldn't be surprised if it was some weird\nbug with Apple's included terminal, but since I've only observed it\nwith git branch, I thought I'd try contact git maintainers first.\n"},{"id":"274114","messageId":"20151207165810.GA25856@hashpling.org","threadId":"40955","inReplyTo":"CAJGk2Wx4B=AHFxkJep=yYVKU9UyJieazoNaC44W5yBXoMi+gdg@mail.gmail.com","subject":"Re: git branch in Bash subshell \"$(git branch -a)\" including ls output as part of return?","fromName":"Charles Bailey","fromEmail":"charles@hashpling.org","sentAt":"2015-12-07T16:58:10Z","receivedAt":"2015-12-07T16:58:10Z","isPatch":false,"sender":{"key":"charles@hashpling.org","avatar":"https://avatars.githubusercontent.com/u/1668475?v=4"},"body":"On Mon, Dec 07, 2015 at 11:52:28AM -0500, Alex Jones wrote:\n> git branch -a output:\n> \n> ajonespro:Deploy_Script ajones$ git branch -a\n> \n> * DWH_concurrent_api\n>   Email_No_Error_If_No_Old_Version\n>   IT/configs_in_app_support\n>   PHP_Build_Repo\n>   master\n>   remotes/origin/DWH_concurrent_api\n>   remotes/origin/Email_No_Error_If_No_Old_Version\n>   remotes/origin/IT/configs_in_app_support\n>   remotes/origin/PHP_Build_Repo\n>   remotes/origin/master\n> \n> echo $(git branch -a) output:\n> \n> ajonespro:Deploy_Script ajones$ echo $(git branch -a)\n> AppDeploy WebDeploy DWH_concurrent_api\n> Email_No_Error_If_No_Old_Version IT/configs_in_app_support\n> PHP_Build_Repo master remotes/origin/DWH_concurrent_api\n> remotes/origin/Email_No_Error_If_No_Old_Version\n> remotes/origin/IT/configs_in_app_support remotes/origin/PHP_Build_Repo\n> remotes/origin/master\n> \n> While it might be hard to see from that output, The first two\n> \"branches\" in the subshell's output are actually the directories\n> contained within the repo.\n\nLooking at the two outputs, you are seeing the shell's glob expansion of\nthe '*' current branch marker. You probably want to quote the command\nexpansion to prevent this:\n\necho \"$(git branch -a)\"\n"},{"id":"274117","messageId":"20151207170259.GA25915@hashpling.org","threadId":"40955","inReplyTo":"20151207165810.GA25856@hashpling.org","subject":"Re: git branch in Bash subshell \"$(git branch -a)\" including ls output as part of return?","fromName":"Charles Bailey","fromEmail":"charles@hashpling.org","sentAt":"2015-12-07T17:02:59Z","receivedAt":"2015-12-07T17:02:59Z","isPatch":false,"sender":{"key":"charles@hashpling.org","avatar":"https://avatars.githubusercontent.com/u/1668475?v=4"},"body":"On Mon, Dec 07, 2015 at 04:58:10PM +0000, Charles Bailey wrote:\n> \n> Looking at the two outputs, you are seeing the shell's glob expansion of\n> the '*' current branch marker. You probably want to quote the command\n> expansion to prevent this:\n> \n> echo \"$(git branch -a)\"\n\nPressing send has, of course, caused me to think further. You probably\ndon't want to parse the output of a \"porcelain\" command such as \"git\nbranch\" at all, but instead look at using something like \"git\nfor-each-ref\", perhaps with the --format=%(refname) option, grepping out\nmaster and iterating through the rest.\n"},{"id":"274118","messageId":"CAJGk2Wx9cng8+9f0PxX1WFiGPLnoufMKgN8x0HWP-L0Xj8Nr-Q@mail.gmail.com","threadId":"40955","inReplyTo":"20151207170259.GA25915@hashpling.org","subject":"Re: git branch in Bash subshell \"$(git branch -a)\" including ls output as part of return?","fromName":"Alex Jones","fromEmail":"ajones@netsertive.com","sentAt":"2015-12-07T17:57:30Z","receivedAt":"2015-12-07T17:57:30Z","isPatch":false,"sender":{"key":"ajones@netsertive.com","avatar":null},"body":"That did the trick, thanks for the help and the suggestion.\n\nOn Mon, Dec 7, 2015 at 12:02 PM, Charles Bailey <charles@hashpling.org> wrote:\n> On Mon, Dec 07, 2015 at 04:58:10PM +0000, Charles Bailey wrote:\n>>\n>> Looking at the two outputs, you are seeing the shell's glob expansion of\n>> the '*' current branch marker. You probably want to quote the command\n>> expansion to prevent this:\n>>\n>> echo \"$(git branch -a)\"\n>\n> Pressing send has, of course, caused me to think further. You probably\n> don't want to parse the output of a \"porcelain\" command such as \"git\n> branch\" at all, but instead look at using something like \"git\n> for-each-ref\", perhaps with the --format=%(refname) option, grepping out\n> master and iterating through the rest.\n\n\n\n-- \n\nAlex Jones | Software Engineer\n919-238-4404 direct\n336-263-2099 mobile\nnetsertive.com\n"}]}