Propagate IOException where possible when getting refs.

Currently, Repository.getAllRefs() and Repository.getTags() silently
ignores an IOException and instead returns an empty map. Repository
is a public API and as such cannot be changed until the next major
revision change. Where possible, update the internal jgit APIs to
use the RefDatabase directly, since it propagates the error.

Change-Id: I4e4537d8bd0fa772f388262684c5c4ca1929dc4c
This commit is contained in:
Colby Ranger 2013-10-07 10:25:28 -07:00
parent 7995d87713
commit 5218f7b33a
16 changed files with 72 additions and 22 deletions

View File

@ -44,6 +44,7 @@
package org.eclipse.jgit.http.server;
import static org.eclipse.jgit.http.server.ServletUtils.getRepository;
import static org.eclipse.jgit.lib.RefDatabase.ALL;
import java.io.IOException;
import java.io.OutputStreamWriter;
@ -91,7 +92,7 @@ protected void end() {
adv.init(db);
adv.setDerefTags(true);
Map<String, Ref> refs = db.getAllRefs();
Map<String, Ref> refs = db.getRefDatabase().getRefs(ALL);
refs.remove(Constants.HEAD);
adv.send(refs);
out.close();

View File

@ -43,6 +43,8 @@
package org.eclipse.jgit.pgm;
import static org.eclipse.jgit.lib.RefDatabase.ALL;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
@ -175,7 +177,7 @@ protected void run() throws Exception {
}
private void list() throws Exception {
Map<String, Ref> refs = db.getAllRefs();
Map<String, Ref> refs = db.getRefDatabase().getRefs(ALL);
Ref head = refs.get(Constants.HEAD);
// This can happen if HEAD is stillborn
if (head != null) {

View File

@ -44,6 +44,8 @@
package org.eclipse.jgit.pgm;
import static org.eclipse.jgit.lib.RefDatabase.ALL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@ -64,7 +66,7 @@ class RevParse extends TextBuiltin {
@Override
protected void run() throws Exception {
if (all) {
Map<String, Ref> allRefs = db.getAllRefs();
Map<String, Ref> allRefs = db.getRefDatabase().getRefs(ALL);
for (final Ref r : allRefs.values())
outw.println(r.getObjectId().name());
} else {

View File

@ -47,6 +47,7 @@
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.Option;
@ -55,6 +56,7 @@
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.RefDatabase;
import org.eclipse.jgit.pgm.internal.CLIText;
import org.eclipse.jgit.pgm.opt.PathTreeFilterHandler;
import org.eclipse.jgit.revwalk.FollowFilter;
@ -164,8 +166,10 @@ protected void run() throws Exception {
else if (revLimiter.size() > 1)
walk.setRevFilter(AndRevFilter.create(revLimiter));
if (all)
for (Ref a : db.getAllRefs().values()) {
if (all) {
Map<String, Ref> refs =
db.getRefDatabase().getRefs(RefDatabase.ALL);
for (Ref a : refs.values()) {
ObjectId oid = a.getPeeledObjectId();
if (oid == null)
oid = a.getObjectId();
@ -175,6 +179,7 @@ else if (revLimiter.size() > 1)
// Ignore all refs which are not commits
}
}
}
if (commits.isEmpty()) {
final ObjectId head = db.resolve(Constants.HEAD);

View File

@ -45,6 +45,8 @@
package org.eclipse.jgit.pgm;
import static org.eclipse.jgit.lib.RefDatabase.ALL;
import java.io.IOException;
import java.util.Map;
import java.util.SortedMap;
@ -65,8 +67,8 @@ protected void run() throws Exception {
}
}
private Iterable<Ref> getSortedRefs() {
Map<String, Ref> all = db.getAllRefs();
private Iterable<Ref> getSortedRefs() throws Exception {
Map<String, Ref> all = db.getRefDatabase().getRefs(ALL);
if (all instanceof RefMap
|| (all instanceof SortedMap && ((SortedMap) all).comparator() == null))
return all.values();

View File

@ -43,6 +43,8 @@
package org.eclipse.jgit.pgm.debug;
import static org.eclipse.jgit.lib.RefDatabase.ALL;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
@ -114,7 +116,7 @@ class RebuildCommitGraph extends TextBuiltin {
@Override
protected void run() throws Exception {
if (!really && !db.getAllRefs().isEmpty()) {
if (!really && !db.getRefDatabase().getRefs(ALL).isEmpty()) {
System.err.println(
MessageFormat.format(CLIText.get().fatalThisProgramWillDestroyTheRepository
, db.getDirectory().getAbsolutePath(), REALLY));
@ -241,7 +243,8 @@ private void detachHead() throws IOException {
private void deleteAllRefs() throws Exception {
final RevWalk rw = new RevWalk(db);
for (final Ref r : db.getAllRefs().values()) {
Map<String, Ref> refs = db.getRefDatabase().getRefs(ALL);
for (final Ref r : refs.values()) {
if (Constants.HEAD.equals(r.getName()))
continue;
final RefUpdate u = db.updateRef(r.getName());

View File

@ -152,7 +152,8 @@ public String call() throws GitAPIException {
throw new IllegalArgumentException(JGitText.get().targetIsNotSet);
Map<ObjectId, Ref> tags = new HashMap<ObjectId, Ref>();
for (Ref r : repo.getTags().values()) {
for (Ref r : repo.getRefDatabase().getRefs(R_TAGS).values()) {
ObjectId key = repo.peel(r).getPeeledObjectId();
if (key == null)
key = r.getObjectId();

View File

@ -42,10 +42,13 @@
*/
package org.eclipse.jgit.api;
import static org.eclipse.jgit.lib.RefDatabase.ALL;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.JGitInternalException;
@ -252,7 +255,8 @@ public LogCommand addRange(AnyObjectId since, AnyObjectId until)
* the references could not be accessed
*/
public LogCommand all() throws IOException {
for (Ref ref : getRepository().getAllRefs().values()) {
Map<String, Ref> refs = getRepository().getRefDatabase().getRefs(ALL);
for (Ref ref : refs.values()) {
if(!ref.isPeeled())
ref = getRepository().peel(ref);

View File

@ -48,6 +48,7 @@
import static org.eclipse.jgit.internal.storage.pack.PackExt.BITMAP_INDEX;
import static org.eclipse.jgit.internal.storage.pack.PackExt.INDEX;
import static org.eclipse.jgit.internal.storage.pack.PackExt.PACK;
import static org.eclipse.jgit.lib.RefDatabase.ALL;
import java.io.IOException;
import java.util.ArrayList;
@ -194,7 +195,7 @@ public boolean pack(ProgressMonitor pm) throws IOException {
refdb.clearCache();
objdb.clearCache();
refsBefore = repo.getAllRefs();
refsBefore = refdb.getRefs(ALL);
packsBefore = packsToRebuild();
if (packsBefore.isEmpty())
return true;

View File

@ -46,6 +46,8 @@
package org.eclipse.jgit.internal.storage.file;
import static org.eclipse.jgit.lib.RefDatabase.ALL;
import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;
@ -382,7 +384,7 @@ public void openPack(final File pack) throws IOException {
@Override
public void scanForRepoChanges() throws IOException {
getAllRefs(); // This will look for changes to refs
getRefDatabase().getRefs(ALL); // This will look for changes to refs
detectIndexChanges();
}

View File

@ -45,6 +45,7 @@
import static org.eclipse.jgit.internal.storage.pack.PackExt.BITMAP_INDEX;
import static org.eclipse.jgit.internal.storage.pack.PackExt.INDEX;
import static org.eclipse.jgit.lib.RefDatabase.ALL;
import java.io.File;
import java.io.FileOutputStream;
@ -457,7 +458,7 @@ private static boolean equals(Ref r1, Ref r2) {
* @throws IOException
*/
public void packRefs() throws IOException {
Collection<Ref> refs = repo.getAllRefs().values();
Collection<Ref> refs = repo.getRefDatabase().getRefs(ALL).values();
List<String> refsToBePacked = new ArrayList<String>(refs.size());
pm.beginTask(JGitText.get().packRefs, refs.size());
try {
@ -574,7 +575,7 @@ private Set<ObjectId> listRefLogObjects(Ref ref, long minTime) throws IOExceptio
* @throws IOException
*/
private Map<String, Ref> getAllRefs() throws IOException {
Map<String, Ref> ret = repo.getAllRefs();
Map<String, Ref> ret = repo.getRefDatabase().getRefs(ALL);
for (Ref ref : repo.getRefDatabase().getAdditionalRefs())
ret.put(ref.getName(), ref);
return ret;

View File

@ -45,6 +45,8 @@
package org.eclipse.jgit.transport;
import static org.eclipse.jgit.lib.RefDatabase.ALL;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@ -52,6 +54,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import org.eclipse.jgit.errors.PackProtocolException;
@ -400,7 +403,8 @@ private int maxTimeWanted(final Collection<Ref> wants) {
private void markReachable(final Set<ObjectId> have, final int maxTime)
throws IOException {
for (final Ref r : local.getAllRefs().values()) {
Map<String, Ref> refs = local.getRefDatabase().getRefs(ALL);
for (final Ref r : refs.values()) {
ObjectId id = r.getPeeledObjectId();
if (id == null)
id = r.getObjectId();

View File

@ -47,6 +47,8 @@
package org.eclipse.jgit.transport;
import static org.eclipse.jgit.lib.RefDatabase.ALL;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
@ -242,7 +244,13 @@ private void verifyPrerequisites() throws TransportException {
throw new MissingBundlePrerequisiteException(transport.uri,
missing);
for (final Ref r : transport.local.getAllRefs().values()) {
Map<String, Ref> localRefs;
try {
localRefs = transport.local.getRefDatabase().getRefs(ALL);
} catch (IOException e) {
throw new TransportException(transport.uri, e.getMessage(), e);
}
for (final Ref r : localRefs.values()) {
try {
rw.markStart(rw.parseCommit(r.getObjectId()));
} catch (IOException readError) {

View File

@ -46,6 +46,8 @@
package org.eclipse.jgit.transport;
import static org.eclipse.jgit.lib.RefDatabase.ALL;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
@ -644,8 +646,9 @@ public static Collection<RemoteRefUpdate> findRemoteRefUpdatesFor(
}
private static Collection<RefSpec> expandPushWildcardsFor(
final Repository db, final Collection<RefSpec> specs) {
final Map<String, Ref> localRefs = db.getAllRefs();
final Repository db, final Collection<RefSpec> specs)
throws IOException {
final Map<String, Ref> localRefs = db.getRefDatabase().getRefs(ALL);
final Collection<RefSpec> procRefs = new HashSet<RefSpec>();
for (final RefSpec spec : specs) {

View File

@ -43,6 +43,8 @@
package org.eclipse.jgit.transport;
import static org.eclipse.jgit.lib.RefDatabase.ALL;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
@ -1129,7 +1131,7 @@ public void checkWants(UploadPack up, List<ObjectId> wants)
new ReachableCommitTipRequestValidator().checkWants(up, wants);
else if (!wants.isEmpty()) {
Set<ObjectId> refIds =
refIdSet(up.getRepository().getAllRefs().values());
refIdSet(up.getRepository().getRefDatabase().getRefs(ALL).values());
for (ObjectId obj : wants) {
if (!refIds.contains(obj))
throw new PackProtocolException(MessageFormat.format(
@ -1149,7 +1151,7 @@ public static final class ReachableCommitTipRequestValidator
public void checkWants(UploadPack up, List<ObjectId> wants)
throws PackProtocolException, IOException {
checkNotAdvertisedWants(up.getRevWalk(), wants,
refIdSet(up.getRepository().getAllRefs().values()));
refIdSet(up.getRepository().getRefDatabase().getRefs(ALL).values()));
}
}

View File

@ -44,6 +44,8 @@
package org.eclipse.jgit.transport;
import static org.eclipse.jgit.lib.RefDatabase.ALL;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
@ -56,6 +58,7 @@
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.eclipse.jgit.errors.CompoundException;
@ -668,7 +671,13 @@ private Collection<WalkRemoteObjectDatabase> expandOneAlternate(
}
private void markLocalRefsComplete(final Set<ObjectId> have) throws TransportException {
for (final Ref r : local.getAllRefs().values()) {
Map<String, Ref> refs;
try {
refs = local.getRefDatabase().getRefs(ALL);
} catch (IOException e) {
throw new TransportException(e.getMessage(), e);
}
for (final Ref r : refs.values()) {
try {
markLocalObjComplete(revWalk.parseAny(r.getObjectId()));
} catch (IOException readError) {