Fourth batch
[gitweb.git] / t / t6050-replace.sh
index fb07ad24259c3ee059097f4b4d84a68450b88ab3..e7e64e085ddcfe7d7cf4df844472a6bf456ab079 100755 (executable)
@@ -4,11 +4,10 @@
 #
 test_description='Tests replace refs functionality'
 
-exec </dev/null
-
 . ./test-lib.sh
+. "$TEST_DIRECTORY/lib-gpg.sh"
 
-add_and_commit_file()
+add_and_commit_file ()
 {
     _file="$1"
     _msg="$2"
@@ -18,6 +17,39 @@ add_and_commit_file()
     git commit --quiet -m "$_file: $_msg"
 }
 
+commit_buffer_contains_parents ()
+{
+    git cat-file commit "$1" >payload &&
+    sed -n -e '/^$/q' -e '/^parent /p' <payload >actual &&
+    shift &&
+    for _parent
+    do
+       echo "parent $_parent"
+    done >expected &&
+    test_cmp expected actual
+}
+
+commit_peeling_shows_parents ()
+{
+    _parent_number=1
+    _commit="$1"
+    shift &&
+    for _parent
+    do
+       _found=$(git rev-parse --verify $_commit^$_parent_number) || return 1
+       test "$_found" = "$_parent" || return 1
+       _parent_number=$(( $_parent_number + 1 ))
+    done &&
+    test_must_fail git rev-parse --verify $_commit^$_parent_number 2>err &&
+    test_i18ngrep "Needed a single revision" err
+}
+
+commit_has_parents ()
+{
+    commit_buffer_contains_parents "$@" &&
+    commit_peeling_shows_parents "$@"
+}
+
 HASH1=
 HASH2=
 HASH3=
@@ -82,6 +114,12 @@ test_expect_success 'test GIT_NO_REPLACE_OBJECTS env variable' '
      GIT_NO_REPLACE_OBJECTS=1 git show $HASH2 | grep "A U Thor"
 '
 
+test_expect_success 'test core.usereplacerefs config option' '
+       test_config core.usereplacerefs false &&
+       git cat-file commit $HASH2 | grep "author A U Thor" &&
+       git show $HASH2 | grep "A U Thor"
+'
+
 cat >tag.sig <<EOF
 object $HASH2
 type commit
@@ -96,8 +134,8 @@ test_expect_success 'tag replaced commit' '
 
 test_expect_success '"git fsck" works' '
      git fsck master >fsck_master.out &&
-     grep "dangling commit $R" fsck_master.out &&
-     grep "dangling tag $(cat .git/refs/tags/mytag)" fsck_master.out &&
+     test_i18ngrep "dangling commit $R" fsck_master.out &&
+     test_i18ngrep "dangling tag $(cat .git/refs/tags/mytag)" fsck_master.out &&
      test -z "$(git fsck)"
 '
 
@@ -318,11 +356,15 @@ test_expect_success 'test --format long' '
        test_cmp expected actual
 '
 
-test_expect_success 'setup a fake editor' '
-       write_script fakeeditor <<-\EOF
+test_expect_success 'setup fake editors' '
+       write_script fakeeditor <<-\EOF &&
                sed -e "s/A U Thor/A fake Thor/" "$1" >"$1.new"
                mv "$1.new" "$1"
        EOF
+       write_script failingfakeeditor <<-\EOF
+               ./fakeeditor "$@"
+               false
+       EOF
 '
 
 test_expect_success '--edit with and without already replaced object' '
@@ -339,7 +381,7 @@ test_expect_success '--edit with and without already replaced object' '
 test_expect_success '--edit and change nothing or command failed' '
        git replace -d "$PARA3" &&
        test_must_fail env GIT_EDITOR=true git replace --edit "$PARA3" &&
-       test_must_fail env GIT_EDITOR="./fakeeditor;false" git replace --edit "$PARA3" &&
+       test_must_fail env GIT_EDITOR="./failingfakeeditor" git replace --edit "$PARA3" &&
        GIT_EDITOR=./fakeeditor git replace --edit "$PARA3" &&
        git replace -l | grep "$PARA3" &&
        git cat-file commit "$PARA3" | grep "A fake Thor"
@@ -351,4 +393,115 @@ test_expect_success 'replace ref cleanup' '
        test -z "$(git replace)"
 '
 
