Don't use deprecated Repository#getAllRefs in FileRepository

Also expose the potentially IOException thrown by RefDatabase#getRefs.
Hence the following methods now potentially throw IOException:
- AdvertiseRefsHook#advertiseRefs
- ReceivePack#setAdvertisedRefs
- Repository#getAdditionalHaves

Bug: 534731
Change-Id: I85bc6ce5815d40be5f80042c53f4663072d96be5
This commit is contained in:
Matthias Sohn 2021-06-23 09:41:04 +02:00
parent c20dd8206e
commit 403338e116
7 changed files with 31 additions and 14 deletions

View File

@ -142,7 +142,7 @@ ReceivePack createReceivePack(Repository db) {
rp.setAdvertiseRefsHook(new AdvertiseRefsHook() {
@Override
public void advertiseRefs(ReceivePack rp2)
throws ServiceMayNotContinueException {
throws IOException {
rp.setAdvertisedRefs(rp.getRepository().getAllRefs(),
null);
new HidePrivateHook().advertiseRefs(rp);

View File

@ -420,9 +420,11 @@ private File descriptionFile() {
* advertise that it safely has that other repository's references, without
* exposing any other details about the other repository. This may help a
* client trying to push changes avoid pushing more than it needs to.
*
* @throws IOException
*/
@Override
public Set<ObjectId> getAdditionalHaves() {
public Set<ObjectId> getAdditionalHaves() throws IOException {
return getAdditionalHaves(null);
}
@ -438,8 +440,11 @@ public Set<ObjectId> getAdditionalHaves() {
* Set of AlternateHandle Ids already seen
*
* @return unmodifiable collection of other known objects.
* @throws IOException
* if getting refs hits an IO error
*/
private Set<ObjectId> getAdditionalHaves(Set<AlternateHandle.Id> skips) {
private Set<ObjectId> getAdditionalHaves(Set<AlternateHandle.Id> skips)
throws IOException {
HashSet<ObjectId> r = new HashSet<>();
skips = objectDatabase.addMe(skips);
for (AlternateHandle d : objectDatabase.myAlternates()) {
@ -447,7 +452,7 @@ private Set<ObjectId> getAdditionalHaves(Set<AlternateHandle.Id> skips) {
FileRepository repo;
repo = ((AlternateRepository) d).repository;
for (Ref ref : repo.getAllRefs().values()) {
for (Ref ref : repo.getRefDatabase().getRefs()) {
if (ref.getObjectId() != null)
r.add(ref.getObjectId());
if (ref.getPeeledObjectId() != null)

View File

@ -1051,13 +1051,14 @@ public String getBranch() throws IOException {
* <p>
* When a repository borrows objects from another repository, it can
* advertise that it safely has that other repository's references, without
* exposing any other details about the other repository. This may help
* a client trying to push changes avoid pushing more than it needs to.
* exposing any other details about the other repository. This may help a
* client trying to push changes avoid pushing more than it needs to.
*
* @return unmodifiable collection of other known objects.
* @throws IOException
*/
@NonNull
public Set<ObjectId> getAdditionalHaves() {
public Set<ObjectId> getAdditionalHaves() throws IOException {
return Collections.emptySet();
}

View File

@ -42,6 +42,7 @@
package org.eclipse.jgit.transport;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
@ -65,10 +66,12 @@ public void advertiseRefs(UploadPack uploadPack)
uploadPack.getRepository(), uploadPack.getRevWalk()));
}
/** {@inheritDoc} */
/**
* {@inheritDoc}
*/
@Override
public void advertiseRefs(ReceivePack receivePack)
throws ServiceMayNotContinueException {
throws IOException {
Map<String, Ref> refs = getAdvertisedRefs(receivePack.getRepository(),
receivePack.getRevWalk());
Set<ObjectId> haves = getAdvertisedHaves(receivePack.getRepository(),

View File

@ -42,6 +42,8 @@
package org.eclipse.jgit.transport;
import java.io.IOException;
/**
* Hook to allow callers to take over advertising refs to the client.
*
@ -89,8 +91,9 @@ void advertiseRefs(UploadPack uploadPack)
* if necessary.
* @throws org.eclipse.jgit.transport.ServiceMayNotContinueException
* abort; the message will be sent to the user.
* @throws IOException
* @since 5.6
*/
void advertiseRefs(ReceivePack receivePack)
throws ServiceMayNotContinueException;
throws ServiceMayNotContinueException, IOException;
}

View File

@ -10,6 +10,7 @@
package org.eclipse.jgit.transport;
import java.io.IOException;
import java.util.List;
/**
@ -49,10 +50,12 @@ public static AdvertiseRefsHook newChain(List<? extends AdvertiseRefsHook> hooks
}
}
/** {@inheritDoc} */
/**
* {@inheritDoc}
*/
@Override
public void advertiseRefs(ReceivePack rp)
throws ServiceMayNotContinueException {
throws IOException {
for (int i = 0; i < count; i++)
hooks[i].advertiseRefs(rp);
}

View File

@ -440,9 +440,10 @@ public Map<String, Ref> getAdvertisedRefs() {
* explicit set of additional haves to claim as advertised. If
* null, assumes the default set of additional haves from the
* repository.
* @throws IOException
*/
public void setAdvertisedRefs(Map<String, Ref> allRefs,
Set<ObjectId> additionalHaves) {
Set<ObjectId> additionalHaves) throws IOException {
refs = allRefs != null ? allRefs : getAllRefs();
refs = refFilter.filter(refs);
advertisedHaves.clear();
@ -1187,8 +1188,9 @@ protected void init(final InputStream input, final OutputStream output,
* Get advertised refs, or the default if not explicitly advertised.
*
* @return advertised refs, or the default if not explicitly advertised.
* @throws IOException
*/
private Map<String, Ref> getAdvertisedOrDefaultRefs() {
private Map<String, Ref> getAdvertisedOrDefaultRefs() throws IOException {
if (refs == null)
setAdvertisedRefs(null, null);
return refs;