git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* Bug: git branch returns EXIT_SUCCESS when no branches found
@ 2019-04-24 10:13 Christoffer Stjernlöf
  2019-04-24 10:36 ` Junio C Hamano
  0 siblings, 1 reply; 6+ messages in thread
From: Christoffer Stjernlöf @ 2019-04-24 10:13 UTC (permalink / raw)
  To: git; +Cc: a

[-- Attachment #1: Type: text/plain, Size: 983 bytes --]

Dear Sirs/Madams,

A common use case of git branch – for me – is to use it to test whether
a particular branch satisfies some conditions. A recent example is this:

    if ! git branch "$DEV_BRANCH" --contains master; then
        echo "The development branch ($DEV_BRANCH) is not up to date"
        echo "with the latest master."
        exit 1
    fi

Except this doesn't work, because git branch returns success even though
it failed to list any branches satisfying the condition. My workaround
has been to rewrite the condition as

    git branch "$DEV_BRANCH" --contains master \
        | grep -xE '\* *'"$DEV_BRANCH"

but this is obviously not desirable in comparison.

The syntax of e.g. --contains makes it seem almost like this use case of
mine was intended, but the exit code makes me doubt it. Is there any
consideration I'm missing that's a reason for not returning a failure
code when not matching any branches?

Yours faithfully,
Chris

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 227 bytes --]

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

* Re: Bug: git branch returns EXIT_SUCCESS when no branches found
  2019-04-24 10:13 Bug: git branch returns EXIT_SUCCESS when no branches found Christoffer Stjernlöf
@ 2019-04-24 10:36 ` Junio C Hamano
  2019-04-24 10:44   ` Junio C Hamano
                     ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Junio C Hamano @ 2019-04-24 10:36 UTC (permalink / raw)
  To: Christoffer Stjernlöf; +Cc: git

a@xkqr.org (Christoffer Stjernlöf) writes:

> A common use case of git branch – for me – is to use it to test whether
> a particular branch satisfies some conditions. A recent example is this:
>
>     if ! git branch "$DEV_BRANCH" --contains master; then

This invocation is not in line with how "git branch" subcommand is
designed to work.

The subcommand operates in various modes (like creating a new one,
renaming an existing one, deleting existing one(s)), among which
there is a mode to "list existing ones".  Without an explicit option
on the command line to tell what mode the user wants (e.g. "-d" for
deleting), it defaults to the listing mode, "git branch --list".

The list mode can limit what is listed via different criteria, one
of which is with a pattern that the shown branches must match, e.g.

	$ git branch --list "cs/*"

which shows only the branches whose names begin with "cs/".  It can
also limit the branches whose tip commits can reach a named commit
with the "--contains".

	$ DEV_BRANCH=cs/topic
	$ git branch --contains master "$DEV_BRANCH"

asks the subcommand to show only the branches that can reach the
commit at the tip of 'master', *AND* whose name match cs/topic.  So
it may show the cs/topic branch (and nothing else, even there are
cs/topic1 or cs/topic/2 branches) if and only if that branch
can reach the tip of 'master'.

If you want to ask "does the tip of $DEV_BRANCH reach commit
'master'?", the right way would probably be to ask

	if git merge-base --is-ancestor master "$DEV_BRANCH"
	then
		echo "master is an ancestor of $DEV_BRANCH"
	else
		echo "master has commits not in $DEV_BRANCH"
		git --no-pager log "master..$DEV_BRANCH" --
	fi



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

* Re: Bug: git branch returns EXIT_SUCCESS when no branches found
  2019-04-24 10:36 ` Junio C Hamano
@ 2019-04-24 10:44   ` Junio C Hamano
  2019-04-24 10:53     ` Christoffer Stjernlöf
  2019-04-24 10:47   ` Christoffer Stjernlöf
  2019-04-24 11:11   ` SZEDER Gábor
  2 siblings, 1 reply; 6+ messages in thread
From: Junio C Hamano @ 2019-04-24 10:44 UTC (permalink / raw)
  To: Christoffer Stjernlöf; +Cc: git

Junio C Hamano <gitster@pobox.com> writes:

> The list mode ...
> ... can
> also limit the branches whose tip commits can reach a named commit
> with the "--contains".
>
> 	$ DEV_BRANCH=cs/topic
> 	$ git branch --contains master "$DEV_BRANCH"
>
> asks the subcommand to show only the branches that can reach the
> commit at the tip of 'master', *AND* whose name match cs/topic.  So
> it may show the cs/topic branch (and nothing else, even there are
> cs/topic1 or cs/topic/2 branches) if and only if that branch
> can reach the tip of 'master'.

One crucial bit is missing from my response.  In general, a git
subcommand that shows zero or more things in response to a request
to list things that match criteria does *NOT* consider it an error
to make a request that happens to produce a zero result.  The above
command will exit with non-zero status if 'master' does not name a
commit.  But if cs/topic branch does not exist, of if the branch
does not contain 'master', the command will show "here are the
branches that match the criteria you gave me" by giving no lines in
its standard output, and exits with 0 to signal that what you see on
the screen indeed was computed correctly (as opposed to the command
internally crashing and failing to produce a result, in which case
you would want to be told with non-zero exit status).



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

