Eclipse Yoxos Services Downloads Blogs About
Home > Blogs >

Archive for May, 2011

on May 30th, 2011Merging branches in Eclipse git (EGit)

It’s always a great idea to use a Source Control Management (SCM) system — even when you are working on personal projects, without any collaborators.  However, once you start to work with others and multiple streams / branches start to emerge, you need an SCM system that can handle a variety of different merge cases.  In this article I will describe a few different ways you can merge two branches using Eclipse Git (EGit).

In this article I will assume we have the following simple git repository:

 Merging branches in Eclipse git (EGit)

Figure 1: Simple git repository

 

A UI and Core bug have each been fixed in the master branch. Also, security was added to the application.  In the new_idea branch, an alternate approach to the UI and Core bugs were tried.  The security feature does not exist in this branch. The EGit history view would show this repository as follows:

screenshot 084 Merging branches in Eclipse git (EGit)

Figure 2: Initial history view

Now, assuming you have chosen alternate approach to the Core and UI bugs, there are at least four (4) different ways to merge. (If you have other ideas on how to merge these branches, please leave a comment).

1. Merge Tool

For those of you new to git, the most intuitive way to merge these branches is to use the merge tool.  You checkout the master branch and merge the new_idea branch. Since the same functionality was implemented in two different ways, you are almost guaranteed to have merge conflicts.

screenshot 085 Merging branches in Eclipse git (EGit)

Figure 3: Merge conflicts

Once you have resolved these conflicts, you must explicitly add the merged files to the git index (Team->Add) and then commit your changes.  The more seasoned git users will probably suggested that you rebase your new_idea branch on top of the commit C first (I didn’t do this).

screenshot 0871 Merging branches in Eclipse git (EGit)

Figure 4: Merged history view

Once you have successfully resolved the merge conflicts, added the affected file to the git index and committed, your history view should look something like Figure 4.

You can also cherry pick commits. For example, if you only want the alternate UI fix (commit G), but not the alternate core fix (commit F), you can cherry pick commit G.

2. Reset your Master Branch

Since git is a directed acyclic graph of commits with pointers (or references) to the tip of the branches, you can manipulate this graph.  If you checkout the master branch and then issue a hard reset to the new_idea branch (in the history view, right click on the new_idea branch and selected Reset->Hard), the master branch will now point to the tip of the new_idea branch.  You have essentially tried two different ways to implement the same functionality, and when finished, you choose the second approach.  However, you will not only lose the UI and Core fixes, but you will also lose the new security feature that you added (commit C, D and E).  In this case, it’s important that you rebase your new_idea branch off commit C first.

screenshot 088 Merging branches in Eclipse git (EGit)

Figure 5: Reset your master branch

Once completed, your history view should look something like Figure 5.  Note: you should never attempt to reset branches like this if you’ve shared your repository. If anybody else has worked on the master branch, you will cause them (and likely you) a lot of headaches.  This should only be used on private repositories, before you push.

3. Re-write history

A similar approach to #2, you can forcefully remove commits. Since you tried two different ways to address the same bugs, you can now delete the unwanted attempts.  If you have your master branch checked out, you can reset the branch to point to Commit C (Add Security).  This will effectively remove Commits D and E from your history.  Now you can merge (without conflicts) the new_idea branch.

screenshot 089 Merging branches in Eclipse git (EGit)

Figure 6: Rewrite history

Once completed, your history view should look something like Figure 6.  Note: you should never re-write history once you’ve shared a repository. Similar to #2, this will cause you, and any collaborators, a great deal of stress.  Another problem with this approach is that you are hiding what you really did.  If you ever wish to reexamine the original attempts to fix the UI and Core, you can’t.

4. Revert and merge

The best approach to this merge problem (IMHO), is to communicate to git exactly what you want to do.  In this case you fixed a few problems, were not happy with your solution, so you fixed them a different way.  In git speak, you committed D and E, you now want to revert D and E and merge commits F and G instead.  Unlike the rewriting history, this will actually create new commits to represent the reverted changes.    The original attempts are recorded in the SCM along with the the fact that you reverted them.

