Saturday, September 10, 2011

Recover lost commits in GIT

Recover lost commits in GIT:
It's a big day for me!!…I am working with GIT from last 5 days and due to some or other reason I could not push my changes to a central repository.
Generally the process I follow is commit, fetch, rebase and push paradigm everyday to make sure that my workspace is clean and latest. But, today I couldn’t go through complete process due to “fatal: malloc, out of memory “issue while doing the rebase. In the process I lost few commits but I am not sure what had happened.
I tried different options and finally I could manage to recover my lost commits with the following approach.
Please find the screenshot for memory issue.

GIT provides a greater flexibility and it manages commits as a snapshot.  Even though git status command is not saying anything about your commits but it will be there somewhere in your local repository.
Here you go...
Step 1: Check git reflog to see all your commits.
Ø  Git  reflog show

This will show all commits which you have done in your local repository including your lost commits.
Every commit will have a hash id, identify the hash id for your lost commit.
Ex:  Here, my lost commit id is “85123ef”.
Step 2:  Confirm your commit
Ø  Git  show  85123ef
This will clearly show you the description and change id of your commit.

Step 3:  Do checkout for your hash id.
Ø  Git  checkout   85123ef
This will recover your lost commit and take you back to where we were at the beginning.

Working with GIT

 How does it work?
       GIT is a version control system, works completely based on local repository concept. It’s completely different than any other source version control systems like Perforce (I was using this before we migrate to git). It will maintain local repository and central repository and whatever your changes you make to your files will be added to staging Area.
     When you commit your changes it actually records in a local repository as a snapshot and when you push your changes it will be merged with a central repository. Will use fetch concept to get the remote changes to your local repository.

Here you will find a list of GIT commands I generally use it during my development every day. Hope this will help you guys.

1.       Creating a new branch
Ø  git branch  [new branch name] 
Ex: > git branch mylocalJavabranch

2.       Checking existing branches in your local repository.
Ø  git branch

It will list down all your branches reside in your local repository.

3.       Checkout to the branch
Ø  Git checkout  [your branch name]

Ex: git checkout customer-fixes

If you are working with multiple branches. May be you could have created different branches for different features which you have been working with.

4.       Checking the status in your branch- It will show the added files and untracked files.
Ø  Git status

5.       Add unstaged files.
Ø  Git add –all

6.       Commit all your added and modified files to a local repository.
Ø  Git  commit –a  -m “description about your commit”

7.       Fetching from a central repository. It will bring all the modified files from a central repository to local repository.
Ø  Git fetch

8.       It’s always a good idea to fetch and rebase before you push your changes. This will bring all files from repository to your local branch and rebase your local changes on top of it.
Ø  Git rebase  [your remote branch]

Ex: git rebase origin/java

If any conflicts it will try to auto merge the files. If not, user has to merge manually and add that file to staging area so that it will be available for the next commit.

Due to conflicts, if rebase could not continue, use the following command to continue the rebase.
Ø  Git rebase –continue

9.       Push your changes to a central repository.
Ø  Git  push  gerrit  HEAD:refs/for/java

10.   Checking the log history. It will list down all the local commits.
Ø  Git reflog show
                     Every commit will generate a hash id to uniquely identify the commit. To check what you have submitted in that commit use this command.
Ø  Git reflog  [hash id]

Here is the cheat sheet for git @ git-cheat-sheet.pdf


Thursday, September 1, 2011

My favourite eclipse shortcuts

Are you looking for eclipse shortcut keys ??? or Do you want to know how to configure the shortcuts in eclipse?? Then this could be the best post for you !!

These are my favourite shortcuts, I generally use in my development. Hope this helps you guys to increase your productivity.
  1. Ctrl + Space bar – Content Assistance 
It will automatically give you the parameter hints or content assistance. This will help you to develop the code faster and as a developer need not to remember all the methods.
Ex1: Just type “sysout” and hit Ctrl+ Space . It automatically fills up  System.out.println()
Ex2: Just type “System.” and hit Ctrl + Space. It will show the proposals, this includes methods and variables which are possible on the particular element.

