blame: allow to blame paths freshly added to the index
authorMike Hommey <mh@glandium.org>
Fri, 15 Jul 2016 23:23:45 +0000 (08:23 +0900)
committerJunio C Hamano <gitster@pobox.com>
Mon, 18 Jul 2016 21:33:38 +0000 (14:33 -0700)
When blaming files, changes in the work tree are taken into account
and displayed as being "Not Committed Yet".

However, when blaming a file that is not known to the current HEAD,
git blame fails with `no such path 'foo' in HEAD`, even when the file
was git add'ed.

Allowing such a blame is useful when the new file added to the index
(not yet committed) was created by renaming an existing file. It
also is useful when the new file was created from pieces already in
HEAD, moved or copied from other files and blaming with copy
detection (i.e. "-C").

Signed-off-by: Mike Hommey <mh@glandium.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/blame.c
t/t8003-blame-corner-cases.sh
index 21f42b0b62b81b637f1cc9589cd6c0306a93d05e..12c765acfede5727b27b2afaed2eb290fd930fc6 100644 (file)
@@ -2230,6 +2230,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)
 {
        struct commit_list *parents;
+       int pos;
 
        for (parents = work_tree->parents; parents; parents = parents->next) {
                const unsigned char *commit_sha1 = parents->item->object.oid.hash;
@@ -2240,7 +2241,14 @@ static void verify_working_tree_path(struct commit *work_tree, const char *path)
                    sha1_object_info(blob_sha1, NULL) == OBJ_BLOB)
                        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)
index a9b266f0d3d029229a346fb21c703acb84e4c37d..eab2e28094e32289b8a1b4f2e06cefe7a82fc74c 100755 (executable)
@@ -137,6 +137,51 @@ test_expect_success 'blame wholesale copy and more' '
 
 '
 
+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' '
        mkdir path &&
        echo A A A A A >path/file &&