]> git.ipfire.org Git - thirdparty/git.git/commitdiff
blame: default to HEAD in a bare repo when no start commit is given
authorSZEDER Gábor <szeder.dev@gmail.com>
Sun, 7 Apr 2019 23:43:27 +0000 (01:43 +0200)
committerJunio C Hamano <gitster@pobox.com>
Mon, 8 Apr 2019 08:02:26 +0000 (17:02 +0900)
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 <szeder.dev@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/blame.c
t/annotate-tests.sh

index 581de0d8322681ef11026b3e36a81c7baf0fda38..8f66eb478606ce468244cd195507bd4c7f2c748f 100644 (file)
@@ -27,6 +27,7 @@
 #include "object-store.h"
 #include "blame.h"
 #include "string-list.h"
+#include "refs.h"
 
 static char blame_usage[] = N_("git blame [<options>] [<rev-opts>] [<rev>] [--] <file>");
 
@@ -993,6 +994,18 @@ parse_done:
 
        revs.disable_stdin = 1;
        setup_revisions(argc, argv, &revs, NULL);
+       if (!revs.pending.nr && is_bare_repository()) {
+               struct commit *head_commit;
+               struct object_id head_oid;
+
+               if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
+                                       &head_oid, NULL) ||
+                   !(head_commit = lookup_commit_reference_gently(revs.repo,
+                                                            &head_oid, 1)))
+                       die("no such ref: HEAD");
+
+               add_pending_object(&revs, &head_commit->object, "HEAD");
+       }
 
        init_scoreboard(&sb);
        sb.revs = &revs;
index 6da48a2e0a461e35f31fdd05712ee5c2bb9fda31..d933af571474f365c8e6ad478f5c833f3b67b315 100644 (file)
@@ -68,6 +68,14 @@ test_expect_success 'blame 1 author' '
        check_count A 2
 '
 
+test_expect_success 'blame in a bare repo without starting commit' '
+       git clone --bare . bare.git &&
+       (
+               cd bare.git &&
+               check_count A 2
+       )
+'
+
 test_expect_success 'blame by tag objects' '
        git tag -m "test tag" testTag &&
        git tag -m "test tag #2" testTag2 testTag &&