2.       Ctrl + 1 – Quick Assist!
Depending on the context this will show the possible actions which are possible on it.

3.       Ctrl + Shift + R.- Open Resource
If you want to open any file quickly without browsing for it in the Package Explorer then this is the shortcut for you. It will open a dialog box that accepts the name of the file you’re looking for. It even accepts wildcard characters.

4.       Ctrl + Shift + T – Open Type
If you want to find a Java class without browsing for it in the Package Explorer use this key, it will open up a dialog box for searching the class.

5.       Ctrl + O – Quick Outline
It shows the quick outline of the class. It will list down all variables and methods which are exist in the class.

6.       Ctrl + O + O – Quick outline with all levels
It shows the quick outline of the class. It includes super and sub classes methods and also variables.

7.       F4  - Hierarchy view
To find out the hierarchy of the class. It will list down all the implemented classes. In hierarchy view, it provides an option to check it’s super classes and subclasses.

  1. Ctrl + Shift + G – Finding the references
To find out the references of the selected element or method. All the results will be shown in the  search view.

  1. Ctrl + D – Deletion
This will delete the selected lines of code.

  1. Alt + Down/Up Arrow  key – Moving the code
For moving the selected code in a very simple way.

  1. Ctrl +T – Outline the hierarchy
To find out the hierarchy of the class. It will show the complete list, this includes super and subclasses.

12.   Ctrl + T+ T – Outline the hierarchy with only super types 
     To find out only super types.

  1. Ctrl + Q – Last edit location
To go to the last edit location. You might be modifying lot of code and suddenly you want to go back to the last edited location.

  1. Hold Ctrl  and Click the hyperlink –
To open the declaration or implementation of the class or method of which you want to see.

15.   Ctrl + F8 – Change Perspective
Quickly if you want to change the perspective then this is the key for you.

  1. Ctrl + Shift+ F  -Code Formatter
Just clean your code by using this. It will adjust the complete code based upon the formatting which you have configured.

It's all upto the developer to configure the shortcut keys. Preference Page will help you to configure these.

Open your Preference page and type "keys" in the search filter it will point to the right place where it will allow you to modify or configure new keys. 

Here is the screenshot for the same.

All the best!!


Wednesday, August 31, 2011

Notifier Design Pattern

Say you want to develop an application which will have many viewers/pages and all these have to be refreshed with the latest data as soon as the data changes in the engine.

A very simple example is - Weather Reports. When the data changes in the engine, the weather report page has to be refreshed to show the latest data.

Let's follow the below steps:
  1. Develop your report dialog - which will show the weather information.
  2. Implement the ChangeListener in the dialog report- This will act as a listener
  3. Implement your report data class, which contains the weather information. This will act a notifier and also as a registry.
  4. Register your listener with the data class so that the viewer will get a notification if the data changes.

Here is the sample code for it:

import java.util.List;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
 * Weather report dialog.
