# git branch in Bash subshell "$(git branch -a)" including ls output as part of return?

4 messages from 2015-12-07 to 2015-12-07. Participants: Alex Jones, Charles Bailey.
Thread: https://gitlist.dev/t/40955

## Alex Jones, 2015-12-07 16:52

Subject: git branch in Bash subshell "$(git branch -a)" including ls output as part of return?
Message-ID: <CAJGk2Wx4B=AHFxkJep=yYVKU9UyJieazoNaC44W5yBXoMi+gdg@mail.gmail.com>
URL: https://gitlist.dev/e/CAJGk2Wx4B%3DAHFxkJep%3DyYVKU9UyJieazoNaC44W5yBXoMi%2Bgdg%40mail.gmail.com

```
Hello Folks,

I am Running OSX 10.10.5 Yosemite along with git 2.6.3 installed via
homebrew package manager.

I recently stumbled across the following bug in some scripting I was
doing. "git branch -a --list" and "git branch -a" seem to include the
output of an "ls" command if executed as part of a subshell in a bash
script. I can't speculate to the reason.

Script goal: Delete all branches aside from master in the repository
Example outputs from several commands  repo:

ls output:

ajonespro:Deploy_Script ajones$ ls -l
total 0
drwxr-xr-x  11 ajones  wheel  374 Dec  7 10:50 AppDeploy
drwxr-xr-x  11 ajones  wheel  374 Dec  7 11:15 WebDeploy


git branch -a output:

ajonespro:Deploy_Script ajones$ git branch -a

* DWH_concurrent_api
  Email_No_Error_If_No_Old_Version
  IT/configs_in_app_support
  PHP_Build_Repo
  master
  remotes/origin/DWH_concurrent_api
  remotes/origin/Email_No_Error_If_No_Old_Version
  remotes/origin/IT/configs_in_app_support
  remotes/origin/PHP_Build_Repo
  remotes/origin/master

echo $(git branch -a) output:

ajonespro:Deploy_Script ajones$ echo $(git branch -a)
AppDeploy WebDeploy DWH_concurrent_api
Email_No_Error_If_No_Old_Version IT/configs_in_app_support
PHP_Build_Repo master remotes/origin/DWH_concurrent_api
remotes/origin/Email_No_Error_If_No_Old_Version
remotes/origin/IT/configs_in_app_support remotes/origin/PHP_Build_Repo
remotes/origin/master

While it might be hard to see from that output, The first two
"branches" in the subshell's output are actually the directories
contained within the repo. If I place a file at the root it includes
that in the branch list as well. Since my test file "test.txt" (not
shown in example) and "WebDeploy" are sorted before all the branches,
I suspect some output buffer is being accidentally being written to.

Has any experienced this before, and can anyone reproduce it on a
different configuration? I wouldn't be surprised if it was some weird
bug with Apple's included terminal, but since I've only observed it
with git branch, I thought I'd try contact git maintainers first.

```

## Charles Bailey, 2015-12-07 16:58

Subject: Re: git branch in Bash subshell "$(git branch -a)" including ls output as part of return?
Message-ID: <20151207165810.GA25856@hashpling.org>
URL: https://gitlist.dev/e/20151207165810.GA25856%40hashpling.org
In-Reply-To: <CAJGk2Wx4B=AHFxkJep=yYVKU9UyJieazoNaC44W5yBXoMi+gdg@mail.gmail.com>

```
On Mon, Dec 07, 2015 at 11:52:28AM -0500, Alex Jones wrote:
> git branch -a output:
> 
> ajonespro:Deploy_Script ajones$ git branch -a
> 
> * DWH_concurrent_api
>   Email_No_Error_If_No_Old_Version
>   IT/configs_in_app_support
>   PHP_Build_Repo
>   master
>   remotes/origin/DWH_concurrent_api
>   remotes/origin/Email_No_Error_If_No_Old_Version
>   remotes/origin/IT/configs_in_app_support
>   remotes/origin/PHP_Build_Repo
>   remotes/origin/master
> 
> echo $(git branch -a) output:
> 
> ajonespro:Deploy_Script ajones$ echo $(git branch -a)
> AppDeploy WebDeploy DWH_concurrent_api
> Email_No_Error_If_No_Old_Version IT/configs_in_app_support
> PHP_Build_Repo master remotes/origin/DWH_concurrent_api
> remotes/origin/Email_No_Error_If_No_Old_Version
> remotes/origin/IT/configs_in_app_support remotes/origin/PHP_Build_Repo
> remotes/origin/master
> 
> While it might be hard to see from that output, The first two
> "branches" in the subshell's output are actually the directories
> contained within the repo.

Looking at the two outputs, you are seeing the shell's glob expansion of
the '*' current branch marker. You probably want to quote the command
expansion to prevent this:

echo "$(git branch -a)"

```

## Charles Bailey, 2015-12-07 17:02

Subject: Re: git branch in Bash subshell "$(git branch -a)" including ls output as part of return?
Message-ID: <20151207170259.GA25915@hashpling.org>
URL: https://gitlist.dev/e/20151207170259.GA25915%40hashpling.org
In-Reply-To: <20151207165810.GA25856@hashpling.org>

```
On Mon, Dec 07, 2015 at 04:58:10PM +0000, Charles Bailey wrote:
> 
> Looking at the two outputs, you are seeing the shell's glob expansion of
> the '*' current branch marker. You probably want to quote the command
> expansion to prevent this:
> 
> echo "$(git branch -a)"

Pressing send has, of course, caused me to think further. You probably
don't want to parse the output of a "porcelain" command such as "git
branch" at all, but instead look at using something like "git
for-each-ref", perhaps with the --format=%(refname) option, grepping out
master and iterating through the rest.

```

## Alex Jones, 2015-12-07 17:57

Subject: Re: git branch in Bash subshell "$(git branch -a)" including ls output as part of return?
Message-ID: <CAJGk2Wx9cng8+9f0PxX1WFiGPLnoufMKgN8x0HWP-L0Xj8Nr-Q@mail.gmail.com>
URL: https://gitlist.dev/e/CAJGk2Wx9cng8%2B9f0PxX1WFiGPLnoufMKgN8x0HWP-L0Xj8Nr-Q%40mail.gmail.com
In-Reply-To: <20151207170259.GA25915@hashpling.org>

```
That did the trick, thanks for the help and the suggestion.

On Mon, Dec 7, 2015 at 12:02 PM, Charles Bailey <charles@hashpling.org> wrote:
> On Mon, Dec 07, 2015 at 04:58:10PM +0000, Charles Bailey wrote:
>>
>> Looking at the two outputs, you are seeing the shell's glob expansion of
>> the '*' current branch marker. You probably want to quote the command
>> expansion to prevent this:
>>
>> echo "$(git branch -a)"
>
> Pressing send has, of course, caused me to think further. You probably
> don't want to parse the output of a "porcelain" command such as "git
> branch" at all, but instead look at using something like "git
> for-each-ref", perhaps with the --format=%(refname) option, grepping out
> master and iterating through the rest.



-- 

Alex Jones | Software Engineer
919-238-4404 direct
336-263-2099 mobile
netsertive.com

```
