{"thread":{"id":"7350","subject":"[EGIT PATCH] Use the alternates links for finding objects","startedAt":"2007-03-21T23:59:32Z","lastAt":"2007-03-21T23:59:32Z","messageCount":1,"participants":["Robin Rosenberg"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"37705","messageId":"20070321235931.23981.41286.stgit@lathund.dewire.com","threadId":"7350","inReplyTo":null,"subject":"[EGIT PATCH] Use the alternates links for finding objects","fromName":"Robin Rosenberg","fromEmail":"robin.rosenberg@dewire.com","sentAt":"2007-03-21T23:59:32Z","receivedAt":"2007-03-21T23:59:32Z","isPatch":true,"sender":{"key":"robin.rosenberg@dewire.com","avatar":"https://avatars.githubusercontent.com/u/46357?v=4"},"body":"Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>\n---\n\n .../src/org/spearce/jgit/lib/Repository.java       |   58 +++++++++++++++++------\n 1 files changed, 44 insertions(+), 14 deletions(-)\n\ndiff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java b/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java\nindex f1999d1..d74da7c 100644\n--- a/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java\n+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java\n@@ -39,7 +39,7 @@ public class Repository {\n \n \tprivate final File gitDir;\n \n-\tprivate final File objectsDir;\n+\tprivate final File[] objectsDirs;\n \n \tprivate final File refsDir;\n \n@@ -54,11 +54,17 @@ public class Repository {\n \n \tpublic Repository(final File d) throws IOException {\n \t\tgitDir = d.getAbsoluteFile();\n-\t\tobjectsDir = new File(gitDir, \"objects\");\n+\t\ttry {\n+\t\t\tobjectsDirs = (File[])readObjectsDirs(new File(gitDir, \"objects\"), new ArrayList()).toArray(new File[0]);\n+\t\t} catch (IOException e) {\n+\t\t\tIOException ex = new IOException(\"Cannot find all object dirs for \" + gitDir);\n+\t\t\tex.initCause(e);\n+\t\t\tthrow ex;\n+\t\t}\n \t\trefsDir = new File(gitDir, \"refs\");\n \t\tpacks = new PackFile[0];\n \t\tconfig = new RepositoryConfig(this);\n-\t\tif (objectsDir.exists()) {\n+\t\tif (objectsDirs[0].exists()) {\n \t\t\tgetConfig().load();\n \t\t\tfinal String repositoryFormatVersion = getConfig().getString(\n \t\t\t\t\t\"core\", \"repositoryFormatVersion\");\n@@ -71,6 +77,19 @@ public class Repository {\n \t\t}\n \t}\n \n+\tprivate Collection readObjectsDirs(File objectsDir, Collection ret) throws IOException {\n+\t\tret.add(objectsDir);\n+\t\tFile alternatesFile = new File(objectsDir,\"info/alternates\");\n+\t\tif (alternatesFile.exists()) {\n+\t\t\tBufferedReader ar = new BufferedReader(new FileReader(alternatesFile));\n+\t\t\tfor (String alt=ar.readLine(); alt!=null; alt=ar.readLine()) {\n+\t\t\t\treadObjectsDirs(new File(alt), ret);\n+\t\t\t}\n+\t\t\tar.close();\n+\t\t}\n+\t\treturn ret;\n+\t}\n+\n \tpublic void create() throws IOException {\n \t\tif (gitDir.exists()) {\n \t\t\tthrow new IllegalStateException(\"Repository already exists: \"\n@@ -79,9 +98,9 @@ public class Repository {\n \n \t\tgitDir.mkdirs();\n \n-\t\tobjectsDir.mkdirs();\n-\t\tnew File(objectsDir, \"pack\").mkdir();\n-\t\tnew File(objectsDir, \"info\").mkdir();\n+\t\tobjectsDirs[0].mkdirs();\n+\t\tnew File(objectsDirs[0], \"pack\").mkdir();\n+\t\tnew File(objectsDirs[0], \"info\").mkdir();\n \n \t\trefsDir.mkdir();\n \t\tnew File(refsDir, \"heads\").mkdir();\n@@ -106,7 +125,7 @@ public class Repository {\n \t}\n \n \tpublic File getObjectsDirectory() {\n-\t\treturn objectsDir;\n+\t\treturn objectsDirs[0];\n \t}\n \n \tpublic RepositoryConfig getConfig() {\n@@ -119,7 +138,14 @@ public class Repository {\n \n \tpublic File toFile(final ObjectId objectId) {\n \t\tfinal String n = objectId.toString();\n-\t\treturn new File(new File(objectsDir, n.substring(0, 2)), n.substring(2));\n+\t\tString d=n.substring(0, 2);\n+\t\tString f=n.substring(2);\n+\t\tfor (int i=0; i<objectsDirs.length; ++i) {\n+\t\t\tFile ret = new File(new File(objectsDirs[i], d), f);\n+\t\t\tif (ret.exists())\n+\t\t\t\treturn ret;\n+\t\t}\n+\t\treturn new File(new File(objectsDirs[0], d), f);\n \t}\n \n \tpublic boolean hasObject(final ObjectId objectId) {\n@@ -291,7 +317,15 @@ public class Repository {\n \t}\n \n \tpublic void scanForPacks() {\n-\t\tfinal File packDir = new File(objectsDir, \"pack\");\n+\t\tfinal ArrayList p = new ArrayList();\n+\t\tfor (int i=0; i<objectsDirs.length; ++i)\n+\t\t\tscanForPacks(new File(objectsDirs[i], \"pack\"), p);\n+\t\tfinal PackFile[] arr = new PackFile[p.size()];\n+\t\tp.toArray(arr);\n+\t\tpacks = arr;\n+\t}\n+\n+\tpublic void scanForPacks(final File packDir, Collection packList) {\n \t\tfinal File[] list = packDir.listFiles(new FileFilter() {\n \t\t\tpublic boolean accept(final File f) {\n \t\t\t\tfinal String n = f.getName();\n@@ -304,18 +338,14 @@ public class Repository {\n \t\t\t\t\t\t&& idx.canRead();\n \t\t\t}\n \t\t});\n-\t\tfinal ArrayList p = new ArrayList(list.length);\n \t\tfor (int k = 0; k < list.length; k++) {\n \t\t\ttry {\n-\t\t\t\tp.add(new PackFile(this, list[k]));\n+\t\t\t\tpackList.add(new PackFile(this, list[k]));\n \t\t\t} catch (IOException ioe) {\n \t\t\t\t// Whoops. That's not a pack!\n \t\t\t\t//\n \t\t\t}\n \t\t}\n-\t\tfinal PackFile[] arr = new PackFile[p.size()];\n-\t\tp.toArray(arr);\n-\t\tpacks = arr;\n \t}\n \n \tprivate void writeSymref(final String name, final String target)\n"}]}