Skip to content

Commit 699a0f3

Browse files
committed
Merge branch 'jk/log-missing-default-HEAD'
"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 7aa67f6 + ce11360 commit 699a0f3

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

revision.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2187,6 +2187,21 @@ static int handle_revision_pseudo_opt(const char *submodule,
21872187
return 1;
21882188
}
21892189

2190+
static void NORETURN diagnose_missing_default(const char *def)
2191+
{
2192+
unsigned char sha1[20];
2193+
int flags;
2194+
const char *refname;
2195+
2196+
refname = resolve_ref_unsafe(def, 0, sha1, &flags);
2197+
if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN))
2198+
die(_("your current branch appears to be broken"));
2199+
2200+
skip_prefix(refname, "refs/heads/", &refname);
2201+
die(_("your current branch '%s' does not have any commits yet"),
2202+
refname);
2203+
}
2204+
21902205
/*
21912206
* Parse revision information, filling in the "rev_info" structure,
21922207
* and removing the used arguments from the argument list.
@@ -2316,7 +2331,7 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s
23162331
struct object *object;
23172332
struct object_context oc;
23182333
if (get_sha1_with_context(revs->def, 0, sha1, &oc))
2319-
die("bad default revision '%s'", revs->def);
2334+
diagnose_missing_default(revs->def);
23202335
object = get_reference(revs, revs->def, sha1, 0);
23212336
add_pending_object_with_mode(revs, object, revs->def, oc.mode);
23222337
}

t/t4202-log.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,4 +894,18 @@ test_expect_success 'log --graph --no-walk is forbidden' '
894894
test_must_fail git log --graph --no-walk
895895
'
896896

897+
test_expect_success 'log diagnoses bogus HEAD' '
898+
git init empty &&
899+
test_must_fail git -C empty log 2>stderr &&
900+
test_i18ngrep does.not.have.any.commits stderr &&
901+
echo 1234abcd >empty/.git/refs/heads/master &&
902+
test_must_fail git -C empty log 2>stderr &&
903+
test_i18ngrep broken stderr &&
904+
echo "ref: refs/heads/invalid.lock" >empty/.git/HEAD &&
905+
test_must_fail git -C empty log 2>stderr &&
906+
test_i18ngrep broken stderr &&
907+
test_must_fail git -C empty log --default totally-bogus 2>stderr &&
908+
test_i18ngrep broken stderr
909+
'
910+
897911
test_done

0 commit comments

Comments
 (0)