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

Re: command return values

From
Jeff King <peff@peff.net>
Date
Jul 11, 2010, 11:37 UTC
Message-ID
<20100711113733.GA19113@coredump.intra.peff.net>
In-Reply-To
<AANLkTimQcqhD8FClAXef5dGAWRDbAGdVBmIVXxotzKoa@mail.gmail.com>
On Sat, Jul 10, 2010 at 09:11:18PM -0700, Brian Craft wrote:
Show 5 quoted lines
> I'm finding that "git clone" doesn't return useful error codes, e.g.
> trying to clone from a bad repository.  Also, it doesn't abort if you
> try to clone a branch that doesn't exist. The command succeeds,
> leaving you with the wrong result. I haven't found a way to tell when
> the command really succeeds, except for scraping the output.
It should give useful error codes. I see:
  $ git clone parent child; echo $?
  Cloning into child...
  done.
  0
  $ git clone bogus child; echo $?
  Cloning into child...
  fatal: '/home/peff/foo/bogus' does not appear to be a git repository
  fatal: The remote end hung up unexpectedly
  128
  $ find parent -depth | xargs chmod ogu-r
  $ git clone parent child; echo $?
  Cloning into child...
  fatal: failed to open '/home/peff/foo/parent/.git/objects': Permission
  denied
  128

So those all seem reasonable. Is there some other case of a "bad repository" that fails but gets you a zero exit code?

For the case of a non-existent branch, I see:
  $ git clone -b bogus parent child; echo $?
  Cloning into child...
  done.
  warning: Remote branch bogus not found in upstream origin, using HEAD
  instead
  0

So yes, it completes with a warning. I agree that is not ideal, as a script that clones has no idea that it did not actually get the data it was looking for.

I think the rationale for not aborting totally is that we have done significant work (including network traffic) during the clone, and the warning can generally be remedied with "git checkout the-right-branch". We could perhaps keep the repository but signal with a non-zero exit code.

The other option for a script is not to use "-b", which only impacts checkout. Instead, you could do:

  $ git clone -n parent child &&
    cd child &&
    git checkout -b interesting-branch origin/interesting-branch

which is just as efficient, but lets you react differently to failure of each part. You can also break it down further into:

  $ git init &&
    git remote add origin parent &&
    git fetch origin &&
    git branch interesting-branch origin/interesting-branch &&
    git checkout interesting-branch
but I don't think there is much point in doing so.
-Peff
Previous: Sverre RabbelierNext: Brian Craft
Message 3 of 4 in “command return values”
  1. Brian CraftJul 11, 2010
  2. Sverre RabbelierJul 11, 2010
  3. Jeff KingJul 11, 2010
  4. Brian CraftJul 11, 2010

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.