All of lore.kernel.org
 help / color / mirror / Atom feed
* Strange behavior (possible bug) using bash command subsitution with "git branch"
@ 2009-11-08 22:11 Kate Ebneter
  2009-11-08 22:46 ` Sverre Rabbelier
  2009-11-08 22:49 ` Björn Steinbrink
  0 siblings, 2 replies; 6+ messages in thread
From: Kate Ebneter @ 2009-11-08 22:11 UTC (permalink / raw)
  To: git

Hi, folks,

I ran into a weird situation while working on a script, which is best
described with a little snippet from my gitosis-admin repository:

    $ git branch
    * master
    $ branch=$(git branch)
    $ echo $branch
    gitosis.conf keydir master


That is, if you run 'git branch' inside $() or ``, it lists the directories,
too. I can't imagine that this is intended behavior, but perhaps I'm wrong.

I can reproduce this on CentOS, Ubuntu, Solaris, and Mac OS X using git
1.6.2.3, 1.6.3.2, 1.6.3.3, 1.6.4.2, and 1.6.5.2.

(In the end, I figured out a better way to write my script anyway, but this
is just too weird not to ask about.)

Thanks,
Kate Ebneter
Somewhat befuddled build engineer

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Strange behavior (possible bug) using bash command subsitution  with "git branch"
  2009-11-08 22:11 Strange behavior (possible bug) using bash command subsitution with "git branch" Kate Ebneter
@ 2009-11-08 22:46 ` Sverre Rabbelier
  2009-11-08 22:50   ` Kate Ebneter
  2009-11-08 22:49 ` Björn Steinbrink
  1 sibling, 1 reply; 6+ messages in thread
From: Sverre Rabbelier @ 2009-11-08 22:46 UTC (permalink / raw)
  To: Kate Ebneter; +Cc: git

Heya

On Sun, Nov 8, 2009 at 23:11, Kate Ebneter <kate@ning.com> wrote:
> That is, if you run 'git branch' inside $() or ``, it lists the directories,
> too. I can't imagine that this is intended behavior, but perhaps I'm wrong.

Try enclosing it in quotes ;).

That is, compare:
$ echo "`git branch`"
with
$ echo `git branch`

The kicker is that there's a * in the output of git branch, which your
shell helpfully substitutes with the contents of your current
directory :).

-- 
Cheers,

Sverre Rabbelier

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Strange behavior (possible bug) using bash command subsitution with "git branch"
  2009-11-08 22:11 Strange behavior (possible bug) using bash command subsitution with "git branch" Kate Ebneter
  2009-11-08 22:46 ` Sverre Rabbelier
@ 2009-11-08 22:49 ` Björn Steinbrink
  2009-11-08 23:03   ` Jeff King
  1 sibling, 1 reply; 6+ messages in thread
From: Björn Steinbrink @ 2009-11-08 22:49 UTC (permalink / raw)
  To: Kate Ebneter; +Cc: git

On 2009.11.08 14:11:56 -0800, Kate Ebneter wrote:
> Hi, folks,
> 
> I ran into a weird situation while working on a script, which is best
> described with a little snippet from my gitosis-admin repository:
> 
>     $ git branch
>     * master
     ^^^

>     $ branch=$(git branch)
>     $ echo $branch
>     gitosis.conf keydir master

Your shell expands the *, thus echo sees all the things in the current
directory. Use 'echo "$branch"' and you'll see what you expected.

Björn

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Strange behavior (possible bug) using bash command subsitution  with "git branch"
  2009-11-08 22:46 ` Sverre Rabbelier
@ 2009-11-08 22:50   ` Kate Ebneter
  0 siblings, 0 replies; 6+ messages in thread
From: Kate Ebneter @ 2009-11-08 22:50 UTC (permalink / raw)
  To: Sverre Rabbelier; +Cc: git

On 11/8/09 2:46 PM, "Sverre Rabbelier" <srabbelier@gmail.com> wrote:

