Example usage for org.eclipse.jgit.lib Repository getWorkTree

List of usage examples for org.eclipse.jgit.lib Repository getWorkTree

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib Repository getWorkTree.

Prototype

@NonNull
public File getWorkTree() throws NoWorkTreeException 

Source Link

Document

Get the root directory of the working tree, where files are checked out for viewing and editing.

Usage

From source file:org.eclipse.egit.ui.internal.factories.GitAdapterFactory.java

License:Open Source License

@Nullable
private IResource getWorkspaceResourceFromGitPath(IPath gitPath) {
    Repository repository = Activator.getDefault().getRepositoryCache().getRepository(gitPath);
    if (repository == null || repository.isBare()) {
        return null;
    }/* w  w  w  .j a v a2s  .  co m*/
    try {
        IPath repoRelativePath = gitPath.makeRelativeTo(new Path(repository.getWorkTree().getAbsolutePath()));
        IProject[] projects = ProjectUtil.getProjectsContaining(repository,
                Collections.singleton(repoRelativePath.toString()));
        if (projects.length > 0) {
            IPath projectRelativePath = gitPath.makeRelativeTo(projects[0].getLocation());
            if (projectRelativePath.isEmpty()) {
                return projects[0];
            } else {
                return projects[0].getFile(projectRelativePath);
            }
        }
    } catch (CoreException e) {
        // Ignore and fall through
    }
    return root.getFile(gitPath);
}

From source file:org.eclipse.egit.ui.internal.history.command.AbstractHistoryCommandHandler.java

License:Open Source License

protected String getRepoRelativePath(Repository repo, File file) {
    IPath workdirPath = new Path(repo.getWorkTree().getPath());
    IPath filePath = new Path(file.getPath()).setDevice(null);
    return filePath.removeFirstSegments(workdirPath.segmentCount()).toString();
}

From source file:org.eclipse.egit.ui.internal.history.command.ImportChangedProjectsCommand.java

License:Open Source License

private Set<File> findDotProjectFiles(Set<File> changedFiles, Repository repository) {
    Set<File> result = new HashSet<>();
    String workingTreeRootPath = repository.getWorkTree().toString();
    for (File changedFile : changedFiles) {
        File projectFile = searchEnclosingProjectInWorkDir(changedFile.getParentFile().getAbsoluteFile(),
                workingTreeRootPath);//from  w w w. java2s.c  o  m
        if (projectFile != null)
            result.add(projectFile);
    }
    return result;
}

From source file:org.eclipse.egit.ui.internal.history.FileDiff.java

License:Open Source License

private String getProjectRelativePath(Repository db, String repoPath) {
    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    IWorkspaceRoot root = workspace.getRoot();
    IPath absolutePath = new Path(db.getWorkTree().getAbsolutePath()).append(repoPath);
    IResource resource = root.getFileForLocation(absolutePath);
    return resource.getProjectRelativePath().toString();
}

From source file:org.eclipse.egit.ui.internal.history.GitHistoryPage.java

License:Open Source License

