Use FileRepository where we assume other file semantics

When the surrounding code is already heavily based upon the
assumption that we have a FileRepository (e.g. because it
created that type of repository) keep the type around and
use it directly.  This permits us to continue to do things
like save the configuration file.

Change-Id: Ib783f0f6a11acd6aa305c16d61ccc368b46beecc
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
This commit is contained in:
Shawn O. Pearce 2010-06-24 15:45:51 -07:00
parent 4c14b7623d
commit 89d4a7377f
13 changed files with 104 additions and 86 deletions

View File

@ -60,6 +60,7 @@
import org.eclipse.jgit.junit.TestRepository;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.FileBasedConfig;
import org.eclipse.jgit.lib.FileRepository;
import org.eclipse.jgit.lib.NullProgressMonitor;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
@ -71,14 +72,14 @@
import org.eclipse.jgit.transport.URIish;
public class AdvertiseErrorTest extends HttpTestCase {
private Repository remoteRepository;
private FileRepository remoteRepository;
private URIish remoteURI;
protected void setUp() throws Exception {
super.setUp();
final TestRepository src = createTestRepository();
final TestRepository<FileRepository> src = createTestRepository();
final String srcName = src.getRepository().getDirectory().getName();
ServletContextHandler app = server.addContext("/git");

View File

@ -62,6 +62,7 @@
import org.eclipse.jgit.junit.TestRepository;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.FileBasedConfig;
import org.eclipse.jgit.lib.FileRepository;
import org.eclipse.jgit.lib.NullProgressMonitor;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
@ -76,14 +77,14 @@
import org.eclipse.jgit.transport.URIish;
public class HookMessageTest extends HttpTestCase {
private Repository remoteRepository;
private FileRepository remoteRepository;
private URIish remoteURI;
protected void setUp() throws Exception {
super.setUp();
final TestRepository src = createTestRepository();
final TestRepository<FileRepository> src = createTestRepository();
final String srcName = src.getRepository().getDirectory().getName();
ServletContextHandler app = server.addContext("/git");

View File

@ -62,6 +62,7 @@
import org.eclipse.jgit.http.test.util.HttpTestCase;
import org.eclipse.jgit.junit.TestRepository;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.FileRepository;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.RefUpdate;
import org.eclipse.jgit.lib.Repository;
@ -71,7 +72,7 @@
import org.eclipse.jgit.transport.URIish;
public class HttpClientTests extends HttpTestCase {
private TestRepository remoteRepository;
private TestRepository<FileRepository> remoteRepository;
private URIish dumbAuthNoneURI;
@ -95,7 +96,7 @@ protected void setUp() throws Exception {
server.setUp();
final String srcName = nameOf(remoteRepository);
final String srcName = nameOf(remoteRepository.getRepository());
dumbAuthNoneURI = toURIish(dNone, srcName);
dumbAuthBasicURI = toURIish(dBasic, srcName);
@ -119,10 +120,10 @@ private ServletContextHandler smart(final String path) {
public Repository open(HttpServletRequest req, String name)
throws RepositoryNotFoundException,
ServiceNotEnabledException {
if (!name.equals(nameOf(remoteRepository)))
final FileRepository db = remoteRepository.getRepository();
if (!name.equals(nameOf(db)))
throw new RepositoryNotFoundException(name);
final Repository db = remoteRepository.getRepository();
db.incrementOpen();
return db;
}
@ -133,8 +134,8 @@ public Repository open(HttpServletRequest req, String name)
return ctx;
}
private static String nameOf(final TestRepository db) {
return db.getRepository().getDirectory().getName();
private static String nameOf(final FileRepository db) {
return db.getDirectory().getName();
}
public void testRepositoryNotFound_Dumb() throws Exception {
@ -198,7 +199,7 @@ public void testListRemote_Dumb_DetachedHEAD() throws Exception {
}
public void testListRemote_Dumb_NoHEAD() throws Exception {
Repository src = remoteRepository.getRepository();
FileRepository src = remoteRepository.getRepository();
File headref = new File(src.getDirectory(), Constants.HEAD);
assertTrue("HEAD used to be present", headref.delete());
assertFalse("HEAD is gone", headref.exists());
@ -306,7 +307,7 @@ public void testListRemote_Smart_UploadPackNeedsAuth() throws Exception {
}
public void testListRemote_Smart_UploadPackDisabled() throws Exception {
Repository src = remoteRepository.getRepository();
FileRepository src = remoteRepository.getRepository();
src.getConfig().setBoolean("http", null, "uploadpack", false);
src.getConfig().save();

View File

@ -77,6 +77,7 @@
import org.eclipse.jgit.junit.TestRng;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.FileBasedConfig;
import org.eclipse.jgit.lib.FileRepository;
import org.eclipse.jgit.lib.NullProgressMonitor;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
@ -94,7 +95,7 @@
public class SmartClientSmartServerTest extends HttpTestCase {
private static final String HDR_TRANSFER_ENCODING = "Transfer-Encoding";
private Repository remoteRepository;
private FileRepository remoteRepository;
private URIish remoteURI;
@ -107,7 +108,7 @@ public class SmartClientSmartServerTest extends HttpTestCase {
protected void setUp() throws Exception {
super.setUp();
final TestRepository src = createTestRepository();
final TestRepository<FileRepository> src = createTestRepository();
final String srcName = src.getRepository().getDirectory().getName();
ServletContextHandler app = server.addContext("/git");
@ -489,10 +490,10 @@ public void testPush_CreateBranch() throws Exception {
}
public void testPush_ChunkedEncoding() throws Exception {
final TestRepository src = createTestRepository();
final TestRepository<FileRepository> src = createTestRepository();
final RevBlob Q_bin = src.blob(new TestRng("Q").nextBytes(128 * 1024));
final RevCommit Q = src.commit().add("Q", Q_bin).create();
final Repository db = src.getRepository();
final FileRepository db = src.getRepository();
final String dstName = Constants.R_HEADS + "new.branch";
Transport t;

View File

@ -57,6 +57,7 @@
import org.eclipse.jgit.junit.TestRepository;
import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.FileRepository;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
@ -82,8 +83,9 @@ protected void tearDown() throws Exception {
super.tearDown();
}
protected TestRepository createTestRepository() throws Exception {
return new TestRepository(createBareRepository());
protected TestRepository<FileRepository> createTestRepository()
throws IOException {
return new TestRepository<FileRepository>(createBareRepository());
}
protected URIish toURIish(String path) throws URISyntaxException {

View File

@ -260,7 +260,7 @@ private static void reportDeleteFailure(final String testName,
* @throws IOException
* the repository could not be created in the temporary area
*/
protected Repository createBareRepository() throws IOException {
protected FileRepository createBareRepository() throws IOException {
return createRepository(true /* bare */);
}
@ -271,7 +271,7 @@ protected Repository createBareRepository() throws IOException {
* @throws IOException
* the repository could not be created in the temporary area
*/
protected Repository createWorkRepository() throws IOException {
protected FileRepository createWorkRepository() throws IOException {
return createRepository(false /* not bare */);
}
@ -285,11 +285,11 @@ protected Repository createWorkRepository() throws IOException {
* @throws IOException
* the repository could not be created in the temporary area
*/
private Repository createRepository(boolean bare) throws IOException {
private FileRepository createRepository(boolean bare) throws IOException {
String uniqueId = System.currentTimeMillis() + "_" + (testCount++);
String gitdirName = "test" + uniqueId + (bare ? "" : "/") + Constants.DOT_GIT;
File gitdir = new File(trash, gitdirName).getCanonicalFile();
Repository db = new FileRepository(gitdir);
FileRepository db = new FileRepository(gitdir);
assertFalse(gitdir.exists());
db.create();

View File

@ -73,10 +73,10 @@
import org.eclipse.jgit.lib.Commit;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.FileMode;
import org.eclipse.jgit.lib.FileRepository;
import org.eclipse.jgit.lib.LockFile;
import org.eclipse.jgit.lib.NullProgressMonitor;
import org.eclipse.jgit.lib.ObjectChecker;
import org.eclipse.jgit.lib.ObjectDatabase;
import org.eclipse.jgit.lib.ObjectDirectory;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectWriter;
@ -99,8 +99,13 @@
import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
/** Wrapper to make creating test data easier. */
public class TestRepository {
/**
* Wrapper to make creating test data easier.
*
* @param <R>
* type of Repository the test data is stored on.
*/
public class TestRepository<R extends Repository> {
private static final PersonIdent author;
private static final PersonIdent committer;
@ -119,7 +124,7 @@ public class TestRepository {
committer = new PersonIdent(cn, ce, now, tz);
}
private final Repository db;
private final R db;
private final RevWalk pool;
@ -132,9 +137,9 @@ public class TestRepository {
*
* @param db
* the test repository to write into.
* @throws Exception
* @throws IOException
*/
public TestRepository(Repository db) throws Exception {
public TestRepository(R db) throws IOException {
this(db, new RevWalk(db));
}
@ -145,9 +150,9 @@ public TestRepository(Repository db) throws Exception {
* the test repository to write into.
* @param rw
* the RevObject pool to use for object lookup.
* @throws Exception
* @throws IOException
*/
public TestRepository(Repository db, RevWalk rw) throws Exception {
public TestRepository(R db, RevWalk rw) throws IOException {
this.db = db;
this.pool = rw;
this.writer = new ObjectWriter(db);
@ -155,7 +160,7 @@ public TestRepository(Repository db, RevWalk rw) throws Exception {
}
/** @return the repository this helper class operates against. */
public Repository getRepository() {
public R getRepository() {
return db;
}
@ -443,25 +448,27 @@ public <T extends AnyObjectId> T update(String ref, T obj) throws Exception {
* @throws Exception
*/
public void updateServerInfo() throws Exception {
final ObjectDatabase odb = db.getObjectDatabase();
if (odb instanceof ObjectDirectory) {
RefWriter rw = new RefWriter(db.getAllRefs().values()) {
if (db instanceof FileRepository) {
final FileRepository fr = (FileRepository) db;
RefWriter rw = new RefWriter(fr.getAllRefs().values()) {
@Override
protected void writeFile(final String name, final byte[] bin)
throws IOException {
TestRepository.this.writeFile(name, bin);
File path = new File(fr.getDirectory(), name);
TestRepository.this.writeFile(path, bin);
}
};
rw.writePackedRefs();
rw.writeInfoRefs();
final StringBuilder w = new StringBuilder();
for (PackFile p : ((ObjectDirectory) odb).getPacks()) {
for (PackFile p : fr.getObjectDatabase().getPacks()) {
w.append("P ");
w.append(p.getPackFile().getName());
w.append('\n');
}
writeFile("objects/info/packs", Constants.encodeASCII(w.toString()));
writeFile(new File(new File(fr.getObjectDatabase().getDirectory(),
"info"), "packs"), Constants.encodeASCII(w.toString()));
}
}
@ -563,38 +570,40 @@ private static void assertHash(RevObject id, byte[] bin) {
* @throws Exception
*/
public void packAndPrune() throws Exception {
final ObjectDirectory odb = (ObjectDirectory) db.getObjectDatabase();
final PackWriter pw = new PackWriter(db, NullProgressMonitor.INSTANCE);
if (db.getObjectDatabase() instanceof ObjectDirectory) {
ObjectDirectory odb = (ObjectDirectory) db.getObjectDatabase();
PackWriter pw = new PackWriter(db, NullProgressMonitor.INSTANCE);
Set<ObjectId> all = new HashSet<ObjectId>();
for (Ref r : db.getAllRefs().values())
all.add(r.getObjectId());
pw.preparePack(all, Collections.<ObjectId> emptySet());
Set<ObjectId> all = new HashSet<ObjectId>();
for (Ref r : db.getAllRefs().values())
all.add(r.getObjectId());
pw.preparePack(all, Collections.<ObjectId> emptySet());
final ObjectId name = pw.computeName();
OutputStream out;
final ObjectId name = pw.computeName();
OutputStream out;
final File pack = nameFor(odb, name, ".pack");
out = new BufferedOutputStream(new FileOutputStream(pack));
try {
pw.writePack(out);
} finally {
out.close();
final File pack = nameFor(odb, name, ".pack");
out = new BufferedOutputStream(new FileOutputStream(pack));
try {
pw.writePack(out);
} finally {
out.close();
}
pack.setReadOnly();
final File idx = nameFor(odb, name, ".idx");
out = new BufferedOutputStream(new FileOutputStream(idx));
try {
pw.writeIndex(out);
} finally {
out.close();
}
idx.setReadOnly();
odb.openPack(pack, idx);
updateServerInfo();
prunePacked(odb);
}
pack.setReadOnly();
final File idx = nameFor(odb, name, ".idx");
out = new BufferedOutputStream(new FileOutputStream(idx));
try {
pw.writeIndex(out);
} finally {
out.close();
}
idx.setReadOnly();
odb.openPack(pack, idx);
updateServerInfo();
prunePacked(odb);
}
private void prunePacked(ObjectDirectory odb) {
@ -609,9 +618,8 @@ private static File nameFor(ObjectDirectory odb, ObjectId name, String t) {
return new File(packdir, "pack-" + name.name() + t);
}
private void writeFile(final String name, final byte[] bin)
throws IOException, ObjectWritingException {
final File p = new File(db.getDirectory(), name);
private void writeFile(final File p, final byte[] bin) throws IOException,
ObjectWritingException {
final LockFile lck = new LockFile(p);
if (!lck.lock())
throw new ObjectWritingException("Can't write " + p);

View File

@ -82,6 +82,8 @@ class Clone extends AbstractFetchCommand {
@Argument(index = 1, metaVar = "metaVar_directory")
private String localName;
private FileRepository dst;
@Override
protected final boolean requiresRepository() {
return false;
@ -103,10 +105,11 @@ protected void run() throws Exception {
if (gitdir == null)
gitdir = new File(localName, Constants.DOT_GIT);
db = new FileRepository(gitdir);
db.create();
db.getConfig().setBoolean("core", null, "bare", false);
db.getConfig().save();
dst = new FileRepository(gitdir);
dst.create();
dst.getConfig().setBoolean("core", null, "bare", false);
dst.getConfig().save();
db = dst;
out.format(CLIText.get().initializedEmptyGitRepositoryIn, gitdir.getAbsolutePath());
out.println();
@ -120,13 +123,13 @@ protected void run() throws Exception {
private void saveRemote(final URIish uri) throws URISyntaxException,
IOException {
final RemoteConfig rc = new RemoteConfig(db.getConfig(), remoteName);
final RemoteConfig rc = new RemoteConfig(dst.getConfig(), remoteName);
rc.addURI(uri);
rc.addFetchRefSpec(new RefSpec().setForceUpdate(true)
.setSourceDestination(Constants.R_HEADS + "*",
Constants.R_REMOTES + remoteName + "/*"));
rc.update(db.getConfig());
db.getConfig().save();
rc.update(dst.getConfig());
dst.getConfig().save();
}
private FetchResult runFetch() throws NotSupportedException,

View File

@ -177,14 +177,14 @@ private File getFile(String... pathComponents) {
}
private void setBare(File gitDir, boolean bare) throws IOException {
Repository repo = new FileRepository(gitDir, null);
FileRepository repo = new FileRepository(gitDir, null);
repo.getConfig().setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
ConfigConstants.CONFIG_KEY_BARE, bare);
repo.getConfig().save();
}
private void setWorkTree(File gitDir, File workTree) throws IOException {
Repository repo = new FileRepository(gitDir, null);
FileRepository repo = new FileRepository(gitDir, null);
repo.getConfig()
.setString(ConfigConstants.CONFIG_CORE_SECTION, null,
ConfigConstants.CONFIG_KEY_WORKTREE,

View File

@ -102,7 +102,7 @@ protected static void checkFile(File f, final String checkData)
}
/** Test repository, initialized for this test case. */
protected Repository db;
protected FileRepository db;
/** Working directory of {@link #db}. */
protected File trash;

View File

@ -159,7 +159,7 @@ public void test000_openrepo_default_absolute_workdirconfig()
File repo1Parent = new File(trash.getParentFile(), "r1");
File workdir = new File(trash.getParentFile(), "rw");
workdir.mkdir();
Repository repo1initial = new FileRepository(new File(repo1Parent, Constants.DOT_GIT));
FileRepository repo1initial = new FileRepository(new File(repo1Parent, Constants.DOT_GIT));
repo1initial.create();
repo1initial.getConfig().setString("core", null, "worktree",
workdir.getAbsolutePath());
@ -184,7 +184,7 @@ public void test000_openrepo_default_relative_workdirconfig()
File repo1Parent = new File(trash.getParentFile(), "r1");
File workdir = new File(trash.getParentFile(), "rw");
workdir.mkdir();
Repository repo1initial = new FileRepository(new File(repo1Parent, Constants.DOT_GIT));
FileRepository repo1initial = new FileRepository(new File(repo1Parent, Constants.DOT_GIT));
repo1initial.create();
repo1initial.getConfig()
.setString("core", null, "worktree", "../../rw");
@ -321,7 +321,7 @@ public void test006_ReadUglyConfig() throws IOException,
}
public void test007_Open() throws IOException {
final Repository db2 = new FileRepository(db.getDirectory());
final FileRepository db2 = new FileRepository(db.getDirectory());
assertEquals(db.getDirectory(), db2.getDirectory());
assertEquals(db.getObjectsDirectory(), db2.getObjectsDirectory());
assertNotSame(db.getConfig(), db2.getConfig());

View File

@ -47,18 +47,19 @@
import org.eclipse.jgit.dircache.DirCacheEntry;
import org.eclipse.jgit.junit.TestRepository;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.RepositoryTestCase;
/** Support for tests of the {@link RevWalk} class. */
public abstract class RevWalkTestCase extends RepositoryTestCase {
private TestRepository util;
private TestRepository<Repository> util;
protected RevWalk rw;
@Override
public void setUp() throws Exception {
super.setUp();
util = new TestRepository(db, createRevWalk());
util = new TestRepository<Repository>(db, createRevWalk());
rw = util.getRevWalk();
}

View File

@ -292,7 +292,7 @@ public void testUsingHiddenDeltaBaseFails() throws Exception {
public void testUsingHiddenCommonBlobFails() throws Exception {
// Try to use the 'b' blob that is hidden.
//
TestRepository s = new TestRepository(src);
TestRepository<Repository> s = new TestRepository<Repository>(src);
RevCommit N = s.commit().parent(B).add("q", s.blob("b")).create();
// But don't include it in the pack.
@ -333,7 +333,7 @@ public void testUsingHiddenCommonBlobFails() throws Exception {
public void testUsingUnknownBlobFails() throws Exception {
// Try to use the 'n' blob that is not on the server.
//
TestRepository s = new TestRepository(src);
TestRepository<Repository> s = new TestRepository<Repository>(src);
RevBlob n = s.blob("n");
RevCommit N = s.commit().parent(B).add("q", n).create();
@ -373,7 +373,7 @@ public void testUsingUnknownBlobFails() throws Exception {
}
public void testUsingUnknownTreeFails() throws Exception {
TestRepository s = new TestRepository(src);
TestRepository<Repository> s = new TestRepository<Repository>(src);
RevCommit N = s.commit().parent(B).add("q", s.blob("a")).create();
RevTree t = s.parseBody(N).getTree();