Skip to content

Commit d8620d3

Browse files
committed
Merge branch 'sg/blame-in-bare-start-at-head'
"git blame -- path" in a non-bare repository starts blaming from the working tree, and the same command in a bare repository errors out because there is no working tree by definition. The command has been taught to instead start blaming from the commit at HEAD, which is more useful. * sg/blame-in-bare-start-at-head: blame: default to HEAD in a bare repo when no start commit is given
2 parents 503f580 + a544fb0 commit d8620d3

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

builtin/blame.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "object-store.h"
2828
#include "blame.h"
2929
#include "string-list.h"
30+
#include "refs.h"
3031

3132
static char blame_usage[] = N_("git blame [<options>] [<rev-opts>] [<rev>] [--] <file>");
3233

@@ -993,6 +994,18 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
993994

994995
revs.disable_stdin = 1;
995996
setup_revisions(argc, argv, &revs, NULL);
997+
if (!revs.pending.nr && is_bare_repository()) {
998+
struct commit *head_commit;
999+
struct object_id head_oid;
1000+
1001+
if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
1002+
&head_oid, NULL) ||
1003+
!(head_commit = lookup_commit_reference_gently(revs.repo,
1004+
&head_oid, 1)))
1005+
die("no such ref: HEAD");
1006+
1007+
add_pending_object(&revs, &head_commit->object, "HEAD");
1008+
}
9961009

9971010
init_scoreboard(&sb);
9981011
sb.revs = &revs;

t/annotate-tests.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ test_expect_success 'blame 1 author' '
6868
check_count A 2
6969
'
7070

71+
test_expect_success 'blame in a bare repo without starting commit' '
72+
git clone --bare . bare.git &&
73+
(
74+
cd bare.git &&
75+
check_count A 2
76+
)
77+
'
78+
7179
test_expect_success 'blame by tag objects' '
7280
git tag -m "test tag" testTag &&
7381
git tag -m "test tag #2" testTag2 testTag &&

0 commit comments

Comments
 (0)