public class WeatherDetailsDialog implements ChangeListener {
 private TableViewer weatherViewer;
 protected WeatherDetailsDialog() {  
  //your specific data passed through constructor 
 protected void createReport(Composite topComposite) {
  Group dependantsViewerGroup = new Group(topComposite, SWT.NONE);
  dependantsViewerGroup.setLayout(new GridLayout());
  GridData gridData3 = new GridData(SWT.FILL, SWT.FILL, true, false);
  dependantsViewerGroup.setText("Weather Report"); //$NON-NLS-1$
  weatherViewer = new TableViewer(
    dependantsViewerGroup, SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.BORDER);
  final Table reportTable = weatherViewer.getTable();
  GridData gridData = new GridData(SWT.FILL, SWT.TOP, true, false);
  gridData.heightHint = 140;
  TableColumn cityName = new TableColumn(reportTable, SWT.LEFT);
  cityName.setText("City"); //$NON-NLS-1$
  TableColumn valueColumn = new TableColumn(reportTable, SWT.LEFT);
  valueColumn.setText("Value"); //$NON-NLS-1$
  TableColumn descriptionColumn = new TableColumn(reportTable, SWT.LEFT);
  descriptionColumn.setText("Description"); //$NON-NLS-1$
  weatherViewer.setContentProvider(new WeatherContentProvider()); 
  weatherViewer.setLabelProvider(new WeatherLabelProvider());
  //Register viewer with the weathe report data.
  List<IWeather> reportData = computingEngine.getReportData();
  for (IWeather data : reportData) {
     * Invoked when the target of the listener has changed its state.
     * @param e  a ChangeEvent object
 public void stateChanged(ChangeEvent arg0) {
  Display.getDefault().asyncExec(new Runnable() {
   public void run() {

This will notfiy as soon the data changes.

import java.util.ArrayList;
import java.util.List;
import javax.swing.event.ChangeListener;

public class Weather {

 private List<ChangeListener> registeredListners = new ArrayList<ChangeListener>();
 private ComputingEngine ComputingData;
 private Float newValue;
 private String description;
 public Weather(ComputingEngine ComputingData) {
  this.ComputingData = ComputingData;
  * Updating the status change. This will notify to all the registered listeners
  * Ex: It will notify to the viewer/UI about the weather change report
  *   */
 public void setValue(Float newValue) {
  this.newValue = newValue;
 public ComputingEngine getComputingData() {
  return ComputingData;
 public void setDescription(String description) {
  this.description = description;
 public String getDescription() {
  return description;
 public Float getNewValue() {
  return newValue;
 //Add your listener here
 public void addChangeListener(ChangeListener listener) {
  if (!registeredListners.contains(listener))
 //removing the listener
 public void removeChangeListener(ChangeListener listener) {
  if (registeredListners.contains(listener))
 //This will be triggered when the state changes
 public void notifyListeners() {
  for (ChangeListener listener : registeredListners) {

Tuesday, August 30, 2011

Progress bar in the Table Viewer

Use Case:
If you want to show a progress bar with in the Table viewer and it should represent what percentage of job has finished.

May be you want to show something like this:

We should implement the OwnerDrawLabelProvider on the particular column where you want to show the progress bar.

OwnerDrawLabelProvider: Is an abstract implementation of a label provider that handles custom draw. So this can be used to implement progress bar in the table viewer.

Let's take the example:

//Table viewer
tableViewer = new TableViewer(composite, SWT.FULL_SELECTION | SWT.V_SCROLL | SWT.H_SCROLL);
  reportTable = tableViewer.getTable();
  reportTable.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));

//Progress bar column
TableViewerColumn progressColumn = new TableViewerColumn(tableViewer, SWT.CENTER);
  progressColumn.getColumn().setText(""); //$NON-NLS-1$

//set progress bar lable provider on the progresbar column
progressColumn.setLabelProvider(new ProgressLabelProvider(tableViewer));

//Implemention of OwnerDrawLabelProvider

import java.util.List;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.viewers.OwnerDrawLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;

public class ProgressLabelProvider extends OwnerDrawLabelProvider {
 private TableViewer tableViewer;
 public ProgressLabelProvider(TableViewer tableViewer) {
  this.tableViewer = tableViewer;
 protected void measure(Event event, Object element) {
 protected void paint(Event event, Object element) {
  //Total Units to be executed
   int totalUnits = getObjects().size();
    //Identify the completed units by reading specific information
   int completedUnits = getCompletedUnits();
  //Calculate Percentage
   int percentage = (completedUnits * 100/totalUnits);
   Table table = tableViewer.getTable();
   TableItem item = (TableItem) event.item;
         int index = table.indexOf(item);
         Color foreground = gc.getForeground();
         Color background = gc.getBackground();
         Rectangle bounds = ((TableItem) event.item).getBounds(event.index);

         int width = (bounds.width - 1) * percentage / 100;
         gc.fillGradientRectangle(event.x, event.y, width, event.height, true);
         Rectangle rect2 = new Rectangle(event.x, event.y, width - 1, event.height - 1);
         String text = percentage + "%";
         Point size = event.gc.textExtent(text);
         int offset = Math.max(0, (event.height - size.y) / 2);
         gc.drawText(text, event.x + 2, event.y + offset, true);
 public Image getColumnImage(Object element) {
  return null;