* Re: Bug: git branch returns EXIT_SUCCESS when no branches found
  2019-04-24 10:36 ` Junio C Hamano
  2019-04-24 10:44   ` Junio C Hamano
@ 2019-04-24 10:47   ` Christoffer Stjernlöf
  2019-04-24 11:11   ` SZEDER Gábor
  2 siblings, 0 replies; 6+ messages in thread
From: Christoffer Stjernlöf @ 2019-04-24 10:47 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

[-- Attachment #1: Type: text/plain, Size: 922 bytes --]

Dear Junio,

Junio C Hamano (gitster@pobox.com) 2019-04-24:
> This invocation is not in line with how "git branch" subcommand is
> designed to work.

This is fair. I completely accept a "you're wielding it wrong" answer!
However, your explanation sounds to me like that invocation is exactly
in line with how git branch is designed to work! What might I be
missing?


Junio C Hamano (gitster@pobox.com) 2019-04-24:
> If you want to ask "does the tip of $DEV_BRANCH reach commit
> 'master'?", the right way would probably be to ask
>
> 	if git merge-base --is-ancestor master "$DEV_BRANCH"
> 	then
> 		echo "master is an ancestor of $DEV_BRANCH"
> 	else
> 		echo "master has commits not in $DEV_BRANCH"B
> 		git --no-pager log "master..$DEV_BRANCH" --
> 	fi

This seems like a more convenient command for my current purpose,
although it's not a complete replacement for git branch. Thank you!

Sincerely yours,
Christoffer

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 227 bytes --]

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

* Re: Bug: git branch returns EXIT_SUCCESS when no branches found
  2019-04-24 10:44   ` Junio C Hamano
@ 2019-04-24 10:53     ` Christoffer Stjernlöf
  0 siblings, 0 replies; 6+ messages in thread
From: Christoffer Stjernlöf @ 2019-04-24 10:53 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

[-- Attachment #1: Type: text/plain, Size: 862 bytes --]

Dear Junio,

Junio C Hamano (gitster@pobox.com) 2019-04-24:
> if cs/topic branch does not exist, of if the branch
> does not contain 'master', the command will show "here are the
> branches that match the criteria you gave me" by giving no lines in
> its standard output, and exits with 0 to signal that what you see on
> the screen indeed was computed correctly (as opposed to the command
> internally crashing and failing to produce a result, in which case
> you would want to be told with non-zero exit status).

Thank you for taking the time to elaborate on your already detailed
answer. I can see the concern about separating internal command errors
from correctly computed results, even if those results seem to indicate
something being amiss. Is this not a place where different error codes
can be useful?

Sincerely yours,
Christoffer

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 227 bytes --]

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

* Re: Bug: git branch returns EXIT_SUCCESS when no branches found
  2019-04-24 10:36 ` Junio C Hamano
  2019-04-24 10:44   ` Junio C Hamano
  2019-04-24 10:47   ` Christoffer Stjernlöf
@ 2019-04-24 11:11   ` SZEDER Gábor
  2 siblings, 0 replies; 6+ messages in thread
From: SZEDER Gábor @ 2019-04-24 11:11 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Christoffer Stjernlöf, git

On Wed, Apr 24, 2019 at 07:36:34PM +0900, Junio C Hamano wrote:
> 	if git merge-base --is-ancestor master "$DEV_BRANCH"
> 	then
> 		echo "master is an ancestor of $DEV_BRANCH"
> 	else
> 		echo "master has commits not in $DEV_BRANCH"
> 		git --no-pager log "master..$DEV_BRANCH" --
> 	fi

A non-zero exit code might indicate an error in 'git merge-base'
itself; I had basically the same if-else condition in a script of my
own that had to check the same thing...  until once I mistyped the
branchname, and, well, undesired behavior ensued.  After that I
added one more branch to the condition like this:

  git merge-base --is-ancestor master "$DEV_BRANCH"
  ret=$?
  if test $ret -gt 1
  then
        die "uh-oh, git merge-base errored out"
  elif test $ret -eq 0
  then
        echo "master is an ancestor of $DEV_BRANCH"
  else
        echo "master has commits not in $DEV_BRANCH"
  fi


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

end of thread, other threads:[~2019-04-24 11:11 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-04-24 10:13 Bug: git branch returns EXIT_SUCCESS when no branches found Christoffer Stjernlöf
2019-04-24 10:36 ` Junio C Hamano
2019-04-24 10:44   ` Junio C Hamano
2019-04-24 10:53     ` Christoffer Stjernlöf
2019-04-24 10:47   ` Christoffer Stjernlöf
2019-04-24 11:11   ` SZEDER Gábor

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).