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

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

Introduction

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

Prototype

@Deprecated
@NonNull
public Map<String, Ref> getAllRefs() 

Source Link

Document

Get mutable map of all known refs, including symrefs like HEAD that may not point to any object yet.

Usage

From source file:org.wso2.carbon.deployment.synchronizer.git.util.GitUtilities.java

License:Open Source License

/**
 * Checks if an existing local repository is a valid git repository
 *
 * @param repository Repository instance
 * @return true if a valid git repo, else false
 *//*from w w  w.  ja v a2 s.  co m*/
public static boolean isValidGitRepo(Repository repository) {

    for (Ref ref : repository.getAllRefs().values()) { //check if has been previously cloned successfully, not empty
        if (ref.getObjectId() == null)
            continue;
        return true;
    }

    return false;
}

From source file:org.wso2.security.tools.dependencycheck.scanner.handler.GitHandler.java

License:Open Source License

/**
 * Check whether a git clone operation is success
 *
 * @param repo Repository to check/*  www  .  j  a  v a2  s . c  om*/
 * @return Boolean to indicate whether a git operation is success
 */
public static boolean hasAtLeastOneReference(Repository repo) {
    for (Ref ref : repo.getAllRefs().values()) {
        if (ref.getObjectId() == null) {
            continue;
        }
        return true;
    }
    return false;
}

From source file:org.zanata.sync.plugin.git.service.impl.GitSyncService.java

License:Open Source License

private static boolean hasAtLeastOneReference(File folder) {
    try {/*  ww w .j  a  v  a  2s.c  o  m*/
        Repository repo = Git.open(folder).getRepository();
        return repo.getAllRefs().values().stream().anyMatch(ref -> ref.getObjectId() != null);
    } catch (IOException e) {
        return false;
    }
}

From source file:playRepository.GitRepository.java

License:Apache License

/**
 * Clones a local repository./*from   w ww .  java2s .  c om*/
 *
 * This doesn't copy Git objects but hardlink them to save disk space.
 *
 * @param originalProject
 * @param forkProject
 * @throws IOException
 */
protected static void cloneHardLinkedRepository(Project originalProject, Project forkProject)
        throws IOException {
    Repository origin = GitRepository.buildGitRepository(originalProject);
    Repository forked = GitRepository.buildGitRepository(forkProject);
    forked.create();

    final Path originObjectsPath = Paths.get(new File(origin.getDirectory(), "objects").getAbsolutePath());
    final Path forkedObjectsPath = Paths.get(new File(forked.getDirectory(), "objects").getAbsolutePath());

    // Hardlink files .git/objects/ directory to save disk space,
    // but copy .git/info/alternates because the file can be modified.
    SimpleFileVisitor<Path> visitor = new SimpleFileVisitor<Path>() {
        public FileVisitResult visitFile(Path file, BasicFileAttributes attr) throws IOException {
            Path newPath = forkedObjectsPath.resolve(originObjectsPath.relativize(file.toAbsolutePath()));
            if (file.equals(forkedObjectsPath.resolve("/info/alternates"))) {
                Files.copy(file, newPath);
            } else {
                FileUtils.mkdirs(newPath.getParent().toFile(), true);
                Files.createLink(newPath, file);
            }
            return java.nio.file.FileVisitResult.CONTINUE;
        }
    };
    Files.walkFileTree(originObjectsPath, visitor);

    // Import refs.
    for (Map.Entry<String, Ref> entry : origin.getAllRefs().entrySet()) {
        RefUpdate updateRef = forked.updateRef(entry.getKey());
        Ref ref = entry.getValue();
        if (ref.isSymbolic()) {
            updateRef.link(ref.getTarget().getName());
        } else {
            updateRef.setNewObjectId(ref.getObjectId());
            updateRef.update();
        }
    }
}

From source file:svnserver.repository.git.LayoutHelper.java

License:GNU General Public License

/**
 * Get active branches with commits from repository.
 *
 * @param repository Repository.//from ww  w  . ja  v a  2  s .  co  m
 * @return Branches with commits.
 * @throws IOException
 */
public static Map<String, RevCommit> getBranches(@NotNull Repository repository) throws IOException {
    final RevWalk revWalk = new RevWalk(repository);
    final Map<String, RevCommit> result = new TreeMap<>();
    for (Ref ref : repository.getAllRefs().values()) {
        try {
            final String svnPath = layoutMapping.gitToSvn(ref.getName());
            if (svnPath != null) {
                final RevCommit revCommit = unwrapCommit(revWalk, ref.getObjectId());
                if (revCommit != null) {
                    result.put(svnPath, revCommit);
                }
            }
        } catch (MissingObjectException ignored) {
        }
    }
    return result;
}

From source file:svnserver.SvnTestServer.java

License:GNU General Public License

private void cleanupBranches(Repository repository) {
    final List<String> branches = new ArrayList<>();
    for (String ref : repository.getAllRefs().keySet()) {
        if (ref.startsWith(Constants.R_HEADS + TEST_BRANCH_PREFIX)) {
            branches.add(ref.substring(Constants.R_HEADS.length()));
        }//from w w w  .j a  v  a 2  s. c  o m
    }
    if (!branches.isEmpty()) {
        for (String branch : branches) {
            log.info("Cleanup branch: {}", branch);
            try {
                new Git(repository).branchDelete().setBranchNames(branch).setForce(true).call();
            } catch (GitAPIException e) {
                log.error("Cleanup branch: " + branch, e);
            }
        }
    }
}