filter-branch: eliminate duplicate mapped parents
authorCharles Bailey <cbailey32@bloomberg.net>
Mon, 30 Jun 2014 21:20:27 +0000 (22:20 +0100)
committerJunio C Hamano <gitster@pobox.com>
Tue, 1 Jul 2014 15:30:41 +0000 (08:30 -0700)
When multiple parents of a merge commit get mapped to the same
commit, filter-branch used to pass all instances of the parent
commit to the parent and commit filters and to "git commit-tree" or
"git_commit_non_empty_tree".

This can often happen when extracting a small project from a large
repository; merges can join history with no commits on any branch
which affect the paths being retained. Once the intermediate
commits have been filtered out, all the immediate parents of the
merge commit can end up being mapped to the same commit - either the
original merge-base or an ancestor of it.

"git commit-tree" would display an error but write the commit with
the normalized parents in any case. "git_commit_non_empty_tree"
would fail to notice that the commit being made was in fact a
non-merge commit and would retain it even if a further pass with
"--prune-empty" would discard the commit as empty.

Ensure that duplicate parents are pruned before the parent filter to
make "--prune-empty" idempotent, removing all empty non-merge
commits in a singe pass.

Signed-off-by: Charles Bailey <cbailey32@bloomberg.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
git-filter-branch.sh
t/t7003-filter-branch.sh
index 86d6994619151fb63327848e6763b07971017ad7..e6e99f5bb5102d394a25f156dbcba956f246054b 100755 (executable)
@@ -332,7 +332,13 @@ while read commit parents; do
        parentstr=
        for parent in $parents; do
                for reparent in $(map "$parent"); do
-                       parentstr="$parentstr -p $reparent"
+                       case "$parentstr " in
+                       *" -p $reparent "*)
+                               ;;
+                       *)
+                               parentstr="$parentstr -p $reparent"
+                               ;;
+                       esac
                done
        done
        if [ "$filter_parent" ]; then
index 9496736a89eb6b0b1ece64052cd2726c516c952b..66643e4bd758aa55f4f58d70675e32f1ad173460 100755 (executable)
@@ -308,6 +308,17 @@ test_expect_success 'Prune empty commits' '
        test_cmp expect actual
 '
 
+test_expect_success 'prune empty collapsed merges' '
+       test_config merge.ff false &&
+       git rev-list HEAD >expect &&
+       test_commit to_remove_2 &&
+       git reset --hard HEAD^ &&
+       test_merge non-ff to_remove_2 &&
+       git filter-branch -f --index-filter "git update-index --remove to_remove_2.t" --prune-empty HEAD &&
+       git rev-list HEAD >actual &&
+       test_cmp expect actual
+'
+
 test_expect_success '--remap-to-ancestor with filename filters' '
        git checkout master &&
        git reset --hard A &&