Wednesday, November 4, 2015

Unleashing the Git - part 1 - Fundamentals

Git is a distributed version control system. It has become extremely popular and replacing other version control systems in the industry very fast. Hence, knowing some day to day uses of git is very essential for all developers. I thought of writing some series of posts to cover some very useful git usages along with an example for you to grasp it easily. This is the first post of that series.

Before we delve into details on using the git, make sure you have git installed. If not, you can find details of git installation in the following location.

https://git-scm.com/book/en/v2/Getting-Started-Installing-Git

Ok. Assuming you have git installed, let's dive into using it. 

Go into a directory where you want to monitor for changes. Let's create an empty directory called 'example' in $HOME. I'll refer the directory as $HOME/example from this point.

Most of the commands and examples are in par with the following two books related to Git. It's really worth the time to read these two books if you are working with git on day to day basis.

Git: Version control for everyone
Pragmatic Guide to Git
$ cd $HOME/example
First, let's find out some initial git commands you might come across when initializing a git repository.
$ git init
Initializes empty Git repository in /home/shazni/example

To configure Git, you can use the following commands:
$ git config --global user.name "your full name"
$ git config --local user.name "your full name"
$ git config --global user.email "your email id"
$ git config --local user.email "your email id"
$ git config -l // lists all the configuration variables
$ git config --global color.ui auto  // Optional
$ git config --global core.editor /path/to/editor  // Optional
$ git config --global merge.tool "gvimdiff"   // Setting the diff tool for git mergetool command
Before we proceed, if you need any help on git, issue the following command.
$ git help
If you need to see the built-in documentation of a particular command, you may issue
$ git help <command name=""></command>     // e.g. git help init  
Now copy a file called abc.doc to the directory. This file is still not being monitored by git. Hence, it is referred to be in unstaged state. To make it into staged state, i.e. to make it available for git to monitor it, we have to issue the following command.
$ git add abc.doc
Issue the following command to see the status of all the files.
$ git status
To add multiple files, we may use the following commands,
$ git add .
$ git add *.docx
Remember these commands would add all the files and all the .docx files to the staged state respectively. This includes all the temporary files of the original files, which is often named after ~ with it's original name to save state of the files for disaster recovery. For example when abc.doc is open there can be another file name ~abc.doc. When we use 'git add' even this file is staged. This is at times troublesome. Git has a workaround for this. We can create a file named .gitignore and specify what files and what types of files we need to ignore from 'git add'. Create a file named .gitignore and type the following.

~*.*

This is a pattern. This says to avoid all temporary files like ~abc.doc

Undoing Uncommitted Changes
git reset abc.doc // To make from staged to unstaged before commiting or more specifically

git reset HEAD -- abc.doc // Here -- says Git that everything follows are file names, not commands
With git reset you are telling Git, “Change the index— the staging area — to the latest version of this file.”. Normally git reset is used when you want to unstage already tracked files

Following is normally used to un-stage new files that you staged but still not in git's control. The file will still be in the local working tree
git rm --cached abc.doc // git rm is usually done to rm files from remote repository. Adding --cached options tells to leave the working tree alone.

git checkout -- abc.doc // Caution : Be careful with this command. This will completely wipe out all your current changes and brings back the old file you committed
Though the files can be staged/unstaged it's still not under version control until we commit the files. Only then Git will look for changes of the current version with the last saved version.
$ git commit -m "Initial commit"
$ git status // There won't be any unstaged or even staged files. All are commited now
Now if you modify a file and issue a 'git status' command. It should show that your file is modified. To include it for next commit, let's add it.
$ git add abc.doc
$ git commit -m "Second commit with changes"
$ git log    // Provides history of the repository. The ouput will be as follows.
commit ca8da1d78daa8453dd478da7610e3c375ae917db
Author: Shazni Nazeer <mshazninazeer gmail.com="">
Date:   Tue Dec 3 12:37:24 2013 +0530

    More changes to Refactor doc

commit 93c1e9296a901bee38806e216d72abd74509a18f
Author: Shazni Nazeer <mshazninazeer gmail.com="">
Date:   Tue Dec 3 12:30:05 2013 +0530

    Initial commit
