Skip to content

Commit a544fb0

Browse files
szedergitster
authored andcommitted
blame: default to HEAD in a bare repo when no start commit is given
When 'git blame' is invoked without specifying the commit to start blaming from, it starts from the given file's state in the work tree. However, when invoked in a bare repository without a start commit, then there is no work tree state to start from, and it dies with the following error message: $ git rev-parse --is-bare-repository true $ git blame file.c fatal: this operation must be run in a work tree This is misleading, because it implies that 'git blame' doesn't work in bare repositories at all, but it does, in fact, work just fine when it is given a commit to start from. We could improve the error message, of course, but let's just default to HEAD in a bare repository instead, as most likely that is what the user wanted anyway (if they wanted to start from an other commit, then they would have specified that in the first place). 'git annotate' is just a thin wrapper around 'git blame', so in the same situation it printed the same misleading error message, and this patch fixes it, too. Signed-off-by: SZEDER Gábor <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent aeb582a commit a544fb0

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)