diff options
author | Matthias Sohn | 2016-03-15 14:59:19 +0000 |
---|---|---|
committer | Gerrit Code Review @ Eclipse.org | 2016-03-15 14:59:19 +0000 |
commit | f2767898fa49724d795225f8f25f61f1538d08ea (patch) | |
tree | 78100023e21505f39ff80419ca0ce1a80236e7c9 | |
parent | f737c86f7f616049671977b054a5d62d0fa36348 (diff) | |
parent | 71d222e1e23e1579ebde825fe84053fae2f8ea97 (diff) | |
download | egit-f2767898fa49724d795225f8f25f61f1538d08ea.tar.gz egit-f2767898fa49724d795225f8f25f61f1538d08ea.tar.xz egit-f2767898fa49724d795225f8f25f61f1538d08ea.zip |
Merge "Support copy/move of workspace if Git repository is under workspace"
4 files changed, 99 insertions, 17 deletions
diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/RepositoryUtil.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/RepositoryUtil.java index 70053f4253..3f26c19dfc 100644 --- a/org.eclipse.egit.core/src/org/eclipse/egit/core/RepositoryUtil.java +++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/RepositoryUtil.java @@ -65,9 +65,23 @@ import org.osgi.service.prefs.BackingStoreException; */ public class RepositoryUtil { - /** The preferences to store the directories known to the Git Repositories view */ + /** + * The preferences to store the absolute paths of all repositories shown in + * the Git Repositories view + * + * @deprecated maintained to ensure compatibility for old EGit versions + */ public static final String PREFS_DIRECTORIES = "GitRepositoriesView.GitDirectories"; //$NON-NLS-1$ + /** + * The preferences to store paths of all repositories shown in the Git + * Repositories view. For repositories located in the Eclipse workspace + * store the relative path to the workspace root to enable moving and + * copying the workspace. For repositories outside the Eclipse workspace + * store their absolute path. + */ + public static final String PREFS_DIRECTORIES_REL = "GitRepositoriesView.GitDirectories.relative"; //$NON-NLS-1$ + private final Map<String, Map<String, String>> commitMappingCache = new HashMap<String, Map<String, String>>(); private final Map<String, String> repositoryNameCache = new HashMap<String, String>(); @@ -75,11 +89,14 @@ public class RepositoryUtil { private final IEclipsePreferences prefs = InstanceScope.INSTANCE .getNode(Activator.getPluginId()); + private final java.nio.file.Path workspacePath; + /** * Clients should obtain an instance from {@link Activator} */ RepositoryUtil() { - // nothing + workspacePath = ResourcesPlugin.getWorkspace().getRoot().getLocation() + .toFile().toPath(); } /** @@ -377,24 +394,61 @@ public class RepositoryUtil { } /** - * Returns the configured repositories. + * Get the set of absolute path strings of all configured repositories. * - * @return set of configured repositories' .git directories + * @return set of absolute paths of all configured repositories' .git + * directories * * @since 4.2 */ @NonNull public Set<String> getRepositories() { - String dirs; + String dirString; + Set<String> dirs; synchronized (prefs) { - dirs = prefs.get(PREFS_DIRECTORIES, ""); //$NON-NLS-1$ + dirString = prefs.get(PREFS_DIRECTORIES_REL, ""); //$NON-NLS-1$ + if (dirString.equals("")) { //$NON-NLS-1$ + dirs = migrateAbolutePaths(); + } else { + dirs = toDirSet(dirString); + } } - if (dirs == null || dirs.length() == 0) + return dirs; + } + + /** + * Migrate set of absolute paths created by an older version of EGit to the + * new format using relative paths for repositories located under the + * Eclipse workspace + * + * @return set of absolute paths of all configured git repositories + */ + private Set<String> migrateAbolutePaths() { + String dirString; + Set<String> dirs; + dirString = prefs.get(PREFS_DIRECTORIES, ""); //$NON-NLS-1$ + dirs = toDirSet(dirString); + // save migrated list + saveDirs(dirs); + return dirs; + } + + /** + * @param dirs + * String with repository directories separated by path separator + * @return set of absolute paths of repository directories, relative paths + * are resolved against the workspace root + */ + private Set<String> toDirSet(String dirs) { + if (dirs == null || dirs.isEmpty()) { return Collections.emptySet(); + } Set<String> configuredStrings = new HashSet<String>(); StringTokenizer tok = new StringTokenizer(dirs, File.pathSeparator); - while (tok.hasMoreTokens()) - configuredStrings.add(tok.nextToken()); + while (tok.hasMoreTokens()) { + configuredStrings + .add(workspacePath.resolve(tok.nextToken()).toString()); + } return configuredStrings; } @@ -458,13 +512,19 @@ public class RepositoryUtil { } private void saveDirs(Set<String> gitDirStrings) { - StringBuilder sb = new StringBuilder(); + StringBuilder sbRelative = new StringBuilder(); + StringBuilder sbAbsolute = new StringBuilder(); for (String gitDirString : gitDirStrings) { - sb.append(gitDirString); - sb.append(File.pathSeparatorChar); + sbRelative.append(relativizeToWorkspace(gitDirString)); + sbRelative.append(File.pathSeparatorChar); + sbAbsolute.append(gitDirString); + sbAbsolute.append(File.pathSeparatorChar); } - prefs.put(PREFS_DIRECTORIES, sb.toString()); + prefs.put(PREFS_DIRECTORIES_REL, sbRelative.toString()); + // redundantly store absolute paths to ensure compatibility with older + // EGit versions + prefs.put(PREFS_DIRECTORIES, sbAbsolute.toString()); try { prefs.flush(); } catch (BackingStoreException e) { @@ -475,6 +535,23 @@ public class RepositoryUtil { } /** + * @param pathString + * an absolute path String + * @return if the given {@code pathString} is under the workspace root the + * relative path of {@code pathString} relative to the workspace + * root, otherwise the absolute path {@code pathString}. This + * enables moving or copying the workspace. + */ + private String relativizeToWorkspace(String pathString) { + java.nio.file.Path p = java.nio.file.Paths.get(pathString); + if (p.startsWith(workspacePath)) { + return workspacePath.relativize(p).toString(); + } else { + return pathString; + } + } + + /** * Does the collection of repository returned by * {@link #getConfiguredRepositories()} contain the given repository? * diff --git a/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/view/repositories/GitRepositoriesViewTestBase.java b/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/view/repositories/GitRepositoriesViewTestBase.java index fa196db566..88766f80a9 100644 --- a/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/view/repositories/GitRepositoriesViewTestBase.java +++ b/org.eclipse.egit.ui.test/src/org/eclipse/egit/ui/view/repositories/GitRepositoriesViewTestBase.java @@ -63,8 +63,10 @@ public abstract class GitRepositoriesViewTestBase extends * remove all configured repositories from the view */ protected static void clearView() { - InstanceScope.INSTANCE.getNode(Activator.getPluginId()).remove( - RepositoryUtil.PREFS_DIRECTORIES); + InstanceScope.INSTANCE.getNode(Activator.getPluginId()) + .remove(RepositoryUtil.PREFS_DIRECTORIES); + InstanceScope.INSTANCE.getNode(Activator.getPluginId()) + .remove(RepositoryUtil.PREFS_DIRECTORIES_REL); } protected static void createStableBranch(Repository myRepository) diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/rebase/RebaseInteractiveView.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/rebase/RebaseInteractiveView.java index e24684cb43..5d21e370c8 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/rebase/RebaseInteractiveView.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/rebase/RebaseInteractiveView.java @@ -331,8 +331,10 @@ public class RebaseInteractiveView extends ViewPart implements prefListener = new IPreferenceChangeListener() { @Override public void preferenceChange(PreferenceChangeEvent event) { - if (!RepositoryUtil.PREFS_DIRECTORIES.equals(event.getKey())) + if (!RepositoryUtil.PREFS_DIRECTORIES_REL + .equals(event.getKey())) { return; + } final Repository repo = currentRepository; if (repo == null) diff --git a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/staging/StagingView.java b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/staging/StagingView.java index 011117e26a..82cc3f04e3 100644 --- a/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/staging/StagingView.java +++ b/org.eclipse.egit.ui/src/org/eclipse/egit/ui/internal/staging/StagingView.java @@ -513,8 +513,9 @@ public class StagingView extends ViewPart implements IShowInSource { @Override public void preferenceChange(PreferenceChangeEvent event) { - if (!RepositoryUtil.PREFS_DIRECTORIES.equals(event.getKey())) + if (!RepositoryUtil.PREFS_DIRECTORIES_REL.equals(event.getKey())) { return; + } final Repository repo = currentRepository; if (repo == null) |