Example usage for org.eclipse.jgit.lib RepositoryCache open

List of usage examples for org.eclipse.jgit.lib RepositoryCache open

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib RepositoryCache open.

Prototype

public static Repository open(Key location) throws IOException, RepositoryNotFoundException 

Source Link

Document

Open an existing repository, reusing a cached instance if possible.

Usage

From source file:com.gitblit.tests.GitBlitSuite.java

License:Apache License

public static void close(File repository) {
    try {/*ww w. j a v  a 2 s .c  om*/
        File gitDir = FileKey.resolve(repository, FS.detect());
        if (gitDir != null && gitDir.exists()) {
            close(RepositoryCache.open(FileKey.exact(gitDir, FS.detect())));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.google.gerrit.server.git.LocalDiskRepositoryManager.java

License:Apache License

private Repository openRepository(Path path, Project.NameKey name) throws RepositoryNotFoundException {
    if (isUnreasonableName(name)) {
        throw new RepositoryNotFoundException("Invalid name: " + name);
    }//from  w ww  . ja va  2s.c om
    File gitDir = path.resolve(name.get()).toFile();
    if (!names.contains(name)) {
        // The this.names list does not hold the project-name but it can still exist
        // on disk; for instance when the project has been created directly on the
        // file-system through replication.
        //
        if (!name.get().endsWith(Constants.DOT_GIT_EXT)) {
            if (FileKey.resolve(gitDir, FS.DETECTED) != null) {
                onCreateProject(name);
            } else {
                throw new RepositoryNotFoundException(gitDir);
            }
        } else {
            final File directory = gitDir;
            if (FileKey.isGitRepository(new File(directory, Constants.DOT_GIT), FS.DETECTED)) {
                onCreateProject(name);
            } else if (FileKey.isGitRepository(
                    new File(directory.getParentFile(), directory.getName() + Constants.DOT_GIT_EXT),
                    FS.DETECTED)) {
                onCreateProject(name);
            } else {
                throw new RepositoryNotFoundException(gitDir);
            }
        }
    }
    final FileKey loc = FileKey.lenient(gitDir, FS.DETECTED);
    try {
        return RepositoryCache.open(loc);
    } catch (IOException e1) {
        final RepositoryNotFoundException e2;
        e2 = new RepositoryNotFoundException("Cannot open repository " + name);
        e2.initCause(e1);
        throw e2;
    }
}

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

License:Open Source License

public boolean merge(ServiceInvocationContext context, String repositoryLocation, String refName,
        boolean squash) {
    try {//from   w ww.  ja v  a 2 s .c o m
        Repository repo = RepositoryCache.open(FileKey.exact(new File(repositoryLocation), FS.DETECTED));

        MergeOperation op = new MergeOperation(repo, refName, squash, context.getCommunicationChannel());
        op.execute();

        String result = GitPlugin.getInstance().getUtils().handleMergeResult(op.getMergeResult());
        if (result != null) {
            context.getCommunicationChannel()
                    .appendOrSendCommand(new DisplaySimpleMessageClientCommand(
                            GitPlugin.getInstance().getMessage("git.merge.result"), result,
                            DisplaySimpleMessageClientCommand.ICON_INFORMATION));
        }
        return true;
    } catch (Exception e) {
        logger.debug(CommonPlugin.getInstance().getMessage("error"), 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

public boolean rebase(ServiceInvocationContext context, String repositoryLocation, String refName) {
    try {//from  w ww .j  a  va 2s  . c  o  m
        Repository repo = RepositoryCache.open(FileKey.exact(new File(repositoryLocation), FS.DETECTED));

        if (!repo.getFullBranch().startsWith(Constants.R_HEADS)) {
            context.getCommunicationChannel()
                    .appendOrSendCommand(new DisplaySimpleMessageClientCommand(
                            CommonPlugin.getInstance().getMessage("error"),
                            GitPlugin.getInstance().getMessage("git.rebase.noLocalBranch"),
                            DisplaySimpleMessageClientCommand.ICON_ERROR));
            return false;
        }
        RebaseOperation op = new RebaseOperation(repo, refName, context.getCommunicationChannel());
        op.execute();

        String result = op.handleRebaseResult();
        if (result != null) {
            context.getCommunicationChannel()
                    .appendOrSendCommand(new DisplaySimpleMessageClientCommand(
                            GitPlugin.getInstance().getMessage("git.rebase.result"), result,
                            DisplaySimpleMessageClientCommand.ICON_INFORMATION));
        }
        return true;
    } catch (Exception e) {
        logger.debug(CommonPlugin.getInstance().getMessage("error"), 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 reset(ServiceInvocationContext context, String repositoryLocation, String targetName,
        int resetType) {
    try {//from  w ww  . j a  v a  2s. com
        Repository repo = RepositoryCache.open(FileKey.exact(new File(repositoryLocation), FS.DETECTED));

        ResetType type;
        switch (resetType) {
        case 0:
            type = ResetType.SOFT;
            break;
        case 1:
            type = ResetType.MIXED;
            break;
        default:
            type = ResetType.HARD;
        }

        new ResetOperation(repo, targetName, type, context.getCommunicationChannel()).execute();
    } catch (Exception e) {
        logger.debug(CommonPlugin.getInstance().getMessage("error"), e);
        context.getCommunicationChannel().appendOrSendCommand(
                new DisplaySimpleMessageClientCommand(CommonPlugin.getInstance().getMessage("error"),
                        e.getMessage(), DisplaySimpleMessageClientCommand.ICON_ERROR));
        return false;
    }
    return true;
}

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

License:Open Source License

public Repository getRepository(File repoFile) {
    File gitDir = getGitDir(repoFile);
    if (gitDir != null) {
        try {/*from  ww  w. j  a  v  a  2  s  . c  om*/
            Repository repository = RepositoryCache.open(FileKey.exact(gitDir, FS.DETECTED));
            return repository;
        } catch (IOException e) {
            // TODO CC: log
        }
    }
    return null;
}

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

License:Open Source License

@RemoteInvocation
public List<HistoryFileDiffEntryDto> getCommitFileDiffs(StatefulServiceInvocationContext context,
        HistoryEntryDto entry, HistoryViewInfoDto info) {
    List<HistoryFileDiffEntryDto> entries = new ArrayList<HistoryFileDiffEntryDto>();
    try {//from w  ww.  j  av  a2 s .c o  m
        Repository repo = RepositoryCache
                .open(FileKey.exact(new File(info.getRepositoryLocation()), FS.DETECTED));
        RevWalk walk = new RevWalk(repo);

        RevCommit commit = walk.parseCommit(repo.resolve(entry.getId()));
        for (RevCommit parent : commit.getParents()) {
            walk.parseBody(parent);
        }
        FileDiff[] fileDiffs = FileDiff.compute(createFileWalker(new WebWalk(repo), repo, info.getPath()),
                commit, TreeFilter.ALL);
        for (FileDiff fd : fileDiffs) {
            HistoryFileDiffEntryDto fileEntry = new HistoryFileDiffEntryDto();
            fileEntry.setFile(fd.getLabel(fd));
            //fileEntry.setImage(GitPlugin.getInstance().getGitUtils().getImageURL(fd.getImageDescriptor(fd)));   

            entries.add(fileEntry);
        }
        walk.dispose();
    } catch (Exception e) {
        context.getCommunicationChannel().sendCommandWithPush(
                new DisplaySimpleMessageClientCommand(CommonPlugin.getInstance().getMessage("error"),
                        e.getMessage(), DisplaySimpleMessageClientCommand.ICON_ERROR));
        return null;
    }
    return entries;
}

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

License:Open Source License

@RemoteInvocation
public String getCommitMessage(StatefulServiceInvocationContext context, HistoryEntryDto entry,
        String repositoryLocation) {
    try {/*from w w w .  j  a v  a2  s .c  om*/
        Repository repo = RepositoryCache.open(FileKey.exact(new File(repositoryLocation), FS.DETECTED));
        ;
        RevCommit commit = new RevWalk(repo).parseCommit(repo.resolve(entry.getId()));

        return getCommitMessage(repo, entry, commit);
    } catch (Exception e) {
        context.getCommunicationChannel().sendCommandWithPush(
                new DisplaySimpleMessageClientCommand(CommonPlugin.getInstance().getMessage("error"),
                        e.getMessage(), DisplaySimpleMessageClientCommand.ICON_ERROR));
        return null;
    }
}

From source file:org.webcat.core.git.GitCloner.java

License:Open Source License

private Repository createWorkingCopyRepositoryIfNecessary(File location, File remoteDir) throws IOException {
    Repository wcRepository;/*  w ww. j  a  v a 2  s . c o  m*/

    try {
        wcRepository = RepositoryCache.open(FileKey.lenient(location, FS.DETECTED));
    } catch (RepositoryNotFoundException e) {
        // Create the repository from scratch.

        if (!location.exists()) {
            location.mkdirs();
        }

        InitCommand init = Git.init();
        init.setDirectory(location);
        init.setBare(false);
        wcRepository = init.call().getRepository();

        StoredConfig config = wcRepository.getConfig();
        config.setBoolean("core", null, "bare", false);

        try {
            RefSpec refSpec = new RefSpec().setForceUpdate(true).setSourceDestination(Constants.R_HEADS + "*",
                    Constants.R_REMOTES + "origin" + "/*");

            RemoteConfig remoteConfig = new RemoteConfig(config, "origin");
            remoteConfig.addURI(new URIish(remoteDir.toString()));
            remoteConfig.addFetchRefSpec(refSpec);
            remoteConfig.update(config);
        } catch (URISyntaxException e2) {
            // Do nothing.
        }

        config.save();
    }

    return wcRepository;
}