QGIS Planet

Using git With Multiple QGIS Branches

This post is for those of you that build QGIS on a regular basis and want to keep up with everything going on in the current release branches (1.7.2 and 1.8) as well as the master branch that will eventually become version 2.0.

While you can do all your work in one clone, this method has a couple of advantages, at the expense of a bit of disk space:

  1. Quicker compiles compared to branch switching, especially if you are using ccache
  2. Less likelihood of making a merge mess when switching branches

The basic steps are:

  1. Login to github and clone the QGIS repository (https://github.com/qgis/Quantum-GIS)
  2. Create a working copy of your github clone on your machine
  3. Add a reference to the upstream repository
  4. Fetch and merge (if required) from the upstream repository
  5. Create a new clone for the branch by cloning the working copy created in step 2
  6. Change to the branch clone directory
  7. Add the upstream remote
  8. Fetch from upstream
  9. Create the tracking branch
  10. Checkout the branch

It’s simpler than it sounds—the following steps should work on any platform and assume you have already created your own clone of the QGIS repository on github.

First make a directory for development, change to it, and fetch a copy of your clone of QGIS from github (this will take a while):

gsherman@dork:~$ mkdir qgis_dev
gsherman@dork:~$ cd qgis_dev
gsherman@dork:~/qgis_dev$ git clone [email protected]:g-sherman/Quantum-GIS.git
Cloning into Quantum-GIS...
remote: Counting objects: 183812, done.
remote: Compressing objects: 100% (42255/42255), done.
remote: Total 183812 (delta 140627), reused 183281 (delta 140221)
Receiving objects: 100% (183812/183812), 240.80 MiB | 1.19 MiB/s, done.
Resolving deltas: 100% (140627/140627), done.

Now change to your new clone directory and add the upstream repository (the QGIS repo on github):

gsherman@dork:~/qgis_dev$ cd Quantum-GIS/
gsherman@dork:~/qgis_dev/Quantum-GIS$ git remote add upstream [email protected]:qgis/Quantum-GIS.git

We can then list our config to see that the remote was added:

gsherman@dork:~/qgis_dev/Quantum-GIS$ git config --list
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
[email protected]:g-sherman/Quantum-GIS.git
branch.master.remote=origin
branch.master.merge=refs/heads/master
[email protected]:qgis/Quantum-GIS.git
remote.upstream.fetch=+refs/heads/*:refs/remotes/upstream/*

Then we do a fetch from the QGIS repo to make sure things are up to date:

gsherman@dork:~/qgis_dev/Quantum-GIS$ git fetch upstream

If your clone of the QGIS repository on github is not brand new, you should do a merge before proceeding:

gsherman@dork:~/qgis_dev/Quantum-GIS$ git merge upstream/master

Now we can create a new clone for the 1.7.2 branch using our local master:

gsherman@dork:~/qgis_dev/Quantum-GIS$ cd ..
gsherman@dork:~/qgis_dev$ git clone ./Quantum-GIS Quantum-GIS-1_7_2
Cloning into Quantum-GIS-1_7_2...
done.

Now we need to add the remote for the main QGIS repository:

gsherman@dork:~/qgis_dev$ cd Quantum-GIS-1_7_2/
gsherman@dork:~/qgis_dev/Quantum-GIS-1_7_2$ git remote add upstream [email protected]:qgis/Quantum-GIS.git

The next step is to fetch from upstream to make sure we have references to the branches:

gsherman@dork:~/qgis_dev/Quantum-GIS-1_7_2$ git fetch upstream
* [new branch] dev-threading -> upstream/dev-threading
* [new branch] master -> upstream/master
* [new branch] release-0_0_11 -> upstream/release-0_0_11
* [new branch] release-0_0_12 -> upstream/release-0_0_12
...
* [new branch] release-1_7_1 -> upstream/release-1_7_1
* [new branch] release-1_7_2 -> upstream/release-1_7_2
* [new branch] release-1_8 -> upstream/release-1_8

Now we create the branch to track the release of interest—in this case 1.7.2:

gsherman@dork:~/qgis_dev/Quantum-GIS-1_7_2$ git branch --track release-1_7_2 upstream/release-1_7_2
Branch release-1_7_2 set up to track remote branch release-1_7_2 from upstream.

The last step is to check out the branch which should go pretty quick:

gsherman@dork:~/qgis_dev/Quantum-GIS-1_7_2$ git checkout release-1_7_2
Checking out files: 100% (1809/1809), done.
Switched to branch 'release-1_7_2'

You are now ready to build the 1.7.2 release branch. Repeat the process for any other branches you want to track.

Developing QGIS Plugins with git

Writing a QGIS plugin is not overly complicated but represents a bit of work. Using
git in conjunction with your development efforts can make sure your investment in
coding time is preserved.

Development Tools

The QGIS project team has set up a central location for plugin development which includes pretty much everything you need to develop and support your plugins, including:

  • Issue tracking
  • Wiki
  • Documents
  • Repository

The repository feature allows you to create a central place to store your plugin code using git. Others can clone your repository and contribute through patches or pull requests.

Creating a Plugin

The chore of setting up the boilerplate for a plugin is made simple by using the Plugin Builder. Previously this was a web application but it has now been replaced by a QGIS plugin aptly named Plugin Builder. You can install Plugin Builder from within QGIS by selecting Fetch Python Plugins… from the Plugins menu.

Once Plugin Builder is installed you can quickly create a starter plugin that implements a simple dialog box with OK and Cancel buttons. The plugin created by Plugin Builder is fully functional—it will load in QGIS but not do anything useful until you customize it.

Setting up the Workflow

You have a couple of options for setting up your development directory and workflow:

  1. Copy your plugin template to the QGIS plugin directory, initialize the git repository, and develop from there.
  2. Work within your plugin template directory that was created by Plugin Builder (not within the QGIS plugin directory)

Option one is very convenient as long as you don’t test the uninstall feature of your plugin. This will delete your entire plugin directory and git repository—not what we really want.

Option two is safer, however to test your plugin you have to continually copy from your development area to your QGIS plugin directory. Alternatively you could make commits and pull from your development area to the copy (assuming it is a git repo) in the QGIS plugin directory. If you are on a unix based system you could also create a Makefile to deploy the plugin for you.

The best solution is to use option two and use the QGIS_PLUGINPATH environment variable to point to your development directory. When present, QGIS_PLUGINPATH tells QGIS to search additional directories for plugins. Going this route allows you to develop in the directory created by Plugin Builder and test your plugin without any copying or pulling. The upside to this is since your plugin wasn’t installed through the Plugin Installer it can’t be uninstalled accidentally. When you are ready to test the uninstall and unloading of your plugin you can copy it to the main QGIS plugin directory.

Note: If you are using Windows there was a problem specifying QGIS_PLUGINPATH with colons in the path. This issue is fixed in revision 15073 and will make it into the next release.

No matter how you implement your workflow I suggest creating a repository on the QGIS hub (http://hub.qgis.org) or github.com.

Summary

Here is a summary of the steps to get started. We’ll assume your new plugin is named zoomer:

  1. Install Plugin Builder
  2. Create a directory that will contain all your plugins, for example my_plugins
  3. Create your plugin template using Plugin Builder. Be sure to select my_plugins when Plugin Builder asks where to create your plugin.
  4. Change to your plugin directory (e.g. my_plugins/zoomer) created by Plugin Buidler and create a git repository using:
    git init
  5. Set the QGIS_PLUGINPATH evironment variable to point to the directory containing your plugin directory (my_plugins). Be sure to use the full path to my_plugins
  6. Start QGIS and use the Plugin Manager (Manage Plugins… from the Plugins menu) to enable your plugin. If it doesn’t show up in the list of plugins check to make sure you have set QGIS_PLUGINPATH correctly.
  7. Develop away, testing as you go. Make sure to commit changes regularly and push them to your repository on hub.qgis.org.

If you are new to git, take a look at the Pro Git book at http://progit.org/.

For help on developing QGIS plugins with Python, see the PyQGIS Cookbook.

Contributing to QGIS Using Git

One of the challenges in any open source project is accepting contributions from people that don’t have, need, or want access to your centralized source code repository. Managing repository accounts for occasional or one-time contributors can be come a bit of an administrative issue. To date, the QGIS project has accepted one-time or occasional contributions through patches submitted via a help ticket.

To make it easier for you to contribute to QGIS, we have created a clone of the Subversion repository on GitHub. This allows you to “fork” the QGIS repository and have your own local copy of the source code. Through GitHub you can easily submit your enhancements and bug fixes for inclusion in the Subversion repository.

You will need to install git on your computer and create a GitHub account. Once you have done that, here is the process for creating your working copy and contributing to QGIS:

  1. Login to GitHub
  2. Fork the project at https://github.com/qgis/qgis by clicking “Fork”. This may take a while
  3. Create a working copy, replacing g-sherman with your GitHub name:
    git clone [email protected]:g-sherman/qgis.git

    This will also take a while, depending on the speed of your network connection. My clone downloaded 162 Mb from GitHub.

  4. Change to the directory containing your local clone and add a reference to the original QGIS repo:
    git remote add upstream git://github.com/qgis/qgis.git

    This gives you are reference named upstream that points to the repo you forked.

  5. Now fetch from upstream:
    git fetch upstream

  6. Now you are ready to work with QGIS. Make your changes, commit them, and then push back to your fork on GitHub:
    git push origin master

  7. You can go to your repo on GitHub and you should see your commit message from the push
  8. Now you want to tell the QGIS developer team there is something they should look at. You do this by issuing a pull request by clicking on the “Pull Request” button on your GitHub QGIS fork page.
  9. Fill in a title and message for the request, review the commit(s) and file(s) involved and when satisfied, click “Send pull request”

  10. Now sit back and wait for someone to review, comment on, and hopefully merge your request into the QGIS repo.

    To keep in sync with the QGIS repo, use
    git fetch upstream
    git merge upstream/master

    Make sure to check out these resources for help and more information on working with repositories on GitHub:

Git Merge – No Problem

Using Git with Subversion makes adding new features easy. Here are the metrics for my latest QGIS hack:

  • SVN revisions by others while working on my branch: 177
  • Time to complete merge with latest SVN revision: 1 second
  • Conflicts: None

Coincidence? Maybe not.

Back to Top

Sustaining Members