Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Push of formatted code fails with "fatal: You are not currently on a branch." #11

Closed
barbeau opened this issue Jan 21, 2021 · 4 comments

Comments

@barbeau
Copy link

barbeau commented Jan 21, 2021

First off, great plugin!

I'm trying to apply it in a new PR here:
https://github.com/MobilityData/gtfs-validator/pull/616/checks?check_run_id=1743823100

It's a really basic config inside an existing workflow file:

  formatting:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2 # v2 minimum required
      - uses: axel-op/googlejavaformat-action@v3

However, it's failing with the following:

...
validator/processor/src/main/java/org/mobilitydata/gtfsvalidator/processor/TableLoaderGenerator.java
Committing changes
  /usr/bin/git commit --all -m Google Java Format
  [detached HEAD 681a617] Google Java Format
   2 files changed, 18 insertions(+), 20 deletions(-)
  /usr/bin/git push
  fatal: You are not currently on a branch.
  To push the history leading to the current (detached HEAD)
  state now, use
  
      git push origin HEAD:<name-of-remote-branch>
  
Error: The command 'git' failed with exit code 128

Am I missing something simple here?

@barbeau barbeau changed the title Commit of formatted code fails with "fatal: You are not currently on a branch." Push of formatted code fails with "fatal: You are not currently on a branch." Jan 21, 2021
@axel-op
Copy link
Owner

axel-op commented Jan 22, 2021

Hi,

When you run a workflow on a pull_request event, the actions/checkout step will check out your repository in detached HEAD mode, on a merge reference created by GitHub. You can see it in the logs of this step:

Checking out the ref
  /usr/bin/git checkout --progress --force refs/remotes/pull/616/merge
  Note: switching to 'refs/remotes/pull/616/merge'.
  
  You are in 'detached HEAD' state. You can look around, make experimental
  changes and commit them, and you can discard any commits you make in this
  state without impacting any branches by switching back to a branch.

From this Stackoverflow page, we learn that refs/remotes/pull/616/merge can be seen as "a future commit", which would be the result of merging the base branch and the head branch at this time.

To sum it up, this means that you are checked out on a commit that does not belong to any branch yet.

Now, if you want to commit something on a pull_request event, you'll probably want to do so on the head branch. Then you can modify your workflow file as follows:

jobs:
  formatting:
    runs-on: ubuntu-latest
    steps:
      - if: github.event_name != 'pull_request'
        uses: actions/checkout@v2
      - if: github.event_name == 'pull_request'
        uses: actions/checkout@v2
        with:
          ref: ${{ github.event.pull_request.head.ref }}

The thing you'll have to take care of is the case where the head branch does not belong to the same repository: then you will probably not have the permission to commit on it. In this case you could just skip the commit.

I don't know exactly which scenario you want to implement, but I believe the simplest way to solve your issue is to skip the commit on pull_request events, and to enable it only on push events: if your PR is merged, it will trigger a push event on the base branch.

barbeau added a commit to MobilityData/gtfs-validator that referenced this issue Jan 22, 2021
@barbeau
Copy link
Author

barbeau commented Jan 22, 2021

@axel-op Awesome, thanks for the tip, it works! At least with a PR from a branch on the same repo. Here's the full config for anyone with a similar problem:

  formatting:
    runs-on: ubuntu-latest
    steps:
      - if: github.event_name != 'pull_request'
        uses: actions/checkout@v2
      - if: github.event_name == 'pull_request'
        uses: actions/checkout@v2
        with:
          ref: ${{ github.event.pull_request.head.ref }}
      - uses: axel-op/googlejavaformat-action@v3

We'll have to test to see if the commit works with PRs from forked repos, and if not we'll change the config so it runs on push on main branch instead. I'd prefer to keep it in PRs if possible so it's squashed as part of the PR and we don't clutter the main branch with formatting commits, but it would still work.

Thanks again for the great plugin!

@barbeau barbeau closed this as completed Jan 22, 2021
lionel-nj pushed a commit to MobilityData/gtfs-validator that referenced this issue Jan 22, 2021
* ci: Automatically format code

* Make sure we're on a branch for pull requests

See axel-op/googlejavaformat-action#11 (comment)

* Google Java Format
https://github.com/google/google-java-format


Co-authored-by: github-actions <>
@axel-op axel-op pinned this issue Jan 24, 2021
barbeau added a commit to MobilityData/gtfs-validator that referenced this issue Jan 27, 2021
@daltonfury42
Copy link

It doesn't work on forks, here is a sample run, and this is the config on the fork.

jan-knoblich pushed a commit to cadivus/KIT-PSE_Sensordatenbankmanagementsystem that referenced this issue Jul 26, 2021
@axel-op
Copy link
Owner

axel-op commented Aug 12, 2021

It doesn't work on forks, here is a sample run, and this is the config on the fork.

Hello @daltonfury42,

The commit is from a different repository. Try this:

  formatting:
    runs-on: ubuntu-latest
    steps:
      - if: github.event_name != 'pull_request'
        uses: actions/checkout@v2
      - if: github.event_name == 'pull_request'
        uses: actions/checkout@v2
        with:
          repository: ${{ github.event.pull_request.head.repo.full_name }}
          ref: ${{ github.event.pull_request.head.ref }}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants