From 175c2cd836894795556a997392806be860200af1 Mon Sep 17 00:00:00 2001 From: Stefan Zweifel Date: Fri, 26 Feb 2021 19:39:59 +0100 Subject: [PATCH 1/2] Add failing tests --- tests/git-auto-commit.bats | 69 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/tests/git-auto-commit.bats b/tests/git-auto-commit.bats index e3eae717..32b84e13 100644 --- a/tests/git-auto-commit.bats +++ b/tests/git-auto-commit.bats @@ -225,6 +225,13 @@ git_auto_commit() { run git ls-remote --tags --refs assert_output --partial refs/tags/v1.0.0 + + # Assert that the commit has been pushed with --force and + # sha values are equal on local and remote + current_sha="$(git rev-parse --verify --short master)" + remote_sha="$(git rev-parse --verify --short origin/master)" + + assert_equal $current_sha $remote_sha } @test "It applies INPUT_PUSH_OPTIONS when pushing commit to remote" { @@ -331,3 +338,65 @@ git_auto_commit() { assert_line "::debug::git-fetch has not been executed" } + +@test "It pushes generated commit and tag to remote and actually updates the commit shas" { + INPUT_BRANCH="" + INPUT_TAGGING_MESSAGE="v2.0.0" + + touch "${FAKE_LOCAL_REPOSITORY}"/new-file-{1,2,3}.txt + + run git_auto_commit + + assert_success + + assert_line "INPUT_TAGGING_MESSAGE: v2.0.0" + assert_line "::debug::Create tag v2.0.0" + assert_line "::debug::git push origin --tags" + + # Assert a tag v2.0.0 has been created + run git tag + assert_output v2.0.0 + + # Assert tag v2.0.0 has been pushed to remote + run git ls-remote --tags --refs + assert_output --partial refs/tags/v2.0.0 + + # Assert that branch "master" was updated on remote + current_sha="$(git rev-parse --verify --short master)" + remote_sha="$(git rev-parse --verify --short origin/master)" + + assert_equal $current_sha $remote_sha +} + +@test "It pushes generated commit and tag to remote branch and updates commit sha" { + # Create "a-new-branch"-branch and then immediately switch back to master + git checkout -b a-new-branch + git checkout master + + INPUT_BRANCH="a-new-branch" + INPUT_TAGGING_MESSAGE="v2.0.0" + + touch "${FAKE_LOCAL_REPOSITORY}"/new-file-{1,2,3}.txt + + run git_auto_commit + + assert_success + + assert_line "INPUT_TAGGING_MESSAGE: v2.0.0" + assert_line "::debug::Create tag v2.0.0" + assert_line "::debug::Push commit to remote branch a-new-branch" + + # Assert a tag v2.0.0 has been created + run git tag + assert_output v2.0.0 + + # Assert tag v2.0.0 has been pushed to remote + run git ls-remote --tags --refs + assert_output --partial refs/tags/v2.0.0 + + # Assert that branch "a-new-branch" was updated on remote + current_sha="$(git rev-parse --verify --short a-new-branch)" + remote_sha="$(git rev-parse --verify --short origin/a-new-branch)" + + assert_equal $current_sha $remote_sha +} From 9bb0fd232468a9ace186bd1fbbf057fd00d38e5a Mon Sep 17 00:00:00 2001 From: Stefan Zweifel Date: Fri, 26 Feb 2021 20:17:32 +0100 Subject: [PATCH 2/2] =?UTF-8?q?Push=20tags=20by=20using=20=E2=80=94follow-?= =?UTF-8?q?tags=20and=20=E2=80=94atomic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit By using —follow-tags we push all annotated tags alongside the commit to the remote repository. In addition, we’re using —atomic. This ensures that all refs are updated. - https://github.blog/2015-04-30-git-2-4-atomic-pushes-push-to-deploy-and-more/#atomic-pushes - https://stackoverflow.com/a/3745250 - https://therightstuff.medium.com/the-rights-and-wrongs-of-git-push-with-tags-998667eaed8f --- entrypoint.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 0d11b030..8449b918 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -104,7 +104,7 @@ _push_to_github() { if [ -n "$INPUT_TAGGING_MESSAGE" ] then echo "::debug::git push origin --tags"; - git push origin --tags ${INPUT_PUSH_OPTIONS:+"${INPUT_PUSH_OPTIONS_ARRAY[@]}"}; + git push origin --follow-tags --atomic ${INPUT_PUSH_OPTIONS:+"${INPUT_PUSH_OPTIONS_ARRAY[@]}"}; else echo "::debug::git push origin"; git push origin ${INPUT_PUSH_OPTIONS:+"${INPUT_PUSH_OPTIONS_ARRAY[@]}"}; @@ -112,7 +112,7 @@ _push_to_github() { else echo "::debug::Push commit to remote branch $INPUT_BRANCH"; - git push --set-upstream origin "HEAD:$INPUT_BRANCH" --tags ${INPUT_PUSH_OPTIONS:+"${INPUT_PUSH_OPTIONS_ARRAY[@]}"}; + git push --set-upstream origin "HEAD:$INPUT_BRANCH" --follow-tags --atomic ${INPUT_PUSH_OPTIONS:+"${INPUT_PUSH_OPTIONS_ARRAY[@]}"}; fi }