James Moger
2011-11-11 d65f712ea3d8941f4b9145c0630c30c20af80d13
src/com/gitblit/utils/JGitUtils.java
@@ -37,6 +37,8 @@
import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.FetchCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.ResetCommand;
import org.eclipse.jgit.api.ResetCommand.ResetType;
import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.diff.DiffEntry.ChangeType;
import org.eclipse.jgit.diff.DiffFormatter;
@@ -138,6 +140,7 @@
    * Encapsulates the result of cloning or pulling from a repository.
    */
   public static class CloneResult {
      public String name;
      public FetchResult fetchResult;
      public boolean createdRepository;
   }
@@ -155,7 +158,7 @@
    */
   public static CloneResult cloneRepository(File repositoriesFolder, String name, String fromUrl)
         throws Exception {
      return cloneRepository(repositoriesFolder, name, fromUrl, null);
      return cloneRepository(repositoriesFolder, name, fromUrl, true, null);
   }
   /**
@@ -166,16 +169,27 @@
    * @param repositoriesFolder
    * @param name
    * @param fromUrl
    * @param bare
    * @param credentialsProvider
    * @return CloneResult
    * @throws Exception
    */
   public static CloneResult cloneRepository(File repositoriesFolder, String name, String fromUrl,
         CredentialsProvider credentialsProvider) throws Exception {
         boolean bare, CredentialsProvider credentialsProvider) throws Exception {
      CloneResult result = new CloneResult();
      if (!name.toLowerCase().endsWith(Constants.DOT_GIT_EXT)) {
         name += Constants.DOT_GIT_EXT;
      if (bare) {
         // bare repository, ensure .git suffix
         if (!name.toLowerCase().endsWith(Constants.DOT_GIT_EXT)) {
            name += Constants.DOT_GIT_EXT;
         }
      } else {
         // normal repository, strip .git suffix
         if (name.toLowerCase().endsWith(Constants.DOT_GIT_EXT)) {
            name = name.substring(0, name.indexOf(Constants.DOT_GIT_EXT));
         }
      }
      result.name = name;
      File folder = new File(repositoriesFolder, name);
      if (folder.exists()) {
         File gitDir = FileKey.resolve(new File(repositoriesFolder, name), FS.DETECTED);
@@ -184,7 +198,7 @@
         repository.close();
      } else {
         CloneCommand clone = new CloneCommand();
         clone.setBare(true);
         clone.setBare(bare);
         clone.setCloneAllBranches(true);
         clone.setURI(fromUrl);
         clone.setDirectory(folder);
@@ -229,7 +243,28 @@
         fetch.setCredentialsProvider(credentialsProvider);
      }
      fetch.setRefSpecs(specs);
      FetchResult result = fetch.call();
      FetchResult fetchRes = fetch.call();
      return fetchRes;
   }
   /**
    * Reset HEAD to the latest remote tracking commit.
    *
    * @param repository
    * @param remoteRef
    *            the remote tracking reference (e.g. origin/master)
    * @return Ref
    * @throws Exception
    */
   public static Ref resetHEAD(Repository repository, String remoteRef) throws Exception {
      if (!remoteRef.startsWith(Constants.R_REMOTES)) {
         remoteRef = Constants.R_REMOTES + remoteRef;
      }
      Git git = new Git(repository);
      ResetCommand reset = git.reset();
      reset.setMode(ResetType.SOFT);
      reset.setRef(remoteRef);
      Ref result = reset.call();
      return result;
   }
@@ -264,7 +299,7 @@
      }
      list.addAll(getRepositoryList(repositoriesFolder.getAbsolutePath(), repositoriesFolder,
            exportAll, searchSubfolders));
      Collections.sort(list);
      StringUtils.sortRepositorynames(list);
      return list;
   }
@@ -890,27 +925,6 @@
   }
   /**
    * Enumeration of the search types.
    */
   public static enum SearchType {
      AUTHOR, COMMITTER, COMMIT;
      public static SearchType forName(String name) {
         for (SearchType type : values()) {
            if (type.name().equalsIgnoreCase(name)) {
               return type;
            }
         }
         return COMMIT;
      }
      @Override
      public String toString() {
         return name().toLowerCase();
      }
   }
   /**
    * Search the commit history for a case-insensitive match to the value.
    * Search results require a specified SearchType of AUTHOR, COMMITTER, or
    * COMMIT. Results may be paginated using offset and maxCount. If the
@@ -928,7 +942,7 @@
    * @return matching list of commits
    */
   public static List<RevCommit> searchRevlogs(Repository repository, String objectId,
         String value, final SearchType type, int offset, int maxCount) {
         String value, final com.gitblit.Constants.SearchType type, int offset, int maxCount) {
      final String lcValue = value.toLowerCase();
      List<RevCommit> list = new ArrayList<RevCommit>();
      if (maxCount == 0) {