DVCS part 2: initialize and import from local Repositories (EGit – HgEclipse)

part 1 of this DVCS – blogseries described how to install Git and Mercurial together with Eclipse Team provider Plugins EGit and HgEclipse.

Updated: 2010-02-22, 2010-03-12 more info at the bottom

Importing existing Repositories into Eclipse works well, but what to do if you want to create your own repositories and …

…put Eclipse projects under DVCS control

This part now will take a look how new local repositories can be created. If writing new software or trying to ‘move’ a bunch of existing Eclipse projects to a DVCS repository this would be the first step. And I don’t want to create a local repository for only one project in the workspace – the task is to create a repository for some projects:

An intuitive workflow to do this would be:

  • create a local repository on my disc from inside Eclipse
  • then select and add Eclipse projects to this repository by simply selecting them from inside Eclipse
  • after adding the projects should be under DVCS control

and if the repository already exists, then the workflow should be:

  • select the existing repository
  • select and add projects to this repo

hmmmm – I tried many ways and also read all documentation I found, but there seems no way to fullfill one of these workflows – wether using EGit nor HgEclipse.

What can you do already using EGit or HgEclipse to share a project ?

At first I tried EGit: Select a project, Team… – Share Project…. Git

Then this Dialog appears:

ok – I want to try the default path which is my project in my workspace and hit “Create Repository“.

If I look into my workspace folders, the git repository was created:

.../ekkes.sandbox.one/.git

but the project isn’t under EGit control – no decorators seen and Team… still shows “Share Project …“. I’m using latest nightly build “0.6.0.201002151207”. restarted Eclipse – same. tried to do again “Team… Share Project…” – EGit seems to know that there’s a repository, because the “Create Repository” button was disabled, bit it doesn’t work. perhaps an OSX problem ? Reading Lars Tutorial it should work. Did another try and changed the repository location to a folder outside my workspace (BTW: missed a “Browse” Button – had to enter the path directly). The .git project was created, but the project still isn’t under Version control. trying a second time “Team… Share Project…” then the “Create” button is enabled and no info about the just before created repository. Because there were also some issues reported at EGit newsgroup, I entered Bugzilla 302936.

The Bug is fixed 🙂 – you can create now projects and share as expected 🙂

Team… Share Project Git… Git now (as expected) creates the repository in your workspace and decorates the project as “under version control”:

As next we have to add the resources using “Team… Add to Version Control“:

Finally we can commit our changes “Team… Commit…“:

There’s no easy way to create a Local repository outside Eclipse Workspace using EGit. I’ll describe some workarounds later and one way to go later in this blog.

Now I tried the same using HgEclipse. Select a Project, Team… – Share Project… – Mercurial

Then you can use the default project root:

I used the Project Root and the Mercurial repository was successfully created – same as using EGit.

Now the project was decorated as “under Version Control”:

You can now use “Team… Add…

some decoration changes and the files of the project are added to the repository and you can do your first commit:

We have successfully created a .hg repository and added / committed our project into this repository 🙂

Hint: a really cool feature of HgEclipse is, that you can combine the two steps of “Add…” and “Committ…” – the Committ dialog also displays new files (not added till now):

simply check them – and they will be committed.

No I tried to create a repository outside the workspace and so I changed the dir:

I hoped that HgEclipse will create a new repository at this location, but an error happens:

I opened a Bug: http://www.javaforge.com/issue/10961 – then I looked at the location and recognized that HgEclipse has created a .hg repository at the choosen location. So I tried again “Team… Share Project…” and changed dir to the just created one – now the error says “Repo already exists

This Bug was fixed 🙂

Now you can create Repositories outside of Eclipse  Workspace using HgEclipse – the easiest way is from “Mercurial Repositories” View. From this View you can create Repositories:

the following Dialog got an extra checkbox to init this repository:

Summary: using HgEclipse I can successfully create a new repository at Project Root and add / commit the project. Thanks to fixing Issue 10961, now I can also create a repository outside Eclipse – I’ll describe this workflow later in another post.

Back to the task…

… how to put some projects under version control into a new local repository

