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

Starting from a non-base branch resets away all changes on that branch #3496

Open
antifuchs opened this issue Nov 8, 2024 · 1 comment
Open

Comments

@antifuchs
Copy link

Subject of the issue

I have a pretty complex workflow that runs something like this:

  1. One job to update the repo's main lock file, push the update to a temporary branch on the repo
  2. Run several jobs on that lock file's basis, each of which update a part of the repo; then commits & pushes that change to one temporary branch per job.
  3. A third job that cherry-picks all of these updates onto the current branch and then uses peter-evans/create-pull-request to make a PR

In step 3, an issue occurs: If I start the work on the branch passed to c-p-r as base, the pull request is properly created. If I start the work on the branch with the main lock file update from step 1, create-pull-request resets that branch to the base branch and loses a major part of changes I want to make a pull request for.

Steps to reproduce

I'll make a reproducer repo once I can get this reduced, but here's step three, verbatim that should show the issue:

  file_pr:
    runs-on: ubuntu-latest
    needs: [flake_bump, bump, populate]
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
          # using `ref: ci/bump-versions/${{ github.sha }}/flake` here causes the "create pull request" step to fail
      - uses: fregante/setup-git-user@v2
      - name: "Current date"
        run: |
          echo "date=$(date +'%Y-%m-%d')" >> $GITHUB_OUTPUT
        id: version # this is used on variable path
      - name: Merge them all
        run: |
          set -x
          echo "::group::pull in flake commit"
          git reset --hard origin/ci/bump-versions/${{ github.sha }}/flake
          echo "::endgroup::"
          for attr in ${{ join(fromJSON(needs.populate.outputs.attributes), ' ') }}; do
            echo "::group::cherry-pick $attr"
            git cherry-pick -m1 --allow-empty origin/ci/bump-versions/${{ github.sha }}/attr-$attr || git cherry-pick --skip
            echo "::endgroup::"
          done
      - name: Diff
        run: |
          git diff --stat origin/main..HEAD
          git diff origin/main..HEAD
      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v7
        with:
          token: ${{secrets.GH_REPO_TOKEN}}
          commit-message: "Bump versions"
          assignees: antifuchs
          branch: auto-version-bump/bump
          base: main
          title: "Bump versions"