private ArrayList<String> buildFilterPaths(final IResource[] inResources, final File[] inFiles,
        final Repository db) throws IllegalStateException {
    final ArrayList<String> paths;
    if (inResources != null) {
        paths = new ArrayList<String>(inResources.length);
        for (final IResource r : inResources) {
            final RepositoryMapping map = RepositoryMapping.getMapping(r);
            if (map == null)
                continue;
            if (db != map.getRepository()) {
                throw new IllegalStateException(UIText.AbstractHistoryCommanndHandler_NoUniqueRepository);
            }//w  ww  . j a va2  s  .  c  o m

            if (showAllFilter == ShowFilter.SHOWALLFOLDER) {
                final String path;
                // if the resource's parent is the workspace root, we will
                // get nonsense from map.getRepoRelativePath(), so we
                // check here and use the project instead
                if (r.getParent() instanceof IWorkspaceRoot)
                    path = map.getRepoRelativePath(r.getProject());
                else
                    path = map.getRepoRelativePath(r.getParent());
                if (path != null && path.length() > 0)
                    paths.add(path);
            } else if (showAllFilter == ShowFilter.SHOWALLPROJECT) {
                final String path = map.getRepoRelativePath(r.getProject());
                if (path != null && path.length() > 0)
                    paths.add(path);
            } else if (showAllFilter == ShowFilter.SHOWALLREPO) {
                // nothing
            } else /* if (showAllFilter == ShowFilter.SHOWALLRESOURCE) */ {
                final String path = map.getRepoRelativePath(r);
                if (path != null && path.length() > 0)
                    paths.add(path);
            }
        }
    } else if (inFiles != null) {
        IPath workdirPath = new Path(db.getWorkTree().getPath());
        IPath gitDirPath = new Path(db.getDirectory().getPath());
        int segmentCount = workdirPath.segmentCount();
        paths = new ArrayList<String>(inFiles.length);
        for (File file : inFiles) {
            IPath filePath;
            if (showAllFilter == ShowFilter.SHOWALLFOLDER) {
                filePath = new Path(file.getParentFile().getPath());
            } else if (showAllFilter == ShowFilter.SHOWALLPROJECT || showAllFilter == ShowFilter.SHOWALLREPO) {
                // we don't know of projects here -> treat as SHOWALLREPO
                continue;
            } else /* if (showAllFilter == ShowFilter.SHOWALLRESOURCE) */ {
                filePath = new Path(file.getPath());
            }

            if (gitDirPath.isPrefixOf(filePath)) {
                throw new IllegalStateException(
                        NLS.bind(UIText.GitHistoryPage_FileOrFolderPartOfGitDirMessage, filePath.toOSString()));
            }

            IPath pathToAdd = filePath.removeFirstSegments(segmentCount).setDevice(null);
            if (!pathToAdd.isEmpty()) {
                paths.add(pathToAdd.toString());
            }
        }
    } else {
        paths = new ArrayList<String>(0);
    }
    return paths;
}

From source file:org.eclipse.egit.ui.internal.history.GitHistoryPage.java

License:Open Source License

private static String getRepoRelativePath(Repository repo, File file) {
    IPath workdirPath = new Path(repo.getWorkTree().getPath());
    IPath filePath = new Path(file.getPath()).setDevice(null);
    return filePath.removeFirstSegments(workdirPath.segmentCount()).toString();
}

From source file:org.eclipse.egit.ui.internal.preferences.GitProjectPropertyPage.java

License:Open Source License

private void fillValues(Repository repository) throws IOException {
    gitDir.setText(repository.getDirectory().getAbsolutePath());
    branch.setText(repository.getBranch());
    workDir.setText(repository.getWorkTree().getAbsolutePath());

    state.setText(repository.getRepositoryState().getDescription());

    final ObjectId objectId = repository.resolve(repository.getFullBranch());
    if (objectId == null) {
        if (repository.getAllRefs().size() == 0)
            id.setText(UIText.GitProjectPropertyPage_ValueEmptyRepository);
        else/*  ww w.j a v  a2 s  .  c  om*/
            id.setText(UIText.GitProjectPropertyPage_ValueUnbornBranch);
    } else
        id.setText(objectId.name());
}

From source file:org.eclipse.egit.ui.internal.rebase.RebaseInteractiveView.java

License:Open Source License

private boolean isValidRepo(final Repository repository) {
    return repository != null && !repository.isBare() && repository.getWorkTree().exists();
}

From source file:org.eclipse.egit.ui.internal.repository.RepositoriesViewContentProvider.java

License:Open Source License

