git-blame bug
cover git-blame bug
Hi,
I ran across this git-blame failure the other day: if a directory is deleted and a file is created with the same name in a single commit, then git blame will fail on it with this error:
fatal: internal error in blame::find_origin
Anyway, patch 1/2 shows this failure with a test, and 2/2 is a possible fix. They should apply on maint.
Thanks,
--
Ben
Ben Willard (2):
t8006: Add test to show blame failure
blame: Fix corner case when a directory becomes a file
builtin-blame.c | 12 ++++++++---- t/t8006-blame.sh | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) create mode 100755 t/t8006-blame.sh
PATCH 1/2 t8006: Add test to show blame failure
'git blame' fails on a file which was changed from a directory to a file in a single commit. This test shows the failure.
--- t/t8006-blame.sh | 21 +++++++++++++++++++++ 1 files changed, 21 insertions(+), 0 deletions(-) create mode 100755 t/t8006-blame.sh
t/t8006-blame.sh new file +21 -0
@@ -0,0 +1,21 @@ +#!/bin/sh + +test_description='git blame on file which changed from a directory to a file in the same commit' +. ./test-lib.sh + +test_expect_success 'setup' ' + mkdir dir && + touch dir/file && + git add dir/file && + git commit -m "Add dir/file" && + git rm dir/file && + echo "File contents" > dir && + git add dir && + git commit -m "Add dir as a file" +' + +test_expect_failure 'blame runs on dir' ' + git blame dir +' + +test_done
Signature · 1 line
1.6.3.1.279.gd4bf4
PATCH 2/2 blame: Fix corner case when a directory becomes a file
In reply to Ben Willardfind_origin() assumes that there will be only one listing in diff_queued_diff, but this is not the case when a directory becomes a file in a single commit. So, don't fail in this case.
--- builtin-blame.c | 12 ++++++++---- t/t8006-blame.sh | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-)
builtin-blame.c +8 -4
@@ -363,16 +363,14 @@ static struct origin *find_origin(struct scoreboard *sb,diffcore_std (&diff_opts);/* It is either one entry that says "modified", or "created", -* or nothing. +* or nothing, or two entries (a "deleted" and an "add"). */ if (!diff_queued_diff.nr) {/* The path is the same as parent */ porigin =get_origin (sb, parent, origin->path);hashcpy (porigin->blob_sha1, origin->blob_sha1); } -else if (diff_queued_diff.nr !=1 ) -die ("internal error in blame::find_origin" ); -else { +else if (diff_queued_diff.nr ==1 ) {struct diff_filepair *p = diff_queued_diff.queue[0 ];switch (p->status) {default :
@@ -388,6 +386,12 @@ static struct origin *find_origin(struct scoreboard *sb,break ; } } +else if (diff_queued_diff.nr ==2 ) { +/* Both added and removed, (ie. directory became a file) */ + } +else { +die ("internal error in blame::find_origin" ); + }diff_flush (&diff_opts);diff_tree_release_paths (&diff_opts);if (porigin) {
t/t8006-blame.sh +1 -1
@@ -14,7 +14,7 @@ test_expect_success 'setup' ' git commit -m "Add dir as a file" ' -test_expect_failure 'blame runs on dir' ' +test_expect_success 'blame runs on dir' ' git blame dir '
Signature · 1 line
1.6.3.1.279.gd4bf4
Re: [PATCH 2/2] blame: Fix corner case when a directory becomes a file
Quoted 3 lines · Ben Willard
find_origin()assumes that there will be only one listing indiff_queued_diff, but this is not the case when a directory becomes a file in a single commit. So, don't fail in this case.
Thanks.
Your problem analysis is almost correct but the solution is wrong.
By the way, I'd rather not see people waste a whole _new_ test script when there are existing test scripts availble for the command.
-- >8 --
Subject: [PATCH] blame: correctly handle a path that used to be a directory
When trying to see if the same path exists in the parent, we ran "diff-tree" with pathspec set to the path we are interested in with the parent, and expect either to have exactly one resulting filepair (either "changed from the parent", "created when there was none") or nothing (when there is no change from the parent).
If the path used to be a directory, however, we will also see unbounded number of entries that talk about the files that used to exist underneath the directory in question. Correctly pick only the entry that describes the path we are interested in in such a case (namely, the creation of the path as a regular file).
Noticed by Ben Willard.
--- builtin-blame.c | 26 ++++++++++++++++++-------- t/t8003-blame.sh | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 8 deletions(-)
builtin-blame.c +18 -8
@@ -362,18 +362,28 @@ static struct origin *find_origin(struct scoreboard *sb, "", &diff_opts);diffcore_std (&diff_opts); -/* It is either one entry that says "modified", or "created", -* or nothing. -*/ if (!diff_queued_diff.nr) {/* The path is the same as parent */ porigin =get_origin (sb, parent, origin->path);hashcpy (porigin->blob_sha1, origin->blob_sha1); - } -else if (diff_queued_diff.nr !=1 ) -die ("internal error in blame::find_origin" ); -else { -struct diff_filepair *p = diff_queued_diff.queue[0 ]; + }else { +/* +* Since origin->path is a pathspec, if the parent +* commit had it as a directory, we will see a whole +* bunch of deletion of files in the directory that we +* do not care about. +*/ +int i; +struct diff_filepair *p =NULL ; +for (i =0 ; i < diff_queued_diff.nr; i++) { +const char *name; + p = diff_queued_diff.queue[i]; + name = p->one->path ? p->one->path : p->two->path; +if (!strcmp (name, origin->path)) +break ; + } +if (!p) +die ("internal error in blame::find_origin" );switch (p->status) {default : die("internal error in blame::find_origin (%c)",
t/t8003-blame.sh +15 -0
@@ -129,4 +129,19 @@ test_expect_success 'blame wholesale copy and more' ' ' +test_expect_success 'blame path that used to be a directory' ' + mkdir path && + echo A A A A A >path/file && + echo B B B B B >path/elif && + git add path && + test_tick && + git commit -m "path was a directory" && + rm -fr path && + echo A A A A A >path && + git add path && + test_tick && + git commit -m "path is a regular file" && + git blame HEAD^.. -- path +' + test_done
Signature · 1 line
1.6.3.1.263.g85347
Re: [PATCH 2/2] blame: Fix corner case when a directory becomes a file
Quoted 3 lines · Junio C Hamano
Thanks.
Your problem analysis is almost correct but the solution is wrong.
Ah, I guess it was more complicated than I thought.
Quoted 2 lines
By the way, I'd rather not see people waste a whole _new_ test script when there are existing test scripts availble for the command.
I'll remember that for next time.
Quoted 3 lines
-- >8 --
Subject: [PATCH] blame: correctly handle a path that used to be a directory
...
I tested this patch and it works.
Thanks Junio,
--
Ben