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

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

Introduction

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

Prototype

public File getDirectory() 

Source Link

Document

Get local metadata directory

Usage

From source file:org.flowerplatform.web.git.GitService.java

License:Open Source License

public void deleteRepository(ServiceInvocationContext context, List<PathFragment> selectedNode) {
    Repository repository = (Repository) GenericTreeStatefulService.getNodeByPathFor(selectedNode, null);
    GenericTreeStatefulService service = GenericTreeStatefulService.getServiceFromPathWithRoot(selectedNode);
    NodeInfo repositoryNodeInfo = service.getVisibleNodes().get(repository);

    ProgressMonitor monitor = ProgressMonitor.create(
            GitPlugin.getInstance().getMessage("git.deleteRepo.monitor.title"),
            context.getCommunicationChannel());

    try {//w ww .j a v  a  2 s .  c o  m
        monitor.beginTask(GitPlugin.getInstance().getMessage("git.deleteRepo.monitor.message",
                new Object[] { GitPlugin.getInstance().getUtils().getRepositoryName(repository) }), 2);

        repository.getObjectDatabase().close();
        repository.getRefDatabase().close();

        // delete repositories from cache
        File[] children = repository.getDirectory().getParentFile().getParentFile().listFiles();
        for (File child : children) {
            Repository repo = GitPlugin.getInstance().getUtils().getRepository(child);
            RepositoryCache.close(repo);
        }
        monitor.worked(1);

        // delete repository files
        File repoFile = repository.getDirectory().getParentFile().getParentFile();
        if (GitUtils.GIT_REPOSITORIES_NAME.equals(repoFile.getParentFile().getName())) {
            GitPlugin.getInstance().getUtils().delete(repoFile);
        }
        monitor.worked(1);

        dispatchContentUpdate(repositoryNodeInfo.getParent().getNode());
    } catch (Exception e) {
        logger.debug(GitPlugin.getInstance().getMessage("git.deleteRepo.error"), e);
        context.getCommunicationChannel().appendOrSendCommand(
                new DisplaySimpleMessageClientCommand(CommonPlugin.getInstance().getMessage("error"),
                        GitPlugin.getInstance().getMessage("git.deleteRepo.error"),
                        DisplaySimpleMessageClientCommand.ICON_ERROR));
    } finally {
        monitor.done();
    }
}

From source file:org.flowerplatform.web.git.GitService.java

License:Open Source License

@RemoteInvocation
public GitActionDto getNodeAdditionalData(ServiceInvocationContext context, List<PathFragment> path) {
    try {//w w w  . j  ava  2 s.c  o m
        RefNode refNode = (RefNode) GenericTreeStatefulService.getNodeByPathFor(path, null);
        GenericTreeStatefulService service = GenericTreeStatefulService.getServiceFromPathWithRoot(path);
        NodeInfo refNodeInfo = service.getVisibleNodes().get(refNode);
        Repository repository = getRepository(refNodeInfo);
        if (repository == null) {
            context.getCommunicationChannel()
                    .appendOrSendCommand(new DisplaySimpleMessageClientCommand(
                            CommonPlugin.getInstance().getMessage("error"),
                            "Cannot find repository for node " + refNode,
                            DisplaySimpleMessageClientCommand.ICON_ERROR));
            return null;
        }
        GitActionDto data = new GitActionDto();
        data.setRepository(repository.getDirectory().getAbsolutePath());
        data.setBranch(repository.getBranch());

        return data;
    } catch (Exception e) {
        logger.debug(CommonPlugin.getInstance().getMessage("error"), path, e);
        context.getCommunicationChannel().appendOrSendCommand(
                new DisplaySimpleMessageClientCommand(CommonPlugin.getInstance().getMessage("error"),
                        e.getMessage(), DisplaySimpleMessageClientCommand.ICON_ERROR));
    }
    return null;
}

From source file:org.flowerplatform.web.git.GitService.java

License:Open Source License

