Before You Code

If you consider the following items before you begin coding, you will produce a patch that is easier to assess and more likely to be accepted:

While Your Patch is Being Considered

Coding

Once you're ready to craft your code, the first thing you'll need is a clone of the Clojure or appropriate repository. The examples below are for the Clojure project -- for submissions to Clojure itself:

$ git clone git://github.com/clojure/clojure.git
$ cd clojure

Next, create a new branch for yourself:

$ git checkout -b fixbug42
Switched to a new branch "fixbug42"

Now you're ready to get hacking. Make sure relevant doc strings are up to date and that all existing regression tests still pass e.g. for clojure core run:

$ ./antsetup.sh             # Only needed once in a new Clojure tree, for ant to work
$ ant

# Some prefer to use Maven rather than ant
$ mvn test

As noted in readme.txt, you will need to run ./antsetup.sh as a one-time setup before running ant.  If you want to add new tests, that would be great too. Once you've finished making your changes you need to commit them.

$ git commit -a -m "fixed annoying bug, refs #42"
Created commit 8f7c712: fixed annoying bug, refs #42
1 files changed, 0 insertions(+), 1 deletions(-)

Now that you've made your changes it's time to get them into a patch. You need to update the repo and fix any conflicts you had.

$ git checkout master
Switched to branch "master"
$ git pull
...
$ git checkout fixbug42
Switched to branch "fixbug42"
$ git rebase master

Once you've fixed any conflicts, you're ready to create a patch:

$ git format-patch master --stdout > your-patch-file.diff

Adding patches

Now you can attach that patch file to the JIRA ticket.  In the More Actions menu near the top of the page, select Attach Files.  Please read and follow the recommendations below when writing comments about your attached patch.  Screeners have limited time available for screening.  You are more likely to get your patch approved if you can be as clear as you can, and as efficient with their time as possible.

Mark the ticket as having a patch ready for screening by editing the Patch attribute.  Click the Edit button near the top left of the page for the ticket.  In the next page find the heading "Patch" with a popup menu next to it.  Select "Code" or "Code and Test" from that menu, then click the Update button at the bottom of the page.  If you do not see an Edit button on the page for the ticket, and you have signed a CA, ask on the developer's email list to be given permission to edit Jira tickets.

Removing patches

To remove a patch (e.g. because it is obsolete), go to the page for the ticket and look for the "Attachments" heading beneath the Description text.  Far to the right is a plus sign and a triangle.  Click on the triangle and select "Manage Attachments" from the menu.  Think carefully on which one you want to delete, and click the trash can icon next to it.  Note: Most (or all?) people have permission to remove their own attachments, but not those added by someone else.

Updating stale patches

A stale patch means one that used to apply cleanly to the latest Clojure master version, but due to commits made since the patch was created, it no longer does.  In particular, the output of this command:

$ git am --keep-cr -s --ignore-whitespace < patch_file.diff

includes 'Patch failed' and 'To restore the original branch and stop patching, run "git am --abort"'.  You should do the "git am --abort" to get rid of state of the failed patch attempt left behind by the command above.

"git am" is very "fragile", meaning that if the patch_file was created with one version of the source code, all it takes for the command to fail is a change in any of the lines of context present in the patch file, even if it is not one of the lines being changed by the patch.  This is especially common for files containing unit tests, because people usually add new tests at the end of such a file, and so the lines of context before the new test change if two different patches add a new test to the end of the same file.

To apply such a patch, the 'patch' program by Larry Wall is extremely useful.  It comes preinstalled with Mac OS X and most Linux distributions.  You can easily install it with Cygwin for Windows.

$ patch -p1 < patch_file.diff

The output will give you some hints of whether each "hunk" of the patch file succeeded or failed.  If they all succeed, then likely the only thing wrong with the patch file is that a few context lines were changed.  If any hunks fail, patch creates files ending with ".rej" containing rejected hunks that it did not apply, and you can focus on those as places where the source code likely changed more significantly.  A command like this will find them all:

$ find . -name '*.rej'

You will need to look at those rejected hunks, perhaps think about them for a bit to see if and how they still apply, and apply them by hand-editing the source code yourself.

When recreating a new git format patch with:

$ git format-patch master --stdout > patch_file.diff

it puts your name and the current date near the top of the file.  If the only changes that you have made are in the context lines, please keep the original author's credit intact by copying the name and date from the original patch that you started from, then upload that.

If you write unit tests where there were none in the original patch, but didn't otherwise modify the original patch, and you would like your name in the commit log for your work, create a separate patch of test additions with your name on it, leaving the original author's name on the updated patch.

Screening a patch

If you are a screener testing a patch, you can create a new branch and apply the patch to start working with it:

$ git checkout -b testxyz
$ git am --keep-cr -s --ignore-whitespace < patch_file.diff

And you can throw that branch away when you're done with:

$ git checkout master
$ git branch -D testxyz 

How To Run All Clojure Tests

$ mvn test

To reduce the duration of the pseudo-randomly generated generative tests from 60 sec down to 1 sec (for example), edit the file src/scripts/run_tests.clj and change the 60000 number.  Just be careful not to include such changes in any patches you submit.

Run An Individual Test

First, build the latest Clojure without running any tests:

$ ant jar
# If no compilation errors, 'ant jar' creates clojure.jar in Clojure tree root dir

# Or, if you prefer Maven
$ mvn -Dmaven.test.skip=true package
# If no compilation errors, mvn command above creates target/clojure-1.7.0-master-SNAPSHOT.jar

The commands above build a Clojure jar file, but neither compile nor run the tests.

Start a repl and run individual tests from it:

# Replace clojure.jar with target/clojure-1.7.0-master-SNAPSHOT.jar if you ran mvn command
$ java -cp test:clojure.jar clojure.main
Clojure 1.7.0-master-SNAPSHOT
;; We're testing with clojure.test 
=> (require 'clojure.test)
nil
;; Load a test file 
user=> (require 'clojure.test-clojure.data)
nil
;; Run it 
user=> (clojure.test/run-tests 'clojure.test-clojure.data)
 
Testing clojure.test-clojure.data
Ran 1 tests containing 17 assertions.
0 failures, 0 errors.
{:type :summary, :pass 17, :test 1, :error 0, :fail 0}