/** * Pack all reachable objects in the repository into a single pack file. * * <p>All loose objects are automatically pruned. Existing packs however are not removed. * * @throws Exception */ public void packAndPrune() throws Exception { if (db.getObjectDatabase() instanceof ObjectDirectory) { ObjectDirectory odb = (ObjectDirectory) db.getObjectDatabase(); NullProgressMonitor m = NullProgressMonitor.INSTANCE; final File pack, idx; PackWriter pw = new PackWriter(db); try { Set<ObjectId> all = new HashSet<ObjectId>(); for (Ref r : db.getAllRefs().values()) all.add(r.getObjectId()); pw.preparePack(m, all, Collections.<ObjectId>emptySet()); final ObjectId name = pw.computeName(); OutputStream out; pack = nameFor(odb, name, ".pack"); out = new SafeBufferedOutputStream(new FileOutputStream(pack)); try { pw.writePack(m, m, out); } finally { out.close(); } pack.setReadOnly(); idx = nameFor(odb, name, ".idx"); out = new SafeBufferedOutputStream(new FileOutputStream(idx)); try { pw.writeIndex(out); } finally { out.close(); } idx.setReadOnly(); } finally { pw.release(); } odb.openPack(pack, idx); updateServerInfo(); prunePacked(odb); } }
private static File nameFor(ObjectDirectory odb, ObjectId name, String t) { File packdir = new File(odb.getDirectory(), "pack"); return new File(packdir, "pack-" + name.name() + t); }
private void prunePacked(ObjectDirectory odb) throws IOException { for (PackFile p : odb.getPacks()) { for (MutableEntry e : p) FileUtils.delete(odb.fileFor(e.toObjectId())); } }