Automatic creation of repository when using gitolite

The last couple of days I’ve been playing around with git and setup my own git server using first gitosis and then gitolite. When playing around I created a lot of different test repositories to make sure everything work. To stop repeating myself I created three minor bash functions to make it easier to add repositories in gitocline. The script I ended up with are the following:

ar() {
    if [ "$1" == "" ] ; then echo "[E] One arg is needed!"; return 1; 
else
        cd ~/<some path to your>/gitolite-admin/conf
        echo -e "" >> gitolite.conf
        echo -e "repo ${1}" >> gitolite.conf
        echo -e "RW+ = <your gitolite username>" >> gitolite.conf
        cd ..
        git commit -am "Added repo ${1}"
        git push
    fi  
}

sr() {
    if [ "$1" == "" ] ; then echo "[E] One arg is needed!"; return 1; 
    else
        mkdir <path to where you have your repositories>/${1}
        cd <path to where you have your repositories>/${1}
        git init
        git remote add origin gitolite@e2n.no:${1}
        touch README
        git add .
        git commit -am "Initial commit"
        git push origin master
    fi
}

cr() {
    if [ "$1" == "" ] ; then echo "[E] One arg is needed!"; return 1; 
    else
        ar ${1}
        sr ${1}
    fi
}

It is somewhat static code, but I’m not that likely to change where I have the folders on my local computer anyway. I should probably mention that these three functions are stored in the .bashrc file and that I assumes you have checked out the gitolite-admin repository. Let us walk through the three functions.

  • ar (short for add repository): will navigate to the configuration folder for gitolite and add the repository plus read and write access for a user name (should probably use a group instead). It will also commit the changes and push them to the server to create the repository.
  • sr (short for set up repository): will first create a folder for the repository, then initialize git in that folder, add a remote to your new repository and make an initial commit and push.
  • cr (short for create repository): will execute both the above functions.

This will allow us to create a repository and set it up on our local machine by running

$ cr repository_name

That’s all.

Leave a comment

5 Comments.

  1. Gasser El-sayed

    i tried the following script all what it made that update the gitolite.conf file with the new repo and commits the changes to the gitolite server but didn’t create the repo there. and when run it from java application gives I/O exception.

    createNewRepository() {

    if      [ "$1" == "" ] ; then echo "the repo name is needed"; return 1; 
    elif [ "$2" == "" ]; then echo echo "the gitolite-admin pass is needed"; return 1; 
    elif [ "$3" == "" ]; then echo echo "the user name is needed"; return 1;
    else

    $PATH=$2 cd $PATH/conf echo -e "" >> gitolite.conf echo -e "repo ${1}" >> gitolite.conf echo -e "RW+ = $3" >> gitolite.conf cd $PATH git add . git commit -am "Added repo ${1}" git push

    fi

    }

    the output after running the command “createNewRepository testRepo /home/gasser/gitolite-admin Demo “

    gasserbash: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/lib/mvn/bin:/usr/lib/jvm/jdk1.6.0_21/bin:/usr/lib/mvn/bin:/usr/lib/jvm/jdk1.6.0_21/bin=/home/gasser/gitolite-admin: No such file or directory bash: cd: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/lib/mvn/bin:/usr/lib/jvm/jdk1.6.0_21/bin:/usr/lib/mvn/bin:/usr/lib/jvm/jdk1.6.0_21/bin/conf: No such file or directory bash: cd: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/lib/mvn/bin:/usr/lib/jvm/jdk1.6.0_21/bin:/usr/lib/mvn/bin:/usr/lib/jvm/jdk1.6.0_21/bin: No such file or directory [master 4acb7fc] Added repo testRepo 1 files changed, 3 insertions(+), 0 deletions(-) ssh: connect to host 192.168.3.104 port 22: No route to host fatal: The remote end hung up unexpectedly

    • You could try to implement my commands on your server where you have your java application and then use your java application to execute those commands. Hard to say what you are doing wrong. It also seems like you fails to connect to the host.

  2. Gasser El-sayed

    hi, sorry i didn’t make the server up as i was virtual box server and this is the result after running it. but the massages for no such file or directory that blocks the java program.

    bash: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/lib/mvn/bin:/usr/lib/jvm/jdk1.6.0_21/bin:/usr/lib/mvn/bin:/usr/lib/jvm/jdk1.6.0_21/bin=/home/gasser/gitolite-admin: No such file or directory bash: cd: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/lib/mvn/bin:/usr/lib/jvm/jdk1.6.0_21/bin:/usr/lib/mvn/bin:/usr/lib/jvm/jdk1.6.0_21/bin/conf: No such file or directory bash: cd: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/lib/mvn/bin:/usr/lib/jvm/jdk1.6.0_21/bin:/usr/lib/mvn/bin:/usr/lib/jvm/jdk1.6.0_21/bin: No such file or directory [master 38edcad] Added repo testRepo2 1 files changed, 3 insertions(+), 0 deletions(-) Counting objects: 8, done. Delta compression using up to 2 threads. Compressing objects: 100% (6/6), done. Writing objects: 100% (6/6), 669 bytes, done. Total 6 (delta 1), reused 0 (delta 0) remote: Already on ‘master’ To gitolite@192.168.3.104:gitolite-admin f6ed0a8..38edcad master -> master

    • Are you using the scripts in my example? I would suggest you to try them out first one by one so you get them to work. After that I would implement the java program as a wrapper if you need it. When implementing the java program I would suggest you to implement three different methods the same way as I did.

  3. thanks Tomas,

    it is solved by making the paths exported in the bashrc file to be defined variables ‘exported’ in the .bashrc file.