Skip to content

Commit 835950b

Browse files
hanyang-tonygitster
authored andcommitted
blame: allow --contents to work with bare repo
The --contents option can be used with git blame to blame the file as if it had the contents from the specified file. Since 1a3119e (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 1a3119e, 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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cba07a3 commit 835950b

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

blame.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2806,7 +2806,9 @@ void setup_scoreboard(struct blame_scoreboard *sb,
28062806
parent_oid = &head_oid;
28072807
}
28082808

2809-
setup_work_tree();
2809+
if (!sb->contents_from)
2810+
setup_work_tree();
2811+
28102812
sb->final = fake_working_tree_commit(sb->repo,
28112813
&sb->revs->diffopt,
28122814
sb->path, sb->contents_from,

t/annotate-tests.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ test_expect_success 'blame with --contents' '
8383
check_count --contents=file A 2
8484
'
8585

86+
test_expect_success 'blame with --contents in a bare repo' '
87+
git clone --bare . bare-contents.git &&
88+
(
89+
cd bare-contents.git &&
90+
echo "1A quick brown fox jumps over the" >contents &&
91+
check_count --contents=contents A 1
92+
)
93+
'
94+
8695
test_expect_success 'blame with --contents changed' '
8796
echo "1A quick brown fox jumps over the" >contents &&
8897
echo "another lazy dog" >>contents &&

0 commit comments

Comments
 (0)