screenshot 090 Merging branches in Eclipse git (EGit)

Figure 7: Reverting commits

Reverting a commit is as easy as selecting it in the history view, and choosing Revert Commit from the context menu.  You can now use the merge tool with ease.  Once completed, your history view should look something like Figure 7.  Because you haven’t removed any commits, this approach is safe even if you’ve shared your repository.

What do other think? Do you have different ideas for merging branches with conflicting changes?

Remember, don’t fight your tools, just git ‘er done.

on May 29th, 2011Git Lesson: Be mindful of a detached head

A severed head is never fun, and in git this is no different. In fact, a detached head can cause quite the headache.  In this article I will discuss what a detached head is, how it can happen, and most importantly, what you can do about it.  But before I begin, let’s rehash a bit about how git works.

A git repository is a direct acyclic graph of commits.  Here is a very simple git repository.

 Git Lesson: Be mindful of a detached head

In the EGit history view it would look like this:

screenshot 078 Git Lesson: Be mindful of a detached head

Now, let’s assume that core and ui bugs (commits E and F) were not actually bugs, but rather controversial design decisions. And the the fixes broke other parts of the application.  After a quick meeting, someone suggests a new way of addressing the problems that will make everyone happy. One problem, they’re not sure if it will actually work.   Since they are using git, they checkout the code before the controversial bugs were addressed (Commit B) and they start hacking away.  Now here’s the crux of the problem: a branch is a linear set of commits, so where do these new commits live? The truth is, head is not actually visible from any branch now.  This is a detached head.

 

 Git Lesson: Be mindful of a detached head

In the EGit history view you can see you have no head.

screenshot 079 Git Lesson: Be mindful of a detached head

The good news is that you can quickly fix this problem by creating a new branch.  In egit, this is as simple as Team -> Switch To -> New Branch. Now, all these ‘detached commits’ will live on the new branch (new_idea).

 Git Lesson: Be mindful of a detached head

You can now see the new commits (and new branch) in the EGit history view:

screenshot 080 Git Lesson: Be mindful of a detached head

What if you accidentally  switched branches while you were on a detached head.  At this point, there is no way to access the commits because they are not attached to any branch. Luckily, git remembers all the commits, even the ones that happened while your head was detached.  Simply drop the command line and issue: git reflog

screenshot 081 Git Lesson: Be mindful of a detached head

You can now see the missing commits.  If you return to Eclipse and use Navigate -> Open Git Commit you can open the commit 73df399

screenshot 082 Git Lesson: Be mindful of a detached head

And directly create a branch from here

screenshot 083 Git Lesson: Be mindful of a detached head

Another very common way to lose your head is to checkout a remote branch. Again, if you checkout a remote branch, immediately create a new local branch for your new commits; otherwise you’ll be working with a detached head.

There is more information about detached heads here.  Remember, don’t fight your tools, just git ‘er done.

on May 11th, 2011Welcome Maggie

I’m proud to introduce the newest member of our family, Maggie Anne Bull.  Maggie and Mom are at home and both doing well.

For those of you keeping score at home; Maggie is our third, joining Sadie and Lily.

I may be a little slow on the bug reports for the next few days.

 Welcome Maggie

on May 10th, 2011And the winner is…

The results of the evaluations for the EclipseCon 2011 talks and tutorials is out. I thought it would be interesting to know which talks and tutorials were rated best. So I took the data from the PDF and did a little calculation.

Disclaimer / Government Health Warning: Any of these calculations may or may not be statistically significant.  They may include huge biases such as time of day, my personal decisions on how to do it and there might be errors in the data. So be forewarned, this is not an official ranking.

What I did was to count the +1′s, ignore the 0′s and substract the -1′s. The result was divided by the total number of votes resulting in an average value between -1 and 1. A very important decision is the cut point for being excluded from the sample, as there were a lot of sessions with very few votes. Some of them have an average of 1, but with very few votes this is quite arbitrary. So I decided to ignore sessions with less then 25 votes. That does not mean they were not great, I just don’t think it makes sense to include them from a statistical point of view. Further, if the average only differed 1%, I rated sessions equally to the next higher one.

