How to programmatically import a reference to an external project - eclipse

I want to programmatically import a project (in this case a project in a Git repo) into an eclipse workspace in a way that does not copy the files but instead creates a reference to them.
I have tried
ImportOperation importOperation = new ImportOperation(path,
folder, FileSystemStructureProvider.INSTANCE, overwriteQuery);
importOperation.setCreateContainerStructure(false);
importOperation.run(new NullProgressMonitor());
but this copies everything.
I have tried using setCreateLinks(true) and setVirtualFolders(true) but the best result still created a folder containing just a .project file in the workspace. When I look at what egit does, there is no folder in the workspace but there is still some kind of link to the external git repo.
I see that egit calls SmartImportJob when importing projects but this is in an internal package.
How can I link to the external project and import it into my workspace?

Related

Some or all projects can not be imported because they already exist in the workspace error in Eclipse" [duplicate]

I just added repository A to my Git perspective in Eclipse (which also instanced a local Git repository under some path) and can now see the working tree. Now how do I work on the files in the package explorer?
From what I've seen I have to use "import projects" - what does this do? My Eclipse workspace seems to be empty, while made changes will all display in the git repository.
Also, how do I work with multiple repositories and how do I switch between them? When I try to "import projects" from repository B I get the message that Some or all projects cannot be imported because they already exist in the workspace
The issue here is that Eclipse maintains its own file system representation that includes projects. This was required historically to support incremental compilers, even if the file system is not capable of sending notifications.
The Import projects option you have seen relates to importing projects from a git repository into the Eclipse file system; adding the repository to Eclipse allows Eclipse to know about the repository, and execute repository operations (e.g. fetch, commit, push).
About why doesn't the project import work, the Eclipse file system contains a set of top-level projects (each can be either in the workspace folder or somewhere on the disk, e.g. in a git repository); however, it is not possible to have two projects with the same name in the same workspace.
If you want to open the "same" project from two repositories (e.g. two development branches), you have to rename them not to have a common name. This name is stored in a .project file in the project root (or if none exists, it will be derived); you can use a refactor operation to change the project names. After that, you can import both projects into your Eclipse workspace.

How to initialize git for a new eclipse (neon) java project

