/** * 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; }
/** * Read the specified references. * <p> * This method expects a list of unshortened reference names and returns * a map from reference names to refs. Any named references that do not * exist will not be included in the returned map. * * @param refs * the unabbreviated names of references to look up. * @return modifiable map describing any refs that exist among the ref * ref names supplied. The map can be an unsorted map. * @throws java.io.IOException * the reference space cannot be accessed. * @since 4.1 */ @NonNull public Map<String, Ref> exactRef(String... refs) throws IOException { Map<String, Ref> result = new HashMap<>(refs.length); for (String name : refs) { Ref ref = exactRef(name); if (ref != null) { result.put(name, ref); } } return result; }
/** {@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; }
/** * Get a ref by name. * * @param name * the name of the ref to lookup. Must not be a short-hand * form; e.g., "master" is not automatically expanded to * "refs/heads/master". * @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 exactRef(String name) throws IOException { return getRefDatabase().exactRef(name); }
/** {@inheritDoc} */ @Override public Map<String, Ref> getRefs(String prefix) throws IOException { if (!prefix.isEmpty() && prefix.charAt(prefix.length() - 1) != '/') { return new HashMap<>(0); } Ref src = bootstrap.exactRef(txnCommitted); Scanner.Result c = refs; if (c == null || !c.refTreeId.equals(idOf(src))) { c = Scanner.scanRefTree(repo, src, prefix, true); if (prefix.isEmpty()) { refs = c; } } return new RefMap(prefix, RefList.<Ref> emptyList(), c.all, c.sym); }
/** * Read a ref on behalf of the client. * <p> * This checks that the ref is present in the ref advertisement since * otherwise the client might not be supposed to be able to read it. * * @param name * the unabbreviated name of the reference. * @return the requested Ref, or {@code null} if it is not visible or * does not exist. * @throws java.io.IOException * on failure to read the ref or check it for visibility. */ @Nullable private Ref getRef(String name) throws IOException { if (refs != null) { return refs.get(name); } if (!advertiseRefsHookCalled) { advertiseRefsHook.advertiseRefs(this); advertiseRefsHookCalled = true; } if (refs == null && refFilter == RefFilter.DEFAULT && transferConfig.hasDefaultRefFilter()) { // Fast path: no ref filtering is needed. return db.getRefDatabase().exactRef(name); } return getAdvertisedOrDefaultRefs().get(name); }
/** {@inheritDoc} */ @Override public Ref exactRef(String name) throws IOException { if (!repo.isBare() && name.indexOf('/') < 0 && !HEAD.equals(name)) { // Pass through names like MERGE_HEAD, ORIG_HEAD, FETCH_HEAD. return bootstrap.exactRef(name); } else if (conflictsWithBootstrap(name)) { return null; } boolean partial = false; Ref src = bootstrap.exactRef(txnCommitted); Scanner.Result c = refs; if (c == null || !c.refTreeId.equals(idOf(src))) { c = Scanner.scanRefTree(repo, src, prefixOf(name), false); partial = true; } Ref r = c.all.get(name); if (r != null && r.isSymbolic()) { r = c.sym.get(name); if (partial && r.getObjectId() == null) { // Attempting exactRef("HEAD") with partial scan will leave // an unresolved symref as its target e.g. refs/heads/master // was not read by the partial scan. Scan everything instead. return getRefs(ALL).get(name); } } return r; }
void load() throws IOException { close(); reader = db.newObjectReader(); Ref ref = db.getRefDatabase().exactRef(REF_NAME); if (ref == null) { // No ref, same as empty. return; } try (RevWalk rw = new RevWalk(reader)) { commit = rw.parseCommit(ref.getObjectId()); } }
/** * Initializes local replica by reading accepted and committed references. * <p> * Loads accepted and committed references from the reference database of * the local replica and stores their current ObjectIds in memory. * * @param repo * repository to initialize state from. * @throws IOException * cannot read repository state. */ void initialize(Repository repo) throws IOException { RefDatabase refdb = repo.getRefDatabase(); if (refdb instanceof RefTreeDatabase) { RefTreeDatabase treeDb = (RefTreeDatabase) refdb; String txnNamespace = getSystem().getTxnNamespace(); if (!txnNamespace.equals(treeDb.getTxnNamespace())) { throw new IOException(MessageFormat.format( KetchText.get().mismatchedTxnNamespace, txnNamespace, treeDb.getTxnNamespace())); } refdb = treeDb.getBootstrap(); } initialize(refdb.exactRef( getSystem().getTxnAccepted(), getSystem().getTxnCommitted())); }
void init(RevWalk rw) throws IOException { src = refdb.getBootstrap().exactRef(refdb.getTxnCommitted()); if (src != null && src.getObjectId() != null) { RevCommit c = rw.parseCommit(src.getObjectId()); parentCommitId = c; parentTreeId = c.getTree(); tree = RefTree.read(rw.getObjectReader(), c.getTree()); } else { parentCommitId = ObjectId.zeroId(); parentTreeId = new ObjectInserter.Formatter() .idFor(OBJ_TREE, new byte[] {}); tree = RefTree.newEmptyTree(); } }
if (!failed.isEmpty()) { String[] arr = failed.toArray(new String[0]); req.setRefs(refdb.exactRef(arr));
/** * Get a ref by name. * * @param name * the name of the ref to lookup. Must not be a short-hand * form; e.g., "master" is not automatically expanded to * "refs/heads/master". * @return the Ref with the given name, or {@code null} if it does not exist * @throws IOException * @since 4.2 */ @Nullable public Ref exactRef(String name) throws IOException { return getRefDatabase().exactRef(name); }
/** * Get a ref by name. * * @param name * the name of the ref to lookup. Must not be a short-hand * form; e.g., "master" is not automatically expanded to * "refs/heads/master". * @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 exactRef(String name) throws IOException { return getRefDatabase().exactRef(name); }
@Override public Map<String, Ref> getRefs(String prefix) throws IOException { if (!prefix.isEmpty() && prefix.charAt(prefix.length() - 1) != '/') { return new HashMap<>(0); } Ref src = bootstrap.exactRef(txnCommitted); Scanner.Result c = refs; if (c == null || !c.refTreeId.equals(idOf(src))) { c = Scanner.scanRefTree(repo, src, prefix, true); if (prefix.isEmpty()) { refs = c; } } return new RefMap(prefix, RefList.<Ref> emptyList(), c.all, c.sym); }
/** {@inheritDoc} */ @Override public Map<String, Ref> getRefs(String prefix) throws IOException { if (!prefix.isEmpty() && prefix.charAt(prefix.length() - 1) != '/') { return new HashMap<>(0); } Ref src = bootstrap.exactRef(txnCommitted); Scanner.Result c = refs; if (c == null || !c.refTreeId.equals(idOf(src))) { c = Scanner.scanRefTree(repo, src, prefix, true); if (prefix.isEmpty()) { refs = c; } } return new RefMap(prefix, RefList.<Ref> emptyList(), c.all, c.sym); }
private static void testDoUpdateRef(String ref, ObjectId commitId, boolean tagExists) throws Exception { final org.eclipse.jgit.lib.Repository jGitRepo = mock(org.eclipse.jgit.lib.Repository.class); final RefDatabase refDatabase = mock(RefDatabase.class); final RevWalk revWalk = mock(RevWalk.class); final RefUpdate refUpdate = mock(RefUpdate.class); when(jGitRepo.getRefDatabase()).thenReturn(refDatabase); when(refDatabase.exactRef(ref)).thenReturn(tagExists ? mock(Ref.class) : null); when(jGitRepo.updateRef(ref)).thenReturn(refUpdate); when(refUpdate.update(revWalk)).thenReturn(RefUpdate.Result.NEW); GitRepository.doRefUpdate(jGitRepo, revWalk, ref, commitId); when(refUpdate.update(revWalk)).thenReturn(RefUpdate.Result.FAST_FORWARD); GitRepository.doRefUpdate(jGitRepo, revWalk, ref, commitId); when(refUpdate.update(revWalk)).thenReturn(RefUpdate.Result.LOCK_FAILURE); assertThatThrownBy(() -> GitRepository.doRefUpdate(jGitRepo, revWalk, ref, commitId)) .isInstanceOf(StorageException.class); }
void load() throws IOException { close(); reader = db.newObjectReader(); Ref ref = db.getRefDatabase().exactRef(REF_NAME); if (ref == null) { // No ref, same as empty. return; } try (RevWalk rw = new RevWalk(reader)) { commit = rw.parseCommit(ref.getObjectId()); } }
void load() throws IOException { close(); reader = db.newObjectReader(); Ref ref = db.getRefDatabase().exactRef(REF_NAME); if (ref == null) { // No ref, same as empty. return; } try (RevWalk rw = new RevWalk(reader)) { commit = rw.parseCommit(ref.getObjectId()); } }
void init(RevWalk rw) throws IOException { src = refdb.getBootstrap().exactRef(refdb.getTxnCommitted()); if (src != null && src.getObjectId() != null) { RevCommit c = rw.parseCommit(src.getObjectId()); parentCommitId = c; parentTreeId = c.getTree(); tree = RefTree.read(rw.getObjectReader(), c.getTree()); } else { parentCommitId = ObjectId.zeroId(); parentTreeId = new ObjectInserter.Formatter() .idFor(OBJ_TREE, new byte[] {}); tree = RefTree.newEmptyTree(); } }
void init(RevWalk rw) throws IOException { src = refdb.getBootstrap().exactRef(refdb.getTxnCommitted()); if (src != null && src.getObjectId() != null) { RevCommit c = rw.parseCommit(src.getObjectId()); parentCommitId = c; parentTreeId = c.getTree(); tree = RefTree.read(rw.getObjectReader(), c.getTree()); } else { parentCommitId = ObjectId.zeroId(); parentTreeId = new ObjectInserter.Formatter() .idFor(OBJ_TREE, new byte[] {}); tree = RefTree.newEmptyTree(); } }