Skip to main content

Stack diff support for GitHub

Project description

ghstack

Conveniently submit stacks of diffs to GitHub as separate pull requests.

uv tool install ghstack

ghstack is tested with several different Python versions. It requires at least Python 3.9.1.

How to setup

Go to github.com Settings→Developer Settings→Personal Access Tokens and generate a token with public_repo access only. Create a ~/.ghstackrc as shown below:

λ cat ~/.ghstackrc
[ghstack]
github_url = github.com
github_oauth = [your_own_token]
github_username = [your_username]
remote_name = upstream [if remote is called upstream and not origin]

How to use

Make sure you have write permission to the repo you're opening PR with.

Prepare a series of commits on top of master, then run ghstack. This tool will push and create pull requests for each commit on the stack.

How do I stack another PR on top of an existing one? Assuming you've checked out the latest commit from the existing PR, just git commit a new commit on top, and then run ghstack.

How do I modify a PR? Just edit the commit in question, and then run ghstack again. If the commit is at the top of your stack, you can edit it with git commit --amend; otherwise, you'll have to use git rebase -i to edit the commit directly.

How do I rebase? The obvious way: git rebase origin/master. Don't do a git merge; ghstack will throw a hissy fit if you do that. (There's also a more fundamental reason why this won't work: since each commit is a separate PR, you have to resolve conflicts in each PR, not just for the entire stack.)

How do I start a new feature? Just checkout master on a new branch, and start working on a fresh branch.

How do I merge my changes? Warning: You will NOT be able to merge these commits using the normal GitHub UI, as their branch bases won't be master.
Use ghstack land $PR_URL (or alternatively ghstack land #PR_NUM) to land a ghstack'ed pull request.

You can also setup a GitHub action to allow a bot land. ghstack_land_example is an end-to-end example of how to do this.

Structure of submitted pull requests

Every commit in your local commit stack gets submitted into a separate pull request and pushes commits onto three branches:

  • gh/username/1/base - think of this like "master": it's the base branch that your commit was based upon. It is never force pushed; whenever you rebase your local stack, we add merge commits on top of base from the true upstream master.

  • gh/username/1/head - this branch is your change, on top of the base branch. Like base, it is never force pushed. We open a pull request on this branch, requesting to merge into base.

  • gh/username/1/orig - this is the actual commit as per your local copy. GitHub pull requests never sees this commit, but if you want to get a "clean" commit all by itself, for example, because you want to work on the commits from another machine, this is the best way to get it.

Design constraints

There are some weird aspects about GitHub's design which lead to unusual design decisions on this tool.

  1. When you create a PR on GitHub, it is ALWAYS created on the repository that the base branch exists on. Thus, we MUST push branches to the upstream repository that you want PRs to be created on. This can result in a lot of stale branches hanging around; you'll need to setup some other mechanism for pruning these branches.

  2. Branch name does not correspond to pull request number. While this would be excellent, we have no way of reserving a pull request number, so we have no idea what it's going to be until we open the pull request, but we can't open the pull request without a branch.

Ripley Cupboard

Channeling Conor McBride, this section documents mistakes worth mentioning.

Non-stack mode. ghstack processes your entire stack when it uploads updates, but it doesn't have to be that way; you could imagine that you could ask ghstack to only process the topmost commit and leave the rest alone. An easy and attractive looking way of doing this is to edit the stack selection algorithm to look a single commit, rather than all the commits from merge-base to head.

This sounds OK but you try it and you realize two things:

  1. This is wrong, if you exclude the commits before your commit you'll end up with a base commit based on the "literal" commit in your Git repository. But this has no relationship with the base commit that was previously uploaded, which was synthetically constructed.

  2. You also have do extra work to pull out an up to date stack to write into the pull request body.

So, this is not impossible to do, but it will need some work. You have to work out what the real base commit is, whether or not you need to advance it, and also rewrite the stack rendering code.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ghstack-0.14.0.tar.gz (96.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ghstack-0.14.0-py3-none-any.whl (108.9 kB view details)

Uploaded Python 3

File details

Details for the file ghstack-0.14.0.tar.gz.

File metadata

  • Download URL: ghstack-0.14.0.tar.gz
  • Upload date:
  • Size: 96.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ghstack-0.14.0.tar.gz
Algorithm Hash digest
SHA256 90545081fcef6c327844424d73b8a0644626c29edd05604bc835840d2db11406
MD5 631c6f9f599e4583c0850106023fc982
BLAKE2b-256 eae985fd69ce8df7726ec1ea49396c128b890d6f778db8e90b850b47b635d657

See more details on using hashes here.

Provenance

The following attestation bundles were made for ghstack-0.14.0.tar.gz:

Publisher: publish.yml on ezyang/ghstack

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ghstack-0.14.0-py3-none-any.whl.

File metadata

  • Download URL: ghstack-0.14.0-py3-none-any.whl
  • Upload date:
  • Size: 108.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ghstack-0.14.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0ba192eaab92e5f053d8d84c64378a84a3e1d867684d956e392de00ea405862e
MD5 e34be06122672acb7f9b1e4f4c7c43fe
BLAKE2b-256 2badf22bccd79dc1c7b6dbca991282de5f305bc1fb41ffdd80bde7013e139e61

See more details on using hashes here.

Provenance

The following attestation bundles were made for ghstack-0.14.0-py3-none-any.whl:

Publisher: publish.yml on ezyang/ghstack

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page