From 8056699617f30526b19f5283f6310d5113e4ffd7 Mon Sep 17 00:00:00 2001 From: Stefan Zweifel Date: Sun, 7 Feb 2021 15:44:12 +0100 Subject: [PATCH 1/5] Add Testcase --- tests/git-auto-commit.bats | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/git-auto-commit.bats b/tests/git-auto-commit.bats index 94e2357..0479731 100644 --- a/tests/git-auto-commit.bats +++ b/tests/git-auto-commit.bats @@ -331,3 +331,25 @@ git_auto_commit() { assert_line "::debug::git-fetch has not been executed" } + +@test "If INPUT_BRANCH is set and the branch does not exist it creates one" { + INPUT_BRANCH="new-branch" + + run git branch + refute_line "new-branch" + + touch "${FAKE_LOCAL_REPOSITORY}"/new-file-{1,2,3}.txt + + run git_auto_commit + + assert_success + + assert_line "INPUT_BRANCH value: new-branch" + assert_line --partial "::debug::Push commit to remote branch new-branch" + + # Assert that branch "new-branch" was updated on remote + current_sha="$(git rev-parse --verify --short new-branch)" + remote_sha="$(git rev-parse --verify --short origin/new-branch)" + + assert_equal $current_sha $remote_sha +} From 53438d58ce6383842b60d7ce86be286429d4253d Mon Sep 17 00:00:00 2001 From: Stefan Zweifel Date: Sun, 7 Feb 2021 15:44:48 +0100 Subject: [PATCH 2/5] Create new branch if NAME is given --- entrypoint.sh | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 0d11b03..3890c05 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -48,9 +48,40 @@ _switch_to_branch() { git fetch --depth=1; fi - # Switch to branch from current Workflow run - # shellcheck disable=SC2086 - git checkout $INPUT_BRANCH; + + + + # If INPUT_BRANCH is empty, just run `git checkout` + if [ -z "$INPUT_BRANCH" ] + then + + # Switch to branch from current Workflow run + # shellcheck disable=SC2086 + git checkout $INPUT_BRANCH; + + else + + # If INPUT_BRANCH is given, check if a branch already exists + # If not create one + + + if [ -n "$(git branch | grep $INPUT_BRANCH)" ] + then + + # Switch to branch from current Workflow run + # shellcheck disable=SC2086 + git checkout $INPUT_BRANCH; + + else + + # Switch to branch from current Workflow run + # shellcheck disable=SC2086 + git checkout -b $INPUT_BRANCH; + + fi + + fi + } _add_files() { From 7507738fca3b693f9acab619645052e320f693ed Mon Sep 17 00:00:00 2001 From: Stefan Zweifel Date: Sun, 7 Feb 2021 16:07:43 +0100 Subject: [PATCH 3/5] Use git branch --list --- entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index 3890c05..5986d5b 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -65,7 +65,7 @@ _switch_to_branch() { # If not create one - if [ -n "$(git branch | grep $INPUT_BRANCH)" ] + if [ -n "$(git branch --list $INPUT_BRANCH)" ] then # Switch to branch from current Workflow run From 94941cff883114143a7c3e07a8c1c8466ce26b60 Mon Sep 17 00:00:00 2001 From: Stefan Zweifel Date: Sun, 7 Feb 2021 16:09:59 +0100 Subject: [PATCH 4/5] Cleanup Code --- entrypoint.sh | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 5986d5b..bf0f078 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -49,37 +49,24 @@ _switch_to_branch() { fi - - # If INPUT_BRANCH is empty, just run `git checkout` if [ -z "$INPUT_BRANCH" ] then - - # Switch to branch from current Workflow run # shellcheck disable=SC2086 git checkout $INPUT_BRANCH; - else - - # If INPUT_BRANCH is given, check if a branch already exists - # If not create one - - + # If the branch which we should checkout already exists, just + # run `git checkout $INPUT_BRANCH` + # Otherwhise create a new branch by adding the `-b` option to + # `git-checkout` if [ -n "$(git branch --list $INPUT_BRANCH)" ] then - - # Switch to branch from current Workflow run # shellcheck disable=SC2086 git checkout $INPUT_BRANCH; - else - - # Switch to branch from current Workflow run # shellcheck disable=SC2086 git checkout -b $INPUT_BRANCH; - fi - fi } From 1aee6e6b463f527d8442705854b96d00063fb513 Mon Sep 17 00:00:00 2001 From: Stefan Zweifel Date: Sun, 7 Feb 2021 16:14:11 +0100 Subject: [PATCH 5/5] Make Linter Happy --- entrypoint.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/entrypoint.sh b/entrypoint.sh index bf0f078..3ab6ced 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -59,6 +59,7 @@ _switch_to_branch() { # run `git checkout $INPUT_BRANCH` # Otherwhise create a new branch by adding the `-b` option to # `git-checkout` + # shellcheck disable=SC2086 if [ -n "$(git branch --list $INPUT_BRANCH)" ] then # shellcheck disable=SC2086