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
This commit is contained in:
Junio C Hamano 2016-07-25 14:13:45 -07:00
commit 37e9c7f5e1
2 changed files with 71 additions and 18 deletions

View file

@ -2229,6 +2229,7 @@ static int git_blame_config(const char *var, const char *value, void *cb)
static void verify_working_tree_path(struct commit *work_tree, const char *path) static void verify_working_tree_path(struct commit *work_tree, const char *path)
{ {
struct commit_list *parents; struct commit_list *parents;
int pos;
for (parents = work_tree->parents; parents; parents = parents->next) { for (parents = work_tree->parents; parents; parents = parents->next) {
const unsigned char *commit_sha1 = parents->item->object.oid.hash; 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)
sha1_object_info(blob_sha1, NULL) == OBJ_BLOB) sha1_object_info(blob_sha1, NULL) == OBJ_BLOB)
return; return;
} }
die("no such path '%s' in HEAD", path);
pos = cache_name_pos(path, strlen(path));
if (pos >= 0)
; /* path is in the index */
else if (!strcmp(active_cache[-1 - pos]->name, path))
; /* path is in the index, unmerged */
else
die("no such path '%s' in HEAD", path);
} }
static struct commit_list **append_parent(struct commit_list **tail, const unsigned char *sha1) static struct commit_list **append_parent(struct commit_list **tail, const unsigned char *sha1)

View file

@ -41,12 +41,12 @@ test_expect_success setup '
test_tick && test_tick &&
GIT_AUTHOR_NAME=Fourth git commit -m Fourth && GIT_AUTHOR_NAME=Fourth git commit -m Fourth &&
{ cat >cow <<-\EOF &&
echo ABC ABC
echo DEF DEF
echo XXXX XXXX
echo GHIJK GHIJK
} >cow && EOF
git add cow && git add cow &&
test_tick && test_tick &&
GIT_AUTHOR_NAME=Fifth git commit -m Fifth GIT_AUTHOR_NAME=Fifth git commit -m Fifth
@ -115,11 +115,11 @@ test_expect_success 'append with -C -C -C' '
test_expect_success 'blame wholesale copy' ' test_expect_success 'blame wholesale copy' '
git blame -f -C -C1 HEAD^ -- cow | sed -e "$pick_fc" >current && git blame -f -C -C1 HEAD^ -- cow | sed -e "$pick_fc" >current &&
{ cat >expected <<-\EOF &&
echo mouse-Initial mouse-Initial
echo mouse-Second mouse-Second
echo mouse-Third mouse-Third
} >expected && EOF
test_cmp expected current test_cmp expected current
' '
@ -127,16 +127,61 @@ test_expect_success 'blame wholesale copy' '
test_expect_success 'blame wholesale copy and more' ' test_expect_success 'blame wholesale copy and more' '
git blame -f -C -C1 HEAD -- cow | sed -e "$pick_fc" >current && git blame -f -C -C1 HEAD -- cow | sed -e "$pick_fc" >current &&
{ cat >expected <<-\EOF &&
echo mouse-Initial mouse-Initial
echo mouse-Second mouse-Second
echo cow-Fifth cow-Fifth
echo mouse-Third mouse-Third
} >expected && EOF
test_cmp expected current test_cmp expected current
' '
test_expect_success 'blame wholesale copy and more in the index' '
cat >horse <<-\EOF &&
ABC
DEF
XXXX
YYYY
GHIJK
EOF
git add horse &&
test_when_finished "git rm -f horse" &&
git blame -f -C -C1 -- horse | sed -e "$pick_fc" >current &&
cat >expected <<-\EOF &&
mouse-Initial
mouse-Second
cow-Fifth
horse-Not
mouse-Third
EOF
test_cmp expected current
'
test_expect_success 'blame during cherry-pick with file rename conflict' '
test_when_finished "git reset --hard && git checkout master" &&
git checkout HEAD~3 &&
echo MOUSE >> mouse &&
git mv mouse rodent &&
git add rodent &&
GIT_AUTHOR_NAME=Rodent git commit -m "rodent" &&
git checkout --detach master &&
(git cherry-pick HEAD@{1} || test $? -eq 1) &&
git show HEAD@{1}:rodent > rodent &&
git add rodent &&
git blame -f -C -C1 rodent | sed -e "$pick_fc" >current &&
cat current &&
cat >expected <<-\EOF &&
mouse-Initial
mouse-Second
rodent-Not
EOF
test_cmp expected current
'
test_expect_success 'blame path that used to be a directory' ' test_expect_success 'blame path that used to be a directory' '
mkdir path && mkdir path &&
echo A A A A A >path/file && echo A A A A A >path/file &&