Tips and tricks

From Koha Wiki

Jump to: navigation, search
Home > Documentation
Home > Documentation
Koha > Technical > Development
Koha > Technical > Development > Git
Koha > Technical > Development > Quality Assurance Testing
Tips & Tricks > Tips & Tricks/Customising Notices and Slips > Tips & Tricks


git tips and tricks

This page collect all git tips and tricks Koha developers have found

git hook to automatically check some things when commiting/applying

git has a nice feature, the hooks. hooks are small (or large) scripts that are executed before or after running some git command.

all hooks are hidden in .git/hooks

The pre-commit hook is very interesting. If it contains the following code:


use Modern::Perl;
use File::Basename;

my $nb_errors = 0;
for my $filepath (`git diff --cached --name-only`) {
    chomp $filepath;
    open my $fh, "<" . $filepath;

    my @file_infos = fileparse( $filepath, qr/\.[^.]*/ );
    given ( $file_infos[2] ) {
        when ( /^.pl$/ ) {
            system ( qq{/usr/bin/perl -wc $filepath} ) == 0 or say "\n" and $nb_errors++;
        when ( /^.tmpl$/ ) {
        when ( /^.js$/ ) {

my $filepath;
for my $l ( split '\n', `git diff-index -p -M --cached HEAD` ) {
    if ( $l =~ /^diff --git a\/([^ ]*) .*$/ ) {
        $filepath = $1;
    given ( $l ) {
        # if there is a file called *.log, stop
        when ( /\.log/ ) {
            say "$filepath contains console.log ($l)";
        # if there is a warn Data::Dumper::Dumper introduced, stop
        when ( /^[^(\#|\-)]+warn Data::Dumper::Dumper/ ) {
            say "$filepath contains warn Data::Dumper::Dumper ($l)";
        # check if there is a warn, that could be unconditionnal, but just warn, don't stop
        when ( /^[^(\#|\-)]+warn/ ) {
            # stay silent if we're in a template, a "warn" here is fair, it's usually a message or a css class
            unless ($filepath =~ /\.tt$/) {
            say "$filepath contains warn ($l)";
        # check if there is a ` introduced, that is a mysqlism in databases. Can be valid so don't stop, just warn
        when (/^\+.*`.*/) {
            say "$filepath contains a ` ($l)";
        # check if there is a merge conflict marker and just warn (we probably could stop if there is one)
        when ( m/^<<<<<</ or m/^>>>>>>/ or m/^======/ ) {
            say "$filepath contains $& ($l)";

if ( $nb_errors ) {
    say "\nAre you sure you want to commit ?";
    say "You can commit with the --no-verify argument";
    exit 1;
exit 0;

before any commit, the script will automagically check that:

  1. all the perl scripts you want to commit are compiling (perl -wc)
  2. there is no trailing warn, warn Data::Dumper::Dumper, conflict markers (<<<<, >>>>>, =====)
  3. there is no file called {something}.log

A caveat:

The script must -of course- be executable. If it's not, git won't complain

If you're a patch signoff-er and want to run this script when you apply a patch (through git am of git bz), just use .git/hook/pre-applypatch instead of .git/hook/pre-commit !

Display the branch you're on

If you want to permanently display the branch you're on, edit your .bashrc file and add the following at the end:

#prompt git
PS1='\D{%H:%M} \[\033[1;35m\]\w$(__git_ps1 " \[\033[1;34m\](%s)")\[\033[0m\]\$ '

your display will look like this:

17:47 ~/ (new/bug_7190 $%)$ 

displaying the directory you're on, the branch, and some informations about the status of your working directory

Git aliases to simplify command tasks

If you have Git-BZ installed, you can add the following aliases to your .gitconfig file to make applying a patch and reattaching it with your signoff a 1-command affair:

      so = !sh -c 'prove t xt && git commit --amend -s && git bz attach -e $1 HEAD' -
      qa = !sh -c 'git fetch origin master && git checkout -b bug$1-qa origin/master && git bz apply $1' -
      gr = log --graph --full-history --all --color --pretty=tformat:"%x1b[31m%h%x09%x1b[32m%x1b[0m%x20%d%s%x20%x1b[33m(%an)%x1b[0m"
      qa2 = "!f() { c=`expr $1 - 1`; git filter-branch -f --msg-filter \"cat && echo \\\"\nSigned-off-by: author <>\\\"\" HEAD~$c^..; }; f"

git so ####

Runs automated tests, signs off on the patch and attaches it to the bug report, obsoleting the unsigned patch.

git qa ####

Fetches the latest master from, creates a new branch off that for the bug you're testing, then applies the patch from Bugzilla

git gr

Not BZ-specific, but this draws a character-mode graph showing the branch structure and displays it through your pager.

git qa2 N

Not BZ-specific, this alias will add your signature as well as [PASSED QA] string to N patches. git qa2 3 => deal with 3 patches

Personal tools