Here’s one (suboptimal) workflow how to do this:

  1. init a new local repository from commandline
  2. export existing Eclipse projects into this location using Eclipse Export wizard
  3. add / commit from commandline
  4. import / clone from local repositories

Lets examine how it works using EGit and HgEclipse:

Please open a terminal program and cd to the location where you want to create your local repository:

cd /yourPathToRepository
git init yourRepository
cd yourRepository

No go to your Eclipse containing the projects, select the projects you want to put under Version Control and then

Export… -> Filesystem -> Next…

Now we have to adjust the export:

You have to select Option “Create directory structure for files” and its also a good idea to go thru the selected projects and deselect what you don’t want to have under DVCS control. Normaly this is controlled using Eclipse->Preferences->Team->Ignored Resources. I always deselect

  • /bin folders
  • .settings

Then hit “Finish” and switch back to commandline.

If you’re working with OSX, then you know that there can be “.DS_Store” files in your directory – so its a good idea to delete them before adding the projects to the repository, then we’ll add the projects and do a first commit and take a look at the log – all from commandline:

find . -name *.DS_Store -type f -exec rm {} \;
git add .
git commit -m 'initial add'
git log

you should get an output like:

now we

  • switch back to Eclipse
  • open a new empty workspace

to see if we can import our projects from the just created local repository. Import… -> Git -> Git Repository

then you have to select your local repository – unfortunately you have to enter the URI directly – there’s no file browser:

Bugzilla 303402 fixed the missing File Browser:

as next step you have to select the branches you want to import. Our new repository only contains a “master” branch:

now you can configure the storage location (let the default to get it into your workspace) and check if you want to import projects after clone:

now you can select the projects you want to import. please watch that the checkbox “Enable Git operations on imported projects“:

if all went well, you should see the projects in your workspace decorated as “master“:

now let’s do the same using HgEclipse:

Please open a terminal program and cd to the location where you want to create your local repository:

cd /yourPathToRepository
hg init yourRepository
cd yourRepository

Now go to your Eclipse containing the projects, select the projects you want to put under Version Control and then export the projects as described above for EGit.

Then copy the file “hgrc” into your repository directory directly below .hg

/yourRepository/.hg/hgrc

as described in part 1 of this blog series, the “hgrc” file contains your user-data.

Now we’re doing nearly the same as for Git from commandline to init your local Mercurial repository:

find . -name *.DS_Store -type f -exec rm {} \;
hg add
hg commit -m 'initial add'
hg log

Hint: if you don’t want to add a hgrc file with username, you can also add -u ‘username <usermail@xxx.org>’ to the commit command.

you should get an output like:

now we’re doing the same as for EGit and

  • switch back to Eclipse
  • open a new empty workspace

to see if we can import our projects from the just created local repository. Import… -> Mercurial -> Clone repository using Mercurial

as next you have to select your repository – fortunately HgEclipse has a browse – button to select “Local…” repositories. Select your repository and don’t forget to check “checkout as projects in the workspace“:

last step is to select the projects:

if all went well the projects should be decorated as projects under control of Mercurial:

Summary part 2: Both plugins are missing some features to ease the work to create Local Repositories and to add some Eclipse projects to ONE repository. Importing from Local repositories is more comfortable using HgEclipse: I can browse through the filesystem to select a repository and I only have to fill out three views instead of five for EGit.

So we finally got this file structure after importing the projects into our workspace:

Hint: As I told you in part 1 I’m just starting using DVCS – so perhaps I did some things wrong -please correct me and add comments if there are better ways to do this work. thanks.

Update 2010-02-22: EGit team fixed Bugzilla 302936 – now it’s again possible to share a Project on OSX. HgEclipse Team fixed Issue 10961 – now you create and init Mercurial Repositories outside Eclipse workspace.

Update 2010-03-12: EGit team fixed Bugzilla 303402 – now you can browse the file system to select repositories

The overview of this blog series can be found here: https://ekkescorner.wordpress.com/blog-series/git-mercurial/. The next part we will do some work using local repositories of Egit and HgEclipse: change something, commit and compare the Team options both plug-ins provide to you.