@RemoteInvocation
public ImportProjectPageDto getProjects(ServiceInvocationContext context, List<PathFragment> path) {
    try {//from  ww  w . ja  v a 2  s .c  o m
        List<String> directories = new ArrayList<String>();
        List<File> files = new ArrayList<File>();

        @SuppressWarnings("unchecked")
        Pair<File, String> node = (Pair<File, String>) GenericTreeStatefulService.getNodeByPathFor(path, null);
        Repository repo = GitPlugin.getInstance().getUtils().getRepository(node.a);

        GitPlugin.getInstance().getUtils().findProjectFiles(files, repo.getWorkTree(), null);

        List<ProjectDto> projects = new ArrayList<ProjectDto>();

        for (File projectSystemFile : files) {
            // project path
            IPath projPath = new Path(projectSystemFile.getParentFile().getCanonicalPath());
            String projectName = projPath.lastSegment();
            File repoFile = repo.getDirectory().getParentFile().getParentFile();
            String projectRelativeLocation = projPath.makeRelativeTo(new Path(repoFile.getAbsolutePath()))
                    .toString();
            ProjectDto dto = new ProjectDto();
            dto.setName(projectName + " (" + projectRelativeLocation + ")");
            dto.setLocation(projPath.toString());

            projects.add(dto);
        }
        ImportProjectPageDto result = new ImportProjectPageDto();
        result.setExistingProjects(projects);
        result.setSelectedFolders(directories);

        return result;
    } catch (Exception e) {
        logger.debug(GitPlugin.getInstance().getMessage("git.page.populate.error"), e);
        context.getCommunicationChannel().appendOrSendCommand(
                new DisplaySimpleMessageClientCommand(CommonPlugin.getInstance().getMessage("error"),
                        GitPlugin.getInstance().getMessage("git.page.populate.error"),
                        DisplaySimpleMessageClientCommand.ICON_ERROR));
        return null;
    }
}

From source file:org.flowerplatform.web.git.GitService.java

License:Open Source License

@RemoteInvocation
public boolean pull(ServiceInvocationContext context, List<PathFragment> path) {
    tlCommand.set((InvokeServiceMethodServerCommand) context.getCommand());

    try {/* w  w  w.ja  v a  2 s.co  m*/
        RefNode refNode = (RefNode) GenericTreeStatefulService.getNodeByPathFor(path, null);
        GenericTreeStatefulService service = GenericTreeStatefulService.getServiceFromPathWithRoot(path);
        NodeInfo refNodeInfo = service.getVisibleNodes().get(refNode);
        Repository repository = getRepository(refNodeInfo);
        if (repository == null) {
            context.getCommunicationChannel()
                    .appendOrSendCommand(new DisplaySimpleMessageClientCommand(
                            CommonPlugin.getInstance().getMessage("error"),
                            "Cannot find repository for node " + refNode,
                            DisplaySimpleMessageClientCommand.ICON_ERROR));
            return false;
        }

        File mainRepoFile = repository.getDirectory().getParentFile();
        File wdirFile = new File(mainRepoFile.getParentFile(),
                GitUtils.WORKING_DIRECTORY_PREFIX + Repository.shortenRefName(refNode.getRef().getName()));
        if (!wdirFile.exists()) {
            return false;
        }
        new PullOperation(refNode.getRef(), GitPlugin.getInstance().getUtils().getRepository(wdirFile),
                context.getCommunicationChannel()).execute();

        return true;
    } catch (Exception e) {
        context.getCommunicationChannel().appendOrSendCommand(
                new DisplaySimpleMessageClientCommand(CommonPlugin.getInstance().getMessage("error"),
                        e.getMessage(), DisplaySimpleMessageClientCommand.ICON_ERROR));
        return false;
    }
}

From source file:org.flowerplatform.web.git.GitService.java

License:Open Source License

@RemoteInvocation
public boolean deleteBranch(ServiceInvocationContext context, List<PathFragment> path) {
    try {/*from   w ww  .  ja  v  a 2 s . c o m*/
        RefNode node = (RefNode) GenericTreeStatefulService.getNodeByPathFor(path, null);
        GenericTreeStatefulService service = GenericTreeStatefulService.getServiceFromPathWithRoot(path);
        NodeInfo nodeInfo = service.getVisibleNodes().get(node);
        Repository repository = node.getRepository();

        Git git = new Git(repository);

        git.branchDelete().setBranchNames(node.getRef().getName()).setForce(true).call();

        // delete working directory
        String branchName = node.getRef().getName().substring(Constants.R_HEADS.length());
        File mainRepoFile = repository.getDirectory().getParentFile();
        File wdirFile = new File(mainRepoFile.getParentFile(), GitUtils.WORKING_DIRECTORY_PREFIX + branchName);
        if (wdirFile.exists()) {
            GitPlugin.getInstance().getUtils().delete(wdirFile);
        }

        dispatchContentUpdate(nodeInfo.getParent().getNode());
        return true;
    } catch (GitAPIException e) {
        logger.debug(CommonPlugin.getInstance().getMessage("error"), path, e);
        context.getCommunicationChannel().appendOrSendCommand(
                new DisplaySimpleMessageClientCommand(CommonPlugin.getInstance().getMessage("error"),
                        e.getMessage(), DisplaySimpleMessageClientCommand.ICON_ERROR));
        return false;
    }
}

From source file:org.flowerplatform.web.git.GitUtils.java

License:Open Source License

public String getRepositoryName(Repository repo) {
    return repo.getDirectory().getParentFile().getParentFile().getName();
}

From source file:org.flowerplatform.web.git.GitUtils.java

License:Open Source License

