Managing Xen Patches with StGit

This document assumes that you are familiar with the following documents
* Xen Project Repositories
* Submitting Xen Project Patches - the document explains conventions related to cover letters, *-by tags, etc. as well as the tooling involved in sending patches and patch series
This document lays out basic examples and best practice of how to use StGit to manage Xen patches as part of the patch submission process. To be able to manage the patch contribution more easily, we StGit, which is an application that runs on top of git and provides a functionality similar to Mercurial PatchQueue extension. This tutorial will show how to manage branches with git and StGit, so before proceeding please make sure that StGit is installed.
Similar documents exist for 
* Git: see Managing Xen Patches with Git.
* git-series is a tool on top of Git that tracks changes to a patch series over time, including cover letter for the patch series, formats the series for email, and prepares patch submission. You can find instructions at Managing Xen Patches with Git-series.
== StGit Basics ==
StGit is a tool that runs on top of Git which allows you to manage patches in a stack on top of an existing git baseline. The stack of patches that have been created on top of your baseline maps to the patch series you are working on. To get started you have to initialise StGit on your working baseline (e.g. your current development branch of your repository using stg init). 
The, you add a new patch to your stack, with the following the basic workflow
stg new <new-patch-id>
...edit patch description
...develop in the tree
stg refresh
...develop some more
stg refresh
You can add several patches this way and then use stg series to list them. This shows your current stack of patches, where patches preceded by minus signs are not applied, patches preceded by plus signs are applied, while your current working patch is highlighted by a > character in the stg series output. The current working patch is always also the current top of your stack of patches.
At any point you can edit the current tree to update your current working patch : Simply edit the files and use the stg refresh command to record your changes. You can use stg refresh -e to update the patch description as well. stg show will show the current (or any) patch.
You can manipulate the stack of patches using the push, pop, goto, sink and float StGit commands. The most basic option to control the stack - and thus the current working patch - is a sequence of pop commands, followed by an edit-refresh cycle and a matching sequence of push commands. This works very well in a review cycle, where typically you move from one patch of a series to another. The goto command can be used if you need to move the current working patch by more than a few positions in the stack.
Before running any command the first time, we recommended that you at least quickly skim through its man pages. Many of the commands have very useful and interesting features that are not listed here: sometimes there are some extra notes which are very useful. Quick usage help is available for the StGit command using stg help <command>.
If you want to rebase your series of patches against a different baseline, or just rebase an existing one you can use the stg pull command. It will pop all the patches, then update your current branch to the upstream revision using git pull, then push your patches back on top of the new base. If conflicts occur you need to resolve them and tell StGit using the stg resolved command and then update the patch using stg refresh and resume by performing a stg push -a.
== Generating an initial Patch or Patch Series ==
Here's a recommendation on how to manage patches with StGit, as suggested by the Xen maintainers. Before you follow the instructions, you may want to read the following short discussion about whether to develop against staging or master branches. Unlike the git guide, this document assumes you are developing against master.
The first thing to do is cloning the xen git repository:
$ git clone git://xenbits.xen.org/xen.git
$ cd xen
$ stg init
This will create a new folder, called xen, and initialises StGit where you will work on your patches.
=== Create a branch for your changes ===
We are going to create a branch for each series of patches that we are going to work on. This will allow a developer to work on several patch series at the same time, keeping the patches contained and well classified. Now we will create a new branch on top of the default branch, which is called master
$ stg branch -c my_new_feature
You are now working on a different branch, you can switch back to the master branch at any time by using:
$ stg branch master
==== Set up a patch (this happens before you develop it) ====
Now that we are on the branch we wish to use to develop feature X we will start creating patches. The first thing to do is creating a new patch, StGit will ask for a description of the patch, but if unsure you can leave it blank and fill it later. The conventions related to what should be in commit messages are described in Submitting Xen Project Patches. The example below is merely intended to explain the necessary StGit commands: when you submit patches you will likely need more detail than shown in this document.
$ stg new first.patch
Here is an example of a simple description:
foobar: Add a new trondle calls
Add a some new trondle calls to the foobar interface to support
the new zot feature.
Signed-off-by: Joe Smith <joe.smith@citrix.com>
The first line in the description will be used as the subject when the patch is sent to the mailing list, so please make sure it contains an accurate description about what the patch is expected to introduce. It should be followed by a more accurate description and finally a "Signed-off-by".
==== Develop and Test your patch ====
Now you can starting modifying the code, all your changes will be contained inside this patch. 
==== Modify the commit message ====
If you want to modify the description of a patch, you should use the edit command
$ stg edit first.patch
==== Refresh (commit) your patch ====
When finished, you will have to refresh the patch.
$ stg refresh
If you also want to edit the commit message, you can use
$ stg refresh -e
=== Develop a second patch in the patch series and commit it ===
For further patches, you follow the same protocol as outlined in the previous section using a different name for your patch, starting with
$ stg new second.patch
=== Working on patches in a Patch Series ===
At this point, we currently have a two patch series consisting of first.patch and second.patch. We are going to add a third.patch as described before, which we can see using 
$ stg series --description
+ first.patch # My first patch
+ second.patch # My second patch
> third.patch # My third patch
StGit shows patches chronologically ordered from top to bottom. Note that this is the opposite from git log, which by default uses reverse chronological ordering. In the example, third.patch is your current working patch.
If for example you want to work on the first patch, you can choose to perform
$ stg pop
$ stg pop
or 
$ stg goto first.patch
showing the following
$ stg series --description
> first.patch # My first patch
- second.patch # My second patch
- third.patch # My third patch
The command 
$ stg show
shows you the current patch you are working on.
== Sending a Patch or Patch Series to xen-devel@ ==
You can find instructions on how to send patches in our Patch Submission Guide. It is important though, that the current working patch is the last patch in the series, otherwise you will submit only a subset of your series.
In the example above, you want to perform
$ stg goto third.patch
before executing the commands outlined in our Patch Submission Guide.
=== Creating outbound version branches ===
As as the case with a git workflow, you should create an outbound version branch after you sent a patch for review. This allows you to go back to previous versions easily. If you share a branch of a complex series in your cover letter as suggested here, it is important to do this, such that the branch remains static and code reviewers do not get confused by unexpected changes in the shared git branch.
With stgit, you can do this with stg publish <outbound-branch-name>, which creates a new branch with the patches to publish. In other words, if used consistently you end you end up with a git branch that allows you to save your changes for each revision in a separate git branch. 
You can also use the normal git workflow and push the changes to a remote branch, via git push ... <stgit branch>:<remote branch name>.
== Addressing Review Comments ==
Addressing review comments in StGit is much easier than with git, as you can simply move between patches of your series as outlined below
stg goto <patch-id>
...modify the patch
stg refresh -e
and then update the commit message including the change log. This may look something like:
xen/x86: Added trondle feature
Because, ...
Signed-off-by: Joe Blogs <joe...@citrix.com> 
--- 
CC: Another Colleague <ano...@citrix.com> 
Changes in v3:
- s/nGRE/nGnRE/g
- move security support clarification to a separate patch
Changes in v2:
- add #define LIBXL_HAVE_MEMORY_POLICY
- ability to part the memory policy parameter even if gfn is not passed
- rename cache_policy to memory policy
== Common tasks ==
=== Rebasing a series ===
If you are working on a big or controversial patch series, it is very likely that you will have to submit several versions of them, and you will need to rebase your code to match the changes that will be committed to the repository between each revision of your series. You can do that easily with stg rebase. The first step is to update the master branch of your repository.
$ stg branch master
$ git pull
After that you will need to find the commit ID you want to rebase your series on top of, this can be done using git log. Once you have the commit ID, switch to the branch with your patches and execute:
$ stg rebase <commit-id>
This will pop all your patches, move the branch to the commit specified and then push your patches again. There's a chance that your patches don't apply cleanly on top of this commit, if this is the case stg will complain loudly, and you will have to manually edit the conflicting file. After editing the file, you can add it to your repository again and continue with the rebase:
$ git add <conflicting/file>
$ stg refresh
$ stg push
stg rebase is really useful, because it allows you to rebase your series on top of any commit, as an example, you can rebase one of your series on top of another, or rebase a series on top of staging changes.
== StGit Tutorials ==
* StGit Man Pages
* An introduction to Stacked Git
* StGit Tutorial
Category:Developers
Category:Development Process
Category:Xen