Search in sources :

Example 1 with RepositoryCache

use of org.eclipse.egit.core.RepositoryCache in project egit by eclipse.

the class ResourceUtil method splitPathsByRepository.

/**
 * The method splits the given paths by their repository. For each occurring
 * repository a list is built containing the repository relative paths of
 * the related resources.
 * <p>
 * When one of the passed paths corresponds to the working directory,
 * <code>""</code> will be returned as part of the collection.
 *
 * @param paths
 * @return a map containing a list of repository relative paths for each
 *         occurring repository
 */
public static Map<Repository, Collection<String>> splitPathsByRepository(Collection<IPath> paths) {
    RepositoryCache repositoryCache = Activator.getDefault().getRepositoryCache();
    Map<Repository, Collection<String>> result = new HashMap<Repository, Collection<String>>();
    for (IPath path : paths) {
        Repository repository = repositoryCache.getRepository(path);
        if (repository != null) {
            IPath repoPath = new Path(repository.getWorkTree().getAbsolutePath());
            IPath repoRelativePath = path.makeRelativeTo(repoPath);
            addPathToMap(repository, repoRelativePath.toString(), result);
        }
    }
    return result;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) Repository(org.eclipse.jgit.lib.Repository) IPath(org.eclipse.core.runtime.IPath) HashMap(java.util.HashMap) RepositoryCache(org.eclipse.egit.core.RepositoryCache) Collection(java.util.Collection)

Example 2 with RepositoryCache

use of org.eclipse.egit.core.RepositoryCache in project egit by eclipse.

the class RepositoryJobResultAction method run.

@Override
public final void run() {
    Repository repo = null;
    if (!repositoryGone) {
        RepositoryCache repoCache = org.eclipse.egit.core.Activator.getDefault().getRepositoryCache();
        repo = repoCache.getRepository(repositoryDir);
        if (repo == null && FileKey.isGitRepository(repositoryDir, FS.DETECTED)) {
            // No longer in the Egit cache but still on disk
            try {
                repo = repoCache.lookupRepository(repositoryDir);
            } catch (IOException e) {
            // Ignore, repo remains null
            }
        }
        repositoryGone = repo == null;
    }
    if (repositoryGone || repo == null) {
        Activator.showError(MessageFormat.format(UIText.RepositoryJobResultAction_RepositoryGone, repositoryDir), null);
        return;
    }
    showResult(repo);
}
Also used : Repository(org.eclipse.jgit.lib.Repository) RepositoryCache(org.eclipse.egit.core.RepositoryCache) IOException(java.io.IOException)

Example 3 with RepositoryCache

use of org.eclipse.egit.core.RepositoryCache 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;
}
Also used : IAction(org.eclipse.jface.action.IAction) IWorkbenchAction(org.eclipse.ui.actions.ActionFactory.IWorkbenchAction) Action(org.eclipse.jface.action.Action) HashSet(java.util.HashSet) Set(java.util.Set) IAction(org.eclipse.jface.action.IAction) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IOException(java.io.IOException) RepositoryUtil(org.eclipse.egit.core.RepositoryUtil) Repository(org.eclipse.jgit.lib.Repository) RepositoryCache(org.eclipse.egit.core.RepositoryCache) File(java.io.File)

Example 4 with RepositoryCache

use of org.eclipse.egit.core.RepositoryCache in project egit by eclipse.

the class GlobalConfigurationPreferencePage method init.

@Override
public void init(IWorkbench workbench) {
    if (sysConfig == null)
        sysConfig = SystemReader.getInstance().openSystemConfig(null, FS.DETECTED);
    if (userConfig == null)
        // no inherit here!
        userConfig = SystemReader.getInstance().openUserConfig(null, FS.DETECTED);
    if (repositories == null) {
        repositories = new ArrayList<>();
        List<String> repoPaths = Activator.getDefault().getRepositoryUtil().getConfiguredRepositories();
        RepositoryCache repositoryCache = org.eclipse.egit.core.Activator.getDefault().getRepositoryCache();
        for (String repoPath : repoPaths) {
            File gitDir = new File(repoPath);
            if (!gitDir.exists())
                continue;
            try {
                repositories.add(repositoryCache.lookupRepository(gitDir));
            } catch (IOException e) {
                continue;
            }
        }
        sortRepositoriesByName();
    }
}
Also used : RepositoryCache(org.eclipse.egit.core.RepositoryCache) IOException(java.io.IOException) File(java.io.File)

Example 5 with RepositoryCache

use of org.eclipse.egit.core.RepositoryCache in project jbosstools-openshift by jbosstools.

the class TestRepository method cloneRepository.

public TestRepository cloneRepository(File path) throws URISyntaxException, InvocationTargetException, InterruptedException, IOException {
    URIish uri = new URIish("file:///" + repository.getDirectory().toString());
    CloneOperation clop = new CloneOperation(uri, true, null, path, Constants.R_HEADS + Constants.MASTER, Constants.DEFAULT_REMOTE_NAME, 0);
    clop.run(null);
    RepositoryCache repositoryCache = Activator.getDefault().getRepositoryCache();
    Repository clonedRepository = repositoryCache.lookupRepository(new File(path, Constants.DOT_GIT));
    return new TestRepository(clonedRepository);
}
Also used : URIish(org.eclipse.jgit.transport.URIish) Repository(org.eclipse.jgit.lib.Repository) RepositoryCache(org.eclipse.egit.core.RepositoryCache) IFile(org.eclipse.core.resources.IFile) File(java.io.File) CloneOperation(org.eclipse.egit.core.op.CloneOperation)

Aggregations

RepositoryCache (org.eclipse.egit.core.RepositoryCache)7 Repository (org.eclipse.jgit.lib.Repository)6 File (java.io.File)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 IFile (org.eclipse.core.resources.IFile)1 IPath (org.eclipse.core.runtime.IPath)1 Path (org.eclipse.core.runtime.Path)1 IEclipsePreferences (org.eclipse.core.runtime.preferences.IEclipsePreferences)1 RepositoryUtil (org.eclipse.egit.core.RepositoryUtil)1 CloneOperation (org.eclipse.egit.core.op.CloneOperation)1 Action (org.eclipse.jface.action.Action)1 IAction (org.eclipse.jface.action.IAction)1 URIish (org.eclipse.jgit.transport.URIish)1 IWorkbenchAction (org.eclipse.ui.actions.ActionFactory.IWorkbenchAction)1