|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -eu |
| 4 | + |
| 5 | +if "$INPUT_DISABLE_GLOBBING"; then |
| 6 | + set -o noglob; |
| 7 | +fi |
| 8 | + |
| 9 | +_set_github_output() { |
| 10 | + local name=${1} |
| 11 | + local value=${2} |
| 12 | + |
| 13 | + # Check if $GITHUB_OUTPUT is available |
| 14 | + # (Feature detection will be removed in spring 2023) |
| 15 | + if [ -z ${GITHUB_OUTPUT+x} ]; then |
| 16 | + echo "::set-output name=$name::$value"; |
| 17 | + else |
| 18 | + echo "$name=$value" >> $GITHUB_OUTPUT; |
| 19 | + fi |
| 20 | +} |
| 21 | + |
| 22 | +_log() { |
| 23 | + local level=${1} |
| 24 | + local message=${2} |
| 25 | + |
| 26 | + echo "::$level::$message"; |
| 27 | +} |
| 28 | + |
| 29 | +_main() { |
| 30 | + _check_if_git_is_available |
| 31 | + |
| 32 | + _switch_to_repository |
| 33 | + if "$INPUT_CREATE_GIT_TAG_ONLY"; then |
| 34 | + _log "debug" "Create git tag only"; |
| 35 | + _set_github_output "create_git_tag_only" "true" |
| 36 | + _tag_commit |
| 37 | + _push_to_github |
| 38 | + elif _git_is_dirty || "$INPUT_SKIP_DIRTY_CHECK"; then |
| 39 | + |
| 40 | + _set_github_output "changes_detected" "true" |
| 41 | + |
| 42 | + _switch_to_branch |
| 43 | + |
| 44 | + _add_files |
| 45 | + |
| 46 | + # Check dirty state of repo again using git-diff. |
| 47 | + # (git-diff detects better if CRLF of files changes and does NOT |
| 48 | + # proceed, if only CRLF changes are detected. See #241 and #265 |
| 49 | + # for more details.) |
| 50 | + if [ -n "$(git diff --staged)" ] || "$INPUT_SKIP_DIRTY_CHECK"; then |
| 51 | + _local_commit |
| 52 | + |
| 53 | + _tag_commit |
| 54 | + |
| 55 | + _push_to_github |
| 56 | + else |
| 57 | + _set_github_output "changes_detected" "false" |
| 58 | + |
| 59 | + echo "Working tree clean. Nothing to commit."; |
| 60 | + fi |
| 61 | + else |
| 62 | + _set_github_output "changes_detected" "false" |
| 63 | + |
| 64 | + echo "Working tree clean. Nothing to commit."; |
| 65 | + fi |
| 66 | +} |
| 67 | + |
| 68 | +_check_if_git_is_available() { |
| 69 | + if hash -- "$INPUT_INTERNAL_GIT_BINARY" 2> /dev/null; then |
| 70 | + _log "debug" "git binary found."; |
| 71 | + else |
| 72 | + _log "error" "git-auto-commit could not find git binary. Please make sure git is available." |
| 73 | + exit 1; |
| 74 | + fi |
| 75 | +} |
| 76 | + |
| 77 | +_switch_to_repository() { |
| 78 | + echo "INPUT_REPOSITORY value: $INPUT_REPOSITORY"; |
| 79 | + cd "$INPUT_REPOSITORY"; |
| 80 | +} |
| 81 | + |
| 82 | +_git_is_dirty() { |
| 83 | + echo "INPUT_STATUS_OPTIONS: ${INPUT_STATUS_OPTIONS}"; |
| 84 | + _log "debug" "Apply status options ${INPUT_STATUS_OPTIONS}"; |
| 85 | + |
| 86 | + echo "INPUT_FILE_PATTERN: ${INPUT_FILE_PATTERN}"; |
| 87 | + read -r -a INPUT_FILE_PATTERN_EXPANDED <<< "$INPUT_FILE_PATTERN"; |
| 88 | + |
| 89 | + # capture stderr |
| 90 | + gitStatusMessage="$((git status -s $INPUT_STATUS_OPTIONS -- ${INPUT_FILE_PATTERN_EXPANDED:+${INPUT_FILE_PATTERN_EXPANDED[@]}} >/dev/null ) 2>&1)"; |
| 91 | + # shellcheck disable=SC2086 |
| 92 | + gitStatus="$(git status -s $INPUT_STATUS_OPTIONS -- ${INPUT_FILE_PATTERN_EXPANDED:+${INPUT_FILE_PATTERN_EXPANDED[@]}})"; |
| 93 | + if [ $? -ne 0 ]; then |
| 94 | + _log "error" "git-status failed with:<$gitStatusMessage>"; |
| 95 | + exit 1; |
| 96 | + fi |
| 97 | + [ -n "$gitStatus" ] |
| 98 | +} |
| 99 | +
|
| 100 | +_switch_to_branch() { |
| 101 | + echo "INPUT_BRANCH value: $INPUT_BRANCH"; |
| 102 | +
|
| 103 | + # Fetch remote to make sure that repo can be switched to the right branch. |
| 104 | + if "$INPUT_SKIP_FETCH"; then |
| 105 | + _log "debug" "git-fetch will not be executed."; |
| 106 | + else |
| 107 | + git fetch --depth=1; |
| 108 | + fi |
| 109 | +
|
| 110 | + # If `skip_checkout`-input is true, skip the entire checkout step. |
| 111 | + if "$INPUT_SKIP_CHECKOUT"; then |
| 112 | + _log "debug" "git-checkout will not be executed."; |
| 113 | + else |
| 114 | + # Create new local branch if `create_branch`-input is true |
| 115 | + if "$INPUT_CREATE_BRANCH"; then |
| 116 | + # shellcheck disable=SC2086 |
| 117 | + git checkout -B $INPUT_BRANCH --; |
| 118 | + else |
| 119 | + # Switch to branch from current Workflow run |
| 120 | + # shellcheck disable=SC2086 |
| 121 | + git checkout $INPUT_BRANCH --; |
| 122 | + fi |
| 123 | + fi |
| 124 | +} |
| 125 | +
|
| 126 | +_add_files() { |
| 127 | + echo "INPUT_ADD_OPTIONS: ${INPUT_ADD_OPTIONS}"; |
| 128 | + _log "debug" "Apply add options ${INPUT_ADD_OPTIONS}"; |
| 129 | +
|
| 130 | + echo "INPUT_FILE_PATTERN: ${INPUT_FILE_PATTERN}"; |
| 131 | + read -r -a INPUT_FILE_PATTERN_EXPANDED <<< "$INPUT_FILE_PATTERN"; |
| 132 | +
|
| 133 | + # shellcheck disable=SC2086 |
| 134 | + git add ${INPUT_ADD_OPTIONS} ${INPUT_FILE_PATTERN:+"${INPUT_FILE_PATTERN_EXPANDED[@]}"}; |
| 135 | +} |
| 136 | +
|
| 137 | +_local_commit() { |
| 138 | + echo "INPUT_COMMIT_OPTIONS: ${INPUT_COMMIT_OPTIONS}"; |
| 139 | + _log "debug" "Apply commit options ${INPUT_COMMIT_OPTIONS}"; |
| 140 | +
|
| 141 | + # shellcheck disable=SC2206 |
| 142 | + INPUT_COMMIT_OPTIONS_ARRAY=( $INPUT_COMMIT_OPTIONS ); |
| 143 | +
|
| 144 | + echo "INPUT_COMMIT_USER_NAME: ${INPUT_COMMIT_USER_NAME}"; |
| 145 | + echo "INPUT_COMMIT_USER_EMAIL: ${INPUT_COMMIT_USER_EMAIL}"; |
| 146 | + echo "INPUT_COMMIT_MESSAGE: ${INPUT_COMMIT_MESSAGE}"; |
| 147 | + echo "INPUT_COMMIT_AUTHOR: ${INPUT_COMMIT_AUTHOR}"; |
| 148 | +
|
| 149 | + git -c user.name="$INPUT_COMMIT_USER_NAME" -c user.email="$INPUT_COMMIT_USER_EMAIL" \ |
| 150 | + commit -m "$INPUT_COMMIT_MESSAGE" \ |
| 151 | + --author="$INPUT_COMMIT_AUTHOR" \ |
| 152 | + ${INPUT_COMMIT_OPTIONS:+"${INPUT_COMMIT_OPTIONS_ARRAY[@]}"}; |
| 153 | +
|
| 154 | + _set_github_output "commit_hash" $(git rev-parse HEAD) |
| 155 | +} |
| 156 | +
|
| 157 | +_tag_commit() { |
| 158 | + echo "INPUT_TAGGING_MESSAGE: ${INPUT_TAGGING_MESSAGE}" |
| 159 | +
|
| 160 | + if [ -n "$INPUT_TAGGING_MESSAGE" ] |
| 161 | + then |
| 162 | + _log "debug" "Create tag $INPUT_TAGGING_MESSAGE"; |
| 163 | + git -c user.name="$INPUT_COMMIT_USER_NAME" -c user.email="$INPUT_COMMIT_USER_EMAIL" tag -a "$INPUT_TAGGING_MESSAGE" -m "$INPUT_TAGGING_MESSAGE"; |
| 164 | + else |
| 165 | + echo "No tagging message supplied. No tag will be added."; |
| 166 | + fi |
| 167 | +} |
| 168 | +
|
| 169 | +_push_to_github() { |
| 170 | +
|
| 171 | + echo "INPUT_PUSH_OPTIONS: ${INPUT_PUSH_OPTIONS}"; |
| 172 | + _log "debug" "Apply push options ${INPUT_PUSH_OPTIONS}"; |
| 173 | +
|
| 174 | + # shellcheck disable=SC2206 |
| 175 | + INPUT_PUSH_OPTIONS_ARRAY=( $INPUT_PUSH_OPTIONS ); |
| 176 | +
|
| 177 | + if [ -z "$INPUT_BRANCH" ] |
| 178 | + then |
| 179 | + # Only add `--tags` option, if `$INPUT_TAGGING_MESSAGE` is set |
| 180 | + if [ -n "$INPUT_TAGGING_MESSAGE" ] |
| 181 | + then |
| 182 | + _log "debug" "git push origin --tags"; |
| 183 | + git push origin --follow-tags --atomic ${INPUT_PUSH_OPTIONS:+"${INPUT_PUSH_OPTIONS_ARRAY[@]}"}; |
| 184 | + else |
| 185 | + _log "debug" "git push origin"; |
| 186 | + git push origin ${INPUT_PUSH_OPTIONS:+"${INPUT_PUSH_OPTIONS_ARRAY[@]}"}; |
| 187 | + fi |
| 188 | +
|
| 189 | + else |
| 190 | + _log "debug" "Push commit to remote branch $INPUT_BRANCH"; |
| 191 | + git push --set-upstream origin "HEAD:$INPUT_BRANCH" --follow-tags --atomic ${INPUT_PUSH_OPTIONS:+"${INPUT_PUSH_OPTIONS_ARRAY[@]}"}; |
| 192 | + fi |
| 193 | +} |
| 194 | +
|
| 195 | +_main |
0 commit comments