Skip to content

Commit

Permalink
Merge pull request #156 from funkjedi/status-add-options
Browse files Browse the repository at this point in the history
Add options for git add/status commands
  • Loading branch information
stefanzweifel authored May 3, 2021
2 parents 48d37c1 + ccad859 commit 68f0d95
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
8 changes: 8 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ inputs:
description: Commit options (eg. --no-verify)
required: false
default: ''
add_options:
description: Add options (eg. -u)
required: false
default: ''
status_options:
description: Status options (eg. --untracked-files=no)
required: false
default: ''
file_pattern:
description: File pattern used for `git add`. For example `src/\*.js`
required: false
Expand Down
10 changes: 8 additions & 2 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ _switch_to_repository() {
}

_git_is_dirty() {
echo "INPUT_STATUS_OPTIONS: ${INPUT_STATUS_OPTIONS}";
echo "::debug::Apply status options ${INPUT_STATUS_OPTIONS}";

# shellcheck disable=SC2086
[ -n "$(git status -s -- $INPUT_FILE_PATTERN)" ]
[ -n "$(git status -s $INPUT_STATUS_OPTIONS -- $INPUT_FILE_PATTERN)" ]
}

_switch_to_branch() {
Expand All @@ -58,10 +61,13 @@ _switch_to_branch() {
}

_add_files() {
echo "INPUT_ADD_OPTIONS: ${INPUT_ADD_OPTIONS}";
echo "::debug::Apply add options ${INPUT_ADD_OPTIONS}";

echo "INPUT_FILE_PATTERN: ${INPUT_FILE_PATTERN}";

# shellcheck disable=SC2086
git add ${INPUT_FILE_PATTERN};
git add ${INPUT_ADD_OPTIONS} ${INPUT_FILE_PATTERN};
}

_local_commit() {
Expand Down
38 changes: 38 additions & 0 deletions tests/git-auto-commit.bats
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ setup() {
export INPUT_COMMIT_MESSAGE="Commit Message"
export INPUT_BRANCH="master"
export INPUT_COMMIT_OPTIONS=""
export INPUT_ADD_OPTIONS=""
export INPUT_STATUS_OPTIONS=""
export INPUT_FILE_PATTERN="."
export INPUT_COMMIT_USER_NAME="Test Suite"
export INPUT_COMMIT_USER_EMAIL="[email protected]"
Expand Down Expand Up @@ -115,6 +117,20 @@ git_auto_commit() {
assert_line "::debug::Push commit to remote branch master"
}

@test "It applies INPUT_STATUS_OPTIONS when running dirty check" {
INPUT_STATUS_OPTIONS="--untracked-files=no"

touch "${FAKE_LOCAL_REPOSITORY}"/new-file-{1,2}.php

run git_auto_commit

assert_success

assert_line "INPUT_REPOSITORY value: ${INPUT_REPOSITORY}"
assert_line "::set-output name=changes_detected::false"
assert_line "Working tree clean. Nothing to commit."
}

@test "It prints a 'Nothing to commit' message in a clean repository" {
run git_auto_commit

Expand Down Expand Up @@ -142,6 +158,28 @@ git_auto_commit() {
assert_line "::debug::Apply commit options "
}

@test "It applies INPUT_ADD_OPTIONS when adding files" {
INPUT_FILE_PATTERN=""
INPUT_STATUS_OPTIONS="--untracked-files=no"
INPUT_ADD_OPTIONS="-u"

date > "${FAKE_LOCAL_REPOSITORY}"/remote-files1.txt
touch "${FAKE_LOCAL_REPOSITORY}"/new-file-{1,2}.php

run git_auto_commit

assert_success

assert_line "INPUT_STATUS_OPTIONS: --untracked-files=no"
assert_line "INPUT_ADD_OPTIONS: -u"
assert_line "INPUT_FILE_PATTERN: "
assert_line "::debug::Push commit to remote branch master"

# Assert that PHP files have not been added.
run git status
assert_output --partial 'new-file-1.php'
}

@test "It applies INPUT_FILE_PATTERN when creating commit" {
INPUT_FILE_PATTERN="*.txt *.html"

Expand Down

0 comments on commit 68f0d95

Please sign in to comment.