From 6c847ad7ebf7760dc307660d9a9f71f459905a7f Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Mon, 5 May 2008 18:19:04 -0400 Subject: [PATCH] Change IndexPack.renamePack to renameAndOpenPack If an IndexPack instance has just finished indexing a pack file and has moved it into the Repository's objects directory then we did all of that work for a reason; we probably want to access the objects contained within that pack and start to use them for other processing, such as to checkout updated files in the local working directory. Instead of making the caller of IndexPack invoke scanForPacks on their own we automatically insert the pack ourselves at the front of the Repository's pack search list. This is based on the assumption that we are likely to want to access these new objects almost immediately, and are more likely to need these new objects to update the working directory than we are going to access older objects, as the older objects are unchanged. Signed-off-by: Shawn O. Pearce --- .../src/org/spearce/jgit/lib/Repository.java | 29 ++++++++++++++++++++++ .../spearce/jgit/transport/FullFetchClient.java | 3 +-- .../src/org/spearce/jgit/transport/IndexPack.java | 21 +++++++++++++--- 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java b/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java index 1db75b69..a1b58423 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java +++ b/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java @@ -691,6 +691,35 @@ public class Repository { } /** + * Add a single existing pack to the list of available pack files. + * + * @param pack + * path of the pack file to open. + * @param idx + * path of the corresponding index file. + * @throws IOException + * index file could not be opened, read, or is not recognized as + * a Git pack file index. + */ + public void openPack(final File pack, final File idx) throws IOException { + final String p = pack.getName(); + final String i = idx.getName(); + if (p.length() != 50 || !p.startsWith("pack-") || !p.endsWith(".pack")) + throw new IllegalArgumentException("Not a valid pack " + pack); + if (i.length() != 49 || !i.startsWith("pack-") || !i.endsWith(".idx")) + throw new IllegalArgumentException("Not a valid pack " + idx); + if (!p.substring(0,45).equals(i.substring(0,45))) + throw new IllegalArgumentException("Pack " + pack + + "does not match index " + idx); + + final PackFile[] cur = packs; + final PackFile[] arr = new PackFile[cur.length + 1]; + System.arraycopy(cur, 0, arr, 1, cur.length); + arr[0] = new PackFile(this, idx, pack); + packs = arr; + } + + /** * Scan the object dirs, including alternates for packs * to use. */ diff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/FullFetchClient.java b/org.spearce.jgit/src/org/spearce/jgit/transport/FullFetchClient.java index 9ef258f5..6c0a7caf 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/transport/FullFetchClient.java +++ b/org.spearce.jgit/src/org/spearce/jgit/transport/FullFetchClient.java @@ -31,7 +31,7 @@ public class FullFetchClient extends FetchClient { try { pack = new IndexPack(repository, pi, new File(new File(repository.getObjectsDirectory(), "pack"), "packtmp_pack"+System.currentTimeMillis())); pack.index(monitor); - pack.renamePack(); + pack.renameAndOpenPack(); } catch (Throwable e) { e.printStackTrace(); fetchThread.interrupt(); @@ -44,7 +44,6 @@ public class FullFetchClient extends FetchClient { super.run(monitor); os.close(); indexThread.join(); - repository.scanForPacks(); updateRemoteRefs(remote); } catch (InterruptedException e) { if (threadError[0] != null) diff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/IndexPack.java b/org.spearce.jgit/src/org/spearce/jgit/transport/IndexPack.java index f2c05832..b6e5956f 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/transport/IndexPack.java +++ b/org.spearce.jgit/src/org/spearce/jgit/transport/IndexPack.java @@ -75,7 +75,7 @@ public class IndexPack { *

* The received pack data and generated index will be saved to temporary * files within the repository's objects directory. To use - * the data contained within them call {@link #renamePack()} once the + * the data contained within them call {@link #renameAndOpenPack()} once the * indexing is complete. * * @param db @@ -757,11 +757,18 @@ public class IndexPack { } /** - * Rename the temporary pack to it's final name and location. + * Rename the pack to it's final name and location and open it. + *

+ * If the call completes successfully the repository this IndexPack instance + * was created with will have the objects in the pack available for reading + * and use, without needing to scan for packs. * * @throws IOException + * The pack could not be inserted into the repository's objects + * directory. The pack no longer exists on disk, as it was + * removed prior to throwing the exception to the caller. */ - public void renamePack() throws IOException { + public void renameAndOpenPack() throws IOException { final MessageDigest d = Constants.newMessageDigest(); final byte[] oeBytes = new byte[Constants.OBJECT_ID_LENGTH]; for (int i = 0; i < entryCount; i++) { @@ -793,6 +800,14 @@ public class IndexPack { finalPack.deleteOnExit(); throw new IOException("Cannot move index to " + finalIdx); } + + try { + repo.openPack(finalPack, finalIdx); + } catch (IOException err) { + finalPack.delete(); + finalIdx.delete(); + throw err; + } } private void cleanupTemporaryFiles() { -- 2.11.4.GIT