+test_expect_success '--graft with and without already replaced object' '
+       git log --oneline >log &&
+       test_line_count = 7 log &&
+       git replace --graft $HASH5 &&
+       git log --oneline >log &&
+       test_line_count = 3 log &&
+       commit_has_parents $HASH5 &&
+       test_must_fail git replace --graft $HASH5 $HASH4 $HASH3 &&
+       git replace --force -g $HASH5 $HASH4 $HASH3 &&
+       commit_has_parents $HASH5 $HASH4 $HASH3 &&
+       git replace -d $HASH5
+'
+
+test_expect_success '--graft using a tag as the new parent' '
+       git tag new_parent $HASH5 &&
+       git replace --graft $HASH7 new_parent &&
+       commit_has_parents $HASH7 $HASH5 &&
+       git replace -d $HASH7 &&
+       git tag -a -m "annotated new parent tag" annotated_new_parent $HASH5 &&
+       git replace --graft $HASH7 annotated_new_parent &&
+       commit_has_parents $HASH7 $HASH5 &&
+       git replace -d $HASH7
+'
+
+test_expect_success '--graft using a tag as the replaced object' '
+       git tag replaced_object $HASH7 &&
+       git replace --graft replaced_object $HASH5 &&
+       commit_has_parents $HASH7 $HASH5 &&
+       git replace -d $HASH7 &&
+       git tag -a -m "annotated replaced object tag" annotated_replaced_object $HASH7 &&
+       git replace --graft annotated_replaced_object $HASH5 &&
+       commit_has_parents $HASH7 $HASH5 &&
+       git replace -d $HASH7
+'
+
+test_expect_success GPG 'set up a signed commit' '
+       echo "line 17" >>hello &&
+       echo "line 18" >>hello &&
+       git add hello &&
+       test_tick &&
+       git commit --quiet -S -m "hello: 2 more lines in a signed commit" &&
+       HASH8=$(git rev-parse --verify HEAD) &&
+       git verify-commit $HASH8
+'
+
+test_expect_success GPG '--graft with a signed commit' '
+       git cat-file commit $HASH8 >orig &&
+       git replace --graft $HASH8 &&
+       git cat-file commit $HASH8 >repl &&
+       commit_has_parents $HASH8 &&
+       test_must_fail git verify-commit $HASH8 &&
+       sed -n -e "/^tree /p" -e "/^author /p" -e "/^committer /p" orig >expected &&
+       echo >>expected &&
+       sed -e "/^$/q" repl >actual &&
+       test_cmp expected actual &&
+       git replace -d $HASH8
+'
+
+test_expect_success GPG 'set up a merge commit with a mergetag' '
+       git reset --hard HEAD &&
+       git checkout -b test_branch HEAD~2 &&
+       echo "line 1 from test branch" >>hello &&
+       echo "line 2 from test branch" >>hello &&
+       git add hello &&
+       test_tick &&
+       git commit -m "hello: 2 more lines from a test branch" &&
+       HASH9=$(git rev-parse --verify HEAD) &&
+       git tag -s -m "tag for testing with a mergetag" test_tag HEAD &&
+       git checkout master &&
+       git merge -s ours test_tag &&
+       HASH10=$(git rev-parse --verify HEAD) &&
+       git cat-file commit $HASH10 | grep "^mergetag object"
+'
+
+test_expect_success GPG '--graft on a commit with a mergetag' '
+       test_must_fail git replace --graft $HASH10 $HASH8^1 &&
+       git replace --graft $HASH10 $HASH8^1 $HASH9 &&
+       git replace -d $HASH10
+'
+
+test_expect_success '--convert-graft-file' '
+       git checkout -b with-graft-file &&
+       test_commit root2 &&
+       git reset --hard root2^ &&
+       test_commit root1 &&
+       test_commit after-root1 &&
+       test_tick &&
+       git merge -m merge-root2 root2 &&
+
+       : add and convert graft file &&
+       printf "%s\n%s %s\n\n# comment\n%s\n" \
+               $(git rev-parse HEAD^^ HEAD^ HEAD^^ HEAD^2) \
+               >.git/info/grafts &&
+       git status 2>stderr &&
+       test_i18ngrep "hint:.*grafts is deprecated" stderr &&
+       git replace --convert-graft-file 2>stderr &&
+       test_i18ngrep ! "hint:.*grafts is deprecated" stderr &&
+       test_path_is_missing .git/info/grafts &&
+
+       : verify that the history is now "grafted" &&
+       git rev-list HEAD >out &&
+       test_line_count = 4 out &&
+
+       : create invalid graft file and verify that it is not deleted &&
+       test_when_finished "rm -f .git/info/grafts" &&
+       echo $EMPTY_BLOB $EMPTY_TREE >.git/info/grafts &&
+       test_must_fail git replace --convert-graft-file 2>err &&
+       test_i18ngrep "$EMPTY_BLOB $EMPTY_TREE" err &&
+       test_i18ngrep "$EMPTY_BLOB $EMPTY_TREE" .git/info/grafts
+'
+
 test_done