All the mail mirrored from lore.kernel.org
 help / color / mirror / Atom feed
* Troubles redirecting git output
@ 2021-07-09 11:59 Baptiste Chocot
  2021-07-09 13:40 ` Marc Branchaud
  0 siblings, 1 reply; 3+ messages in thread
From: Baptiste Chocot @ 2021-07-09 11:59 UTC (permalink / raw
  To: git

Hello guys, hope you're doing great.

I'm reaching out today because I cannot find a fix for my issue. I'm 
using git 2.25.1 from the debian repo. I'm having difficulties to 
redirect git stderr, here is an example:

---
$ git init
Initialized empty lol/.git/
$ git branch >&2 2>/dev/null
fatal: your current branch 'master' does not have any commits yet
---

Is this an expected behavior? I find it weird not to be able to silent 
git. Are you aware of a workaround? I've been browsing a bit but did not 
find an answer yet.

Thanks for your time!

Baptiste


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

* Re: Troubles redirecting git output
  2021-07-09 11:59 Troubles redirecting git output Baptiste Chocot
@ 2021-07-09 13:40 ` Marc Branchaud
  2021-07-09 19:20   ` Baptiste Chocot
  0 siblings, 1 reply; 3+ messages in thread
From: Marc Branchaud @ 2021-07-09 13:40 UTC (permalink / raw
  To: Baptiste Chocot, git

On 2021-07-09 7:59 a.m., Baptiste Chocot wrote:
> Hello guys, hope you're doing great.
> 
> I'm reaching out today because I cannot find a fix for my issue. I'm 
> using git 2.25.1 from the debian repo. I'm having difficulties to 
> redirect git stderr, here is an example:
> 
> ---
> $ git init
> Initialized empty lol/.git/
> $ git branch >&2 2>/dev/null

Try swapping the two redirections:
	git branch 2>/dev/null >&2

I believe that standard shell behaviour is to apply a redirection 
immediately when it appears while parsing the command.  I believe that 
although the man pages say "redirect" internally the code just 
*replaces* the old file descriptor, e.g. basically something equivalent to
	stdout = 2

So when you say

	git branch >&2 2>/dev/null

The shell sees

">&2" -- The shell redirects stdout to file descriptor 2.

"2>/dev/null" -- The shell opens /dev/null (as descriptor 3) and 
*replaces* descriptor 2 with the new descriptor.  But at this point, 
stdout is still being sent to the original descriptor 2.

When I redirect, I'm in the habit of setting stdout first, then set 
stderr to stdout. The numerical ordering helps me avoid this mistake:
	git branch >/dev/null 2>&1

		M.


> fatal: your current branch 'master' does not have any commits yet
> ---
> 
> Is this an expected behavior? I find it weird not to be able to silent 
> git. Are you aware of a workaround? I've been browsing a bit but did not 
> find an answer yet.
> 
> Thanks for your time!
> 
> Baptiste
> 

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

* Re: Troubles redirecting git output
  2021-07-09 13:40 ` Marc Branchaud
@ 2021-07-09 19:20   ` Baptiste Chocot
  0 siblings, 0 replies; 3+ messages in thread
From: Baptiste Chocot @ 2021-07-09 19:20 UTC (permalink / raw
  To: Marc Branchaud, git

Marc,

Thanks for your help... :facepalm: I look like a noob but at least I've 
learnt something useful.

Have a good day!

On 7/9/21 9:40 AM, Marc Branchaud wrote:
> On 2021-07-09 7:59 a.m., Baptiste Chocot wrote:
>> Hello guys, hope you're doing great.
>>
>> I'm reaching out today because I cannot find a fix for my issue. I'm 
>> using git 2.25.1 from the debian repo. I'm having difficulties to 
>> redirect git stderr, here is an example:
>>
>> ---
>> $ git init
>> Initialized empty lol/.git/
>> $ git branch >&2 2>/dev/null
>
> Try swapping the two redirections:
>     git branch 2>/dev/null >&2
>
> I believe that standard shell behaviour is to apply a redirection 
> immediately when it appears while parsing the command.  I believe that 
> although the man pages say "redirect" internally the code just 
> *replaces* the old file descriptor, e.g. basically something 
> equivalent to
>     stdout = 2
>
> So when you say
>
>     git branch >&2 2>/dev/null
>
> The shell sees
>
> ">&2" -- The shell redirects stdout to file descriptor 2.
>
> "2>/dev/null" -- The shell opens /dev/null (as descriptor 3) and 
> *replaces* descriptor 2 with the new descriptor.  But at this point, 
> stdout is still being sent to the original descriptor 2.
>
> When I redirect, I'm in the habit of setting stdout first, then set 
> stderr to stdout. The numerical ordering helps me avoid this mistake:
>     git branch >/dev/null 2>&1
>
>         M.
>
>
>> fatal: your current branch 'master' does not have any commits yet
>> ---
>>
>> Is this an expected behavior? I find it weird not to be able to 
>> silent git. Are you aware of a workaround? I've been browsing a bit 
>> but did not find an answer yet.
>>
>> Thanks for your time!
>>
>> Baptiste
>>

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

end of thread, other threads:[~2021-07-09 19:20 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-07-09 11:59 Troubles redirecting git output Baptiste Chocot
2021-07-09 13:40 ` Marc Branchaud
2021-07-09 19:20   ` Baptiste Chocot

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.