Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: stefanzweifel/git-auto-commit-action
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v4.13.1
Choose a base ref
...
head repository: stefanzweifel/git-auto-commit-action
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v4.14.0
Choose a head ref
  • 8 commits
  • 5 files changed
  • 3 contributors

Commits on Jan 13, 2022

  1. Update CHANGELOG

    stefanzweifel authored and actions-user committed Jan 13, 2022

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    8f67368 View commit details

Commits on Jan 24, 2022

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    22695fb View commit details
  2. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    2d5591a View commit details
  3. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    248fbac View commit details

Commits on Feb 22, 2022

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    dee58f4 View commit details

Commits on Feb 27, 2022

  1. Add create_branch option to force create a new branch (#203)

    * Add create_branch option
    
    * Checkout new branch if create_branch input is true
    
    * Add tests
    
    * Update README
    stefanzweifel authored Feb 27, 2022

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    ebe7739 View commit details
  2. Update README

    stefanzweifel committed Feb 27, 2022

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    19c7b84 View commit details
  3. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    c4b132e View commit details
Showing with 311 additions and 29 deletions.
  1. +7 −1 CHANGELOG.md
  2. +27 −20 README.md
  3. +4 −1 action.yml
  4. +11 −5 entrypoint.sh
  5. +262 −2 tests/git-auto-commit.bats
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -5,10 +5,16 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased](https://github.com/stefanzweifel/git-auto-commit-action/compare/v4.13.0...HEAD)
## [Unreleased](https://github.com/stefanzweifel/git-auto-commit-action/compare/v4.13.1...HEAD)

> TBD
## [v4.13.1](https://github.com/stefanzweifel/git-auto-commit-action/compare/v4.13.0...v4.13.1) - 2022-01-13

## Fixed

- Properly disambiguate between branch or file checkout (#199) @kenodegard

## [v4.13.0](https://github.com/stefanzweifel/git-auto-commit-action/compare/v4.12.0...v4.13.0) - 2022-01-10

## Added
47 changes: 27 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -28,15 +28,16 @@ The following is an extended example with all possible options available for thi
```yaml
- uses: stefanzweifel/git-auto-commit-action@v4
with:
# Optional, but recommended
# Optional. Commit message for the created commit.
# Defaults to "Apply automatic changes"
commit_message: Automated Change
# Optional branch name where commit should be pushed to.
# Defaults to the current branch.
# Optional. Local and remote branch name where commit is going to be pushed
# to. Defaults to the current branch.
# You might need to set `create_branch: true` if the branch does not exist.
branch: feature-123

# Optional. Used by `git-commit`.
# Optional. Options used by `git-commit`.
# See https://git-scm.com/docs/git-commit#_options
commit_options: '--no-verify --signoff'

@@ -47,28 +48,28 @@ The following is an extended example with all possible options available for thi
# - https://git-scm.com/docs/gitglossary#Documentation/gitglossary.txt-aiddefpathspecapathspec
file_pattern: src/*.js tests/*.js *.php

# Optional local file path to the repository
# Defaults to the root of the repository
# Optional. Local file path to the repository.
# Defaults to the root of the repository.
repository: .

# Optional commit user and author settings
commit_user_name: My GitHub Actions Bot # defaults to "GitHub Actions"
commit_user_email: my-github-actions-bot@example.org # defaults to "actions@github.com"
commit_author: Author <actions@github.com> # defaults to author of the commit that triggered the run

# Optional tag message
# Action will create and push a new tag to the remote repository and the defined branch
# Optional. Tag name being created in the local repository and
# pushed to remtoe repository and defined branch.
tagging_message: 'v1.0.0'

# Optional. Used by `git-status`
# See https://git-scm.com/docs/git-status#_options
# Optional. Option used by `git-status` to determine if the repository is
# dirty. See https://git-scm.com/docs/git-status#_options
status_options: '--untracked-files=no'

# Optional. Used by `git-add`
# Optional. Options used by `git-add`.
# See https://git-scm.com/docs/git-add#_options
add_options: '-u'

# Optional. Used by `git-push`
# Optional. Options used by `git-push`.
# See https://git-scm.com/docs/git-push#_options
push_options: '--force'

@@ -84,6 +85,10 @@ The following is an extended example with all possible options available for thi
# Optional. Prevents the shell from expanding filenames.
# Details: https://www.gnu.org/software/bash/manual/html_node/Filename-Expansion.html
disable_globbing: true

# Optional. Create given branch name in local and remote repository.
# (Not released yet, available when @master is used)
create_branch: true
```
Please note that the Action depends on `bash`. If you're using the Action in a job in combination with a custom Docker container, make sure that `bash` is installed.
@@ -102,7 +107,7 @@ on:
pull_request:
push:
branches:
- "main"
- main
jobs:
php-cs-fixer:
@@ -150,31 +155,31 @@ The goal of this Action is to be "the Action for committing files for the 80% us

The following is a list of edge cases the Action knowingly does not support:

**No `git pull` when the repository is out of the date with remote.** The will not do a `git pull` before doing the `git push`. **You** are responsible for keeping the repository up to date in your Workflow runs.
**No `git pull` when the repository is out of the date with remote.** The Action will not do a `git pull` before doing the `git push`. **You** are responsible for keeping the repository up to date in your Workflow runs.

**No support for running the Action in build matrices**. For example, if your Workflow is using build matrices, and you want that each job commits and pushes files to the remote, you will run into the issue, that the repository in the workflow will become out of date. As the Action will not do a `git pull` for you, you have to do that yourself.
**No support for running the Action in build matrices**. If your Workflow is using build matrices, and you want that each job commits and pushes files to the remote, you will run into the issue, that the repository in the workflow will become out of date. As the Action will not do a `git pull` for you, you have to do that yourself.

**No support for `git rebase` or `git merge`**. There are many strategies on how to integrate remote upstream changes to a local repository. `git-auto-commit` does not want to be responsible for doing that.

If this Action doesn't work for your workflow, check out [EndBug/add-and-commit](https://github.com/EndBug/add-and-commit).

### Checkout the correct branch

You must use `action/checkout@v2` or later versions to checkout the repository.
In non-`push` events, such as `pull_request`, make sure to specify the `ref` to checkout:
You must use `action/checkout@v2` or later versions to check out the repository.
In non-`push` events, such as `pull_request`, make sure to specify the `ref` to check out:

```yaml
- uses: actions/checkout@v2
with:
ref: ${{ github.head_ref }}
```

You have to do this to avoid that the `checkout`-Action clones your repository in a detached state.
Do this to avoid checking out the repository in a detached state.

### Commits made by this Action do not trigger new Workflow runs

The resulting commit **will not trigger** another GitHub Actions Workflow run.
This is due to [limitations set by GitHub](https://help.github.com/en/actions/reference/events-that-trigger-workflows#triggering-new-workflows-using-a-personal-access-token).
This is due to [limitations set by GitHub](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#triggering-a-workflow-from-a-workflow).

> When you use the repository's GITHUB_TOKEN to perform tasks on behalf of the GitHub Actions app, events triggered by the GITHUB_TOKEN will not create a new workflow run. This prevents you from accidentally creating recursive workflow runs.

@@ -187,6 +192,8 @@ storing the token as a secret in your repository and then passing the new token
token: ${{ secrets.PAT }}
```

If you create a personal access token, apply the `repo` and `workflow` scopes.

If you work in an organization and don't want to create a PAT from your personal account, we recommend using a [robot account](https://docs.github.com/en/github/getting-started-with-github/types-of-github-accounts) for the token.

## Advanced Uses
@@ -356,7 +363,7 @@ This is due to limitations set up by GitHub, [commits of this Action do not trig

## Running the tests

The package has tests written in [bats](https://github.com/bats-core/bats-core). Before you can run the test suite locally, you have to install the dependencies with `npm` or `yarn`.
The Action has tests written in [bats](https://github.com/bats-core/bats-core). Before you can run the test suite locally, you have to install the dependencies with `npm` or `yarn`.

```shell
npm install
5 changes: 4 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
@@ -25,7 +25,7 @@ inputs:
required: false
default: ''
file_pattern:
description: File pattern used for `git add`. For example `src/\*.js`
description: File pattern used for `git add`. For example `src/*.js`
required: false
default: '.'
repository:
@@ -67,6 +67,9 @@ inputs:
disable_globbing:
description: Stop the shell from expanding filenames (https://www.gnu.org/software/bash/manual/html_node/Filename-Expansion.html)
default: false
create_branch:
description: Create new branch with the name of `branch`-input in local and remote repository, if it doesn't exist yet.
default: false

outputs:
changes_detected:
16 changes: 11 additions & 5 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -47,20 +47,26 @@ _git_is_dirty() {
_switch_to_branch() {
echo "INPUT_BRANCH value: $INPUT_BRANCH";

# Fetch remote to make sure that repo can be switched to the right branch.

# Fetch remote to make sure that repo can be switched to the right branch.
if "$INPUT_SKIP_FETCH"; then
echo "::debug::git-fetch has not been executed";
else
git fetch --depth=1;
fi

# If `skip_checkout`-input is true, skip the entire checkout step.
if "$INPUT_SKIP_CHECKOUT"; then
echo "::debug::git-checkout has not been executed";
else
# Switch to branch from current Workflow run
# shellcheck disable=SC2086
git checkout $INPUT_BRANCH --;
# Create new local branch if `create_branch`-input is true
if "$INPUT_CREATE_BRANCH"; then
# shellcheck disable=SC2086
git checkout -B $INPUT_BRANCH --;
else
# Switch to branch from current Workflow run
# shellcheck disable=SC2086
git checkout $INPUT_BRANCH --;
fi
fi
}

264 changes: 262 additions & 2 deletions tests/git-auto-commit.bats
Original file line number Diff line number Diff line change
@@ -26,6 +26,7 @@ setup() {
export INPUT_SKIP_FETCH=false
export INPUT_SKIP_CHECKOUT=false
export INPUT_DISABLE_GLOBBING=false
export INPUT_CREATE_BRANCH=false

# Configure Git
if [[ -z $(git config user.name) ]]; then
@@ -185,7 +186,10 @@ git_auto_commit() {
}

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

mkdir src;
touch src/new-file-{1,2}.js;

touch "${FAKE_LOCAL_REPOSITORY}"/new-file-{1,2}.php
touch "${FAKE_LOCAL_REPOSITORY}"/new-file-{1,2}.html
@@ -194,7 +198,7 @@ git_auto_commit() {

assert_success

assert_line "INPUT_FILE_PATTERN: *.txt *.html"
assert_line "INPUT_FILE_PATTERN: src/*.js *.txt *.html"
assert_line "::debug::Push commit to remote branch master"

# Assert that PHP files have not been added.
@@ -533,3 +537,259 @@ git_auto_commit() {
assert_line "::set-output name=changes_detected::true"
assert_line "::debug::Push commit to remote branch dev"
}

@test "script fails to push commit to new branch that does not exist yet" {
INPUT_BRANCH="not-existend-branch"
INPUT_CREATE_BRANCH=false

run git branch
refute_line --partial "not-existend-branch"

run git branch -r
refute_line --partial "origin/not-existend-branch"

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

run git_auto_commit

assert_failure

assert_line "INPUT_REPOSITORY value: ${INPUT_REPOSITORY}"
assert_line "::set-output name=changes_detected::true"
assert_line "INPUT_BRANCH value: not-existend-branch"
assert_line "fatal: invalid reference: not-existend-branch"

run git branch
refute_line --partial "not-existend-branch"

run git branch -r
refute_line --partial "origin/not-existend-branch"
}

@test "It creates new local branch and pushes the new branch to remote" {
INPUT_BRANCH="not-existend-branch"
INPUT_CREATE_BRANCH=true

run git branch
refute_line --partial "not-existend-branch"

run git branch -r
refute_line --partial "origin/not-existend-branch"

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

run git_auto_commit

assert_success

assert_line "INPUT_REPOSITORY value: ${INPUT_REPOSITORY}"
assert_line "::set-output name=changes_detected::true"
assert_line -e "::set-output name=commit_hash::[0-9a-f]{40}$"
assert_line "INPUT_BRANCH value: not-existend-branch"
assert_line "INPUT_FILE_PATTERN: ."
assert_line "INPUT_COMMIT_OPTIONS: "
assert_line "::debug::Apply commit options "
assert_line "INPUT_TAGGING_MESSAGE: "
assert_line "No tagging message supplied. No tag will be added."
assert_line "INPUT_PUSH_OPTIONS: "
assert_line "::debug::Apply push options "
assert_line "::debug::Push commit to remote branch not-existend-branch"

run git branch
assert_line --partial "not-existend-branch"

run git branch -r
assert_line --partial "origin/not-existend-branch"
}

@test "it does not create new local branch if local branch already exists" {

git checkout -b not-existend-remote-branch
git checkout master

INPUT_BRANCH="not-existend-remote-branch"
INPUT_CREATE_BRANCH=true

run git branch
assert_line --partial "not-existend-remote-branch"

run git branch -r
refute_line --partial "origin/not-existend-remote-branch"

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

run git_auto_commit

assert_success

assert_line "INPUT_REPOSITORY value: ${INPUT_REPOSITORY}"
assert_line "::set-output name=changes_detected::true"
assert_line -e "::set-output name=commit_hash::[0-9a-f]{40}$"
assert_line "INPUT_BRANCH value: not-existend-remote-branch"
assert_line "INPUT_FILE_PATTERN: ."
assert_line "INPUT_COMMIT_OPTIONS: "
assert_line "::debug::Apply commit options "
assert_line "INPUT_TAGGING_MESSAGE: "
assert_line "No tagging message supplied. No tag will be added."
assert_line "INPUT_PUSH_OPTIONS: "
assert_line "::debug::Apply push options "
assert_line "::debug::Push commit to remote branch not-existend-remote-branch"

run git branch
assert_line --partial "not-existend-remote-branch"

run git branch -r
assert_line --partial "origin/not-existend-remote-branch"
}

@test "it creates new local branch and pushes branch to remote even if the remote branch already exists" {

# Create `existing-remote-branch` on remote with changes the local repository does not yet have
cd $FAKE_TEMP_LOCAL_REPOSITORY;
git checkout -b "existing-remote-branch"
touch new-branch-file.txt
git add new-branch-file.txt
git commit -m "Add additional file";
git push origin existing-remote-branch;

run git branch;
assert_line --partial "existing-remote-branch"

# ---------
# Switch to our regular local repository and run `git-auto-commit`
cd $FAKE_LOCAL_REPOSITORY;

INPUT_BRANCH="existing-remote-branch"
INPUT_CREATE_BRANCH=true

run git branch
refute_line --partial "existing-remote-branch"

run git fetch --all;
run git pull origin existing-remote-branch;
run git branch -r;
assert_line --partial "origin/existing-remote-branch"

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

run git_auto_commit

assert_success

assert_line "INPUT_REPOSITORY value: ${INPUT_REPOSITORY}"
assert_line "::set-output name=changes_detected::true"
assert_line -e "::set-output name=commit_hash::[0-9a-f]{40}$"
assert_line "INPUT_BRANCH value: existing-remote-branch"
assert_line "INPUT_FILE_PATTERN: ."
assert_line "INPUT_COMMIT_OPTIONS: "
assert_line "::debug::Apply commit options "
assert_line "INPUT_TAGGING_MESSAGE: "
assert_line "No tagging message supplied. No tag will be added."
assert_line "INPUT_PUSH_OPTIONS: "
assert_line "::debug::Apply push options "
assert_line "::debug::Push commit to remote branch existing-remote-branch"

run git branch
assert_line --partial "existing-remote-branch"

run git branch -r
assert_line --partial "origin/existing-remote-branch"

# Assert that branch "existing-remote-branch" was updated on remote
current_sha="$(git rev-parse --verify --short existing-remote-branch)"
remote_sha="$(git rev-parse --verify --short origin/existing-remote-branch)"

assert_equal $current_sha $remote_sha;
}

@test "script fails if new local branch is checked out and push fails as remote has newer commits than local" {
# Create `existing-remote-branch` on remote with changes the local repository does not yet have
cd $FAKE_TEMP_LOCAL_REPOSITORY;
git checkout -b "existing-remote-branch"
touch new-branch-file.txt
git add new-branch-file.txt
git commit -m "Add additional file";
git push origin existing-remote-branch;

run git branch;
assert_line --partial "existing-remote-branch"

# ---------
# Switch to our regular local repository and run `git-auto-commit`
cd $FAKE_LOCAL_REPOSITORY;

INPUT_BRANCH="existing-remote-branch"
INPUT_CREATE_BRANCH=true

run git branch
refute_line --partial "existing-remote-branch"

run git fetch --all;
run git branch -r;
assert_line --partial "origin/existing-remote-branch"

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

run git_auto_commit

assert_failure

assert_line "hint: Updates were rejected because the tip of your current branch is behind"

# Assert that branch exists locally and on remote
run git branch
assert_line --partial "existing-remote-branch"

run git branch -r
assert_line --partial "origin/existing-remote-branch"

# Assert that branch "existing-remote-branch" was not updated on remote
current_sha="$(git rev-parse --verify --short existing-remote-branch)"
remote_sha="$(git rev-parse --verify --short origin/existing-remote-branch)"

refute [assert_equal $current_sha $remote_sha];
}

@test "It pushes commit to remote if branch already exists and local repo is behind its remote counterpart" {
# Create `new-branch` on remote with changes the local repository does not yet have
cd $FAKE_TEMP_LOCAL_REPOSITORY;

git checkout -b "new-branch"
touch new-branch-file.txt
git add new-branch-file.txt

git commit --quiet -m "Add additional file";
git push origin new-branch;

run git branch -r
assert_line --partial "origin/new-branch"

# ---------
# Switch to our regular local repository and run `git-auto-commit`
cd $FAKE_LOCAL_REPOSITORY;

INPUT_BRANCH="new-branch"

# Assert that local remote does not know have "new-branch" locally nor does
# know about the remote branch.
run git branch
refute_line --partial "new-branch"

run git branch -r
refute_line --partial "origin/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;
}