I installed a fresh copy of eclipse Neon, and created a new gradle java project in a new and shiny workspace. What is the best practice for adding git to the party?
I read that initializing git in the project directory is really a bad idea.
What is a particularly good idea then?!
Thanks!
The good idea is to git init a parent folder of the project different from the main workspace folder.
This is exactly what happens if you let Egit git init your project (Right click on project -> Team -> Share Project -> Git ->...) and select an external folder as repository, say c:\users\john\my-git-repository.
You will then have two folders:
c:\users\john\my-git-repository containing the \.git folder and \<my-project> folder
c:\users\john\<eclipse-workspace> the eclipse workspace folder, which will NOT contain your project folder (remember that eclipse workspace is just a logical container for projects, they don't need to be physically there).
Another option is to create a folder inside the workspace, create the project as a subfolder of that folder and then git init that folder. This way:
c:\users\john\<eclipse-workspace>\shared-projects\<my-project>
You will create the repository in the \shared-projects folder (either by command line with git init or from whithin Eclipse with the wizard), which will contain the \.git folder, \<my-project> folder and any other project you want to share.
Remember why Eclipse suggests to keep repositories outside the workspace (https://wiki.eclipse.org/EGit/User_Guide#Creating_Repositories):
It is a good idea to keep your Repository outside of your Eclipse Workspace.
There are several reasons for this:
The new Repository will consider the complete folder structure of the Eclipse workspace as (potential) content. This can result in performance issues, for example when calculating the changes before committing (which will scan the complete .metadata folder, for example)
If your git repo is in the \shared-projects folder the repository will NOT consider the complete folder structure of the Eclipse workspace as (potential) content and will NOT scan the .metadata folder since it's outside the repo. The only contents of the repo will be your shared projects!

Correct way to import projects in EGit?

I just added repository A to my Git perspective in Eclipse (which also instanced a local Git repository under some path) and can now see the working tree. Now how do I work on the files in the package explorer?
From what I've seen I have to use "import projects" - what does this do? My Eclipse workspace seems to be empty, while made changes will all display in the git repository.
Also, how do I work with multiple repositories and how do I switch between them? When I try to "import projects" from repository B I get the message that Some or all projects cannot be imported because they already exist in the workspace
The issue here is that Eclipse maintains its own file system representation that includes projects. This was required historically to support incremental compilers, even if the file system is not capable of sending notifications.
The Import projects option you have seen relates to importing projects from a git repository into the Eclipse file system; adding the repository to Eclipse allows Eclipse to know about the repository, and execute repository operations (e.g. fetch, commit, push).
About why doesn't the project import work, the Eclipse file system contains a set of top-level projects (each can be either in the workspace folder or somewhere on the disk, e.g. in a git repository); however, it is not possible to have two projects with the same name in the same workspace.
If you want to open the "same" project from two repositories (e.g. two development branches), you have to rename them not to have a common name. This name is stored in a .project file in the project root (or if none exists, it will be derived); you can use a refactor operation to change the project names. After that, you can import both projects into your Eclipse workspace.

Eclipse Egit will not import repo in workspace

I am trying to import an existing git repository as an eclipse project. The repository is stored in the location ~/src/repo_dir, and ~/src/ is my eclipse workspace directory.
If I use the sequence of menu operations:
File --> Import --> Git --> Projects from Git --> Local --> (Select my repo)
--> "Import as General Project"
I get the error:
/Users/me/src/repo_dir overlaps the location of another project: 'repo_dir'
and I can't import the project. It seems to be because Egit does not want the original repo that is being imported to already be in the workspace. However, if I then move the repo_dir out of the workspace directory ~/src/, and then import the project via the method above, it doesn't copy the contents to the workspace directory, so now my files live somewhere else, which is undesirable. The only workaround that I have found is to move the repo out of ~/src, import it, delete the resulting project, move the repo back into ~/src, and then import it with git as an 'Existing project.' Does anyone know of a cleaner way to handle this?
There are two different issues here:
Git handles a selected folder in your computer as a repository - and also stores some git-related metadata in it.
Eclipse handles a selected folder in your computer as your workspace. It stores that Eclipse configuration files (not meant to be shared), and even worse, it expects a single-level folder hierarchy for projects.
This means, putting a Git repository inside the workspace might cause a lot of unwanted issues - so I do not recommend this way (even if by some hack it is workable). However, if you want to organize all stuff related to an Eclipse workspace, you could create a folder structure as this:
eclipse-stuff
workspace
git
git-repo1
git-repo2
The workspace folder is given to Eclipse as the workspace folder, while git-repo1 and git-repo2 are your Git repositories.
On the other hand, I like to put all my git repositories into a common folder, regardless of the Eclipse workspace I use them in, but if you want to organize contents, this might not be enough for you.
Had this problem with Eclipse Kepler.
End up with installing Eclipse Mars and import the new project in another workspace.(Luna will also work fine)
If you need to work on current projects and set up environment for the coming one...

Importing with EGit clones repository but doesn't let me import project

I just started my third repository on Github. I initialized the repository on the website with a README file and tried importing it into Eclipse with Egit. Like my other projects from Github, the import dialog worked fine and the repository was cloned. In my file explorer, I can see the folder Egit created, as well as the .git folder and the README from the repository.
However, when I get to the point where Eclipse wants me to choose a wizard for project import, everything stops working. I can't import an existing project, because none exists yet. If I try to import it as a general project, it doesn't let me go further because it claims:
/path/to/my/folder overlaps the location of another project: 'gnu_magic'
This is indeed the name of the project I'm trying to import, but as I just imported it for the first time that shouldn't be a problem.
If I use the new project wizard, it doesn't let me use the same folder name as the project, claiming that the folder is not empty (which is true, but strange when I'm using the wizard to create an entirely new project). If I give the folder another name, it creates a project but the project isn't connected to the repository - I have no way of committing my changes back to Github.
Deleting the gnu_magic folder and starting all over again brings the same results.
Is there a way to remove the metadata over that specific project from Eclipse's configuration? I don't want to lose my other projects but I would like to work with Egit on that project.
Another interesting fact: If I clone the repository manually and then try to import it into Eclipse, as a project from my file system, it doesn't let me becasue the source is in the heirarchy of the destination.
Or am I missing the point here completely? I'm just surprised that I was able to clone the other repositories without any problems. Working with them over the past few days has been quite easy.
The solution was a bit strange. Here's the steps to solve it:
Clone repository using EGit
Eclipse refuses to import project, claiming it already exists. Exit import dialog.
Create new project using existing code, using cloned repository
Share project with old repository
Now the project is once again connected to the repository. Be careful with this method because if something goes wrong you might commit over your old code. This solution works with Indigo and Juno.
I'm using Eclipse 4.2 Indigo, and I've been struggling with these same problems for a while now.
If you have already cloned a repository on your machine somewhere, using EGit or whatever, you can:
1) Create a new Eclipse project.
2) File -> Import -> General -> File System Choose the cloned repository location. This will import everything, including the .git folder within the repository, into your Eclipse project in your workspace. For this it doesn't matter whether there's .project files anywhere in the imported files or not.
3) Team -> Share Project -> Git The EGit plugin should detect the .git folder within your project and suggest settings accordingly. You will have a new local repository location addded to EGit's repositories which will point to the .git folder under your project's directory.