Skip to content

Commit 5af77d1

Browse files
committed
Merge branch 'jk/log-missing-default-HEAD' into maint
"git init empty && git -C empty log" said "bad default revision 'HEAD'", which was found to be a bit confusing to new users. * jk/log-missing-default-HEAD: log: diagnose empty HEAD more clearly
2 parents 9d93988 + ce11360 commit 5af77d1

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
@@ -2173,6 +2173,21 @@ static int handle_revision_pseudo_opt(const char *submodule,
21732173
return 1;
21742174
}
21752175

2176+
static void NORETURN diagnose_missing_default(const char *def)
2177+
{
2178+
unsigned char sha1[20];
2179+
int flags;
2180+
const char *refname;
2181+
2182+
refname = resolve_ref_unsafe(def, 0, sha1, &flags);
2183+
if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2184+
die(_("your current branch appears to be broken"));
2185+
2186+
skip_prefix(refname, "refs/heads/", &refname);
2187+
die(_("your current branch '%s' does not have any commits yet"),
2188+
refname);
2189+
}
2190+
21762191
/*
21772192
* Parse revision information, filling in the "rev_info" structure,
21782193
* and removing the used arguments from the argument list.
@@ -2302,7 +2317,7 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s
23022317
struct object *object;
23032318
struct object_context oc;
23042319
if (get_sha1_with_context(revs->def, 0, sha1, &oc))
2305-
die("bad default revision '%s'", revs->def);
2320+
diagnose_missing_default(revs->def);
23062321
object = get_reference(revs, revs->def, sha1, 0);
23072322
add_pending_object_with_mode(revs, object, revs->def, oc.mode);
23082323
}

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)