> Heya
> 
> On Sun, Nov 8, 2009 at 23:11, Kate Ebneter <kate@ning.com> wrote:
>> That is, if you run 'git branch' inside $() or ``, it lists the directories,
>> too. I can't imagine that this is intended behavior, but perhaps I'm wrong.
> 
> Try enclosing it in quotes ;).
> 
> That is, compare:
> $ echo "`git branch`"
> with
> $ echo `git branch`
> 
> The kicker is that there's a * in the output of git branch, which your
> shell helpfully substitutes with the contents of your current
> directory :).

*head desk*

You know, I spent hours last night trying to figure that out. So simple.
Duh.

Thanks for pointing that out -- I knew it had to be something simple, but
sometimes you just can't see the forest for the trees! :-)

Thanks again,
Kate Ebneter

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Strange behavior (possible bug) using bash command subsitution with "git branch"
  2009-11-08 22:49 ` Björn Steinbrink
@ 2009-11-08 23:03   ` Jeff King
  2009-11-08 23:10     ` Kate Ebneter
  0 siblings, 1 reply; 6+ messages in thread
From: Jeff King @ 2009-11-08 23:03 UTC (permalink / raw)
  To: Björn Steinbrink; +Cc: Kate Ebneter, git

On Sun, Nov 08, 2009 at 11:49:55PM +0100, Björn Steinbrink wrote:

> > I ran into a weird situation while working on a script, which is best
> > described with a little snippet from my gitosis-admin repository:
> > 
> >     $ git branch
> >     * master
>      ^^^
> 
> >     $ branch=$(git branch)
> >     $ echo $branch
> >     gitosis.conf keydir master
> 
> Your shell expands the *, thus echo sees all the things in the current
> directory. Use 'echo "$branch"' and you'll see what you expected.

And more to the point, this is just one reason why one should use
for-each-ref when scripting (the other is that git-branch's output is
considered porcelain, and is not guaranteed to remain stable). E.g.:

  git for-each-ref --format='%(refname:short)' refs/heads/

would produce the output the original poster wanted.

-Peff

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Strange behavior (possible bug) using bash command subsitution with "git branch"
  2009-11-08 23:03   ` Jeff King
@ 2009-11-08 23:10     ` Kate Ebneter
  0 siblings, 0 replies; 6+ messages in thread
From: Kate Ebneter @ 2009-11-08 23:10 UTC (permalink / raw)
  To: Jeff King, Björn Steinbrink; +Cc: git

On 11/8/09 3:03 PM, "Jeff King" <peff@peff.net> wrote:

> On Sun, Nov 08, 2009 at 11:49:55PM +0100, Björn Steinbrink wrote:
> 
>>> I ran into a weird situation while working on a script, which is best
>>> described with a little snippet from my gitosis-admin repository:
>>> 
>>>     $ git branch
>>>     * master
>>      ^^^
>> 
>>>     $ branch=$(git branch)
>>>     $ echo $branch
>>>     gitosis.conf keydir master
>> 
>> Your shell expands the *, thus echo sees all the things in the current
>> directory. Use 'echo "$branch"' and you'll see what you expected.
> 
> And more to the point, this is just one reason why one should use
> for-each-ref when scripting (the other is that git-branch's output is
> considered porcelain, and is not guaranteed to remain stable). E.g.:
> 
>   git for-each-ref --format='%(refname:short)' refs/heads/
> 
> would produce the output the original poster wanted.

Ah, sweet, I did not know about that. I ended up doing something entirely
different (short form: my original idea about what I wanted to do was wrong)
but that's very helpful for the future.

Thanks,
Kate Ebneter (who is gradually learning more and more about git)

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2009-11-08 23:20 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-11-08 22:11 Strange behavior (possible bug) using bash command subsitution with "git branch" Kate Ebneter
2009-11-08 22:46 ` Sverre Rabbelier
2009-11-08 22:50   ` Kate Ebneter
2009-11-08 22:49 ` Björn Steinbrink
2009-11-08 23:03   ` Jeff King
2009-11-08 23:10     ` Kate Ebneter

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.