Don't auto-share for closed projects or for bare repositories

For bare repositories, there's no working directory, and thus there
cannot be anything to share. And closed projects cannot be auto-shared
since one cannot get or set session properties. 

Change-Id: Id3e054fc2bda94df03e1243093a8a5c250be2190
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/tree/command/AddCommand.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/tree/command/AddCommand.java
index 3012839..b80b04f 100644
--- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/tree/command/AddCommand.java
+++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/repository/tree/command/AddCommand.java
@@ -12,6 +12,7 @@
 package org.eclipse.egit.ui.internal.repository.tree.command;
 
 import java.io.File;
+import java.io.IOException;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -40,6 +41,7 @@
 import org.eclipse.egit.ui.internal.repository.tree.RepositoryTreeNode;
 import org.eclipse.jface.window.Window;
 import org.eclipse.jface.wizard.WizardDialog;
+import org.eclipse.jgit.lib.Repository;
 import org.eclipse.jgit.util.FileUtils;
 import org.eclipse.team.core.RepositoryProvider;
 
@@ -71,26 +73,41 @@
 	}
 
 	private void autoShareProjects(File repositoryDir) {
-		IPath workingDirPath = new Path(repositoryDir.getAbsolutePath())
-				.removeLastSegments(1);
+		// Don't even try to auto-share for bare repositories.
+		IPath workingDirPath;
+		try {
+			Repository repo = Activator.getDefault().getRepositoryCache()
+					.lookupRepository(repositoryDir);
+			if (repo.isBare()) {
+				return;
+			}
+			workingDirPath = new Path(repo.getWorkTree().getAbsolutePath());
+		} catch (IOException e) {
+			org.eclipse.egit.ui.Activator.logError(e.getLocalizedMessage(), e);
+			return;
+		}
 		Map<IProject, File> connections = new HashMap<>();
 		IProject[] projects = ResourcesPlugin.getWorkspace().getRoot()
 				.getProjects();
 		for (IProject project : projects) {
+			// Skip closed projects
+			if (!project.isAccessible()) {
+				continue;
+			}
 			RepositoryProvider provider = RepositoryProvider
 					.getProvider(project);
-			if (provider != null)
+			if (provider != null) {
 				continue;
-
+			}
 			IPath location = project.getLocation();
-			if (location == null)
+			if (location == null) {
 				continue;
-
+			}
 			// In case the project is not inside the working directory, don't
 			// even search for a mapping.
-			if (!workingDirPath.isPrefixOf(location))
+			if (!workingDirPath.isPrefixOf(location)) {
 				continue;
-
+			}
 			RepositoryFinder f = new RepositoryFinder(project);
 			f.setFindInChildren(false);
 			try {