Skip to contentSkip to conversation

Bug: git branch returns EXIT_SUCCESS when no branches found

DISCUSSION Christoffer Stjernlöf 3 participants 6 messages Apr 24, 2019

lore
Share & tools
lore
#0 lore raw

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

#1 lore raw
Quoted 4 lines · [email protected] (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
#2 lore raw
Quoted 5 lines · Junio C Hamano

The list mode ...
... can
also limit the branches whose tip commits can reach a named commit with the "--contains".

Quoted 8 lines
	$ 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).

#3 lore raw

Dear Junio,

Junio C Hamano ([email protected]) 2019-04-24:

Quoted 7 lines

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

#4 lore raw
In reply to Junio C Hamano

Dear Junio,

Junio C Hamano ([email protected]) 2019-04-24:

Quoted 2 lines

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 ([email protected]) 2019-04-24:

Quoted 10 lines

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

#5 lore raw
In reply to Junio C Hamano
Quoted 7 lines · Junio C Hamano
	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