git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Bug: git branch returns EXIT_SUCCESS when no branches found

From
Junio C Hamano <gitster@pobox.com>
Date
Apr 24, 2019, 10:36 UTC
Message-ID
<xmqqmukf1yh9.fsf@gitster-ct.c.googlers.com>
In-Reply-To
<875zr3iucd.fsf@xkqr.org>
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
Previous: Christoffer StjernlöfNext: Junio C Hamano
Message 2 of 6 in “Bug: git branch returns EXIT_SUCCESS when no branches found”
  1. Christoffer StjernlöfApr 24, 2019
  2. Junio C HamanoApr 24, 2019
  3. Junio C HamanoApr 24, 2019
  4. Christoffer StjernlöfApr 24, 2019
  5. Christoffer StjernlöfApr 24, 2019
  6. SZEDER GáborApr 24, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.