git/t/t4301-merge-tree-write-tree.sh

242 lines
7.4 KiB
Bash
Raw Normal View History

merge-tree: implement real merges This adds the ability to perform real merges rather than just trivial merges (meaning handling three way content merges, recursive ancestor consolidation, renames, proper directory/file conflict handling, and so forth). However, unlike `git merge`, the working tree and index are left alone and no branch is updated. The only output is: - the toplevel resulting tree printed on stdout - exit status of 0 (clean), 1 (conflicts present), anything else (merge could not be performed; unknown if clean or conflicted) This output is meant to be used by some higher level script, perhaps in a sequence of steps like this: NEWTREE=$(git merge-tree --write-tree $BRANCH1 $BRANCH2) test $? -eq 0 || die "There were conflicts..." NEWCOMMIT=$(git commit-tree $NEWTREE -p $BRANCH1 -p $BRANCH2) git update-ref $BRANCH1 $NEWCOMMIT Note that higher level scripts may also want to access the conflict/warning messages normally output during a merge, or have quick access to a list of files with conflicts. That is not available in this preliminary implementation, but subsequent commits will add that ability (meaning that NEWTREE would be a lot more than a tree in the case of conflicts). This also marks the traditional trivial merge of merge-tree as deprecated. The trivial merge not only had limited applicability, the output format was also difficult to work with (and its format undocumented), and will generally be less performant than real merges. Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
2022-06-18 08:20:47 +08:00
#!/bin/sh
test_description='git merge-tree --write-tree'
leak tests: mark passing SANITIZE=leak tests as leak-free Mark those remaining tests that pass when run under SANITIZE=leak with TEST_PASSES_SANITIZE_LEAK=true, these were either omitted in f346fcb62a0 (Merge branch 'ab/mark-leak-free-tests-even-more', 2021-12-15) and 5a4f8381b68 (Merge branch 'ab/mark-leak-free-tests', 2021-10-25), or have had their memory leaks fixed since then. With this change there's now a a one-to-one mapping between those tests that we have opted-in via "TEST_PASSES_SANITIZE_LEAK=true", and those that pass with the new "check" mode: GIT_TEST_PASSING_SANITIZE_LEAK=check \ GIT_TEST_SANITIZE_LEAK_LOG=true \ make test SANITIZE=leak Note that the "GIT_TEST_SANITIZE_LEAK_LOG=true" is needed due to the edge cases noted in a preceding commit, i.e. in some cases we'd pass the test itself, but still have outstanding leaks due to ignored exit codes. The "GIT_TEST_SANITIZE_LEAK_LOG=true" corrects for that, we're only marking those tests as passing that really don't have any leaks, whether that was reflected in their exit code or not. Note that the change here to "t9100-git-svn-basic.sh" is marking that test as passing under SANITIZE=leak, we're removing a "TEST_FAILS_SANITIZE_LEAK=true" line, not "TEST_PASSES_SANITIZE_LEAK=true". See 7a98d9ab00d (revisions API: have release_revisions() release "cmdline", 2022-04-13) for the introduction of that t/lib-git-svn.sh-specific variable. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
2022-07-28 07:13:41 +08:00
TEST_PASSES_SANITIZE_LEAK=true
merge-tree: implement real merges This adds the ability to perform real merges rather than just trivial merges (meaning handling three way content merges, recursive ancestor consolidation, renames, proper directory/file conflict handling, and so forth). However, unlike `git merge`, the working tree and index are left alone and no branch is updated. The only output is: - the toplevel resulting tree printed on stdout - exit status of 0 (clean), 1 (conflicts present), anything else (merge could not be performed; unknown if clean or conflicted) This output is meant to be used by some higher level script, perhaps in a sequence of steps like this: NEWTREE=$(git merge-tree --write-tree $BRANCH1 $BRANCH2) test $? -eq 0 || die "There were conflicts..." NEWCOMMIT=$(git commit-tree $NEWTREE -p $BRANCH1 -p $BRANCH2) git update-ref $BRANCH1 $NEWCOMMIT Note that higher level scripts may also want to access the conflict/warning messages normally output during a merge, or have quick access to a list of files with conflicts. That is not available in this preliminary implementation, but subsequent commits will add that ability (meaning that NEWTREE would be a lot more than a tree in the case of conflicts). This also marks the traditional trivial merge of merge-tree as deprecated. The trivial merge not only had limited applicability, the output format was also difficult to work with (and its format undocumented), and will generally be less performant than real merges. Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
2022-06-18 08:20:47 +08:00
. ./test-lib.sh
# This test is ort-specific
if test "$GIT_TEST_MERGE_ALGORITHM" != "ort"
then
skip_all="GIT_TEST_MERGE_ALGORITHM != ort"
test_done
fi
test_expect_success setup '
test_write_lines 1 2 3 4 5 >numbers &&
echo hello >greeting &&
echo foo >whatever &&
git add numbers greeting whatever &&
test_tick &&
git commit -m initial &&
git branch side1 &&
git branch side2 &&
git branch side3 &&
git checkout side1 &&
test_write_lines 1 2 3 4 5 6 >numbers &&
echo hi >greeting &&
echo bar >whatever &&
git add numbers greeting whatever &&
test_tick &&
git commit -m modify-stuff &&
git checkout side2 &&
test_write_lines 0 1 2 3 4 5 >numbers &&
echo yo >greeting &&
git rm whatever &&
mkdir whatever &&
>whatever/empty &&
git add numbers greeting whatever/empty &&
test_tick &&
git commit -m other-modifications &&
git checkout side3 &&
git mv numbers sequence &&
test_tick &&
git commit -m rename-numbers &&
git switch --orphan unrelated &&
>something-else &&
git add something-else &&
test_tick &&
git commit -m first-commit
merge-tree: implement real merges This adds the ability to perform real merges rather than just trivial merges (meaning handling three way content merges, recursive ancestor consolidation, renames, proper directory/file conflict handling, and so forth). However, unlike `git merge`, the working tree and index are left alone and no branch is updated. The only output is: - the toplevel resulting tree printed on stdout - exit status of 0 (clean), 1 (conflicts present), anything else (merge could not be performed; unknown if clean or conflicted) This output is meant to be used by some higher level script, perhaps in a sequence of steps like this: NEWTREE=$(git merge-tree --write-tree $BRANCH1 $BRANCH2) test $? -eq 0 || die "There were conflicts..." NEWCOMMIT=$(git commit-tree $NEWTREE -p $BRANCH1 -p $BRANCH2) git update-ref $BRANCH1 $NEWCOMMIT Note that higher level scripts may also want to access the conflict/warning messages normally output during a merge, or have quick access to a list of files with conflicts. That is not available in this preliminary implementation, but subsequent commits will add that ability (meaning that NEWTREE would be a lot more than a tree in the case of conflicts). This also marks the traditional trivial merge of merge-tree as deprecated. The trivial merge not only had limited applicability, the output format was also difficult to work with (and its format undocumented), and will generally be less performant than real merges. Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
2022-06-18 08:20:47 +08:00
'
test_expect_success 'Clean merge' '
TREE_OID=$(git merge-tree --write-tree side1 side3) &&
q_to_tab <<-EOF >expect &&
100644 blob $(git rev-parse side1:greeting)Qgreeting
100644 blob $(git rev-parse side1:numbers)Qsequence
100644 blob $(git rev-parse side1:whatever)Qwhatever
EOF
git ls-tree $TREE_OID >actual &&
test_cmp expect actual
'
test_expect_success 'Content merge and a few conflicts' '
git checkout side1^0 &&
test_must_fail git merge side2 &&
expected_tree=$(git rev-parse AUTO_MERGE) &&
# We will redo the merge, while we are still in a conflicted state!
git ls-files -u >conflicted-file-info &&
merge-tree: implement real merges This adds the ability to perform real merges rather than just trivial merges (meaning handling three way content merges, recursive ancestor consolidation, renames, proper directory/file conflict handling, and so forth). However, unlike `git merge`, the working tree and index are left alone and no branch is updated. The only output is: - the toplevel resulting tree printed on stdout - exit status of 0 (clean), 1 (conflicts present), anything else (merge could not be performed; unknown if clean or conflicted) This output is meant to be used by some higher level script, perhaps in a sequence of steps like this: NEWTREE=$(git merge-tree --write-tree $BRANCH1 $BRANCH2) test $? -eq 0 || die "There were conflicts..." NEWCOMMIT=$(git commit-tree $NEWTREE -p $BRANCH1 -p $BRANCH2) git update-ref $BRANCH1 $NEWCOMMIT Note that higher level scripts may also want to access the conflict/warning messages normally output during a merge, or have quick access to a list of files with conflicts. That is not available in this preliminary implementation, but subsequent commits will add that ability (meaning that NEWTREE would be a lot more than a tree in the case of conflicts). This also marks the traditional trivial merge of merge-tree as deprecated. The trivial merge not only had limited applicability, the output format was also difficult to work with (and its format undocumented), and will generally be less performant than real merges. Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
2022-06-18 08:20:47 +08:00
test_when_finished "git reset --hard" &&
test_expect_code 1 git merge-tree --write-tree side1 side2 >RESULT &&
actual_tree=$(head -n 1 RESULT) &&
# Due to differences of e.g. "HEAD" vs "side1", the results will not
# exactly match. Dig into individual files.
# Numbers should have three-way merged cleanly
test_write_lines 0 1 2 3 4 5 6 >expect &&
git show ${actual_tree}:numbers >actual &&
test_cmp expect actual &&
# whatever and whatever~<branch> should have same HASHES
git rev-parse ${expected_tree}:whatever ${expected_tree}:whatever~HEAD >expect &&
git rev-parse ${actual_tree}:whatever ${actual_tree}:whatever~side1 >actual &&
test_cmp expect actual &&
# greeting should have a merge conflict
git show ${expected_tree}:greeting >tmp &&
sed -e s/HEAD/side1/ tmp >expect &&
git show ${actual_tree}:greeting >actual &&
test_cmp expect actual
'
test_expect_success 'Barf on misspelled option, with exit code other than 0 or 1' '
# Mis-spell with single "s" instead of double "s"
test_expect_code 129 git merge-tree --write-tree --mesages FOOBAR side1 side2 2>expect &&
grep "error: unknown option.*mesages" expect
'
test_expect_success 'Barf on too many arguments' '
test_expect_code 129 git merge-tree --write-tree side1 side2 invalid 2>expect &&
grep "^usage: git merge-tree" expect
'
anonymize_hash() {
sed -e "s/[0-9a-f]\{40,\}/HASH/g" "$@"
}
test_expect_success 'test conflict notices and such' '
test_expect_code 1 git merge-tree --write-tree --name-only side1 side2 >out &&
anonymize_hash out >actual &&
# Expected results:
# "greeting" should merge with conflicts
# "numbers" should merge cleanly
# "whatever" has *both* a modify/delete and a file/directory conflict
cat <<-EOF >expect &&
HASH
greeting
whatever~side1
Auto-merging greeting
CONFLICT (content): Merge conflict in greeting
Auto-merging numbers
CONFLICT (file/directory): directory in the way of whatever from side1; moving it to whatever~side1 instead.
CONFLICT (modify/delete): whatever~side1 deleted in side2 and modified in side1. Version side1 of whatever~side1 left in tree.
EOF
test_cmp expect actual
'
for opt in $(git merge-tree --git-completion-helper-all)
do
if test $opt = "--trivial-merge" || test $opt = "--write-tree"
then
continue
fi
test_expect_success "usage: --trivial-merge is incompatible with $opt" '
test_expect_code 128 git merge-tree --trivial-merge $opt side1 side2 side3
'
done
test_expect_success 'Just the conflicted files without the messages' '
test_expect_code 1 git merge-tree --write-tree --no-messages --name-only side1 side2 >out &&
anonymize_hash out >actual &&
test_write_lines HASH greeting whatever~side1 >expect &&
test_cmp expect actual
'
test_expect_success 'Check conflicted oids and modes without messages' '
test_expect_code 1 git merge-tree --write-tree --no-messages side1 side2 >out &&
anonymize_hash out >actual &&
# Compare the basic output format
q_to_tab >expect <<-\EOF &&
HASH
100644 HASH 1Qgreeting
100644 HASH 2Qgreeting
100644 HASH 3Qgreeting
100644 HASH 1Qwhatever~side1
100644 HASH 2Qwhatever~side1
EOF
test_cmp expect actual &&
# Check the actual hashes against the `ls-files -u` output too
tail -n +2 out | sed -e s/side1/HEAD/ >actual &&
test_cmp conflicted-file-info actual
'
test_expect_success 'NUL terminated conflicted file "lines"' '
git checkout -b tweak1 side1 &&
test_write_lines zero 1 2 3 4 5 6 >numbers &&
git add numbers &&
git mv numbers "Αυτά μου φαίνονται κινέζικα" &&
git commit -m "Renamed numbers" &&
test_expect_code 1 git merge-tree --write-tree -z tweak1 side2 >out &&
anonymize_hash out >actual &&
printf "\\n" >>actual &&
# Expected results:
# "greeting" should merge with conflicts
# "whatever" has *both* a modify/delete and a file/directory conflict
# "Αυτά μου φαίνονται κινέζικα" should have a conflict
echo HASH | lf_to_nul >expect &&
q_to_tab <<-EOF | lf_to_nul >>expect &&
100644 HASH 1Qgreeting
100644 HASH 2Qgreeting
100644 HASH 3Qgreeting
100644 HASH 1Qwhatever~tweak1
100644 HASH 2Qwhatever~tweak1
100644 HASH 1QΑυτά μου φαίνονται κινέζικα
100644 HASH 2QΑυτά μου φαίνονται κινέζικα
100644 HASH 3QΑυτά μου φαίνονται κινέζικα
EOF
q_to_nul <<-EOF >>expect &&
1QgreetingQAuto-mergingQAuto-merging greeting
Q1QgreetingQCONFLICT (contents)QCONFLICT (content): Merge conflict in greeting
Q2Qwhatever~tweak1QwhateverQCONFLICT (file/directory)QCONFLICT (file/directory): directory in the way of whatever from tweak1; moving it to whatever~tweak1 instead.
Q1Qwhatever~tweak1QCONFLICT (modify/delete)QCONFLICT (modify/delete): whatever~tweak1 deleted in side2 and modified in tweak1. Version tweak1 of whatever~tweak1 left in tree.
Q1QΑυτά μου φαίνονται κινέζικαQAuto-mergingQAuto-merging Αυτά μου φαίνονται κινέζικα
Q1QΑυτά μου φαίνονται κινέζικαQCONFLICT (contents)QCONFLICT (content): Merge conflict in Αυτά μου φαίνονται κινέζικα
Q
EOF
test_cmp expect actual
'
test_expect_success 'error out by default for unrelated histories' '
test_expect_code 128 git merge-tree --write-tree side1 unrelated 2>error &&
grep "refusing to merge unrelated histories" error
'
test_expect_success 'can override merge of unrelated histories' '
git merge-tree --write-tree --allow-unrelated-histories side1 unrelated >tree &&
TREE=$(cat tree) &&
git rev-parse side1:numbers side1:greeting side1:whatever unrelated:something-else >expect &&
git rev-parse $TREE:numbers $TREE:greeting $TREE:whatever $TREE:something-else >actual &&
test_cmp expect actual
'
merge-tree: implement real merges This adds the ability to perform real merges rather than just trivial merges (meaning handling three way content merges, recursive ancestor consolidation, renames, proper directory/file conflict handling, and so forth). However, unlike `git merge`, the working tree and index are left alone and no branch is updated. The only output is: - the toplevel resulting tree printed on stdout - exit status of 0 (clean), 1 (conflicts present), anything else (merge could not be performed; unknown if clean or conflicted) This output is meant to be used by some higher level script, perhaps in a sequence of steps like this: NEWTREE=$(git merge-tree --write-tree $BRANCH1 $BRANCH2) test $? -eq 0 || die "There were conflicts..." NEWCOMMIT=$(git commit-tree $NEWTREE -p $BRANCH1 -p $BRANCH2) git update-ref $BRANCH1 $NEWCOMMIT Note that higher level scripts may also want to access the conflict/warning messages normally output during a merge, or have quick access to a list of files with conflicts. That is not available in this preliminary implementation, but subsequent commits will add that ability (meaning that NEWTREE would be a lot more than a tree in the case of conflicts). This also marks the traditional trivial merge of merge-tree as deprecated. The trivial merge not only had limited applicability, the output format was also difficult to work with (and its format undocumented), and will generally be less performant than real merges. Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
2022-06-18 08:20:47 +08:00
test_done