git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: "Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
Cc: taoqy@ls-a.me, Git Mailing List <git@vger.kernel.org>
Subject: Re: [Question] builtin/branch.c
Date: Mon, 15 Oct 2018 13:14:17 -0400	[thread overview]
Message-ID: <20181015171417.GA1301@sigill.intra.peff.net> (raw)
In-Reply-To: <CACBZZX7i-Uob9EJ8GeDKYVTyKtdRiy=qPbxSOe=FGh2cbHG9Zw@mail.gmail.com>

On Sun, Oct 14, 2018 at 12:19:35PM +0200, Ævar Arnfjörð Bjarmason wrote:

> On Sat, Oct 13, 2018 at 10:12 AM Tao Qingyun <taoqy@ls-a.me> wrote:
> > Hi, I am learning `builtin/branch.c`. I find that it will call `branch_get`
> > before create and [un]set upstream, and die with "no such branch" if failed.
> > but `branch_get` seems never fail, it is a get_or_create. Also, it was
> > confused that getting a branch before it has created.
> >
> > builtin/branch.c #811
> >
> >     } else if (argc > 0 && argc <= 2) {
> >         struct branch *branch = branch_get(argv[0]);
> >
> >         if (!branch)
> >             die(_("no such branch '%s'"), argv[0]);
> 
> From my reading of the source you're correct. That !branch case is
> pointless. The only way that function can fail is in the x*() family
> of functions, which'll make the function die instead of returning
> NULL.

It sometimes returns current_branch, which can be NULL (e.g., if you're
on a detached HEAD). Try:

  $ git branch HEAD
  fatal: no such branch 'HEAD'

  $ git branch ''
  fatal: no such branch ''

However, it seems weird that we'd check those cases here (and provide
such lousy messages). And indeed, dropping that and letting us
eventually hit create_branch() gives a much better message:

  $ git branch HEAD
  fatal: 'HEAD' is not a valid branch name.

  $ git branch ''
  fatal: '' is not a valid branch name.

I think we'd want to see that reasoning in the commit message.

-Peff

      parent reply	other threads:[~2018-10-15 17:14 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-13  8:11 [Question] builtin/branch.c Tao Qingyun
2018-10-14 10:19 ` Ævar Arnfjörð Bjarmason
2018-10-15 14:08   ` [PATCH] builtin/branch.c: remove useless branch_get Tao Qingyun
2018-10-15 17:17     ` Jeff King
2018-10-16 14:54       ` [PATCH v2] " Tao Qingyun
2018-10-17  8:39         ` Jeff King
2018-10-17 12:58           ` Tao Qingyun
2018-10-15 17:14   ` Jeff King [this message]

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

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

  git send-email \
    --in-reply-to=20181015171417.GA1301@sigill.intra.peff.net \
    --to=peff@peff.net \
    --cc=avarab@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=taoqy@ls-a.me \
    /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.
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 NNTP newsgroup(s).