22 responses

  1. Git has the ability to ignore files by name. If you execute git help gitignore, it’ll tell you about them. If you create a file called .gitignore, and put in the following:

    .DS_Store
    ._*

    then it won’t add any of those when you check it in automatically. For what it’s worth, you can have a .gitignore in the root of your project, or for each subdir. There’s also a global one you can use, but it sometimes makes sense to have the .gitignore in your repository as well so that you share the exclusions too. Note that at the moment, EGit doesn’t respect .gitignore files as far as I know (though you can add files to the list by name).

    There’s an .hgignore file too.

  2. I’m very glad I found this. I have recently decided to do the same for the same reason – I’m a PhD student and I need to keep revision control over my software. However, I have a number of frameworks each with their own projects in them, and I would love to be able to have a workspace-wide repository. This seems like a good solution, but I’m using version control to streamline things – I’d want some way to do this inside Eclipse itself.

  3. Did you already create EGit / Mercurial bugs for the multi-project repository? (referring to: Both plugins are missing some features to ease the work to create Local Repositories and to add some Eclipse projects to ONE repository.)

    I also would like to see this feature, it is logical in my opinion to put related projects in the same repository.

  4. EGit fixed Bugzilla 302936 – now it works to put projects under version control,
    HgEclipse enhanced Mercurial Repository Handling to create/init local repositories outside eclipse.
    good news over all.
    …will update this blog soon and also create step-by-step workflows how to work with multi-project-repositories
    ekke

  5. One thing with this method, that may be moot given the bug fix, is that if you use the same directory for your workspace as you do for the repository, it puts a further level of abstraction in:

    Repo
    |
    |- .hg
    |- .metadata
    |- Project 1
    |- Project 2
    |- Repo
    …|- .hg
    …|- Project 1
    …|- Project 2

    This is a bit untidy. However, after playing around for a bit, if you create a new, blank repository, start a new workspace there, and then use the usual ‘Import…From Existing Project’, as long as you make sure you copy the project to the workspace, it will automatically get added to the workspace-level repository, without sub-repositories. As far as I’m aware, this means any changes that are made are made to the repository directly (which can confuse the Synchronise view I think).

    It would be nice if workspace level repositories could sync the .metadata from within eclipse, too. I’d love to have consistent views across any installation of Eclipse.

    This is a fantastic series, by the way – all the other PhD students are looking at it now, too! Thank you very much

    • Ewan,
      I dont have nested Subrepositories in my workspace, its more like this:
      Workspace
      |-Project 1 (a single project under version control)
      |–.hg
      |–src/
      |–…
      |-MyProjectGroup ( a repository holding some projects under version control)
      |–.hg
      |–ProjectA
      |—src/
      |—…
      |–ProjectB
      |—src/
      |—…
      ekke

      • Is that using the method outlined in this post, using the same workspace and repository?

        Here’s what I did:

        1. Create new repository (Folder “Workspace”).
        2. Export existing projects to “Workspace”.
        3. hg add, commit.
        4. Open Eclipse, switch workspace to folder “Workspace”.
        5. Import… Clone Mercurial Repository, chose “Workspace”.

        The import works, but the file structure is that described in my first post after the clone has taken place.

  6. Ewan, I’ll describe some usual workflows next days – then its easier to discuss and follow then in a comment thread.
    ekke

  7. Pingback: updates on DVCS Git/EGit and Mercurial/HgEclipse « ekkes-corner: eclipse | osgi | mdsd | erp

  8. Pingback: Using Git SCM « Lubos Programming Weblog

  9. Thanks Ekke,

    I’m trying to figure all of this out right now. My use case is this.. I want to be able to run / test Hudson locally so I want to be able to have Hudson treat all of my stuff as if it were one big git repos — I don’t want to have to add each project individually. I’m gong to muck around with that a bit..seems like it should be simple enough to do..

    Miles

    • run / test hudson locally is one of my things on my to-do-list. perhaps I’ll come back later and ask you 😉
      but at the moment deeply involved in a customer project.
      ekke

  10. Great post!!! To manage multiple projects in the same repository with EGit, you only need to run “git init” on the empty directory before you open Eclipse. After this trick you can do the Team -> Share and Eclipse will show you the path to only one repository for all the projects in the workspace.

    Hope this helps.

Leave a reply to ekkescorner Cancel reply