Hitesh Bhatia « Intelligrape Groovy & Grails Blogs
Subscribe via E-Mail:

Hitesh Bhatia

http://www.intelliGrape.com/

OpenSource enthusiastic , practitioner of Groovy and Grails , working on SaaS model and looking forward to work on Android.

Posts by Hitesh Bhatia:

  • Creating GIT Alias

    30 Dec 2011 in Grails

    Since Git is awesome. It also provides functionality of making aliases.
    Example

    git config --global alias.co checkout 

    Here we created co as an alias for checkout (All the aliases that are created goes into .gitconfig file under home folder). Now to checkout a branch named testBranch, We can also write

    git co testBranch
    Switched to branch 'testBranch'

    Here are couple of interesting git aliases that we can make

    1.git config –global alias.undo ‘reset –hard’

    Now if we write “git undo” then any of changes after my last commit will be removed.

    2.git config –global alias.cleanup = ! git fsck && git prune && git gc

    In this Command we have used exclamation mark (!), this indicates that these commands would be running on terminal.
    And in git if I want multiple command to be running one after another we use double ampersand (&&) (We cannot use semicolon as we would usually do in terminal).
    This command will run git fsck (file check), git prune (to clean dangling commits/blobs) and git garage collection

    Number of git alias that can be made are endless all it needs is little creativity.

    _________________________________
    Hitesh Bhatia
    Mail,LinkedIn,Facebook,Twitter
    _________________________________
    • Share/Bookmark
  • GIT Maintenance

    30 Dec 2011 in Grails

    Since we use git numerous times a day over and over, hence needs maintenance. There are few commands that will help you to keep our git healthy. If you feel that you git has somewhat slowed down, these would do the trick for you.

    Since ours is a pretty big project . I run it once a week to make sure my git is healthy.

    1.git fsck

    This commands helps to identify any corrupted, unreachable (dangling blobs) objects in your database.
    Example

    git fsck
    dangling commit 652424f02c17b5d881158a625f1a34f6039dc231
    dangling commit 7e6510c2663530d949ea9fb67900496dcf5cbc4c
    dangling commit 177864469ea87564ade9101106d5aa85cceb0d29
    dangling commit 848665007bc412910b68dc3e528e74c23ce1f165
    dangling blob dfa6756511b525996946de84fa7f67b6f9a0d32f
    dangling blob e7f8f1145c7e739a6ffd8806a6d26d60f0443897
    dangling blob f1f905a5eb35724622249f25f6d9ef7c646e4283
    dangling blob 221417781e017cf02f90b872aea80f30ed41787c
    dangling blob c1356b24fb2a4ea453dba87ed4058e7a48e97dce
    dangling blob cb6fe3d5321b7b152a8781541bbc6132c59b9c94
    dangling blob 3ce2e76cd54f0c8265ec6167f78d5dfe71c2242d
    dangling blob daf073729960c40a44c5291c745092445bb2c3b0
    

    2.git prune
    With git fsck, we identified some of dangling commits and blobs, now to remove them we use git prune.
    Now After running git prune, If I run git fsck again, my git would be cleaner than before and healthy.

    git fsck
    dangling commit 177864469ea87564ade9101106d5aa85cceb0d29
    dangling commit 848665007bc412910b68dc3e528e74c23ce1f165
    

    But it still has some dangling commits.This is where git’s garbage collection comes in play.

    3.git gc (Garbage Collection)
    This command will gathers up all the loose objects and places them in packfiles. And removes objects that aren’t reachable from any commit. This command will save some space and speed up many of git commands.
    Example

    git gc
    Counting objects: 121775, done.
    Delta compression using up to 4 threads.
    Compressing objects: 100% (18239/18239), done.
    Writing objects: 100% (121775/121775), done.
    Total 121775 (delta 76189), reused 120913 (delta 75659)
    Removing duplicate objects: 100% (256/256), done.
    

    Output of this commands tells several things to us , that is compressed 18239 objects for us out of total 121775 objects. And it also removed 256 duplicate objects. This way my git stayed healthy.

    _________________________________
    Hitesh Bhatia
    Mail,LinkedIn,Facebook,Twitter
    _________________________________
    • Share/Bookmark
  • Grails:Domain Design Via Intellij Idea’s Diagrams

    23 Nov 2011 in GORM& Grails

    Here is an example of how to design Grails domain class with Intellij Idea.For this we need to have blank domain classes. So lets say we created  three Domain Classes Company,Book,Author

    1. To see relationship diagram, Selected Domain Class, and selected tab Domain Class Dependencies.It should look like this
      image
    2. Lets assume the relation that publication has many books. So I drag mouse from Company to Books. Which invokes a popup asking about relationship between the domains and type and name of the field.
      image
    3. On Selecting ok from previous popup. Their relation is defined in domain and in diagram. Similarly, lets define
      1. Book hasMany Authors,
      2. Authors hasMany Books,
      3. Book belongsTo Author
      4. And Diagram looked like this.

      image

    4. Next Step – Lets add fields to Domain Class all by Intellij Idea’s UI. Select package of domain class and pressshft+ctrl+alt+u. It’ll open package diagram of app.
      image
    5. Now lets add field name to domain Author. For that we’ll need to select domain author,right click and select fields. It’ll invoke a popup asking details about field.
      image
      Similarly we add field name to Author and published on to Book.
    6. Now lets add a method to named “publishedBefore” to Book which takes a date and return list of books published before that date.For this we need to right click on domain and select method.And it’ll invoke popup asking details.Select create and its done.
      image

    This was a simple and easy way to create structure of grails app in Intellij Idea.

    _________________________________
    Hitesh Bhatia
    Mail,LinkedIn,Facebook,Twitter
    _________________________________
    • Share/Bookmark
  • Groovy : Find Index of Element in Map

    10 Nov 2011 in Groovy

    Groovier way of finding index of element from Map. It can be obtained with findIndexOf Method, which accepts closure as argument.

    
    Map map=["groovy":1,"grails":2,"index":3,"element":4]
    assert 3 == map.findIndexOf{it.key=="element"}
    assert 0 == map.findIndexOf{it.value==1}
    
    • Share/Bookmark
  • Creating a Perfect System in few minutes.

    22 Oct 2011 in Grails

    There might come up a case where you need to install same kind of OS and other Softwares( which are required for daily use) on multiple systems. One of the ways to do it is follow the same procedure on each of the machines, which is obviously a pain. Here’s a easy way to do it by using Clonezilla.

    Now with clonezilla you will have to setup one system only, this this would obviously take time. But once the first system is done, you can just setup rest on other machines in few minutes.

    Here are the steps, that I used to follow to create system with both Windows 7 and Ubuntu in under 10 minutes. With all softwares installed which included Counter strike on windows, Couple of IDE’s on Ubuntu and Git , mySql , Java and more on Ubuntu.

    1. Setup a perfect system. Install OS and other software that are required. Once the system is ready we need to download few softwares.
      1. 7zip ( p7zip-full_9.04~dfsg.1-1_i386.deb).
      2. Tuxboot (Tuxboot-linux-8 ).
      3. Clonezilla (clonezilla-live-1.2.6-40-i686.iso).All three of these are freely available.
      4. And we would also need a USB drive preferably with space more than 16GB , as windows 7 installation takes 15 GB approx and Ubuntu 11.10 will take around 6GB and little extra for other softwares. Clonezilla uses very effective compression technique will reduce size to approx half. So 16GB USB drive would work just fine.
    2. Install CloneZilla on USB drive.
    3. Once Clonezilla is installed , its time to make image of Perfect System.
    4. After image is done, we are ready to clone out perfect system, to other machines.
    5. Making Clone of Existing image, Process to make clone are pretty much same as process described in step-3. Except Step-11 of “making image” . Just select restore disk, this will create clone of image on disk. And ALL EXISTING DATA FROM DISK WILL BE ERASED. Following  are the links of images depicting step 5.  Image 1,Image 2,Image 3,Image 4,Image 5,Image 6,Image 7,Image 8,Image 9. There images are taken from a camera, hence please don’t mind  their quality.
    6. This is it , your next system is ready within few minutes. Now this process can be repeated again and again, to create clone of existing systems.

    _________________________________
    Hitesh Bhatia
    Mail,LinkedIn,Facebook,Twitter
    _________________________________
    • Share/Bookmark
  • Create Image of System in USB, Using Clonezilla

    22 Oct 2011 in Grails

    Clonezilla is a software that can be used to make image of system that can later be used to install this image on new system, to make it a clone of former system.

    Here are the steps to create image of a system by clonezilla

    1. Create a Bootable USB drive Using Clonezilla. Follow the link to know more
    2. Boot the system you want you clone using Bootable USB (i.e Perfect System)
    3. On Booting by USB you’ll see Clonezilla home screen, select Clonezilla live.
    4. Next it will ask you for language (Choose the one you prefer)
    5. Then it will ask you for policy to handle keymaps, Select “Dont Touch my keymap”
    6. Next it will ask you whether you want to “start clonezilla” or “launch shell”, select “start clonezilla”
    7. Next it will ask you for mode, select device-image
    8. Next it will ask you where you want to mount image, select skip (as we want image on USB drive)
    9. Next it will ask you for which harddisk you want to make image, Select harddisk and press enter
    10. Next it will prompt you for beginner or advanced mode, select beginner
    11. Then it will ask which operation you want to perform, select savedisk, this will save local disk as image on USB drive
    12. Then it will prompt you fir name by which you want to save it.
    13. Then All you need to do is press enter few times ( only when prompted )
    14. Next it will show you screens actually working, and when it completes. You are ready with image of your system
    15. Go and create clone of your system, using the image in USB drive.

    Below are the links to images depictung full process.But images are taken from a camera, so please bear with their quality.
    Image 1, Image 2, Image 3, Image 4, Image 5, Image 6, Image 7, Image 8, Image 9, Image 10

    _________________________________
    Hitesh Bhatia
    Mail,LinkedIn,Facebook,Twitter
    _________________________________
    • Share/Bookmark
  • Installing CloneZilla on USB drive using Ubuntu.

    22 Oct 2011 in Linux& System

    Clonezilla is a software that allows you to do bare metal backup and recovery.

    To install Clonezilla we would need following Softwares, all of which are available freely.

    1. 7zip ( p7zip-full_9.04~dfsg.1-1_i386.deb)
    2. Tuxboot (tuxboot-linux-8 )
    3. Clonezilla (clonezilla-live-1.2.6-40-i686.iso)

    Here are the steps to install CloneZilla

    1. Install 7zip required to install Tuxboot
      1. sudo dpkg -i p7zip-full_9.04~dfsg.1-1_i386.deb
      2. It would show something like
      3. (Reading database … 168886 files and directories currently installed.)
      4. Preparing to replace p7zip-full 9.04~dfsg.1-1 (using p7zip-full_9.04~dfsg.1-1_i386.deb) …
      5. Unpacking replacement p7zip-full …
      6. Setting up p7zip-full (9.04~dfsg.1-1) …
      7. Processing triggers for man-db …
      8. And Step 1 is done. 7Zip is Installed.

    2. Run tuxboot

      1. By default tuxboot is not executable , change it permission to make it.
      2. chmod +x tuxboot-linux-8
      3. Run tuxboot with sudo permission
      4. sudo ./tuxboot-linux-8
      5. Set the values as shown in image

      Step 5 - Installing Clonezilla

      5.1. Select Diskimage – then in adjacent dropdown select iso
      5.2. And then select the clonezilla image ( the one you downloaded)
      5.3. Type – usb drive
      5.4. Press “OK” . And let it complete. It will ask to reboot afterwards , but you can also reboot later

    3. And your clonezilla usb is ready now. You may now use it to clone systems.

    _________________________________
    Hitesh Bhatia
    Mail,LinkedIn,Facebook,Twitter
    _________________________________
    • Share/Bookmark
  • Git – How to Change Master Branch

    29 Aug 2011 in Grails

    While working on Git with a big team, there are chances that a situation might arise when you want to set some other branch as master branch.Recently we were in a same situation. Several of branches were merged to master branch. And unfortunately one of the branches merged was created from an unstable branch. Making our master branch unstable. And we cherry-picked all stable changes to masterTemp branch.So we landed in a situation where we wanted masterTemp branch as master branch.And Here is how we did it.

    1 ) Renamed master branch to oldmaster.

    git branch -m master oldmaster

    Now there is no master branch on my local machine.

    2) Renamed my masterTemp branch to master

    git branch -m masterTemp master

    The branch which was named masterTemp on my local machine is now master

    3) Delete the branch from remote

    git branch -rD master

    4) Push the new master branch to remote

    git push --force origin master


    And its done.We had to perform step 3 and 4 because git does not allows us to delete master branch from remote. i.e git push origin :master wont work. Hence we forcefully pushed out new master branch to remote. Plus anyone who is working on same repo should delete their master branch , take git pull and create new master branch.
    (i.e git branch -d master; git branch -rD master;git pull ; git checkout -b master origin/master )

    _________________________________
    Hitesh Bhatia
    Mail,LinkedIn,Facebook,Twitter
    _________________________________
    • Share/Bookmark
  • How to Recover lost Commits in GIT

    11 Apr 2011 in Grails

    Recently while working on git I accidently lost my commits. But since git is a fantastic revision control system, it remembers what I committed. In other words it records where its HEAD is every time user commits. Here’s is the example of how I got my lost commits And got them back.

    This is my 5 recent Commits

    git log  -5 --pretty=oneline
    ea007ac3b7d1746d47ed7271a7cfcf262c2df907 Refactored Person Class
    25e48aefec136a0c3ec4d3c09ee0f6f74244db0e modified User controller
    03aa5f44ee9293a2d197edd060720682d6daddb5 modified readMe File
    d3d424dcaeddfb8078cc0d36886aa86a8b6da943 added ipr file to gitignore
    667227783e283c517f4bc879c129f50889f8709c another changes
    

    Now let say , I wanted to undo my changes to User controller and I did a hard reset to my commits .

    git reset --hard 03aa5f44ee9293a2d197edd060720682d6daddb5
    HEAD is now at 03aa5f4 modified readMe File

    And head is now set to commit “modified readMe File” . But later it came to my mind that I had also lost refactoring I did to Person class (Which took hours (whew)) , thats bad. If Now I check my logs it would be like this.

    git log  -5 --pretty=oneline
    03aa5f44ee9293a2d197edd060720682d6daddb5 modified readMe File
    d3d424dcaeddfb8078cc0d36886aa86a8b6da943 added ipr file to gitignore
    667227783e283c517f4bc879c129f50889f8709c another changes
    80eb4396fb70c8f59f624fc3e5a2601cd6b986a3 more changes to Demo File
    16b6202e925093b08e10a68037e7c64b450f0173 dummy commit

    But since Git is fantastic it always comes to rescue. as I told before git always records where HEAD was every time I commit, Which can be seen with git reflog command.

    git reflog
    03aa5f4 HEAD@{0}: 03aa5f44ee9293a2d197edd060720682d6daddb5: updating HEAD
    ea007ac HEAD@{1}: commit: Refactored Person Class
    25e48ae HEAD@{2}: commit: modified User controller
    03aa5f4 HEAD@{3}: commit: modified readMe File
    d3d424d HEAD@{4}: commit: added ipr file to gitignore
    6672277 HEAD@{5}: commit: another changes
    80eb439 HEAD@{6}: commit: more changes to Demo File
    16b6202 HEAD@{7}: commit: dummy commit
    

    To get my lost changes all I need to do is create a new branch with the lost commit that I wanted back.So here I am creating a branch named “lostCommit” with hash “ea007ac” .

    git branch lostCommit ea007ac

    And Now when I checkout my lostCommit branch and check log. Here is what it shows.

    git log  -5 --pretty=oneline
    ea007ac3b7d1746d47ed7271a7cfcf262c2df907 Refactored Person Class
    25e48aefec136a0c3ec4d3c09ee0f6f74244db0e modified User controller
    03aa5f44ee9293a2d197edd060720682d6daddb5 modified readMe File
    d3d424dcaeddfb8078cc0d36886aa86a8b6da943 added ipr file to gitignore
    667227783e283c517f4bc879c129f50889f8709c another changes 

    Woohoo , I got my lost commits.And all of the changes I think were lost.


    To explore more visit. git grep manual page or the git community book which is maintained by Scott Chacon

    _________________________________
    Hitesh Bhatia
    Mail,LinkedIn,Facebook,Twitter
    _________________________________
    • Share/Bookmark
  • GIT –pretty

    18 Feb 2011 in System

    git log is one of most useful commands to see information about commit , author , date and subject(comment)  while using GIT.This displays information in blocks and hardly 5 – 7 can be listed on normal screen size. But git provides –pretty option , so that we can format the output of git log;

    Here are some ways to do that

    git log --pretty=oneline

    Displays just a single line log ,which includes a commit-hash and Subject(Comment).something like this

    d1fc23c1edfd2d4ffe8ef5a873adff9321cde909 comment 3
    a8560c814ece4e03d39164885f44e0c5cb749928 comment 2
    db6195c635179b3556c41e2a974829c7e101ef51 comment 1
    

    some other predefined options are raw,fuller,full,medium and short. Since Git is Cool we can also use some custom formats.Like

    git log --pretty=format:"$h"

    will give abbreviated hash of committs(Where %h defines abbreviated hash).And it would be something like

    d1fc23c
    a8560c8
    db6195c
    

    Which is not quite useful , to make it better lets first see other options

    • %H  – Commit hash
    • %s  – Subject
    • %h  – Abbreviated hash
    • %an – Author name
    • %ae – Author e-mail
    • %ad – Author date
    • %ar – Author date, relative



    While using “format” with option “–pretty” we can also specify string inside quotes.Like

    git log --pretty=format:"%h was committed by %an , %ar"

    Would give something like

    d1fc23c was committed by hiteshBhatia , 5 months ago a8560c8 was committed by hiteshBhatia , 5 months ago db6195c was committed by hiteshBhatia , 5 months ago

    To make this more useful we can make slight changes to string passed to “format”

    git log -3 --pretty=format:"%h ||  %an  ||   %s  (%ar)"

    Which will give us commit hash , author name , subject  and relative date inside paranthesis.And -3 here is number of logs .So that his whole screen is not cluttered.This is the way I like to use it.

    d1fc23c ||  hiteshBhatia  ||   comment 3  (5 months ago)
    a8560c8 ||  hiteshBhatia  ||   comment 2  (5 months ago)
    db6195c ||  hiteshBhatia  ||   comment 1  (5 months ago)



    To explore more visit. git grep manual page or the git community book which is maintained by Scott Chacon

    _________________________________
    Hitesh Bhatia
    Mail,LinkedIn,Facebook,Twitter
    _________________________________
    • Share/Bookmark