Giter Site home page Giter Site logo

github-rebase's Introduction

npm version build status

Goal

github-rebase rebases a pull request using the GitHub REST API. It doesn't merge the pull request, it only rebases its head branch on top of its base branch.

See Autorebase if you want to automatically rebase and merge green and up-to-date pull requests.

Usage

const { rebasePullRequest } = require("github-rebase");

rebasePullRequest({
  // The number of the pull request to rebase.
  number: 1337,
  // An already authenticated instance of https://www.npmjs.com/package/@octokit/rest.
  octokit,
  // The login of the repository owner.
  owner,
  // The name of the repository.
  repo,
}).then(newHeadSha => {
  // Do something.
});

github-rebase can run on Node.js and in recent browsers.

Troubleshooting

github-rebase uses debug to log helpful information at different steps of the cherry-picking process. To enable these logs, set the DEBUG environment variable to github-rebase.

How it Works

The GitHub REST API doesn't provide a direct endpoint to rebase a pull request without merging it. However, a rebase can be seen as one or multiple cherry-pick operations where the head and base branches would be reversed. github-rebase thus relies on github-cherry-pick to perform all the relevant cherry-pick operations needed to perform a rebase.

Step by Step

Let's say we have this Git state:

* 017bffc (feature) C
* 5b5b6e2 B
| * 3c70b13 (HEAD -> master) D
|/
* a5c5755 A

and a pull request where master is the base branch and feature the head branch. GitHub would say: "The user wants to merge 2 commits into master from feature".

To rebase the pull request, github-rebase would then take the following steps:

  1. Create a temp branch from master with POST /repos/:owner/:repo/git/refs.
    * 017bffc (feature) C
    * 5b5b6e2 B
    | * 3c70b13 (HEAD -> temp, master) D
    |/
    * a5c5755 A
    
  2. Cherry-pick 5b5b6e2 and 017bffc on top of temp with github-cherry-pick.
    * 6de5ac0 (HEAD -> temp) C
    * 544d948 B
    * 3c70b13 (master) D
    | * 017bffc (feature) C
    | * 5b5b6e2 B
    |/
    * a5c5755 A
    ```
    
  3. Check that feature's reference is still 017bffc with GET /repos/:owner/:repo/git/refs/:ref or abort by jumpimg to step 5.
  4. Set feature's reference to the same one than temp with PATCH /repos/:owner/:repo/git/refs/:ref.
    * 6de5ac0 (HEAD -> feature, temp) C
    * 544d948 B
    * 3c70b13 (master) D
    * a5c5755 A
    
  5. Delete the temp branch with DELETE /repos/:owner/:repo/git/refs/:ref and we're done!
    * 6de5ac0 (HEAD -> feature) C
    * 544d948 B
    * 3c70b13 (master) D
    * a5c5755 A
    

Atomicity

github-rebase tries as hard as possible to be atomic.

  • The underlying cherry-pick operations are atomic.
  • The only thing that can go wrong is when a commit is pushed on the pull request head branch between the steps 3. and 4. explained above. In that case, the commit that was just pushed won't be part of the pull request head branch anymore. It doesn't mean that this particular commit is completely lost. Commits are immutable and, once pushed, they can always be retrieved from their SHA. There is no way to fix this issue as the GitHub REST API doesn't provide a compare-and-swap endpoint for updating references like it does for merges. Hopefully the issue should almost never occurs since the window during which the head branch is vulnerable usually lasts less than 100 milliseconds (the average GitHub REST API response time).

There are tests for it.

github-rebase's People

Contributors

tibdex avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.