/**
 * This method must be used to set user configuration before running
 * some GIT commands that uses it.//ww w.ja  va2s  .c om
 * 
 * <p>
 * A lock/unlock on repository is done before/after the command is executed
 * because the configuration modifies the same file and this will not be
 * thread safe any more.
 */
public Object runGitCommandInUserRepoConfig(Repository repo, GitCommand<?> command) throws Exception {
    namedLockPool.lock(repo.getDirectory().getPath());

    try {
        StoredConfig c = repo.getConfig();
        c.load();
        User user = (User) CommunicationPlugin.tlCurrentPrincipal.get().getUser();

        c.setString(ConfigConstants.CONFIG_USER_SECTION, null, ConfigConstants.CONFIG_KEY_NAME, user.getName());
        c.setString(ConfigConstants.CONFIG_USER_SECTION, null, ConfigConstants.CONFIG_KEY_EMAIL,
                user.getEmail());

        c.save();

        return command.call();
    } catch (Exception e) {
        throw e;
    } finally {
        namedLockPool.unlock(repo.getDirectory().getPath());
    }
}

From source file:org.flowerplatform.web.git.history.remote.GitHistoryStatefulService.java

License:Open Source License

private void configObjectInfo(CommunicationChannel channel, HistoryViewInfoDto info) throws IOException {
    if (info.getIsResource()) {
        //         IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember((String) info.getSelectedObject());         
        //         RepositoryMapping mapping = RepositoryMapping.getMapping(resource);
        //         
        //         if (mapping == null) {
        //            throw new IOException();
        //         }
        //         info.setResource1(resource);
        //         info.setRepository1(GitPlugin.getInstance().getGitUtils().getRepository(mapping));   
    } else {/* w w  w  .j a  v  a  2s.c o  m*/
        Object node = GenericTreeStatefulService.getNodeByPathFor((List<PathFragment>) info.getSelectedObject(),
                null);
        GenericTreeStatefulService service = GenericTreeStatefulService
                .getServiceFromPathWithRoot((List<PathFragment>) info.getSelectedObject());
        NodeInfo nodeInfo = service.getVisibleNodes().get(node);
        Repository repository = GitService.getInstance().getRepository(nodeInfo);

        if (GitNodeType.NODE_TYPE_FILE.equals(nodeInfo.getPathFragment().getType())
                || GitNodeType.NODE_TYPE_WDIR.equals(nodeInfo.getPathFragment().getType())) {
            repository = GitPlugin.getInstance().getUtils().getRepository((File) node);
        } else if (node instanceof RefNode) {
            // create working directory for local branch
            File mainRepoFile = repository.getDirectory().getParentFile();
            String branchName = ((RefNode) node).getRef().getName().substring(Constants.R_HEADS.length());
            File wdirFile = new File(mainRepoFile.getParentFile(),
                    GitUtils.WORKING_DIRECTORY_PREFIX + branchName);
            repository = GitPlugin.getInstance().getUtils().getRepository(wdirFile);
        } else {
            throw new IOException();
        }
        if (repository == null) {
            throw new IOException();
        }

        info.setFile1(null);
        info.setRepository1(repository);
    }
}

From source file:org.flowerplatform.web.git.operation.CommitOperation.java

License:Open Source License

private String getMergeResolveMessage(Repository mergeRepository) {
    File mergeMsg = new File(mergeRepository.getDirectory(), Constants.MERGE_MSG);
    FileReader reader;//from  ww w  . j a v  a2 s  .com
    try {
        reader = new FileReader(mergeMsg);
        BufferedReader br = new BufferedReader(reader);
        try {
            StringBuilder message = new StringBuilder();
            String s;
            String newLine = newLine();
            while ((s = br.readLine()) != null)
                message.append(s).append(newLine);
            return message.toString();
        } catch (IOException e) {
            throw new IllegalStateException(e);
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                // Empty
            }
        }
    } catch (FileNotFoundException e) {
        return null;
    }
}

From source file:org.gitective.tests.CommitTreeFilterTest.java

License:Open Source License

/**
 * Test resetting commit filter resets tree filter
 *
 * @throws Exception// w ww .  j a v  a 2  s .  c  o m
 */
@Test
public void nestedSetRepository() throws Exception {
    final AtomicReference<Repository> repo = new AtomicReference<Repository>();
    BaseTreeFilter treeFilter = new BaseTreeFilter() {

        public boolean include(TreeWalk walker) throws IOException {
            return true;
        }

        public BaseTreeFilter setRepository(Repository repository) {
            repo.set(repository);
            return super.setRepository(repository);
        }
    };
    CommitTreeFilter filter = new CommitTreeFilter(treeFilter);
    Repository fileRepo = new FileRepository(testRepo);
    filter.setRepository(fileRepo);
    assertNotNull(repo.get());
    assertEquals(fileRepo.getDirectory(), repo.get().getDirectory());
}