diff options
author | Jeff King <peff@peff.net> | 2015-08-29 01:04:18 -0400 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2015-08-31 09:34:20 -0700 |
commit | ce113604672fed9b429b1c162b1005794fff6a17 (patch) | |
tree | 21c002a890487c36226e5723666a2c5b363333ca /revision.c | |
parent | f86f31ab33c3406adebbb9f9f61be550dcc5a472 (diff) | |
download | git-ce113604672fed9b429b1c162b1005794fff6a17.tar.gz |
log: diagnose empty HEAD more clearlyjk/log-missing-default-HEAD
If you init or clone an empty repository, the initial
message from running "git log" is not very friendly:
$ git init
Initialized empty Git repository in /home/peff/foo/.git/
$ git log
fatal: bad default revision 'HEAD'
Let's detect this situation and write a more friendly
message:
$ git log
fatal: your current branch 'master' does not have any commits yet
We also detect the case that 'HEAD' points to a broken ref;
this should be even less common, but is easy to see. Note
that we do not diagnose all possible cases. We rely on
resolve_ref, which means we do not get information about
complex cases. E.g., "--default master" would use dwim_ref
to find "refs/heads/master", but we notice only that
"master" does not exist. Similarly, a complex sha1
expression like "--default HEAD^2" will not resolve as a
ref.
But that's OK. We fall back to a generic error message in
those cases, and they are unlikely to be used anyway.
Catching an empty or broken "HEAD" improves the common case,
and the other cases are not regressed.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'revision.c')
-rw-r--r-- | revision.c | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/revision.c b/revision.c index 7ddbaa083e..734494abe6 100644 --- a/revision.c +++ b/revision.c @@ -2170,6 +2170,21 @@ static int handle_revision_pseudo_opt(const char *submodule, return 1; } +static void NORETURN diagnose_missing_default(const char *def) +{ + unsigned char sha1[20]; + int flags; + const char *refname; + + refname = resolve_ref_unsafe(def, 0, sha1, &flags); + if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN)) + die(_("your current branch appears to be broken")); + + skip_prefix(refname, "refs/heads/", &refname); + die(_("your current branch '%s' does not have any commits yet"), + refname); +} + /* * Parse revision information, filling in the "rev_info" structure, * and removing the used arguments from the argument list. @@ -2299,7 +2314,7 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s struct object *object; struct object_context oc; if (get_sha1_with_context(revs->def, 0, sha1, &oc)) - die("bad default revision '%s'", revs->def); + diagnose_missing_default(revs->def); object = get_reference(revs, revs->def, sha1, 0); add_pending_object_with_mode(revs, object, revs->def, oc.mode); } |