wei / pull

🤖 Keep your forks up-to-date via automated PRs

Home Page:https://github.com/apps/pull

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[FR] New mergeMethod `merge-ff` to only merge when un-fast-forwardable, otherwise hard reset

Rongronggg9 opened this issue · comments

A nice mix of two current mergeMethod: hardreset and merge. Effectively simulate the default git merge behavior and make the commit history clean.

A use case:
DIYgod/RSSHub has a pull.yml configuration file with mergeMethod: merge to prevent pull[bot] from overriding downstream manipulated forks (DIYgod/RSSHub#9710). In such a case, merge-ff is a better solution.

merge ff isn't a supported merge method on GitHub pull requests. I recommend using a github actions to achieve your specific use-case.

merge ff isn't a supported merge method on GitHub pull requests. I recommend using a github actions to achieve your specific use-case.

Nope. It seems that you misunderstood my words. I did know and didn't mean to use the nonexistent ff-merge method. Instead, if the PR is ff-able, hardreset instead of merge it.

The title is:

[FR] New mergeMethod merge-ff to only merge when un-fast-forwardable, otherwise hard reset

Thanks for the clarification. Could you help me identify if ff-able status on a PR can be retrieved using GitHub API?

https://docs.github.com/en/rest/pulls/pulls#get-a-pull-request

Some tricks are needed. It must be a fork of the upstream repo.
Any commits of any forks can be visited from the upstream repo. Thanks to this feature, we can simply compare the branch HEAD between the fork and its upstream repo:

https://api.github.com/repos/{upstream_owner}/{upstream_repo}/compare/{upstream_branch_HEAD}...{fork_branch_HEAD}

https://docs.github.com/en/rest/commits/commits#compare-two-commits

An un-ff-able sample here:

https://api.github.com/repos/DIYgod/RSSHub/compare/0397ca906561bfc760bc2dcf9043dd3121d2294e...eb1a55f5fe356070091b29656af3c3b63ff4b8aa
{
  "status": "diverged",
  "ahead_by": 133, // the fork is ahead upstream by 133 commits
  "behind_by": 1 // the fork is behind upstream by 1 commit
}

An ff-able sample here:

https://api.github.com/repos/DIYgod/RSSHub/compare/0397ca906561bfc760bc2dcf9043dd3121d2294e...ce92931478e6deae9c22cfbad3fcf37e72927cc4
{
  "status": "behind", // ff-able
  "ahead_by": 0,
  "behind_by": 1 // the fork is behind upstream by 1 commit
}

Note: I use a commit SHA for upstream_branch_HEAD to make the result reproducible, but I recommend using its branch name instead, sample here:

https://api.github.com/repos/DIYgod/RSSHub/compare/master...ce92931478e6deae9c22cfbad3fcf37e72927cc4

Alternatively, if it is a fork and does not use a branch name different from upstream, maybe https://docs.github.com/en/rest/branches/branches#sync-a-fork-branch-with-the-upstream-repository? It defaults to ff-merge if ff-able. But the disadvantage is it will create a merge commit automatically if not ff-able and we can't take the control of this behavior.

Thanks for the thorough example! It looks like pull just needs to call the compare endpoint you shared when the mergeMethod: merge-ff is specified. Sounds like a great workaround.