When updating the checkout step to pass the ref as mentioned in the comment, the action outputs the following (trimming to the bit that I'm pretty sure is relevant):

Create or update the pull request branch
  /usr/bin/git symbolic-ref HEAD --short
  ci/bump-versions/9af06fabbed4286f75125bbc83ac238cfee81356/flake
  Working base is branch 'ci/bump-versions/9af06fabbed4286f75125bbc83ac238cfee81356/flake'
  /usr/bin/git checkout --progress -B ad2440cf-e022-4d7d-ab89-f39ebcda12d4 HEAD --
  Switched to a new branch 'ad2440cf-e022-4d7d-ab89-f39ebcda12d4'
  /usr/bin/git status --porcelain -unormal --
  /usr/bin/git diff --quiet --
  /usr/bin/git diff --quiet --staged --
  /usr/bin/git stash push --include-untracked
  No local changes to save
  Resetting working base branch 'ci/bump-versions/9af06fabbed4286f75125bbc83ac238cfee81356/flake'
  /usr/bin/git checkout --progress ci/bump-versions/9af06fabbed4286f75125bbc83ac238cfee81356/flake --
  Switched to branch 'ci/bump-versions/9af06fabbed4286f75125bbc83ac238cfee81356/flake'
  Your branch is up to date with 'origin/ci/bump-versions/9af06fabbed4286f75125bbc83ac238cfee81356/flake'.
  /usr/bin/git reset --hard origin/ci/bump-versions/9af06fabbed4286f75125bbc83ac238cfee81356/flake
  HEAD is now at efdffa6f flake: update inputs and .sri
  Rebasing commits made to branch 'ci/bump-versions/9af06fabbed4286f75125bbc83ac238cfee81356/flake' on to base branch 'main'
  /usr/bin/git -c protocol.version=2 fetch --no-tags --progress --no-recurse-submodules --force --depth=1 origin main:main
  remote: Total 0 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)        
  From https://github.com/antifuchs/home
   * [new branch]        main       -> main
  /usr/bin/git checkout --progress main --
  Switched to branch 'main'
  /usr/bin/git rev-list --reverse ci/bump-versions/9af06fabbed4286f75125bbc83ac238cfee81356/flake..ad2440cf-e022-4d7d-ab89-f39ebcda12d4 .
  /usr/bin/git checkout --progress -B ad2440cf-e022-4d7d-ab89-f39ebcda12d4 HEAD --
  Switched to and reset branch 'ad2440cf-e022-4d7d-ab89-f39ebcda12d4'
  /usr/bin/git -c protocol.version=2 fetch --no-tags --progress --no-recurse-submodules --force --depth=1 origin main:main
  remote: Total 0 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)        
  /usr/bin/git rev-list --right-only --count main...ad2440cf-e022-4d7d-ab89-f39ebcda12d4
  0
  /usr/bin/git -c protocol.version=2 fetch --no-tags --progress --no-recurse-submodules --force --depth=10 origin auto-version-bump/bump:refs/remotes/origin/auto-version-bump/bump
  remote: Total 0 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)        
  Pull request branch 'auto-version-bump/bump' already exists as remote branch 'origin/auto-version-bump/bump'
  /usr/bin/git checkout --progress auto-version-bump/bump --
  Switched to a new branch 'auto-version-bump/bump'
  branch 'auto-version-bump/bump' set up to track 'origin/auto-version-bump/bump'.
  /usr/bin/git rev-list --right-only --count main...auto-version-bump/bump
  11
  /usr/bin/git diff --quiet auto-version-bump/bump..ad2440cf-e022-4d7d-ab89-f39ebcda12d4
  Resetting 'auto-version-bump/bump'
  /usr/bin/git checkout --progress -B auto-version-bump/bump ad2440cf-e022-4d7d-ab89-f39ebcda12d4 --
  Reset branch 'auto-version-bump/bump'
  Your branch and 'origin/auto-version-bump/bump' have diverged,
  and have 1 and 10 different commits each, respectively.
    (use "git pull" if you want to integrate the remote branch with yours)
  /usr/bin/git rev-list --right-only --count origin/auto-version-bump/bump...auto-version-bump/bump
  11
  Updated branch 'auto-version-bump/bump'
  /usr/bin/git rev-list --right-only --count main...auto-version-bump/bump
  0
  /usr/bin/git rev-parse main
  51c794bdd1758404efd8225eb4a21e185040fc06
  /usr/bin/git show --raw --cc --no-renames --no-abbrev --format=%H%n%T%n%P%n%G?%n%s%n%b%n###EOB### 51c794bdd1758404efd8225eb4a21e185040fc06
  51c794bdd1758404efd8225eb4a21e185040fc06
  231ddc77496af853a0b6450f5e9e1b117f06d83e
  
  N
  Diff against origin/main?
  
  ###EOB###
  
  [file list redacted]

  /usr/bin/git rev-parse auto-version-bump/bump
  51c794bdd1758404efd8225eb4a21e185040fc06
  /usr/bin/git branch --delete --force ad2440cf-e022-4d7d-ab89-f39ebcda12d4
  Deleted branch ad2440cf-e022-4d7d-ab89-f39ebcda12d4 (was 51c794bd).
  /usr/bin/git checkout --progress ci/bump-versions/9af06fabbed4286f75125bbc83ac238cfee81356/flake --
  Switched to branch 'ci/bump-versions/9af06fabbed4286f75125bbc83ac238cfee81356/flake'
  Your branch is up to date with 'origin/ci/bump-versions/9af06fabbed4286f75125bbc83ac238cfee81356/flake'.

You'll notice that the sha1 of origin/main is identical to the sha1 of the branch pushed in the end, but it started at efdffa6f: Somewhere in those commands, the changes I wish to file are being thrown out.

@peter-evans
Copy link
Owner

Hi @antifuchs

If your goal is to make a PR targeting main, then checking out main at the start is the correct approach. Then just cherry pick the commits you want on top of main, and then run the action. I don't understand why you want to checkout ci/bump-versions/${{ github.sha }}/flake. If you do that, then it won't take any commits between main and ci/bump-versions/${{ github.sha }}/flake into account. It would only look for commits on top of ci/bump-versions/${{ github.sha }}/flake and try and rebase them on main.

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

2 participants