/** * Check if any refs exist in the ref database. * <p> * This uses the same definition of refs as {@link #getRefs()}. In * particular, returns {@code false} in a new repository with no refs * under {@code refs/} and {@code HEAD} pointing to a branch yet to be * born, and returns {@code true} in a repository with no refs under * {@code refs/} and a detached {@code HEAD} pointing to history. * * @return true if the database has refs. * @throws java.io.IOException * the reference space cannot be accessed. * @since 5.0 */ public boolean hasRefs() throws IOException { return !getRefs().isEmpty(); }
/** * Returns all refs. * <p> * This includes {@code HEAD}, branches under {@code ref/heads/}, tags * under {@code refs/tags/}, etc. It does not include pseudo-refs like * {@code FETCH_HEAD}; for those, see {@link #getAdditionalRefs}. * <p> * Symbolic references to a non-existent ref (for example, * {@code HEAD} pointing to a branch yet to be born) are not included. * <p> * Callers interested in only a portion of the ref hierarchy can call * {@link #getRefsByPrefix} instead. * * @return immutable list of all refs. * @throws java.io.IOException * the reference space cannot be accessed. * @since 5.0 */ @NonNull public List<Ref> getRefs() throws IOException { return getRefsByPrefix(ALL); }
/** * Search for a ref by (possibly abbreviated) name. * * @param name * the name of the ref to lookup. May be a short-hand form, e.g. * "master" which is is automatically expanded to * "refs/heads/master" if "refs/heads/master" already exists. * @return the Ref with the given name, or {@code null} if it does not exist * @throws java.io.IOException * @since 4.2 */ @Nullable public final Ref findRef(String name) throws IOException { return getRefDatabase().getRef(name); }
private Collection<Ref> getAllRefs() throws IOException { Collection<Ref> refs = refdb.getRefs(); List<Ref> addl = refdb.getAdditionalRefs(); if (!addl.isEmpty()) { List<Ref> all = new ArrayList<>(refs.size() + addl.size()); all.addAll(refs); // add additional refs which start with refs/ for (Ref r : addl) { if (r.getName().startsWith(Constants.R_REFS)) { all.add(r); } } return all; } return refs; }
/** {@inheritDoc} */ @Override public List<Ref> getAdditionalRefs() throws IOException { Collection<Ref> txnRefs; if (txnNamespace != null) { txnRefs = bootstrap.getRefsByPrefix(txnNamespace); } else { Ref r = bootstrap.exactRef(txnCommitted); if (r != null && r.getObjectId() != null) { txnRefs = Collections.singleton(r); } else { txnRefs = Collections.emptyList(); } } List<Ref> otherRefs = bootstrap.getAdditionalRefs(); List<Ref> all = new ArrayList<>(txnRefs.size() + otherRefs.size()); all.addAll(txnRefs); all.addAll(otherRefs); return all; }
BatchRefUpdate batch = refdb.newBatchUpdate(); batch.addProposedTimestamp(ts); batch.setRefLogIdent(getSystem().newCommitter(ts)); Map<String, Ref> refs = refdb.getRefs(ALL); batch.addCommand(prepareCommit(git, refs, committed.getNewId())); if (!failed.isEmpty()) { String[] arr = failed.toArray(new String[0]); req.setRefs(refdb.exactRef(arr));
/** * Create a command to update, create or delete a ref in this repository. * * @param ref * name of the ref the caller wants to modify. * @param detach * true to create a detached head * @return an update command. The caller must finish populating this command * and then invoke one of the update methods to actually make a * change. * @throws java.io.IOException * a symbolic ref was passed in and could not be resolved back * to the base ref, as the symbolic ref could not be read. */ @NonNull public RefUpdate updateRef(String ref, boolean detach) throws IOException { return getRefDatabase().newUpdate(ref, detach); }
Collection<Ref>repositoryHeads = repo.getRefDatabase().getRefs(refPrefix).values(); BatchRefUpdate batch = repo.getRefDatabase().newBatchUpdate();
/** * Peel a possibly unpeeled reference to an annotated tag. * <p> * If the ref cannot be peeled (as it does not refer to an annotated tag) * the peeled id stays null, but {@link org.eclipse.jgit.lib.Ref#isPeeled()} * will be true. * * @param ref * The ref to peel * @return <code>ref</code> if <code>ref.isPeeled()</code> is true; else a * new Ref object representing the same data as Ref, but isPeeled() * will be true and getPeeledObjectId will contain the peeled object * (or null). * @deprecated use {@code getRefDatabase().peel(ref)} instead. */ @Deprecated @NonNull public Ref peel(Ref ref) { try { return getRefDatabase().peel(ref); } catch (IOException e) { // Historical accident; if the reference cannot be peeled due // to some sort of repository access problem we claim that the // same as if the reference was not an annotated tag. return ref; } }
final RefUpdate refUpdate = bootstrap.newUpdate(refdb.getTxnCommitted(), false); final Ref ref = bootstrap.exactRef(refdb.getTxnCommitted()); final RefTree tree; if (ref != null && ref.getObjectId() != null) { final Ref ref2 = bootstrap.exactRef(refdb.getTxnCommitted()); if (ref2 == null || ref2.getObjectId().equals(ref != null ? ref.getObjectId() : null)) { cb.setTreeId(tree.writeTree(inserter));
public static List<String> getHeadTags(Repository repository) throws IOException { ObjectId head = repository.resolve(Constants.HEAD); if (head == null) { return Collections.emptyList(); } return repository.getRefDatabase().getRefsByPrefix(Constants.R_TAGS).stream() .map(ref -> { try { return repository.getRefDatabase().peel(ref); } catch (IOException e) { throw new RuntimeException(e); } }) .filter(ref -> { ObjectId objectId; if (ref.getPeeledObjectId() != null) { objectId = ref.getPeeledObjectId(); } else { objectId = ref.getObjectId(); } return objectId.equals(head); }) .map(ref -> ref.getName().replaceFirst("^" + Constants.R_TAGS, "")) .collect(Collectors.toList()); }
/** * Find the first named reference. * <p> * This method expects a list of unshortened reference names and returns * the first that exists. * * @param refs * the unabbreviated names of references to look up. * @return the first named reference that exists (if any); else {@code null}. * @throws java.io.IOException * the reference space cannot be accessed. * @since 4.1 */ @Nullable public Ref firstExactRef(String... refs) throws IOException { for (String name : refs) { Ref ref = exactRef(name); if (ref != null) { return ref; } } return null; }
private void updateBootstrap(RevWalk rw, ReceiveCommand commit) throws IOException { BatchRefUpdate u = refdb.getBootstrap().newBatchUpdate(); u.setAllowNonFastForwards(true); u.setPushCertificate(getPushCertificate()); if (isRefLogDisabled()) { u.disableRefLog(); } else { u.setRefLogIdent(author); u.setRefLogMessage(getRefLogMessage(), false); } u.addCommand(commit); u.execute(rw, NullProgressMonitor.INSTANCE); } }
if (!target.startsWith(Constants.R_REFS)) throw new IllegalArgumentException(MessageFormat.format(JGitText.get().illegalArgumentNotA, Constants.R_REFS)); if (checkConflicting && getRefDatabase().isNameConflicting(getName())) return Result.LOCK_FAILURE; try { return Result.LOCK_FAILURE; final Ref old = getRefDatabase().getRef(getName()); if (old != null && old.isSymbolic()) { final Ref dst = old.getTarget(); setOldObjectId(old.getObjectId()); final Ref dst = getRefDatabase().getRef(target); if (dst != null && dst.getObjectId() != null) setNewObjectId(dst.getObjectId());
BatchRefUpdate batch = refdb.newBatchUpdate(); batch.setRefLogIdent(getSystem().newCommitter()); batch.setRefLogMessage("ketch", false); //$NON-NLS-1$ Map<String, Ref> refs = refdb.getRefs(ALL); batch.addCommand(prepareCommit(git, refs, committed.getNewId())); if (!failed.isEmpty()) { String[] arr = failed.toArray(new String[failed.size()]); req.setRefs(refdb.exactRef(arr));
/** * Returns a collection of all refs and additional refs. * * Additional refs which don't start with "refs/" are not returned because * they should not save objects from being garbage collected. Examples for * such references are ORIG_HEAD, MERGE_HEAD, FETCH_HEAD and * CHERRY_PICK_HEAD. * * @return a collection of refs pointing to live objects. * @throws IOException */ private Collection<Ref> getAllRefs() throws IOException { RefDatabase refdb = repo.getRefDatabase(); Collection<Ref> refs = refdb.getRefs(); List<Ref> addl = refdb.getAdditionalRefs(); if (!addl.isEmpty()) { List<Ref> all = new ArrayList<>(refs.size() + addl.size()); all.addAll(refs); // add additional refs which start with refs/ for (Ref r : addl) { checkCancelled(); if (r.getName().startsWith(Constants.R_REFS)) { all.add(r); } } return all; } return refs; }
/** {@inheritDoc} */ @Override public RefUpdate newUpdate(String name, boolean detach) throws IOException { if (!repo.isBare() && name.indexOf('/') < 0 && !HEAD.equals(name)) { return bootstrap.newUpdate(name, detach); } if (conflictsWithBootstrap(name)) { return new AlwaysFailUpdate(this, name); } Ref r = exactRef(name); if (r == null) { r = new ObjectIdRef.Unpeeled(Storage.NEW, name, null); } boolean detaching = detach && r.isSymbolic(); if (detaching) { r = new ObjectIdRef.Unpeeled(LOOSE, name, r.getObjectId()); } RefTreeUpdate u = new RefTreeUpdate(this, r); if (detaching) { u.setDetachingSymbolicRef(); } return u; }