git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: a@xkqr.org (Christoffer Stjernlöf)
Cc: git@vger.kernel.org
Subject: Re: Bug: git branch returns EXIT_SUCCESS when no branches found
Date: Wed, 24 Apr 2019 19:36:34 +0900	[thread overview]
Message-ID: <xmqqmukf1yh9.fsf@gitster-ct.c.googlers.com> (raw)
In-Reply-To: <875zr3iucd.fsf@xkqr.org> ("Christoffer Stjernlöf"'s message of "Wed, 24 Apr 2019 10:13:54 +0000")

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



  reply	other threads:[~2019-04-24 10:36 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqqmukf1yh9.fsf@gitster-ct.c.googlers.com \
    --to=gitster@pobox.com \
    --cc=a@xkqr.org \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).