Developer Area/Contributing Code

From Mahara Wiki

< Developer Area
Revision as of 14:35, 29 December 2014 by Aaronw (talk | contribs) (If all else fails...)

If all else fails...

Admittedly, it can be a little intimidating for a new developer to go through the process described on this page to submit patches to Mahara. If you can't get this working, or don't want to, feel free to simply attach your patch file directly to a bug report in Launchpad. (It's helpful if you can specify the exact version of Mahara your patch applies against.) Or link to a commit on github. Or even paste code directly into the bug report. We welcome all contributions. :)

From there, another member of the Mahara community can push your contribution into Gerrit, using git commit --author="Your Name <[email protected]>" to credit you as the author of the change.

But if you plan on being a regular contributor to Mahara, it's preferable if you can follow the process described below. If you have difficulties, feel free to ask for help on #mahara-dev on, or in the Mahara developer forums.

Getting ready for using Gerrit

The Mahara code review system uses Gerrit, a web-based collaborative tool for code revision. This allows anyone who wish to contribute to submit code and get it added to Mahara core. You do not need commit permission on the main Mahara repository, or to be part of the "Mahara Reviewers" group to send your patchsets through the Gerrit system. Essentially, Gerrit is an intermediate step on the way of the patch between your local workspace and the main Mahara repository. For your local git clone of Mahara, Gerrit is just another remote branch which one can push changes to that will be shown on the Gerrit web interface.

Initial setup

Developer Area/Developer Tools.

Doing a fresh Mahara clone

If you have not done that already, grab a read-only copy of the Mahara repo from Gitorious:

git clone git://

This new clone will ensure that you do not accidentally push anything to the main repository itself but allows you to push your changes via the review system (Gerrit).

If you need more instructions on how to get your developer environment to work, check out Setting up your developer environment.

Setting up Gerrit in your local Mahara repository

First of all you need to add a new gerrit remote branch to your repo (substitute your gerrit username):

git remote add gerrit ssh://

Apparently, the username is case-sensitive, which is why we recommend using a lowercase one.

Also you will need to add a commit message hook to your repo, this will generate Change-Id: tags in commit messages which is required for proper gerrit functioning:

scp -p -P 29418 .git/hooks/

Creating a new branch for your change

We strongly recommend you create one git branch per change you want to submit, first make sure you are on master branch and that it is up-to-date and clean of any changes:

 git checkout master -f && git reset --hard origin/master && git pull

then create the new branch, it is useful to give it a meaningful name like a Launchpad bug number

 git checkout -b bug123456

make the changes you want to do and check what needs to be added to your patch:

 git status

for any new files that you have added but are not currently tracked by git you will need to add them by name, eg:

 git add htdocs/view/my_new_view_file.php

then you can commit the modified/deleted files with:

 git commit -a -s

or if you want to choose interactively which bits of code to add:

 git add -p
 git commit -s

and finally use this branch when you push to the code review system (see below). Please note; this process has changed several times. Are you using the current best practice?

Preparing / organising your changes

Some basic guidelines:

  • One patchset per logical change
    • If you've fixed three or four bugs in entirely different parts of the system, sending them together as one patchset makes it harder to review, manage, and merge them. For instance, if it turns out that one of the three fixes is incorrect, while the other two are ready to merge, or if only one fix needs to be back-ported to previous versions of Mahara.
  • Include technical details with each change
    • You should briefly describe what the change intends to do. The worst possible description you could use is "fixed bug", or possibly "rawr". What we want to see is enough detail about what was wrong, and if not obvious from the patch, why you fixed the problem the way you did. If your description starts getting too long (more than a paragraph), it's probably a sign that you should split up your patches.
  • Group related patches together into topics
    • If you are developing a larger feature as a series of related patches, group them together into a topic (see the section below on how to do this in gerrit). A topic is especially important if the first few patches in the series don't really achieve anything on their own. A reviewer will be able to make sense of an earlier patch by looking forward to later ones within the same topic. The commit message of the earlier patch should explain the plan, and the whole series of patches should be submitted at the same time.
  • Apply new patch on top of clean checked-out branch
    • Every new patch should be applied on top of the clean checked-out branch to avoid adding dependencies in error.
  • Consider the reviewer
    • Following the guidelines above should help to make your patches easier for the reviewer to read and understand. But also try to remember that the person reviewing your code would probably rather be doing something else, like writing their own code. They don't want to have to review your patch twice, so don't submit patches until they're well-tested, and until you're absolutely sure the code is correct.
  • Make sure your changes "blend in" with the surrounding code
    • By that, we mean make sure you're using four spaces for indentation, the same bracing style the surrounding code uses for if {} etc. Make sure your changes are internationalised using our translation system, and that you throw exceptions the same way the existing code does when errors occur. If your code isn't too much different, we'll probably help you out by fixing it up. But if you're using tabs, weird bracing styles and your own functions for database access, your code is sure to be rejected. Remember to make sure that the new code follows our coding guidelines.

Here are some additional articles you might be interested in reading:

Submitting commits for review

Preferred method

This is the preferred method of pushing things to Gerrit, but you should understand what it does by reading the manual method as well.

Note: To use this, you will need to install xmllint. On debian/ubuntu based systems, this is in libxml2-utils.

To push your change, simply type this in the root directory of your feature/bugfix branch, eg the parent directory of the htdocs/ directory:

 make push

or if you want to add a Gerrit tag:

 make push TAG=topic_for_this_change

Note: You only need to do the topic tag once.

This will run a few initial sanity checks and either push your changes to Gerrit or will return warnings about coding problems that you will need to fix (see below).

Once a patch is pushed to Gerrit it is run through a host of automated tests and (after a short while) will update the Gerrit review with a pass or fail in the automated Tests (AT) column.

If your patch fails the automated tests you can follow the link provided in the comments section to find out why your patch failed to help debug and fix the issues (see below).

Manual method

In order to push your changes for review, just push into the project's magical refs/publish/remote_branchname ref. If you are pushing to the remote master branch and your current local branch is bug123456, your command will be:

git push gerrit HEAD:refs/publish/master

Once you pushed, your change will be available for review on the Mahara code review system. The single change will be created on review system for each commit you pushed. If you push several commits, gerrit will add dependencies between changes based on the git parent ref.

You may also push a single commit, simply use its hash:

git push gerrit 38f4f96df:refs/publish/master

If you like, you may specify the topic tag that will be shown in your change details on review system. This tag may be associated with a group of changes if you use it for several commits. To do that, simply append it to the destination ref, e.g.

git push gerrit HEAD:refs/publish/master/topic_for_this_change

Making changes in existing commits

  1. Either checkout the branch where your original changes are:
 git checkout bug123456

or checkout commit from Gerrit itself - there will be a link under the download section with 'checkout' highlighted - to copy/paste, eg:

 git fetch refs/changes/57/1234/1 && git checkout FETCH_HEAD

Make the changes that you need and add them ready for committing

 git add -p

Then to commit these new changes to the same commit message as before:

 git commit --amend

Another option is commit squashing, below.

When your commit has been reviewed, you may require to make some changes before it will be accepted. The changes you will be doing should be squashed into the same commit you originally pushed for review. You must ensure that the Change-Id: line is preserved the same as in the original commit. This will make Gerrit matching this commit to the original one and appending your changes as a new patchset. The simplest workflow would be:

  1. Checkout the branch where your original changes are (see above)
  2. Make the required changes
  3. Commit the changes
    • The commit message does not matter as it will be removed when the commit is squashed, so random text here is fine.
  4. Use interactive rebasing to squash the latest commit into the previous commit.
    • An example command for this would be: git rebase -i HEAD~2
    • Find the latest commit in the list (it should be the bottom-most commit) and replace the word 'pick' with 'f'. This is short for 'fixup', which will squash the commit into the one before it, and discard the commit message. (This is what we want.) You may also use 's' in which case you will be offered to amend the commit message before squashing (ensure that you leave Change-Id: of the commit you are squashing onto).

Now you may push the amended commit to review system the most convenient way:

git push gerrit HEAD:refs/publish/master


make push

In the Gerrit interface it will appear under the same change as a separate patchset ready for another review.

Ordering commits

If you have pushed some commits to Gerrit but then discover that one of your commits is dependent on another you can adjust the hierarchy of your commits like this.

Checkout the commit that you want to come first/be the parent by getting the checkout code from the review, eg:

 git fetch refs/changes/49/1234/1 && git checkout FETCH_HEAD

Then cherry-pick the second/child commit by selecting 'cherry-pick' before copying the link, eg:

 git fetch refs/changes/50/2345/1 && git cherry-pick FETCH_HEAD

If the child commit merge throws an error, ie you should see lines like: hint: after resolving the conflicts, mark the corrected paths hint: with 'git add <paths>' or 'git rm <paths>' hint: and commit the result with 'git commit'

you will need to fix up the merge conflicts - first go:

 git status

to see which files are causing the merge problems. you should see something like 'Changes to be committed:' listing the files that will merge fine, and 'Unmerged paths:' listing files that will need to be fixed.

Edit the files that need to be fixed and you should see in them some new lines added beginning with <<<<<<< HEAD then some code (part 1), then ======= then some other code (part 2), then >>>>>>> followed by commit id and commit title

What you need to do is to work out which bits of code to keep from part 1 and edit part 2 to suit (or visa versa) delete the <<<<<<<, =======, >>>>>>> lines and any code not needd once you are done.

Save the file and then add the resolved file to the commit by adding the full file directly, eg:

 git add /name/of/file.php

once all the Unmerged paths files are fixed and added then go:

 git commit
 make push

If the child commit has merged cleanly onto the parent one you can then push it back to Gerrit

 make push

And now in Gerrit you will see under the dependencies section that the child patch has a new patchset and that patchset depends on the parent patch/commit.

Committing in some easy condensed steps

First commit

1. Make sure you are on the latest master.

 git checkout master
 git pull

2. Make changes to the file(s) that you want to fix in your favorite editor.

3. Check which files you have changed.

 git status

4. Review your changes by comparing them to the original content of the file.

 git diff

5. Double-check on which branch you are. You should be on "master".

 git branch

6. Create a new branch which contains the changed files. You can give the branch any name you like as long as you remember what you changed on it. Using the bug number in the branch name is easy to identify it.

 git checkout -b bug[bug number]

7. Double-check that you are now on that new branch and that you still see the changed files.

 git branch
 git status

8. Commit your changes to git. Please read how to write good commit messages. Ideally, the Launchpad bug number appears in the first line of the commit so that it appears in the changelog. If the first line would get too long, put the bug number in the second or last line. It will then be linked back to Launchpad and your patch will be associated to the bug correctly. The "-a" means that all files that have changed will be taken into consideration.

 git commit -a

9. Check your local git log that you committed your change and that the commit message doesn't contain any errors. You can make changes to your commit message if necessary with "git --amend".

 git log

10. Push your changes to the review system. Make a note of the URL under which your patch is available for review. You will also see it appear on your Gerrit dashboard.

 make push

Re-submit a patch

You may have to make changes to a patch that you submitted based on feedback you receive from the reviewers.

1. Make sure that you are on the branch in which you fixed the bug. If necessary, switch to it with git checkout.

 git branch
 git checkout bug[bugnumber]

2. Make the necessary changes.

3. Check which files you have changed.

 git status

4. Review your changes by comparing them to the original content of the file.

 git diff

5. Add the file(s) to the commit.

 git add [filename] OR
 git add . (adds all files that have changed)

6. Open the commit message and make changes if necessary. Then save the file. The command given here is for vim.

 git commit --amend
 [in vim] :wq

7. Review that your changes to the commit message made it into the log.

 git log

8. Push your changes to the review system

 make push

Security patches

For patches for security issues, use:

make security

This will submit your patch into gerrit as a "Draft" submission, which will be only visible to you and to people in gerrit's "Security" group. The downside to this is that the code review system doesn't work automatically for Draft submissions, so verifications and review have to be made as freetext comments, rather than +/-.

Useful links