stefanzweifel / git-auto-commit-action

Automatically commit and push changed files back to GitHub with this GitHub Action for the 80% use case.
MIT License
1.98k stars 227 forks source link

With "branch: master" raises "error: pathspec 'master' did not match any file(s) known to git" #141

Closed pablosjv closed 3 years ago

pablosjv commented 3 years ago

Version of the Action v4.8.0

Describe the bug

Setting the parameter branch: maser gives the following error:

Started: bash /home/runner/work/_actions/stefanzweifel/git-auto-commit-action/v4.8.0/entrypoint.sh
INPUT_REPOSITORY value: .
INPUT_BRANCH value: master
error: pathspec 'master' did not match any file(s) known to git
Error: Invalid status code: 1
    at ChildProcess.<anonymous> (/home/runner/work/_actions/stefanzweifel/git-auto-commit-action/v4.8.0/index.js:17:19)
    at ChildProcess.emit (events.js:210:5)
    at maybeClose (internal/child_process.js:1021:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:283:5) {
  code: 1
}
Error: Invalid status code: 1
    at ChildProcess.<anonymous> (/home/runner/work/_actions/stefanzweifel/git-auto-commit-action/v4.8.0/index.js:17:19)
    at ChildProcess.emit (events.js:210:5)
    at maybeClose (internal/child_process.js:1021:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:283:5)

To Reproduce

To reproduce, setup the following workflow and run it. When the branch: master parameter is removed, the workflow works as expected.

Used Workflow

name: refresh

on:
  push:
    branches: [master]

  workflow_dispatch:

jobs:
  refresh:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          ref: ${{ github.head_ref }}
          submodules: "recursive"

      - name: Install python and dependencies
        uses: actions/setup-python@v2
        with:
          python-version: "3.9" 

      # NOTE: here I have a script that creates new files. I simplify it for debugging.
      - name: Create a dummy file 
        run: touch dummy-$(date '+%Y%m%d%H%M%S').txt

      - name: Git Auto Commit
        uses: stefanzweifel/git-auto-commit-action@v4.8.0
        with:
          commit_message: "chore: refresh files"
          branch: master

Additional context

This problem seems to be related with the issue #6

stefanzweifel commented 3 years ago

Thanks again for creating a separate issue for this problem.

After a bit of testing I still can't exactly reproduce this error with the given workflow. (I've added the workflow in my test repo here. This correctly created a new file.)

However, I could reproduce the exact error message by following a post on programmer.help, which also explains, why this error happens: https://programmer.help/blogs/pathspec-master-did-not-match-any-files-known-to-git.html

In short: The git repository tries to switch to a branch, that previously might have existed, but had no commits inside of it. Running the following commands will raise the exact same error message:

mkdir issue-141
cd issue-141
git init
git checkout -b develop
touch a.txt
git add a.txt
git commit -m "Add new File"
git checkout master

As the error seems to be related with checking out the correct branch, I've now merged #140 and tagged a new version: v4.9.0. With #140 merged, the action will not automatically create the branch locally, if it doesn't exist yet.

@pablosjv As you seem to be able to reproduce this error, I would appreciate it if you could upgrade your workflow to v4.9.0 and test, if this resolves the error on your side as well.

If it does: great! 🎉 We can close this issue. If not, we would need a real GitHub repository with a workflow that reliably fails with this error message.

pablosjv commented 3 years ago

Thanks a lot @stefanzweifel for the quick response and feedback. Indeed revisiting the problem it was because I didn't have the branch master in my repository. Instead I have the branch main. This was a recently created repository and I guess Github now uses main instead of master. I was on autopilot and didn't see this, so my apologies.

Anyway, version v4.9.0 seems to work perfectly as it is creating the branch if it doesn't exists. From my side, we can close the issue.

stefanzweifel commented 3 years ago

Thanks for the update. Then let's close this issue. 🤙