]> git.ipfire.org Git - thirdparty/git.git/commitdiff
blame: allow --contents to work with bare repo
authorHan Young <hanyang.tony@bytedance.com>
Fri, 21 Jul 2023 03:57:58 +0000 (11:57 +0800)
committerJunio C Hamano <gitster@pobox.com>
Fri, 21 Jul 2023 14:32:58 +0000 (07:32 -0700)
The --contents option can be used with git blame to blame the file
as if it had the contents from the specified file. Since 1a3119ed
(blame: allow --contents to work with non-HEAD commit, 2023-03-24),
the --contents option can work with non-HEAD commit. However, if you
try to use --contents in a bare repository, you get the following
error:

    fatal: this operation must be run in a work tree

This is because before trying to generate a fake working tree
commit, we always call setup_work_tree(). But in a bare repo,
working tree is not available. The call to setup_work_tree is used
to prepare the reading of the blamed file in the working tree, which
isn't necessary if we are reading the contents from the specific
file instead of the file in the working tree.

Add a check in setup_scoreboard to skip setup_work_tree if we are
reading from the file specified in --contents.

This enables us to use --contents in a bare repo. This is a nice
addition on top of 1a3119ed, having a working tree to use --contents
is optional.

Add test for the --contents option with bare repo to the
annotate-tests.sh test script.

Signed-off-by: Han Young <hanyang.tony@bytedance.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
blame.c
t/annotate-tests.sh

diff --git a/blame.c b/blame.c
index d12bd9f97ba5c1808913c72bac4473509c340827..141756975bf5a58a1744eda78c1750d9d949272f 100644 (file)
--- a/blame.c
+++ b/blame.c
@@ -2806,7 +2806,9 @@ void setup_scoreboard(struct blame_scoreboard *sb,
                        parent_oid = &head_oid;
                }
 
-               setup_work_tree();
+               if (!sb->contents_from)
+                       setup_work_tree();
+
                sb->final = fake_working_tree_commit(sb->repo,
                                                     &sb->revs->diffopt,
                                                     sb->path, sb->contents_from,
index 2ef70235b1010b1179e083e1e390b3ae7afabea4..5e21e84f3884eb8c787ab82a5d1360d2b7cffb74 100644 (file)
@@ -83,6 +83,15 @@ test_expect_success 'blame with --contents' '
        check_count --contents=file A 2
 '
 
+test_expect_success 'blame with --contents in a bare repo' '
+       git clone --bare . bare-contents.git &&
+       (
+               cd bare-contents.git &&
+               echo "1A quick brown fox jumps over the" >contents &&
+               check_count --contents=contents A 1
+       )
+'
+
 test_expect_success 'blame with --contents changed' '
        echo "1A quick brown fox jumps over the" >contents &&
        echo "another lazy dog" >>contents &&