public Object[] getChildren(Object parentElement) {

    RepositoryTreeNode node = (RepositoryTreeNode) parentElement;
    Repository repo = node.getRepository();

    switch (node.getType()) {

    case BRANCHES: {
        List<RepositoryTreeNode> nodes = new ArrayList<RepositoryTreeNode>();
        nodes.add(new LocalNode(node, repo));
        nodes.add(new RemoteTrackingNode(node, repo));
        return nodes.toArray();
    }/*from w w w. j  ava  2s  .c  o m*/

    case LOCAL: {
        if (branchHierarchyMode) {
            BranchHierarchyNode hierNode = new BranchHierarchyNode(node, repo, new Path(Constants.R_HEADS));
            List<RepositoryTreeNode> children = new ArrayList<RepositoryTreeNode>();
            try {
                for (IPath path : hierNode.getChildPaths()) {
                    children.add(new BranchHierarchyNode(node, node.getRepository(), path));
                }
                for (Ref ref : hierNode.getChildRefs()) {
                    children.add(new RefNode(node, node.getRepository(), ref));
                }
            } catch (IOException e) {
                return handleException(e, node);
            }
            return children.toArray();
        } else {
            List<RepositoryTreeNode<Ref>> refs = new ArrayList<RepositoryTreeNode<Ref>>();
            try {
                for (Entry<String, Ref> refEntry : repo.getRefDatabase().getRefs(Constants.R_HEADS)
                        .entrySet()) {
                    if (!refEntry.getValue().isSymbolic())
                        refs.add(new RefNode(node, repo, refEntry.getValue()));
                }
            } catch (IOException e) {
                return handleException(e, node);
            }
            return refs.toArray();
        }
    }

    case REMOTETRACKING: {
        if (branchHierarchyMode) {
            BranchHierarchyNode hierNode = new BranchHierarchyNode(node, repo, new Path(Constants.R_REMOTES));
            List<RepositoryTreeNode> children = new ArrayList<RepositoryTreeNode>();
            try {
                for (IPath path : hierNode.getChildPaths()) {
                    children.add(new BranchHierarchyNode(node, node.getRepository(), path));
                }
                for (Ref ref : hierNode.getChildRefs()) {
                    children.add(new RefNode(node, node.getRepository(), ref));
                }
            } catch (IOException e) {
                return handleException(e, node);
            }
            return children.toArray();
        } else {
            List<RepositoryTreeNode<Ref>> refs = new ArrayList<RepositoryTreeNode<Ref>>();
            try {
                for (Entry<String, Ref> refEntry : repo.getRefDatabase().getRefs(Constants.R_REMOTES)
                        .entrySet()) {
                    if (!refEntry.getValue().isSymbolic())
                        refs.add(new RefNode(node, repo, refEntry.getValue()));
                }
            } catch (IOException e) {
                return handleException(e, node);
            }

            return refs.toArray();
        }
    }

    case BRANCHHIERARCHY: {
        BranchHierarchyNode hierNode = (BranchHierarchyNode) node;
        List<RepositoryTreeNode> children = new ArrayList<RepositoryTreeNode>();
        try {
            for (IPath path : hierNode.getChildPaths()) {
                children.add(new BranchHierarchyNode(node, node.getRepository(), path));
            }
            for (Ref ref : hierNode.getChildRefs()) {
                children.add(new RefNode(node, node.getRepository(), ref));
            }
        } catch (IOException e) {
            return handleException(e, node);
        }
        return children.toArray();
    }

    case TAGS: {
        List<RepositoryTreeNode<Ref>> refs = new ArrayList<RepositoryTreeNode<Ref>>();

        try {
            for (Entry<String, Ref> refEntry : repo.getRefDatabase().getRefs(Constants.R_TAGS).entrySet()) {
                refs.add(new TagNode(node, repo, refEntry.getValue()));
            }
        } catch (IOException e) {
            return handleException(e, node);
        }

        return refs.toArray();
    }

    case ADDITIONALREFS: {
        List<RepositoryTreeNode<Ref>> refs = new ArrayList<RepositoryTreeNode<Ref>>();
        try {
            for (Entry<String, Ref> refEntry : repo.getRefDatabase().getRefs(RefDatabase.ALL).entrySet()) {
                String name = refEntry.getKey();
                if (!(name.startsWith(Constants.R_HEADS) || name.startsWith(Constants.R_TAGS)
                        || name.startsWith(Constants.R_REMOTES)))
                    refs.add(new AdditionalRefNode(node, repo, refEntry.getValue()));
            }
            for (Ref r : repo.getRefDatabase().getAdditionalRefs())
                refs.add(new AdditionalRefNode(node, repo, r));
        } catch (IOException e) {
            return handleException(e, node);
        }
        return refs.toArray();
    }

    case REMOTES: {
        List<RepositoryTreeNode<String>> remotes = new ArrayList<RepositoryTreeNode<String>>();

        Repository rep = node.getRepository();

        Set<String> configNames = rep.getConfig().getSubsections(RepositoriesView.REMOTE);

        for (String configName : configNames) {
            remotes.add(new RemoteNode(node, repo, configName));
        }

        return remotes.toArray();
    }

    case REPO: {

        List<RepositoryTreeNode<? extends Object>> nodeList = new ArrayList<RepositoryTreeNode<? extends Object>>();

        nodeList.add(new BranchesNode(node, repo));
        nodeList.add(new TagsNode(node, repo));
        nodeList.add(new AdditionalRefsNode(node, repo));
        nodeList.add(new WorkingDirNode(node, repo));
        nodeList.add(new RemotesNode(node, repo));

        return nodeList.toArray();
    }

    case WORKINGDIR: {
        List<RepositoryTreeNode<File>> children = new ArrayList<RepositoryTreeNode<File>>();

        if (node.getRepository().isBare())
            return children.toArray();
        File workingDir = repo.getWorkTree();
        if (workingDir == null || !workingDir.exists())
            return children.toArray();

        File[] childFiles = workingDir.listFiles();
        Arrays.sort(childFiles, new Comparator<File>() {
            public int compare(File o1, File o2) {
                if (o1.isDirectory()) {
                    if (o2.isDirectory()) {
                        return o1.compareTo(o2);
                    }
                    return -1;
                } else if (o2.isDirectory()) {
                    return 1;
                }
                return o1.compareTo(o2);
            }
        });
        for (File file : childFiles) {
            if (file.isDirectory()) {
                children.add(new FolderNode(node, repo, file));
            } else {
                children.add(new FileNode(node, repo, file));
            }
        }

        return children.toArray();
    }

    case FOLDER: {
        List<RepositoryTreeNode<File>> children = new ArrayList<RepositoryTreeNode<File>>();

        File parent = ((File) node.getObject());

        File[] childFiles = parent.listFiles();
        Arrays.sort(childFiles, new Comparator<File>() {
            public int compare(File o1, File o2) {
                if (o1.isDirectory()) {
                    if (o2.isDirectory()) {
                        return o1.compareTo(o2);
                    }
                    return -1;
                } else if (o2.isDirectory()) {
                    return 1;
                }
                return o1.compareTo(o2);
            }
        });
        for (File file : childFiles) {
            if (file.isDirectory()) {
                children.add(new FolderNode(node, repo, file));
            } else {
                children.add(new FileNode(node, repo, file));
            }
        }

        return children.toArray();
    }

    case REMOTE: {

        List<RepositoryTreeNode<String>> children = new ArrayList<RepositoryTreeNode<String>>();

        String remoteName = (String) node.getObject();
        RemoteConfig rc;
        try {
            rc = new RemoteConfig(node.getRepository().getConfig(), remoteName);
        } catch (URISyntaxException e) {
            return handleException(e, node);
        }

        if (!rc.getURIs().isEmpty())
            children.add(new FetchNode(node, node.getRepository(), rc.getURIs().get(0).toPrivateString()));

        int uriCount = rc.getPushURIs().size();
        if (uriCount == 0 && !rc.getURIs().isEmpty())
            uriCount++;

        // show push if either a fetch or push URI is specified and
        // at least one push specification
        if (uriCount > 0 && !rc.getPushRefSpecs().isEmpty()) {
            URIish firstUri;
            if (!rc.getPushURIs().isEmpty())
                firstUri = rc.getPushURIs().get(0);
            else
                firstUri = rc.getURIs().get(0);

            if (uriCount == 1)
                children.add(new PushNode(node, node.getRepository(), firstUri.toPrivateString()));
            else
                children.add(new PushNode(node, node.getRepository(), firstUri.toPrivateString() + "...")); //$NON-NLS-1$
        }
        return children.toArray();

    }

    case FILE:
        // fall through
    case REF:
        // fall through
    case PUSH:
        // fall through
    case TAG:
        // fall through
    case FETCH:
        // fall through
    case ERROR:
        // fall through
    case ADDITIONALREF:
        return null;

    }

    return null;

}

From source file:org.eclipse.egit.ui.internal.repository.RepositoriesViewContentProvider.java

License:Open Source License

public boolean hasChildren(Object element) {
    // for some of the nodes we can optimize this call
    RepositoryTreeNode node = (RepositoryTreeNode) element;
    Repository repo = node.getRepository();
    switch (node.getType()) {
    case BRANCHES:
        return true;
    case REPO://from ww  w.  ja v a 2 s.c  o m
        return true;
    case ADDITIONALREFS:
        return true;
    case TAGS:
        try {
            return !repo.getRefDatabase().getRefs(Constants.R_TAGS).isEmpty();
        } catch (IOException e) {
            return true;
        }
    case WORKINGDIR:
        if (node.getRepository().isBare())
            return false;
        File workingDir = repo.getWorkTree();
        if (workingDir == null || !workingDir.exists())
            return false;
        return workingDir.listFiles().length > 0;
    default:
        Object[] children = getChildren(element);
        return children != null && children.length > 0;
    }
}