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

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

Introduction

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

Prototype

@NonNull
public abstract RefDatabase getRefDatabase();

Source Link

Document

Get the reference database which stores the reference namespace.

Usage

From source file:com.google.gitiles.VisibilityCache.java

License:Open Source License

private boolean isVisible(Repository repo, RevWalk walk, ObjectId id) throws IOException {
    RevCommit commit;/*from   www .  ja va  2 s.c om*/
    try {
        commit = walk.parseCommit(id);
    } catch (IncorrectObjectTypeException e) {
        return false;
    }

    // If any reference directly points at the requested object, permit display.
    // Common for displays of pending patch sets in Gerrit Code Review, or
    // bookmarks to the commit a tag points at.
    Collection<Ref> allRefs = repo.getRefDatabase().getRefs(RefDatabase.ALL).values();
    for (Ref ref : allRefs) {
        ref = repo.getRefDatabase().peel(ref);
        if (id.equals(ref.getObjectId()) || id.equals(ref.getPeeledObjectId())) {
            return true;
        }
    }

    // Check heads first under the assumption that most requests are for refs
    // close to a head. Tags tend to be much further back in history and just
    // clutter up the priority queue in the common case.
    return isReachableFrom(walk, commit, filter(allRefs, refStartsWith(Constants.R_HEADS)))
            || isReachableFrom(walk, commit, filter(allRefs, refStartsWith(Constants.R_TAGS)))
            || isReachableFrom(walk, commit, filter(allRefs, not(refStartsWith("refs/changes/"))));
}

From source file:com.googlesource.gerrit.plugins.gitiles.FilteredRepository.java

License:Apache License

private FilteredRepository(ProjectControl ctl, Repository delegate, VisibleRefFilter refFilter) {
    super(toBuilder(delegate));
    this.delegate = delegate;
    if (ctl.allRefsAreVisible()) {
        this.refdb = delegate.getRefDatabase();
    } else {/*from ww  w .j ava 2 s  .c  o m*/
        this.refdb = new FilteredRefDatabase(delegate.getRefDatabase(), refFilter);
    }
}

From source file:com.microsoft.gittf.core.util.RepositoryUtil.java

License:Open Source License

public static boolean isEmptyRepository(final Repository repository) throws IOException {
    final RefDatabase refsDB = repository.getRefDatabase();
    final Map<String, Ref> refs = refsDB.getRefs(RefDatabase.ALL);
    return refs.isEmpty();
}

From source file:de.br0tbox.gitfx.ui.sync.SynchronizationTask.java

License:Apache License

private void refreshBranches() throws IOException {
    final List<String> remoteList = new ArrayList<>();
    final List<String> tagsList = new ArrayList<>();
    final List<String> localList = new ArrayList<>();
    final Repository repository = projectModel.getFxProject().getGit().getRepository();
    final Map<String, Ref> remotes = repository.getRefDatabase().getRefs(Constants.R_REMOTES);
    final Iterator<String> remotesIterator = remotes.keySet().iterator();
    while (remotesIterator.hasNext()) {
        final String remotekey = remotesIterator.next();
        final Ref remote = remotes.get(remotekey);
        remoteList.add(remote.getName());
    }/*from w  w w  .ja  v  a2s.co  m*/
    final Map<String, Ref> tags = repository.getRefDatabase().getRefs(Constants.R_TAGS);
    final Iterator<String> tagsIterator = tags.keySet().iterator();
    while (tagsIterator.hasNext()) {
        final String tagkey = tagsIterator.next();
        final Ref tag = tags.get(tagkey);
        tagsList.add(tag.getName());
    }
    final Map<String, Ref> all = repository.getRefDatabase().getRefs(Constants.R_REFS);
    final Iterator<String> allIterator = all.keySet().iterator();
    while (allIterator.hasNext()) {
        final String allKey = allIterator.next();
        final Ref allRef = all.get(allKey);
        if (allRef != null) {
            final String name = allRef.getName();
            if (!remoteList.contains(name) && !tagsList.contains(name)) {
                localList.add(name);
            }
        }
    }
    Platform.runLater(new Runnable() {

        @Override
        public void run() {
            projectModel.getLocalBranchesProperty().clear();
            projectModel.getLocalBranchesProperty().addAll(localList);
            projectModel.getRemoteBranchesProperty().clear();
            projectModel.getRemoteBranchesProperty().addAll(remoteList);
            projectModel.getTagsProperty().clear();
            projectModel.getTagsProperty().addAll(tagsList);
        }
    });
}

From source file:jbyoshi.gitupdate.Utils.java

License:Apache License

public static Map<String, Ref> getLocalBranches(Repository repo) throws IOException {
    return repo.getRefDatabase().getRefs(Constants.R_HEADS);
}

From source file:jetbrains.buildServer.buildTriggers.vcs.git.GitServerUtil.java

License:Apache License

/**
 * Check all refs successfully updated, throws exception if they are not
 * @param result fetch result// w  w w.  j a  va 2 s  .  com
 * @throws VcsException if any ref was not successfully updated
 */
