use of org.eclipse.egit.core.RepositoryUtil in project egit by eclipse.
the class GitProjectSetCapabilityTest method testImportWhereRepoAlreadyExistsAtDifferentLocation.
@Test
public void testImportWhereRepoAlreadyExistsAtDifferentLocation() throws Exception {
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IPath reposPath = new Path(testDirectory.getPath()).append("repo");
pathsToClean.add(reposPath.toFile());
IPath repoPath = reposPath.append("existingbutdifferent");
IProject project = createProject(repoPath, "project");
project.delete(false, true, null);
String url = createUrl(repoPath);
File repoDir = createRepository(repoPath, url, "master");
IPath otherRepoPath = reposPath.append("other");
File otherRepoDir = createRepository(otherRepoPath, "other-url", "master");
RepositoryUtil util = Activator.getDefault().getRepositoryUtil();
util.addConfiguredRepository(repoDir);
util.addConfiguredRepository(otherRepoDir);
String reference = createProjectReference(repoPath, "master", "project");
addToWorkspace(new String[] { reference });
IProject imported = root.getProject("project");
assertEquals("Expected imported project to be from already existing repository", reposPath.append("existingbutdifferent/project").toOSString(), imported.getLocation().toOSString());
}
use of org.eclipse.egit.core.RepositoryUtil in project egit by eclipse.
the class GitProjectSetCapabilityTest method testImportFromRepoWithUrlOnlyDifferingInUserName.
@Test
public void testImportFromRepoWithUrlOnlyDifferingInUserName() throws Exception {
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IPath reposPath = root.getLocation().append("repos");
pathsToClean.add(reposPath.toFile());
IPath repoPath = reposPath.append("repo");
IProject project = createProject(repoPath, "project");
project.delete(false, true, null);
String url = createUrl(repoPath, "ssh", "userName");
File repoDir = createRepository(repoPath, url, "master");
RepositoryUtil util = Activator.getDefault().getRepositoryUtil();
util.addConfiguredRepository(repoDir);
String reference = createProjectReference(repoPath, "ssh", /* no user */
null, "master", "project");
addToWorkspace(new String[] { reference });
IProject imported = root.getProject("project");
assertEquals("Expected imported project to be from already existing repository. User name must be ignored in URL.", root.getLocation().append("repos/repo/project"), imported.getLocation());
}
use of org.eclipse.egit.core.RepositoryUtil in project egit by eclipse.
the class SelectionForViewsTest method before.
@Before
public void before() throws Exception {
localRepositoryDir = createProjectAndCommitToRepository();
remoteRepositoryDir = createRemoteRepository(localRepositoryDir);
URIish uri = new URIish("file:///" + remoteRepositoryDir.getPath());
File workdir = new File(getTestDirectory(), "ClonedRepo");
CloneOperation op = new CloneOperation(uri, true, null, workdir, "refs/heads/master", "origin", 0);
op.run(null);
clonedRepositoryDir = new File(workdir, Constants.DOT_GIT);
RepositoryUtil repoUtil = Activator.getDefault().getRepositoryUtil();
repoUtil.addConfiguredRepository(localRepositoryDir);
repoUtil.addConfiguredRepository(clonedRepositoryDir);
// it's bare
repoUtil.addConfiguredRepository(remoteRepositoryDir);
stagingView = TestUtil.showView(StagingView.VIEW_ID);
reflogView = TestUtil.showView(ReflogView.VIEW_ID);
rebaseInteractiveView = TestUtil.showView(RebaseInteractiveView.VIEW_ID);
repoView = TestUtil.showView(RepositoriesView.VIEW_ID);
RepositoriesView repos = (RepositoriesView) repoView.getViewReference().getView(false);
repos.setReactOnSelection(true);
historyView = TestUtil.showHistoryView();
IHistoryView history = (IHistoryView) historyView.getViewReference().getView(false);
((GenericHistoryView) history).setLinkingEnabled(true);
// Ensure that the git history page is active
Exception[] exception = { null };
PlatformUI.getWorkbench().getDisplay().syncExec(() -> {
try {
history.showHistoryFor(new RepositoryNode(null, lookupRepository(localRepositoryDir)), true);
} catch (Exception e) {
exception[0] = e;
}
});
if (exception[0] != null) {
throw exception[0];
}
waitForRefreshes();
}
use of org.eclipse.egit.core.RepositoryUtil in project egit by eclipse.
the class RepositoryMenuUtil method getRepositoryActions.
/**
* Creates for each configured repository an {@link IAction} that will
* perform the given {@code action} when invoked.
*
* @param includeBare
* {@code true} if bare repositories should be included in the
* list, {@code false} otherwise
* @param currentRepoDir
* git directory of a repository that is to be marked as
* "current"; may be {@code null}.
* @param action
* to perform on the chosen repository
* @return the (possibly empty) list of actions
*/
public static Collection<IAction> getRepositoryActions(boolean includeBare, @Nullable File currentRepoDir, @NonNull Consumer<Repository> action) {
RepositoryUtil util = org.eclipse.egit.core.Activator.getDefault().getRepositoryUtil();
RepositoryCache cache = org.eclipse.egit.core.Activator.getDefault().getRepositoryCache();
Set<String> repositories = util.getRepositories();
Map<String, Set<File>> repos = new HashMap<>();
for (String repo : repositories) {
File gitDir = new File(repo);
String name = null;
try {
Repository r = cache.lookupRepository(gitDir);
if (!includeBare && r.isBare()) {
continue;
}
name = util.getRepositoryName(r);
} catch (IOException e) {
continue;
}
Set<File> files = repos.get(name);
if (files == null) {
files = new HashSet<>();
files.add(gitDir);
repos.put(name, files);
} else {
files.add(gitDir);
}
}
String[] repoNames = repos.keySet().toArray(new String[repos.size()]);
Arrays.sort(repoNames, CommonUtils.STRING_ASCENDING_COMPARATOR);
List<IAction> result = new ArrayList<>();
for (String repoName : repoNames) {
Set<File> files = repos.get(repoName);
File[] gitDirs = files.toArray(new File[files.size()]);
Arrays.sort(gitDirs);
for (File f : gitDirs) {
IAction menuItem = new Action(repoName, IAction.AS_RADIO_BUTTON) {
@Override
public void run() {
try {
Repository r = cache.lookupRepository(f);
action.accept(r);
} catch (IOException e) {
Activator.showError(e.getLocalizedMessage(), e);
}
}
};
menuItem.setToolTipText(f.getPath());
if (f.equals(currentRepoDir)) {
menuItem.setChecked(true);
}
result.add(menuItem);
}
}
return result;
}
use of org.eclipse.egit.core.RepositoryUtil in project egit by eclipse.
the class LinkHelper method findSelection.
/**
* TODO javadoc missing
*/
@Override
@SuppressWarnings("unchecked")
public IStructuredSelection findSelection(IEditorInput anInput) {
if (!(anInput instanceof IURIEditorInput)) {
return null;
}
URI uri = ((IURIEditorInput) anInput).getURI();
if (// $NON-NLS-1$
!uri.getScheme().equals("file"))
return null;
File file = new File(uri.getPath());
if (!file.exists())
return null;
RepositoryUtil config = Activator.getDefault().getRepositoryUtil();
List<String> repos = config.getConfiguredRepositories();
for (String repo : repos) {
Repository repository;
try {
repository = FileRepositoryBuilder.create(new File(repo));
} catch (IOException e) {
continue;
}
if (repository.isBare())
continue;
if (file.getPath().startsWith(repository.getWorkTree().getPath())) {
RepositoriesViewContentProvider cp = new RepositoriesViewContentProvider();
RepositoryNode repoNode = new RepositoryNode(null, repository);
RepositoryTreeNode result = null;
for (Object child : cp.getChildren(repoNode)) {
if (child instanceof WorkingDirNode) {
result = (WorkingDirNode) child;
break;
}
}
if (result == null)
return null;
IPath remainingPath = new Path(file.getPath().substring(repository.getWorkTree().getPath().length()));
for (String segment : remainingPath.segments()) {
for (Object child : cp.getChildren(result)) {
RepositoryTreeNode<File> fileNode;
try {
fileNode = (RepositoryTreeNode<File>) child;
} catch (ClassCastException e) {
return null;
}
if (fileNode.getObject().getName().equals(segment)) {
result = fileNode;
break;
}
}
}
return new StructuredSelection(result);
}
}
return null;
}
Aggregations