Skip to content

Commit ce11360

Browse files
peffgitster
authored andcommitted
log: diagnose empty HEAD more clearly
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f86f31a commit ce11360

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

revision.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2170,6 +2170,21 @@ static int handle_revision_pseudo_opt(const char *submodule,
21702170
return 1;
21712171
}
21722172

2173+
static void NORETURN diagnose_missing_default(const char *def)
2174+
{
2175+
unsigned char sha1[20];
2176+
int flags;
2177+
const char *refname;
2178+
2179+
refname = resolve_ref_unsafe(def, 0, sha1, &flags);
2180+
if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2181+
die(_("your current branch appears to be broken"));
2182+
2183+
skip_prefix(refname, "refs/heads/", &refname);
2184+
die(_("your current branch '%s' does not have any commits yet"),
2185+
refname);
2186+
}
2187+
21732188
/*
21742189
* Parse revision information, filling in the "rev_info" structure,
21752190
* 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
22992314
struct object *object;
23002315
struct object_context oc;
23012316
if (get_sha1_with_context(revs->def, 0, sha1, &oc))
2302-
die("bad default revision '%s'", revs->def);
2317+
diagnose_missing_default(revs->def);
23032318
object = get_reference(revs, revs->def, sha1, 0);
23042319
add_pending_object_with_mode(revs, object, revs->def, oc.mode);
23052320
}

t/t4202-log.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,4 +871,18 @@ test_expect_success 'log --graph --no-walk is forbidden' '
871871
test_must_fail git log --graph --no-walk
872872
'
873873

874+
test_expect_success 'log diagnoses bogus HEAD' '
875+
git init empty &&
876+
test_must_fail git -C empty log 2>stderr &&
877+
test_i18ngrep does.not.have.any.commits stderr &&
878+
echo 1234abcd >empty/.git/refs/heads/master &&
879+
test_must_fail git -C empty log 2>stderr &&
880+
test_i18ngrep broken stderr &&
881+
echo "ref: refs/heads/invalid.lock" >empty/.git/HEAD &&
882+
test_must_fail git -C empty log 2>stderr &&
883+
test_i18ngrep broken stderr &&
884+
test_must_fail git -C empty log --default totally-bogus 2>stderr &&
885+
test_i18ngrep broken stderr
886+
'
887+
874888
test_done

0 commit comments

Comments
 (0)