Skip to content

Commit 37e9c7f

Browse files
committed
Merge branch 'mh/blame-worktree'
"git blame file" allowed the lineage of lines in the uncommitted, unadded contents of "file" to be inspected, but it refused when "file" did not appear in the current commit. When "file" was created by renaming an existing file (but the change has not been committed), this restriction was unnecessarily tight. * mh/blame-worktree: t/t8003-blame-corner-cases.sh: Use here documents blame: allow to blame paths freshly added to the index
2 parents 9db3979 + c66b470 commit 37e9c7f

File tree

2 files changed

+71
-18
lines changed

2 files changed

+71
-18
lines changed

builtin/blame.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2229,6 +2229,7 @@ static int git_blame_config(const char *var, const char *value, void *cb)
22292229
static void verify_working_tree_path(struct commit *work_tree, const char *path)
22302230
{
22312231
struct commit_list *parents;
2232+
int pos;
22322233

22332234
for (parents = work_tree->parents; parents; parents = parents->next) {
22342235
const unsigned char *commit_sha1 = parents->item->object.oid.hash;
@@ -2239,7 +2240,14 @@ static void verify_working_tree_path(struct commit *work_tree, const char *path)
22392240
sha1_object_info(blob_sha1, NULL) == OBJ_BLOB)
22402241
return;
22412242
}
2242-
die("no such path '%s' in HEAD", path);
2243+
2244+
pos = cache_name_pos(path, strlen(path));
2245+
if (pos >= 0)
2246+
; /* path is in the index */
2247+
else if (!strcmp(active_cache[-1 - pos]->name, path))
2248+
; /* path is in the index, unmerged */
2249+
else
2250+
die("no such path '%s' in HEAD", path);
22432251
}
22442252

22452253
static struct commit_list **append_parent(struct commit_list **tail, const unsigned char *sha1)

t/t8003-blame-corner-cases.sh

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ test_expect_success setup '
4141
test_tick &&
4242
GIT_AUTHOR_NAME=Fourth git commit -m Fourth &&
4343
44-
{
45-
echo ABC
46-
echo DEF
47-
echo XXXX
48-
echo GHIJK
49-
} >cow &&
44+
cat >cow <<-\EOF &&
45+
ABC
46+
DEF
47+
XXXX
48+
GHIJK
49+
EOF
5050
git add cow &&
5151
test_tick &&
5252
GIT_AUTHOR_NAME=Fifth git commit -m Fifth
@@ -115,28 +115,73 @@ test_expect_success 'append with -C -C -C' '
115115
test_expect_success 'blame wholesale copy' '
116116
117117
git blame -f -C -C1 HEAD^ -- cow | sed -e "$pick_fc" >current &&
118-
{
119-
echo mouse-Initial
120-
echo mouse-Second
121-
echo mouse-Third
122-
} >expected &&
118+
cat >expected <<-\EOF &&
119+
mouse-Initial
120+
mouse-Second
121+
mouse-Third
122+
EOF
123123
test_cmp expected current
124124
125125
'
126126

127127
test_expect_success 'blame wholesale copy and more' '
128128
129129
git blame -f -C -C1 HEAD -- cow | sed -e "$pick_fc" >current &&
130-
{
131-
echo mouse-Initial
132-
echo mouse-Second
133-
echo cow-Fifth
134-
echo mouse-Third
135-
} >expected &&
130+
cat >expected <<-\EOF &&
131+
mouse-Initial
132+
mouse-Second
133+
cow-Fifth
134+
mouse-Third
135+
EOF
136136
test_cmp expected current
137137
138138
'
139139

140+
test_expect_success 'blame wholesale copy and more in the index' '
141+
142+
cat >horse <<-\EOF &&
143+
ABC
144+
DEF
145+
XXXX
146+
YYYY
147+
GHIJK
148+
EOF
149+
git add horse &&
150+
test_when_finished "git rm -f horse" &&
151+
git blame -f -C -C1 -- horse | sed -e "$pick_fc" >current &&
152+
cat >expected <<-\EOF &&
153+
mouse-Initial
154+
mouse-Second
155+
cow-Fifth
156+
horse-Not
157+
mouse-Third
158+
EOF
159+
test_cmp expected current
160+
161+
'
162+
163+
test_expect_success 'blame during cherry-pick with file rename conflict' '
164+
165+
test_when_finished "git reset --hard && git checkout master" &&
166+
git checkout HEAD~3 &&
167+
echo MOUSE >> mouse &&
168+
git mv mouse rodent &&
169+
git add rodent &&
170+
GIT_AUTHOR_NAME=Rodent git commit -m "rodent" &&
171+
git checkout --detach master &&
172+
(git cherry-pick HEAD@{1} || test $? -eq 1) &&
173+
git show HEAD@{1}:rodent > rodent &&
174+
git add rodent &&
175+
git blame -f -C -C1 rodent | sed -e "$pick_fc" >current &&
176+
cat current &&
177+
cat >expected <<-\EOF &&
178+
mouse-Initial
179+
mouse-Second
180+
rodent-Not
181+
EOF
182+
test_cmp expected current
183+
'
184+
140185
test_expect_success 'blame path that used to be a directory' '
141186
mkdir path &&
142187
echo A A A A A >path/file &&

0 commit comments

Comments
 (0)