Re: Bug: git branch returns EXIT_SUCCESS when no branches found
- Date: Wed, 24 Apr 2019 19:36:34 +0900
- From: Junio C Hamano <gitster@xxxxxxxxx>
- Subject: Re: Bug: git branch returns EXIT_SUCCESS when no branches found
a@xxxxxxxx (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".
$ 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"
echo "master is an ancestor of $DEV_BRANCH"
echo "master has commits not in $DEV_BRANCH"
git --no-pager log "master..$DEV_BRANCH" --