Commits

Sebastian Sdorra  committed 025ee3c

do not use repository cache to open git repositories

  • Participants
  • Parent commits 11c5ee1
  • Branches issue-500

Comments (0)

Files changed (1)

File scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/GitUtil.java

 import org.eclipse.jgit.lib.Constants;
 import org.eclipse.jgit.lib.ObjectId;
 import org.eclipse.jgit.lib.Ref;
-import org.eclipse.jgit.lib.RepositoryCache;
 import org.eclipse.jgit.revwalk.RevCommit;
 import org.eclipse.jgit.revwalk.RevWalk;
+import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
 import org.eclipse.jgit.transport.FetchResult;
 import org.eclipse.jgit.transport.RefSpec;
 import org.eclipse.jgit.treewalk.TreeWalk;
   public static final String REF_MASTER = "master";
 
   /** Field description */
+  private static final String DIRECTORY_DOTGIT = ".git";
+
+  /** Field description */
+  private static final String DIRECTORY_OBJETCS = "objects";
+
+  /** Field description */
+  private static final String DIRECTORY_REFS = "refs";
+
+  /** Field description */
   private static final String PREFIX_HEADS = "refs/heads/";
 
   /** Field description */
   public static org.eclipse.jgit.lib.Repository open(File directory)
     throws IOException
   {
-    return RepositoryCache.open(RepositoryCache.FileKey.lenient(directory,
-      FS.DETECTED), true);
+    FS fs = FS.DETECTED;
+    FileRepositoryBuilder builder = new FileRepositoryBuilder();
+
+    builder.setFS(fs);
+
+    if (isGitDirectory(fs, directory))
+    {
+
+      // bare repository
+      builder.setGitDir(directory).setBare();
+    }
+    else
+    {
+      builder.setWorkTree(directory);
+    }
+
+    return builder.build();
   }
 
   /**
     String localBranch)
   {
     String branch = localBranch;
-    if ( localBranch.startsWith(REF_HEAD_PREFIX) )
+
+    if (localBranch.startsWith(REF_HEAD_PREFIX))
     {
       branch = localBranch.substring(REF_HEAD_PREFIX.length());
     }
+
     return String.format(REMOTE_REF, repository.getId(), branch);
   }
 
     return name;
   }
 
+  /**
+   * Method description
+   *
+   *
+   * @param dir
+   *
+   * @return
+   */
+  public static boolean isGitDirectory(File dir)
+  {
+    return isGitDirectory(FS.DETECTED, dir);
+  }
+
+  /**
+   * Method description
+   *
+   *
+   * @param fs
+   * @param dir
+   *
+   * @return
+   */
+  public static boolean isGitDirectory(FS fs, File dir)
+  {
+    //J-
+    return fs.resolve(dir, DIRECTORY_OBJETCS).exists()
+      && fs.resolve(dir, DIRECTORY_REFS).exists() 
+      &&!fs.resolve(dir, DIRECTORY_DOTGIT).exists();
+    //J+
+  }
+
   //~--- methods --------------------------------------------------------------
 
   /**