public static void checkFetchSuccessful(Repository db, FetchResult result) throws VcsException {
    for (TrackingRefUpdate update : result.getTrackingRefUpdates()) {
        String localRefName = update.getLocalName();
        RefUpdate.Result status = update.getResult();
        if (status == RefUpdate.Result.REJECTED || status == RefUpdate.Result.LOCK_FAILURE
                || status == RefUpdate.Result.IO_FAILURE) {
            if (status == RefUpdate.Result.LOCK_FAILURE) {
                TreeSet<String> caseSensitiveConflicts = new TreeSet<>();
                TreeSet<String> conflicts = new TreeSet<>();
                try {
                    OSInfo.OSType os = OSInfo.getOSType();
                    if (os == OSInfo.OSType.WINDOWS || os == OSInfo.OSType.MACOSX) {
                        Set<String> refNames = db.getRefDatabase().getRefs(RefDatabase.ALL).keySet();
                        for (String ref : refNames) {
                            if (!localRefName.equals(ref) && localRefName.equalsIgnoreCase(ref))
                                caseSensitiveConflicts.add(ref);
                        }
                    }
                    conflicts.addAll(db.getRefDatabase().getConflictingNames(localRefName));
                } catch (Exception e) {
                    //ignore
                }
                String msg;
                if (!conflicts.isEmpty()) {
                    msg = "Failed to fetch ref " + localRefName + ": it clashes with "
                            + StringUtil.join(", ", conflicts)
                            + ". Please remove conflicting refs from repository.";
                } else if (!caseSensitiveConflicts.isEmpty()) {
                    msg = "Failed to fetch ref " + localRefName
                            + ": on case-insensitive file system it clashes with "
                            + StringUtil.join(", ", caseSensitiveConflicts)
                            + ". Please remove conflicting refs from repository.";
                } else {
                    msg = "Fail to update '" + localRefName + "' (" + status.name() + ")";
                }
                throw new VcsException(msg);
            } else {
                throw new VcsException("Fail to update '" + localRefName + "' (" + status.name() + ")");
            }
        }
    }
}

From source file:jetbrains.buildServer.buildTriggers.vcs.git.GitServerUtil.java

License:Apache License

/**
 * Removes branches of a bare repository which are not present in a remote repository
 *///from  w  w w .  j  ava  2  s. c o m
private static void pruneRemovedBranches(@NotNull Repository db, @NotNull Transport tn) throws IOException {
    FetchConnection conn = null;
    try {
        conn = tn.openFetch();
        Map<String, Ref> remoteRefMap = conn.getRefsMap();
        for (Map.Entry<String, Ref> e : db.getAllRefs().entrySet()) {
            if (!remoteRefMap.containsKey(e.getKey())) {
                try {
                    RefUpdate refUpdate = db.getRefDatabase().newUpdate(e.getKey(), false);
                    refUpdate.setForceUpdate(true);
                    refUpdate.delete();
                } catch (Exception ex) {
                    LOG.info("Failed to prune removed ref " + e.getKey(), ex);
                    break;
                }
            }
        }
    } finally {
        if (conn != null)
            conn.close();
    }
}

From source file:jetbrains.buildServer.buildTriggers.vcs.git.GitServerUtil.java

License:Apache License

public static boolean isCloned(@NotNull Repository db) throws VcsException, IOException {
    if (!db.getObjectDatabase().exists())
        return false;
    ObjectReader reader = db.getObjectDatabase().newReader();
    try {//from w  w w.  j  a va 2s  .  c o  m
        for (Ref ref : db.getRefDatabase().getRefs(RefDatabase.ALL).values()) {
            if (reader.has(ref.getObjectId()))
                return true;
        }
    } finally {
        reader.release();
    }
    return false;
}

From source file:jetbrains.buildServer.buildTriggers.vcs.git.tests.AgentVcsSupportTest.java

License:Apache License

public void do_not_create_branch_when_checkout_tag() throws Exception {
    myRoot.addProperty(Constants.BRANCH_NAME, "refs/tags/v1.0");
    myVcsSupport.updateSources(myRoot, new CheckoutRules(""), GitVcsSupportTest.VERSION_TEST_HEAD,
            myCheckoutDir, myBuild, false);

    Repository r = new RepositoryBuilder().setWorkTree(myCheckoutDir).build();
    Map<String, Ref> refs = r.getRefDatabase().getRefs("refs/");
    assertTrue(refs.containsKey("tags/v1.0"));
    assertTrue(refs.containsKey("tags/v0.7"));//it is reachable from refs/tags/v1.0
    assertTrue(refs.containsKey("tags/v0.5"));//also reachable
    assertEquals(3, refs.size());/*from  w w w. j a  va 2  s  . c  om*/
}

From source file:jetbrains.buildServer.buildTriggers.vcs.git.tests.AgentVcsSupportTest.java

License:Apache License

@Test(dataProvider = "mirrors")
public void fetch_all_heads(boolean useMirrors) throws Exception {
    AgentRunningBuild build = createRunningBuild(map(PluginConfigImpl.USE_MIRRORS, String.valueOf(useMirrors),
            PluginConfigImpl.FETCH_ALL_HEADS, "true"));

    myVcsSupport.updateSources(myRoot, CheckoutRules.DEFAULT, "465ad9f630e451b9f2b782ffb09804c6a98c4bb9",
            myCheckoutDir, build, false);

    Repository remoteRepo = new RepositoryBuilder().setBare().setGitDir(myMainRepo).build();
    Set<String> remoteHeads = remoteRepo.getRefDatabase().getRefs("refs/heads/").keySet();

    Repository r = new RepositoryBuilder().setWorkTree(myCheckoutDir).build();
    then(r.getRefDatabase().getRefs("refs/remotes/origin/").keySet()).containsAll(remoteHeads);
}