Posted on Leave a comment

Enable command and branch name autocompletion for GIT

Git Logo

When working in GIT based software projects that have a broad range of GIT branches the need for autocompletion of these branch names and GIT commands in general in the shell is one of the first tasks you want to setup for your developers (and yourself ;). After all it’s all about removing impediments to get highly motivated developers who produce high quality code in return, isn’t it?

Setup git-completion.bash in your shell to enable command and branch name autocompletion

By default GIT ships a file called git-completion.bash (at least on Linux, Mac and most probably other major platforms as well) that when setup in your shell will autocomplete commands and branch names, nice right! This file actually represent a comprehensive ruleset that will autocomplete all kinds of GIT commands, try it out you will be amazed.

Normally you find this file in contrib/complete/git-completion.bash but you can also download it from github.

All you have to do to enable branch name and command autocompletion for GIT is to add a source reference to git-completion.bash in your shell’s profile (~/.bashrc or ~/.profile). So open up your shell’s profile (I’ve chosen ~/.profile here)

vi ~/.profile

and add a source call to the git-completion.bash file

source ~/.git-completion

In case there’s no immediate effect do a manual source call on your shell profile file:

$ source ~/.profile

This should be all to enable branch name autocompletion for GIT. Enjoy your developers’ and your new productivity!

Posted on Leave a comment

Custom build script for GIT based projects

Git Logo

When managing smaller GIT based projects I find it helpful to opt for custom build scripts rather than having to deal with the overhead of setting up proper Jenkins (or-likewise) runtime environments.

In order to deal with this need I’ve created a simple build script that creates a compressed TAR file based on committed files.

Furthermore, since I use version numbers for e.g. JavaScript builds it’s always nice to have automatic file inclusion/exlusion based on this number, say, read from a config file.

In this particular example I want to read the version number from a variable called MY_VERSION in file config/base.php and include a special JavaScript build-folder in the target compressed TAR file. In addition, I only want to include files in the src-folder, as well as manually include other folders.

So, here it is:

#!/bin/sh
#
# REQUIREMENTS:
# - git
# - php
# - sed
# - xargs
# - tar
#

#(absolute) path to PHP, or simply 'php' if it is in the PATH
PHP_PATH="php"

#release number should be read from config/base.php
PHP_REQUIRE="require('$(pwd)/config/base.php');"
RELEASE_NUMBER=`$PHP_PATH -r "$PHP_REQUIRE echo MY_VERSION;"`

#hold list of files to be added to release tar.gz
TMP_SRC_FILE_LIST=$(pwd)/TMP_RELEASE_INCLUDE

#get list of git's tracked files
git ls-files > $TMP_SRC_FILE_LIST

#filter only SRC directory
sed -i '/^src./!d' $TMP_SRC_FILE_LIST

#manually include additional folders of your choice
sed -i '/^src\/js\/folder1/d' $TMP_SRC_FILE_LIST
sed -i '/^src\/js\/folder2/d' $TMP_SRC_FILE_LIST

#add RELEASE directory only
`echo "src/js/release/$RELEASE_NUMBER" >> $TMP_SRC_FILE_LIST`

# enclose lines in "" to enable spaces in file and folder names
sed -i 's/^/"/g' $TMP_SRC_FILE_LIST
sed -i 's/$/"/g' $TMP_SRC_FILE_LIST

#create compresse TAR
cat $TMP_SRC_FILE_LIST | xargs tar czf release_$RELEASE_NUMBER.tar.gz

#clean up
rm $TMP_SRC_FILE_LIST

That’s it – Feel free to adjust it to your needs.

Posted on Leave a comment

Using multiple SSH Keypairs for Git

Git Logo

When using multiple (GIT) services and repositories from different provider you are very likely to also use different sets of SSH keypairs. At first managing multiple keypairs might sound a little complicated but it is in fact quite easy.

Let’s say you have three keypairs for three different GIT services:

  • GitHub
  • PHP Cloud
  • your local GIT server

In order to separate access to these services you are using different keypairs for each service. In order to tell GIT to automatically choose the right keypair for the service you are trying to access you need to edit your local SSH config file (.ssh/config), as well as the GIT repository’s config file (.git/config).

First, you need to create an alias for each service in your SSH config file:

Host phpcloud-mycontainer
  HostName mycontainer.my.phpcloud.com
  User mycontainer
  IdentityFile ~/.ssh/id_rsa.phpcloud-mycontainer

Host github-myaccount
  HostName git.github.com
  User myaccount
  IdentityFile ~/.ssh/id_rsa_github-myaccount

Host localgitserver-myaccount
  HostName localgitserver.local
  User myaccount
  IdentityFile ~/.ssh/id_rsa.localgitserver-myaccount

As you can see three aliases have been specified for the three services. This provides you with the option to specify what key to use depending on the service.

In order to “activate” these alias for existing repository clones you need to edit .git/config to set the proper remote origin. So for instance your config for GitHub would look something like the following:

[remote "origin"]
  url = ssh://git@github-myaccount:myaccount/myrepo.git
  ...

Now all you need to do is the use the alias when accessing your services. This way the proper SSH key will be determined.

Also note that this approach is not limited to GIT – you are able to use these simple steps for any service based on SSH.