From mboxrd@z Thu Jan 1 00:00:00 1970 From: Florian Koeberle Subject: [JGIT PATCH v3 21/23] Formatted Repository class. Date: Fri, 23 May 2008 22:34:30 +0200 Message-ID: <1211574872-23676-22-git-send-email-florianskarten@web.de> References: <1211574872-23676-1-git-send-email-florianskarten@web.de> Cc: Florian Koeberle To: git@vger.kernel.org X-From: git-owner@vger.kernel.org Fri May 23 22:36:55 2008 Return-path: Envelope-to: gcvg-git-2@gmane.org Received: from vger.kernel.org ([209.132.176.167]) by lo.gmane.org with esmtp (Exim 4.50) id 1Jze09-0006CA-6Y for gcvg-git-2@gmane.org; Fri, 23 May 2008 22:36:34 +0200 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1759298AbYEWUfa (ORCPT ); Fri, 23 May 2008 16:35:30 -0400 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1759260AbYEWUf3 (ORCPT ); Fri, 23 May 2008 16:35:29 -0400 Received: from fmmailgate02.web.de ([217.72.192.227]:39687 "EHLO fmmailgate02.web.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1757525AbYEWUep (ORCPT ); Fri, 23 May 2008 16:34:45 -0400 Received: from smtp06.web.de (fmsmtp06.dlan.cinetic.de [172.20.5.172]) by fmmailgate02.web.de (Postfix) with ESMTP id 048A8DE1A8BD for ; Fri, 23 May 2008 22:34:44 +0200 (CEST) Received: from [84.150.81.80] (helo=localhost.localdomain) by smtp06.web.de with asmtp (WEB.DE 4.109 #226) id 1JzdyN-0005iq-00; Fri, 23 May 2008 22:34:43 +0200 X-Mailer: git-send-email 1.5.5.1 In-Reply-To: <1211574872-23676-1-git-send-email-florianskarten@web.de> X-Sender: florianskarten@web.de X-Provags-ID: V01U2FsdGVkX18aquwZep4V/LdjxDfSqX8PASR+k/M+bMRWEnGr cpZBKp8TyAO4wSiZiifkExPRnGLF5clu2+nvW8vHPUYsHNHKlP aNa6V9hvBWMUqSyKytMQ== Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Archived-At: Signed-off-by: Florian Koeberle --- .../src/org/spearce/jgit/lib/Repository.java | 345 +++++++++++--------- 1 files changed, 192 insertions(+), 153 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 86817c9..06e686f 100644 --- a/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java +++ b/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java @@ -38,24 +38,25 @@ import org.spearce.jgit.util.FS; * Represents a Git repository. A repository holds all objects and refs used for * managing source code (could by any type of file, but source code is what * SCM's are typically used for). - * + * * In Git terms all data is stored in GIT_DIR, typically a directory called * .git. A work tree is maintained unless the repository is a bare repository. * Typically the .git directory is located at the root of the work dir. - * + * *
    *
  • GIT_DIR - *
      - *
    • objects/ - objects
    • - *
    • refs/ - tags and heads
    • - *
    • config - configuration
    • - *
    • info/ - more configurations
    • - *
    + *
      + *
    • objects/ - objects
    • + *
    • refs/ - tags and heads
    • + *
    • config - configuration
    • + *
    • info/ - more configurations
    • + *
    *
  • *
- * - * This implementation only handles a subtly undocumented subset of git features. - * + * + * This implementation only handles a subtly undocumented subset of git + * features. + * */ public class Repository { private static final String[] refSearchPaths = { "", "refs/", @@ -80,7 +81,7 @@ public class Repository { /** * Construct a representation of this git repo managing a Git repository. - * + * * @param d * GIT_DIR * @throws IOException @@ -109,7 +110,8 @@ public class Repository { objectsDirs = readObjectsDirs(FS.resolve(gitDir, "objects"), new ArrayList()).toArray(new File[0]); } catch (IOException e) { - IOException ex = new IOException("Cannot find all object dirs for " + gitDir); + IOException ex = new IOException("Cannot find all object dirs for " + + gitDir); ex.initCause(e); throw ex; } @@ -135,12 +137,13 @@ public class Repository { scanForPacks(); } - private Collection readObjectsDirs(File objectsDir, Collection ret) throws IOException { + private Collection readObjectsDirs(File objectsDir, + Collection ret) throws IOException { ret.add(objectsDir); final File altFile = FS.resolve(objectsDir, "info/alternates"); if (altFile.exists()) { BufferedReader ar = new BufferedReader(new FileReader(altFile)); - for (String alt=ar.readLine(); alt!=null; alt=ar.readLine()) { + for (String alt = ar.readLine(); alt != null; alt = ar.readLine()) { readObjectsDirs(FS.resolve(objectsDir, alt), ret); } ar.close(); @@ -151,7 +154,7 @@ public class Repository { /** * Create a new Git repository initializing the necessary files and * directories. - * + * * @throws IOException */ public void create() throws IOException { @@ -213,15 +216,15 @@ public class Repository { * to the alternative repo will be returned. If the object is not yet store * a usable path in this repo will be returned. It is assumed that callers * will look for objects in a pack first. - * + * * @param objectId * @return suggested file name */ public File toFile(final AnyObjectId objectId) { final String n = objectId.toString(); - String d=n.substring(0, 2); - String f=n.substring(2); - for (int i=0; i - *
  • SHA-1 - a SHA-1
  • - *
  • refs/... - a ref name
  • - *
  • ref^n - nth parent reference
  • - *
  • ref~n - distance via parent reference
  • - *
  • ref@{n} - nth version of ref
  • - *
  • ref^{tree} - tree references by ref
  • - *
  • ref^{commit} - commit references by ref
  • + *
  • SHA-1 - a SHA-1
  • + *
  • refs/... - a ref name
  • + *
  • ref^n - nth parent reference
  • + *
  • ref~n - distance via parent reference
  • + *
  • ref@{n} - nth version of ref
  • + *
  • ref^{tree} - tree references by ref
  • + *
  • ref^{commit} - commit references by ref
  • * - * + * * Not supported is *
      *
    • timestamps in reflogs, ref@{full or relative timestamp}
    • *
    • abbreviated SHA-1's
    • *
    - * - * @param revstr A git object references expression + * + * @param revstr + * A git object references expression * @return an ObjectId - * @throws IOException on serious errors + * @throws IOException + * on serious errors */ public ObjectId resolve(final String revstr) throws IOException { char[] rev = revstr.toCharArray(); @@ -522,7 +543,7 @@ public class Repository { switch (rev[i]) { case '^': if (refId == null) { - String refstr = new String(rev,0,i); + String refstr = new String(rev, 0, i); refId = resolveSimple(refstr); if (refId == null) return null; @@ -542,23 +563,24 @@ public class Repository { int j; ref = mapObject(refId, null); if (!(ref instanceof Commit)) - throw new IncorrectObjectTypeException(refId, Constants.TYPE_COMMIT); - for (j=i+1; j= 0) { - refId = ((Commit)ref).getParentIds()[0]; + refId = ((Commit) ref).getParentIds()[0]; ref = mapCommit(refId); --dist; } @@ -648,14 +671,16 @@ public class Repository { case '@': int m; String time = null; - for (m=i+2; m p = new ArrayList(); - for (int i=0; i getBranches() { return listRefs("heads"); } - + /** * @return the names of all refs (local and remotes branches, tags) */ public Collection getAllRefs() { return listRefs(""); } - + private Collection listRefs(String refSubDir) { // add / to end, unless empty - if (refSubDir.length() > 0 && refSubDir.charAt(refSubDir.length() -1 ) != '/') + if (refSubDir.length() > 0 + && refSubDir.charAt(refSubDir.length() - 1) != '/') refSubDir += "/"; - - Collection branchesRaw = listFilesRecursively(new File(refsDir, refSubDir), null); + + Collection branchesRaw = listFilesRecursively(new File(refsDir, + refSubDir), null); ArrayList branches = new ArrayList(); for (String b : branchesRaw) { branches.add("refs/" + refSubDir + b); } - + refreshPackedRefsCache(); Set keySet = packedRefs.keySet(); for (String s : keySet) @@ -944,22 +973,23 @@ public class Repository { return listRefs("tags"); } - private Map packedRefs = new HashMap(); + private Map packedRefs = new HashMap(); + private long packedrefstime = 0; private void refreshPackedRefsCache() { if (!packedRefsFile.exists()) { if (packedRefs.size() > 0) - packedRefs = new HashMap(); + packedRefs = new HashMap(); return; } if (packedRefsFile.lastModified() == packedrefstime) return; - Map newPackedRefs = new HashMap(); + Map newPackedRefs = new HashMap(); FileReader fileReader = null; try { fileReader = new FileReader(packedRefsFile); - BufferedReader b=new BufferedReader(fileReader); + BufferedReader b = new BufferedReader(fileReader); String p; while ((p = b.readLine()) != null) { if (p.charAt(0) == '#') @@ -968,12 +998,12 @@ public class Repository { continue; } int spos = p.indexOf(' '); - ObjectId id = ObjectId.fromString(p.substring(0,spos)); - String name = p.substring(spos+1); + ObjectId id = ObjectId.fromString(p.substring(0, spos)); + String name = p.substring(spos + 1); newPackedRefs.put(name, id); } } catch (IOException e) { - throw new Error("Cannot read packed refs",e); + throw new Error("Cannot read packed refs", e); } finally { if (fileReader != null) { try { @@ -1016,13 +1046,17 @@ public class Repository { * @return applied patches in a map indexed on current commit id * @throws IOException */ - public Map getAppliedPatches() throws IOException { - Map ret = new HashMap(); + public Map getAppliedPatches() throws IOException { + Map ret = new HashMap(); if (isStGitMode()) { - File patchDir = new File(new File(getDirectory(),"patches"),getBranch()); - BufferedReader apr = new BufferedReader(new FileReader(new File(patchDir,"applied"))); - for (String patchName=apr.readLine(); patchName!=null; patchName=apr.readLine()) { - File topFile = new File(new File(new File(patchDir,"patches"), patchName), "top"); + File patchDir = new File(new File(getDirectory(), "patches"), + getBranch()); + BufferedReader apr = new BufferedReader(new FileReader(new File( + patchDir, "applied"))); + for (String patchName = apr.readLine(); patchName != null; patchName = apr + .readLine()) { + File topFile = new File(new File(new File(patchDir, "patches"), + patchName), "top"); BufferedReader tfr = new BufferedReader(new FileReader(topFile)); String objectId = tfr.readLine(); ObjectId id = ObjectId.fromString(objectId); @@ -1052,7 +1086,7 @@ public class Repository { } return ret; } - + /** Clean up stale caches */ public void refreshFromDisk() { packedRefs = null; @@ -1075,7 +1109,7 @@ public class Repository { static byte[] gitInternalSlash(byte[] bytes) { if (File.separatorChar == '/') return bytes; - for (int i=0; i