To make a long story short, here are the results:

Keynotes

  1. What is Watson? – 0.96
  2. On Apache Hadoop – 0.92
  3. Java Renaissance – 0.57

Top Five Talks

  1. Case Study: NASA Ames uses Eclipse RCP for situational awareness of remote… – 0.98
  2. Modularity Wars Episode IV: A New Hope – 0.95
  3. Catch that bug before it happens! Improve code quality with static analysis – 0.94
  4. Put it in reverse: using eclipse to understand code that has already been… – 0.92
    Android development with Eclipse
    Building web apps with EMF and GWT
    A busy year for the Eclipse platform
  5. Cross-platform mobile development with eclipse - 0.91
    Dawn-Rise of the collaborative user interface

After the first 5 places there is a huge group of almost equally great talks between 0.9 and 0.8. So this is really only the tip of the iceberg.

Top Five Tutorials

  1. Hands on with C/C++ IDE - 0.95
  2. Developing Rich Clients with Eclipse 4.x RCP - 0.88
  3. Architecture patterns and code templates for enterprise RCP apps – 0.87
  4. Functional test automation for eclipse apps with Jubula – -0.85
    Style It! The eclipse 4 styling tutorial
    Pragmatic DSL Design with Xtext, Xbase and Xtend 2
  5. What every Eclipse Developer should know about EMF – 0.74

I would also like to mention the following sessions, which had very close to 25 attendees and had a very high ranking:

  • Building a great community for your open source project (23 attendees) – 1.00
  • The business of selling free software (21 attendees) – 1.00
  • Developing Machine Control Systems with eTrice – live demo (24 attendees) – 0.91

Congratulations to the speakers who gave these great talks.  I hope no one feels offended at not being listed and I’d be glad to hear from you if you think there is something wrong or unfair with regards to the method.  And, don’t forget to hold to the disclaimer icon smile And the winner is...

Here is the raw data:

EclipseCon evaluation

on May 4th, 2011Launch an OSGi app and automatically kill its running instance

If you use Eclipse to develop OSGi based applications you may use the OSGi Launcher provided by the PDE Tooling. It’s cool tooling because it gives you full control over the OSGi instance to be launched.  You can choose the OSGi framework (e.g. Equinox or Felix), select the bundles to install and much more.

But this launcher has one drawback that hurts every time I run across it. It appears when an OSGi application uses the OSGi HttpService. When you used this service you may have configured a port for it via the property “org.osgi.service.http.port” VM Argument.  I use this configuration all the time.

When I launch the application everything works fine the first time. But, during development I often come to the point where I need to relaunch the application. When I forget to kill the previous running instance I get a java.net.BindException because the address is already in use. So, to fix this I have to kill both instances and launch it again. This happens to me a lot just because I forget to terminate the previous instance. Of course this isn’t a bug because it’s often useful to launch a second instance of the same configuration. But, not when it comes to developing with the HttpService.

bindException Launch an OSGi app and automatically kill its running instance

There is a fix now (though not necessarily for my forgetfulness icon wink Launch an OSGi app and automatically kill its running instance ).  Luckily we live in a modular world in Eclipse. As a result, I was able to develop a separate bundle called the “OSGi Eliminator” (what a descriptive name icon smile Launch an OSGi app and automatically kill its running instance ). The bundle contributes the functionality to automatically terminate a running OSGi Instance when you try to launch the same instance a second time. This functionality already existed in the RAP launcher created by Rüdiger Hermann. All I did was to refactor the functionality out to make it run in a separate bundle and contribute to the OSGi Launcher instead of the RAP Launcher. This solves an annoying problem for me – maybe for you as well?

You can find this bundle on GitHub. I also created a p2 repository that lets you install the OSGi Eliminator directly into Eclipse.

© EclipseSource 2008 - 2011