We need the commit id (the 40 character id in front of the commit word). If we need to checkout the files at the first commit.
$ git checkout 93c1e    // Note only the first 5 character is enough to checkout. 
Now when you checkout, the directory would contain the file before you modified. If you want to checkout a previous commit with latest intact, we need to do branching. Will come back to that shortly. But let's get our latest.
$ git checkout master

Previous HEAD position was 93c1e92... Initial commit
Switched to branch 'master'
If you want to permanently move back to the previous state and throw away the latest changes, you can issue the following command. This will revert all the commits you have done after this commit.
$ git reset --hard 93c1e   // What happens is that you have changed the master branch to previous commit
Moving files in git

If you need to rename or mv a file in git we perform the following.
$ git mv abc.doc xyz.doc
$ git commit -m "Renaming abc.doc to xyz.doc"
Now say we have a directory called docs in the repository.
$ git mv xyz.doc docs/
$ git commit -m "Moving xyz.doc to abc.doc"
If a file completely be removed from the repository, you issue the following command.
$ git rm -- abc.doc
$ git rm -r -- docs/
$ git commit -m "Removing abc.doc and the docs directory from git"
Say you issued a git rm command, but didn't commit yet. You need the removed file or directory; as previously mentioned
$ git reset HEAD -- abc.doc docs/

Monday, October 19, 2015

Accessing local H2 database of WSO2 products

All WSO2 products internally use the H2 database by default. H2 is a light weight file based database system. By using H2 WSO2 products can readily be used for experimentation without configuring an industry ready RDBMS. Though, H2 is not recommended for any production systems, it's often the case that many try out WSO2 products with H2 initially thereby frequently needing to view the contents of the tables. 

This short tutorial gives you a guide to view WSO2 database tables in H2.

The process is simple. Add the following configuration to the repository/conf/carbon.xml and restart the server.
    <H2DatabaseConfiguration>
        <property name="web" />
        <property name="webPort">8082</property>
        <property name="webAllowOthers" />
    </H2DatabaseConfiguration>
Once the server is restarted, navigate to http://localhost:8082/. You'll be redirected to a view like following.



In JDBC URL fill out where the WSO2CARBON_DB is. It's located in repository/database directory. Assuming the CARBON_HOME is you product home directory, JDBC URL would be, jdbc:h2:CARBON_HOME/repository/database/WSO2CARBON_DB (make sure to replace the CARBON_HOME with your actual path) Click connect to see the tables and you are good to try out executing some SQL.

Wednesday, April 1, 2015

Installing and testing Django

Django is a web development framework for python. It eases your development time and makes web development a real joy. In this article I'll show you how to install Django and test it, given you already have python installed in Linux. Installing Django is a simple step and it's a single command.
$ sudo pip install django
It assumes you have python-pip module installed. If not install it using the following command.
sudo apt-get install python-pip        // In Debian Linux/Ubuntu

sudo yum install python-pip         // In Red Hat and Fedora
After installing you can check whether everything is setup properly. Use the following commands.
$ python
Python 2.7.8 (default, Nov 10 2014, 08:19:18)
[GCC 4.9.2 20141101 (Red Hat 4.9.2-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> print django.VERSION
(1, 7, 7, 'final', 0)
Let's start off with setting up a default project to see the Django in work.

Create a directory of your choice. Say $HOME/DjangoProjects.

Navigate to that directory and issue the following command.
$ django-admin.py startproject mysite     // Here 'mysite' is your project name
This will create a directory called mysite, inside which you'll find a file named manage.py and another directory named 'mysite' which in turn contains four files; namely, __init__.py, setting.py, urls,py and wsgi.py.

Let's not bog down into details of what these files are all about. You can find detailed information in the Django web site https://www.djangoproject.com/

Let's start the Django's test server for this default site. (This server is merely for testing and not recommended to use in production). Type the following to start the server.
$ python manage.py runserver 9876

This should start run the server on port 9876. To see the welcome page, open a browser and navigate to http://127.0.0.1:9876/.

Hope this was a good starting point for you to learn Django. Enjoy working with Django.