public void updateRefs() throws IOException, GitAPIException { List<Ref> allRefs = gitRepository.getRefs(Ref.class); List<String> refsToBePacked = new ArrayList<String>(allRefs.size()); for (Ref ref : allRefs) { if (ref.isUpdateable()) { if (nullSafeCommitUpdate(ref)) { refsToBePacked.add(ref.getName()); } } } if (!refsToBePacked.isEmpty()) { Repository repo = gitRepository.getRepository(); RefDatabase refDatabase = repo.getRefDatabase(); ((RefDirectory) refDatabase).pack(refsToBePacked); refDatabase.refresh(); } }
private RevWalk createAllCommitsRevWalk(CommitSelection commitSelection) throws IOException, MissingObjectException, IncorrectObjectTypeException { RevWalk revWalk = new RevWalk(repository); RefDatabase refDatabase = repository.getRefDatabase(); Map<String, org.eclipse.jgit.lib.Ref> refs = refDatabase.getRefs(""); for (Entry<String, Ref> entryRef : refs.entrySet()) { ObjectId refObject = entryRef.getValue().getObjectId(); RevObject revObject = revWalk.parseAny(refObject); if (revObject instanceof RevCommit) { revWalk.markStart((RevCommit) revObject); } } revWalk.setRevFilter(new CommitSelectionRevFilter(commitSelection)); revWalk.sort(RevSort.REVERSE); revWalk.sort(RevSort.TOPO, true); return revWalk; }
boolean isReachableFromHeadsOrTags(Repository repo, RevCommit commit) { try { RefDatabase refdb = repo.getRefDatabase(); Collection<Ref> heads = refdb.getRefs(Constants.R_HEADS).values(); Collection<Ref> tags = refdb.getRefs(Constants.R_TAGS).values(); Map<String, Ref> refs = Maps.newHashMapWithExpectedSize(heads.size() + tags.size()); for (Ref r : Iterables.concat(heads, tags)) { refs.put(r.getName(), r); } return commits.isReachableFrom(state, repo, commit, refs); } catch (IOException e) { log.error( String.format( "Cannot verify permissions to commit object %s in repository %s", commit.name(), getProject().getNameKey()), e); return false; } }
private Result resolve() throws IOException { RefDatabase refDb = repo.getRefDatabase(); Collection<Ref> tags = refDb.getRefs(Constants.R_TAGS).values(); Collection<Ref> branches = refDb.getRefs(Constants.R_HEADS).values(); List<Ref> allTagsAndBranches = Lists.newArrayListWithCapacity(tags.size() + branches.size()); allTagsAndBranches.addAll(tags); allTagsAndBranches.addAll(branches); parseCommits(allTagsAndBranches); Set<String> allMatchingTagsAndBranches = includedIn(tipsByCommitTime, 0); Result detail = new Result(); detail.setBranches(getMatchingRefNames(allMatchingTagsAndBranches, branches)); detail.setTags(getMatchingRefNames(allMatchingTagsAndBranches, tags)); return detail; }
@Override public void setAdvertisedRefs(final Map<String, Ref> allRefs, final Set<ObjectId> additionalHaves) { super.setAdvertisedRefs(allRefs, additionalHaves); final Map<String, Ref> refs = getAdvertisedRefs(); if (getRepository().getRefDatabase() instanceof RefTreeDatabase) { final RefDatabase bootstrap = ((RefTreeDatabase) getRepository().getRefDatabase()).getBootstrap(); try { for (Map.Entry<String, Ref> entry : bootstrap.getRefs("").entrySet()) { refs.put(entry.getKey(), entry.getValue()); } } catch (final IOException e) { e.printStackTrace(); } } }
private RefTree rebuild(RefDatabase refdb) throws IOException { RefTree tree = RefTree.newEmptyTree(); List<Command> cmds = new ArrayList<>(); Ref head = refdb.exactRef(HEAD); if (head != null) { cmds.add(new org.eclipse.jgit.internal.storage.reftree.Command( null, head)); } for (Ref r : refdb.getRefs(RefDatabase.ALL).values()) { if (r.getName().equals(txnCommitted) || r.getName().equals(HEAD) || r.getName().startsWith(txnNamespace)) { continue; } cmds.add(new org.eclipse.jgit.internal.storage.reftree.Command( null, git.getRepository().peel(r))); } tree.apply(cmds); return tree; }
private static List<Map<String, Object>> getRefsSoyData( RefDatabase refdb, GitilesView view, String prefix, Ordering<Ref> ordering, @Nullable Ref headLeaf, int limit) throws IOException { Collection<Ref> refs = refdb.getRefs(prefix).values(); refs = ordering.leastOf(refs, limit > 0 ? limit + 1 : refs.size()); List<Map<String, Object>> result = Lists.newArrayListWithCapacity(refs.size()); for (Ref ref : refs) { String name = ref.getName().substring(prefix.length()); boolean needPrefix = !ref.getName().equals(refdb.getRef(name).getName()); Map<String, Object> value = Maps.newHashMapWithExpectedSize(3); value.put("url", GitilesView.revision().copyFrom(view).setRevision( Revision.unpeeled(needPrefix ? ref.getName() : name, ref.getObjectId())).toUrl()); value.put("name", name); if (headLeaf != null) { value.put("isHead", headLeaf.equals(ref)); } result.add(value); } return result; }
/** {@inheritDoc} */ public Set<String> getRefNames(String refPrefix) throws GitException, InterruptedException { if (refPrefix.isEmpty()) { refPrefix = RefDatabase.ALL; } else { refPrefix = refPrefix.replace(' ', '_'); } try (Repository repo = getRepository()) { Map<String, Ref> refList = repo.getRefDatabase().getRefs(refPrefix); // The key set for refList will have refPrefix removed, so to recover it we just grab the full name. Set<String> refs = new HashSet<>(refList.size()); for (Ref ref : refList.values()) { refs.add(ref.getName()); } return refs; } catch (IOException e) { throw new GitException("Error retrieving refs with prefix " + refPrefix, e); } }
public List<String> getRefPrefixes(Class<? extends Ref> refType) { List<String> refPrefixes = new ArrayList<String>(); if (Ref.class.equals(refType)) { refPrefixes.add(RefDatabase.ALL); } else if (Branch.class.equals(refType)) { refPrefixes.addAll(getLocalBranchRefs()); refPrefixes.addAll(getRemoteBranchRefs()); } else if (LocalBranch.class.equals(refType)) { refPrefixes.addAll(getLocalBranchRefs()); } else if (RemoteBranch.class.equals(refType)) { refPrefixes.addAll(getRemoteBranchRefs()); } return refPrefixes; }
@SuppressWarnings("unchecked") public <T extends Ref> List<T> getRefs(Class<T> refType) { Repository repository = getRepository(); RefDatabase refDatabase = repository.getRefDatabase(); refDatabase.refresh(); List<String> prefixes = refFactory.getRefPrefixes(refType); List<T> refList = new ArrayList<T>(); for (String prefix : prefixes) { try { Map<String, org.eclipse.jgit.lib.Ref> refs = refDatabase .getRefs(prefix); for (Entry<String, org.eclipse.jgit.lib.Ref> refEntry : refs .entrySet()) { org.eclipse.jgit.lib.Ref jgitRef = refEntry.getValue(); Ref ref = refFactory.create(jgitRef); if (ref != null) { refList.add((T) ref); } } } catch (IOException e) { throw new GitRepositoryException(e); } } Collections.sort(refList, DefaultRefSorter.INSTANCE); return refList; }
void build(Repository git, TagSet old, TagMatcher m) { if (old != null && m != null && refresh(old, m)) { return; } try (TagWalk rw = new TagWalk(git)) { rw.setRetainBody(false); for (Ref ref : git.getRefDatabase().getRefs(RefDatabase.ALL).values()) { if (skip(ref)) { continue; } else if (isTag(ref)) { // For a tag, remember where it points to. addTag(rw, git.peel(ref)); } else { // New reference to include in the set. addRef(rw, ref); } } // Traverse the complete history. Copy any flags from a commit to // all of its ancestors. This automatically updates any Tag object // as the TagCommit and the stored Tag object share the same // underlying bit set. TagCommit c; while ((c = (TagCommit) rw.next()) != null) { BitSet mine = c.refFlags; int pCnt = c.getParentCount(); for (int pIdx = 0; pIdx < pCnt; pIdx++) { ((TagCommit) c.getParent(pIdx)).refFlags.or(mine); } } } catch (IOException e) { log.warn("Error building tags for repository " + projectName, e); } }
public List<BranchSummary> getAll() { RefDatabase refDatabase = repository.getRefDatabase(); try { Map<String, Ref> branchRefs = localOrRemoteBranches(refDatabase); return summariesForRefs(branchRefs.values()); } catch (IOException e) { throw new RuntimeException(e); } }
private Map<String, Ref> localOrRemoteBranches(RefDatabase refDatabase) throws IOException { Map<String, Ref> branchRefs = refDatabase.getRefs(R_HEADS); if (branchRefs.isEmpty()) { branchRefs = refDatabase.getRefs(R_REMOTES); } return branchRefs; }
private boolean isVisible(Repository repo, RevWalk walk, ObjectId id, Collection<ObjectId> knownReachable) throws IOException { RevCommit commit; 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, knownReachable) || isReachableFromRefs(walk, commit, filter(allRefs, refStartsWith(R_HEADS))) || isReachableFromRefs(walk, commit, filter(allRefs, refStartsWith(R_TAGS))) || isReachableFromRefs(walk, commit, filter(allRefs, otherRefs())); }
static List<Map<String, Object>> getBranchesSoyData(HttpServletRequest req, int limit) throws IOException { RefDatabase refdb = ServletUtils.getRepository(req).getRefDatabase(); Ref head = refdb.getRef(Constants.HEAD); Ref headLeaf = head != null && head.isSymbolic() ? head.getLeaf() : null; return getRefsSoyData( refdb, ViewFilter.getView(req), Constants.R_HEADS, branchComparator(headLeaf), headLeaf, limit); }
private static Map<String, Ref> getRefs(RefDatabase refdb, String path) throws IOException { path = GitilesView.maybeTrimLeadingAndTrailingSlash(path); if (path.isEmpty()) { return refdb.getRefs(RefDatabase.ALL); } path = Constants.R_REFS + path; Ref singleRef = refdb.getRef(path); if (singleRef != null) { return ImmutableMap.of(singleRef.getName(), singleRef); } return refdb.getRefs(path + '/'); }
private void deleteOriginalRefs() throws IOException { try (RevWalk revWalk = new RevWalk(repository)) { Collection<Ref> refs = new ArrayList<>(); RefDatabase refDatabase = repository.getRefDatabase(); Map<String, Ref> originalBranches = refDatabase.getRefs("refs/heads/original/"); Map<String, Ref> originalTags = refDatabase.getRefs("refs/tags/original/"); refs.addAll(originalBranches.values()); refs.addAll(originalTags.values()); for (Ref originalRef : refs) { RefUpdate refUpdate = repository.updateRef(originalRef.getName()); refUpdate.setForceUpdate(true); refUpdate.delete(revWalk); } } }
private static Set<String> getRefNames(Repository repo, String prefix) throws IOException { RefDatabase refDb = repo.getRefDatabase(); return refDb.getRefs(prefix).keySet(); }