diff options
| author | Manuel Doninger | 2011-09-08 17:07:21 +0000 |
|---|---|---|
| committer | Manuel Doninger | 2011-10-16 23:09:16 +0000 |
| commit | 850af11bd2c524b0f9b02634228ff4c4f4c6dce8 (patch) | |
| tree | fe3e152291aacca04b4bd9d3ce8d461612712df5 | |
| parent | 0b17d347fa44de50ce891f784e98e182e0cf1bb9 (diff) | |
| download | egit-850af11bd2c524b0f9b02634228ff4c4f4c6dce8.tar.gz egit-850af11bd2c524b0f9b02634228ff4c4f4c6dce8.tar.xz egit-850af11bd2c524b0f9b02634228ff4c4f4c6dce8.zip | |
Extract ProjectReference as separate class
This enables the use of ProjectReference to keep the configurations of a
project set as collection of suitable data objects and not just strings.
Needed by bug 309578.
Change-Id: I5259b7b23095fcdc4cd7205d648f4714e9458b47
Signed-off-by: Manuel Doninger <manuel.doninger@googlemail.com>
5 files changed, 215 insertions, 67 deletions
diff --git a/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/ProjectReferenceTest.java b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/ProjectReferenceTest.java new file mode 100644 index 0000000000..ed2683e5cf --- /dev/null +++ b/org.eclipse.egit.core.test/src/org/eclipse/egit/core/test/ProjectReferenceTest.java @@ -0,0 +1,50 @@ +/******************************************************************************* + * Copyright (C) 2011, Manuel Doninger <manuel.doninger@googlemail.com> + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + *******************************************************************************/ +package org.eclipse.egit.core.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.net.URISyntaxException; + +import org.eclipse.egit.core.ProjectReference; +import org.junit.Before; +import org.junit.Test; + +public class ProjectReferenceTest { + + private String version = "1.0"; + private String url = "git://egit.eclipse.org/egit.git"; + private String branch = "master"; + private String project = "org.eclipse.egit.core"; + private ProjectReference projectReference; + + @Before + public void createProjectReferenceFromString() throws IllegalArgumentException, URISyntaxException { + String reference = version + "," + url + "," + branch + "," + project; + projectReference = new ProjectReference(reference); + assertNotNull(projectReference); + } + + @Test + public void checkUrl() { + assertEquals(url, projectReference.getRepository().toString()); + } + + @Test + public void checkBranch() { + assertEquals(branch, projectReference.getBranch()); + } + + @Test + public void checkProject() { + assertEquals(project, projectReference.getProjectDir()); + } +} diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/CoreText.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/CoreText.java index eb5d2cb1d7..fba7aedef2 100644 --- a/org.eclipse.egit.core/src/org/eclipse/egit/core/CoreText.java +++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/CoreText.java @@ -297,7 +297,7 @@ public class CoreText extends NLS { public static String OperationNotYetExecuted; /** */ - public static String GitProjectSetCapability_InvalidTokensCount; + public static String ProjectReference_InvalidTokensCount; /** */ public static String GitProjectSetCapability_CloneToExistingDirectory; diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/GitProjectSetCapability.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/GitProjectSetCapability.java index 0e0a7084f3..c7300e06f1 100644 --- a/org.eclipse.egit.core/src/org/eclipse/egit/core/GitProjectSetCapability.java +++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/GitProjectSetCapability.java @@ -6,25 +6,24 @@ * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Manuel Doninger <manuel.doninger@googlemail.com> *******************************************************************************/ package org.eclipse.egit.core; import java.io.File; import java.io.IOException; -import java.io.Serializable; import java.lang.reflect.InvocationTargetException; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.Collection; -import java.util.Comparator; import java.util.HashMap; import java.util.LinkedHashMap; +import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.Map; import java.util.Set; -import java.util.TreeSet; -import java.util.regex.Pattern; - import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IProjectDescription; import org.eclipse.core.resources.IWorkspace; @@ -52,58 +51,8 @@ import org.eclipse.team.core.TeamException; */ public final class GitProjectSetCapability extends ProjectSetCapability { - private static final String SEPARATOR = ","; //$NON-NLS-1$ private static final String VERSION = "1.0"; //$NON-NLS-1$ - private static final class ProjectReferenceComparator implements - Comparator<ProjectReference>, Serializable { - private static final long serialVersionUID = 1L; - - public int compare(ProjectReference o1, ProjectReference o2) { - final boolean reposEqual = o1.repository.equals(o2.repository); - final boolean branchesEqual = o1.branch - .equals(o2.branch); - final boolean projectDirsEqual = o1.projectDir - .equals(o2.projectDir); - return reposEqual && branchesEqual && projectDirsEqual ? 0 : 1; - } - } - - private static final class ProjectReference { - - private static final String DEFAULT_BRANCH = Constants.MASTER; - - /** - * a relative path (from the repository root) to a project - */ - String projectDir; - - /** - * <code>repository</code> parameter - */ - URIish repository; - - /** - * the remote branch that will be checked out, see <code>--branch</code> - * option - */ - String branch = DEFAULT_BRANCH; - - @SuppressWarnings("boxing") - ProjectReference(final String reference) throws URISyntaxException, IllegalArgumentException { - final String[] tokens = reference.split(Pattern.quote(SEPARATOR)); - if (tokens.length != 4) - throw new IllegalArgumentException(NLS.bind( - CoreText.GitProjectSetCapability_InvalidTokensCount, new Object[] { - 4, tokens.length, tokens })); - - this.repository = new URIish(tokens[1]); - if (!"".equals(tokens[2])) //$NON-NLS-1$ - this.branch = tokens[2]; - this.projectDir = tokens[3]; - } - } - @Override public String[] asReference(IProject[] projects, ProjectSetSerializationContext context, IProgressMonitor monitor) @@ -141,11 +90,11 @@ public final class GitProjectSetCapability extends ProjectSetCapability { StringBuilder sb = new StringBuilder(); sb.append(VERSION); - sb.append(SEPARATOR); + sb.append(ProjectReference.SEPARATOR); sb.append(url); - sb.append(SEPARATOR); + sb.append(ProjectReference.SEPARATOR); sb.append(branch); - sb.append(SEPARATOR); + sb.append(ProjectReference.SEPARATOR); sb.append(projectPath); return sb.toString(); @@ -168,16 +117,16 @@ public final class GitProjectSetCapability extends ProjectSetCapability { final ProjectReference projectReference = new ProjectReference( reference); Map<String, Set<ProjectReference>> repositoryBranches = repositories - .get(projectReference.repository); + .get(projectReference.getRepository()); if (repositoryBranches == null) { repositoryBranches = new HashMap<String, Set<ProjectReference>>(); - repositories.put(projectReference.repository, + repositories.put(projectReference.getRepository(), repositoryBranches); } - Set<ProjectReference> projectReferences = repositoryBranches.get(projectReference.branch); + Set<ProjectReference> projectReferences = repositoryBranches.get(projectReference.getBranch()); if (projectReferences == null) { - projectReferences = new TreeSet<ProjectReference>(new ProjectReferenceComparator()); - repositoryBranches.put(projectReference.branch, projectReferences); + projectReferences = new LinkedHashSet<ProjectReference>(); + repositoryBranches.put(projectReference.getBranch(), projectReferences); } projectReferences.add(projectReference); @@ -201,7 +150,7 @@ public final class GitProjectSetCapability extends ProjectSetCapability { if (workDir.toFile().exists()) { final Collection<String> projectNames = new LinkedList<String>(); for (final ProjectReference projectReference : projects) - projectNames.add(projectReference.projectDir); + projectNames.add(projectReference.getProjectDir()); throw new TeamException(NLS.bind( CoreText.GitProjectSetCapability_CloneToExistingDirectory, new Object[] { workDir, projectNames, gitUrl })); @@ -223,7 +172,7 @@ public final class GitProjectSetCapability extends ProjectSetCapability { final IWorkspaceRoot root = workspace.getRoot(); for (final ProjectReference projectToImport : projects) { final IPath projectDir = workDir - .append(projectToImport.projectDir); + .append(projectToImport.getProjectDir()); final IProjectDescription projectDescription = workspace .loadProjectDescription(projectDir .append(IProjectDescription.DESCRIPTION_FILE_NAME)); diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/ProjectReference.java b/org.eclipse.egit.core/src/org/eclipse/egit/core/ProjectReference.java new file mode 100644 index 0000000000..d72db6cc08 --- /dev/null +++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/ProjectReference.java @@ -0,0 +1,149 @@ +/******************************************************************************* + * Copyright (C) 2009, Mykola Nikishov <mn@mn.com.ua> + * Copyright (C) 2011, Robin Stocker <robin@nibor.org> + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Manuel Doninger <manuel.doninger@googlemail.com> + *******************************************************************************/ +package org.eclipse.egit.core; + +import java.net.URISyntaxException; +import java.util.regex.Pattern; + +import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.transport.URIish; +import org.eclipse.osgi.util.NLS; + +/** + * ProjectReference for Team project sets + */ +public final class ProjectReference { + + private static final String DEFAULT_BRANCH = Constants.MASTER; + + /** + * the version of the reference string + */ + private String version; + + /** + * a relative path (from the repository root) to a project + */ + private String projectDir; + + /** + * <code>repository</code> parameter + */ + private URIish repository; + + /** + * the remote branch that will be checked out, see <code>--branch</code> + * option + */ + private String branch = DEFAULT_BRANCH; + + /** + * use this name instead of using the remote name origin to keep track + * of the upstream repository, see <code>--origin</code> option. + */ + private String origin = Constants.DEFAULT_REMOTE_NAME; + + static final String SEPARATOR = ","; //$NON-NLS-1$ + + /** + * @param reference + * @throws URISyntaxException + * @throws IllegalArgumentException + */ + @SuppressWarnings("boxing") + public ProjectReference(final String reference) throws URISyntaxException, IllegalArgumentException { + final String[] tokens = reference.split(Pattern.quote(ProjectReference.SEPARATOR)); + if (tokens.length != 4) + throw new IllegalArgumentException(NLS.bind( + CoreText.ProjectReference_InvalidTokensCount, new Object[] { + 4, tokens.length, tokens })); + + this.version = tokens[0]; + this.repository = new URIish(tokens[1]); + if (!"".equals(tokens[2])) //$NON-NLS-1$ + this.branch = tokens[2]; + this.projectDir = tokens[3]; + } + + /** + * @return <code>repository</code> parameter + */ + public URIish getRepository() { + return repository; + } + + /** + * @return the remote branch that will be checked out, see <code>--branch</code> + * option + */ + public String getBranch() { + return branch; + } + + /** + * @return name of the upstream repository + */ + public String getOrigin() { + return origin; + } + + /** + * @return a relative path (from the repository root) to a project + */ + public String getProjectDir() { + return projectDir; + } + + String getVersion() { + return version; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((branch == null) ? 0 : branch.hashCode()); + result = prime * result + + ((projectDir == null) ? 0 : projectDir.hashCode()); + result = prime * result + + ((repository == null) ? 0 : repository.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (!(obj instanceof ProjectReference)) + return false; + ProjectReference other = (ProjectReference) obj; + if (branch == null) { + if (other.branch != null) + return false; + } else if (!branch.equals(other.branch)) + return false; + if (projectDir == null) { + if (other.projectDir != null) + return false; + } else if (!projectDir.equals(other.projectDir)) + return false; + if (repository == null) { + if (other.repository != null) + return false; + } else if (!repository.equals(other.repository)) + return false; + return true; + } +}
\ No newline at end of file diff --git a/org.eclipse.egit.core/src/org/eclipse/egit/core/coretext.properties b/org.eclipse.egit.core/src/org/eclipse/egit/core/coretext.properties index c9735e0caf..9727a1735b 100644 --- a/org.eclipse.egit.core/src/org/eclipse/egit/core/coretext.properties +++ b/org.eclipse.egit.core/src/org/eclipse/egit/core/coretext.properties @@ -74,7 +74,7 @@ The resource cannot be moved, renamed or deleted due to an internal error. Error_CanonicalFile=Unable to determine a canonical file path. -GitProjectSetCapability_InvalidTokensCount={0} tokens expected in project reference but {1} had been found: {2} +ProjectReference_InvalidTokensCount={0} tokens expected in project reference but {1} had been found: {2} GitProjectSetCapability_CloneToExistingDirectory=Destination directory {0} exists. Don't clone {1} from {2} to prevent data loss. GitProjectSetCapability_ExportCouldNotGetBranch=Could not get current branch from repository of project {0}. GitProjectSetCapability_ExportNoRemote=No remote URL configured for current branch in repository of project {0}. |
