Merge pull request #140 from stefanzweifel/feature/create-new-branch-during-checkout

Feature: Create a new branch, if the given branch name doesn't exist yet
This commit is contained in:
Stefan Zweifel 2021-02-20 20:29:23 +01:00 committed by GitHub
commit 9abc4c41e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 3 deletions

View File

@ -48,9 +48,28 @@ _switch_to_branch() {
git fetch --depth=1; git fetch --depth=1;
fi fi
# Switch to branch from current Workflow run
# shellcheck disable=SC2086 # If INPUT_BRANCH is empty, just run `git checkout`
git checkout $INPUT_BRANCH; if [ -z "$INPUT_BRANCH" ]
then
# shellcheck disable=SC2086
git checkout $INPUT_BRANCH;
else
# 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`
# shellcheck disable=SC2086
if [ -n "$(git branch --list $INPUT_BRANCH)" ]
then
# shellcheck disable=SC2086
git checkout $INPUT_BRANCH;
else
# shellcheck disable=SC2086
git checkout -b $INPUT_BRANCH;
fi
fi
} }
_add_files() { _add_files() {

View File

@ -331,3 +331,25 @@ git_auto_commit() {
assert_line "::debug::git-fetch has not been executed" 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
}