James Moger
2011-06-25 22fc5e48cbe050d8485f78f6165b59e4085eaeef
src/com/gitblit/utils/JGitUtils.java
@@ -22,6 +22,7 @@
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
@@ -32,6 +33,8 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.FetchCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.diff.DiffEntry.ChangeType;
@@ -57,6 +60,9 @@
import org.eclipse.jgit.revwalk.RevTree;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.revwalk.filter.RevFilter;
import org.eclipse.jgit.storage.file.FileRepository;
import org.eclipse.jgit.transport.FetchResult;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.filter.AndTreeFilter;
import org.eclipse.jgit.treewalk.filter.OrTreeFilter;
@@ -90,8 +96,55 @@
      return r.toString().trim();
   }
   public static Repository createRepository(File repositoriesFolder, String name, boolean bare) {
      Git git = Git.init().setDirectory(new File(repositoriesFolder, name)).setBare(bare).call();
   public static FetchResult cloneRepository(File repositoriesFolder, String name, String fromUrl)
         throws Exception {
      FetchResult result = null;
      if (!name.toLowerCase().endsWith(Constants.DOT_GIT_EXT)) {
         name += Constants.DOT_GIT_EXT;
      }
      File folder = new File(repositoriesFolder, name);
      if (folder.exists()) {
         File gitDir = FileKey.resolve(new File(repositoriesFolder, name), FS.DETECTED);
         FileRepository repository = new FileRepository(gitDir);
         result = fetchRepository(repository);
         repository.close();
      } else {
         CloneCommand clone = new CloneCommand();
         clone.setBare(true);
         clone.setCloneAllBranches(true);
         clone.setURI(fromUrl);
         clone.setDirectory(folder);
         clone.call();
         // Now we have to fetch because CloneCommand doesn't fetch
         // refs/notes nor does it allow manual RefSpec.
         File gitDir = FileKey.resolve(new File(repositoriesFolder, name), FS.DETECTED);
         FileRepository repository = new FileRepository(gitDir);
         result = fetchRepository(repository);
         repository.close();
      }
      return result;
   }
   public static FetchResult fetchRepository(Repository repository, RefSpec... refSpecs)
         throws Exception {
      Git git = new Git(repository);
      FetchCommand fetch = git.fetch();
      List<RefSpec> specs = new ArrayList<RefSpec>();
      if (refSpecs == null || refSpecs.length == 0) {
         specs.add(new RefSpec("+refs/heads/*:refs/remotes/origin/*"));
         specs.add(new RefSpec("+refs/tags/*:refs/tags/*"));
         specs.add(new RefSpec("+refs/notes/*:refs/notes/*"));
      } else {
         specs.addAll(Arrays.asList(refSpecs));
      }
      fetch.setRefSpecs(specs);
      FetchResult result = fetch.call();
      repository.close();
      return result;
   }
   public static Repository createRepository(File repositoriesFolder, String name) {
      Git git = Git.init().setDirectory(new File(repositoriesFolder, name)).setBare(true).call();
      return git.getRepository();
   }
@@ -211,7 +264,7 @@
   }
   public static Map<ObjectId, List<RefModel>> getAllRefs(Repository r) {
      List<RefModel> list = getRefs(r, org.eclipse.jgit.lib.RefDatabase.ALL, -1);
      List<RefModel> list = getRefs(r, org.eclipse.jgit.lib.RefDatabase.ALL, true, -1);
      Map<ObjectId, List<RefModel>> refs = new HashMap<ObjectId, List<RefModel>>();
      for (RefModel ref : list) {
         ObjectId objectid = ref.getReferencedObjectId();
@@ -219,7 +272,7 @@
            refs.put(objectid, new ArrayList<RefModel>());
         }
         refs.get(objectid).add(ref);
      }
      }
      return refs;
   }
@@ -350,35 +403,29 @@
   public static List<PathChangeModel> getFilesInCommit(Repository r, RevCommit commit) {
      List<PathChangeModel> list = new ArrayList<PathChangeModel>();
      RevWalk rw = new RevWalk(r);
      TreeWalk tw = new TreeWalk(r);
      try {
         if (commit == null) {
            ObjectId object = r.resolve(Constants.HEAD);
            commit = rw.parseCommit(object);
         }
         RevTree commitTree = commit.getTree();
         tw.reset();
         tw.setRecursive(true);
         if (commit.getParentCount() == 0) {
            tw.addTree(commitTree);
            TreeWalk tw = new TreeWalk(r);
            tw.reset();
            tw.setRecursive(true);
            tw.addTree(commit.getTree());
            while (tw.next()) {
               list.add(new PathChangeModel(tw.getPathString(), tw.getPathString(), 0, tw
                     .getRawMode(0), commit.getId().getName(), ChangeType.ADD));
            }
            tw.release();
         } else {
            RevCommit parent = rw.parseCommit(commit.getParent(0).getId());
            RevTree parentTree = parent.getTree();
            tw.addTree(parentTree);
            tw.addTree(commitTree);
            tw.setFilter(TreeFilter.ANY_DIFF);
            RawTextComparator cmp = RawTextComparator.DEFAULT;
            DiffFormatter df = new DiffFormatter(DisabledOutputStream.INSTANCE);
            df.setRepository(r);
            df.setDiffComparator(cmp);
            df.setDiffComparator(RawTextComparator.DEFAULT);
            df.setDetectRenames(true);
            List<DiffEntry> diffs = df.scan(parentTree, commitTree);
            List<DiffEntry> diffs = df.scan(parent.getTree(), commit.getTree());
            for (DiffEntry diff : diffs) {
               if (diff.getChangeType().equals(ChangeType.DELETE)) {
                  list.add(new PathChangeModel(diff.getOldPath(), diff.getOldPath(), 0, diff
@@ -395,7 +442,6 @@
         LOGGER.error("failed to determine files in commit!", t);
      } finally {
         rw.dispose();
         tw.release();
      }
      return list;
   }
@@ -480,6 +526,9 @@
   public static List<RevCommit> getRevLog(Repository r, String objectId, String path, int offset,
         int maxCount) {
      List<RevCommit> list = new ArrayList<RevCommit>();
      if (maxCount == 0) {
         return list;
      }
      if (!hasCommits(r)) {
         return list;
      }
@@ -545,6 +594,9 @@
         final SearchType type, int offset, int maxCount) {
      final String lcValue = value.toLowerCase();
      List<RevCommit> list = new ArrayList<RevCommit>();
      if (maxCount == 0) {
         return list;
      }
      if (!hasCommits(r)) {
         return list;
      }
@@ -613,31 +665,38 @@
      return list;
   }
   public static List<RefModel> getTags(Repository r, int maxCount) {
      return getRefs(r, Constants.R_TAGS, maxCount);
   public static List<RefModel> getTags(Repository r, boolean fullName, int maxCount) {
      return getRefs(r, Constants.R_TAGS, fullName, maxCount);
   }
   public static List<RefModel> getLocalBranches(Repository r, int maxCount) {
      return getRefs(r, Constants.R_HEADS, maxCount);
   public static List<RefModel> getLocalBranches(Repository r, boolean fullName, int maxCount) {
      return getRefs(r, Constants.R_HEADS, fullName, maxCount);
   }
   public static List<RefModel> getRemoteBranches(Repository r, int maxCount) {
      return getRefs(r, Constants.R_REMOTES, maxCount);
   public static List<RefModel> getRemoteBranches(Repository r, boolean fullName, int maxCount) {
      return getRefs(r, Constants.R_REMOTES, fullName, maxCount);
   }
   public static List<RefModel> getNotesRefs(Repository r, int maxCount) {
      return getRefs(r, Constants.R_NOTES, maxCount);
   public static List<RefModel> getNotesRefs(Repository r, boolean fullName, int maxCount) {
      return getRefs(r, Constants.R_NOTES, fullName, maxCount);
   }
   private static List<RefModel> getRefs(Repository r, String refs, int maxCount) {
   private static List<RefModel> getRefs(Repository r, String refs, boolean fullName, int maxCount) {
      List<RefModel> list = new ArrayList<RefModel>();
      if (maxCount == 0) {
         return list;
      }
      try {
         Map<String, Ref> map = r.getRefDatabase().getRefs(refs);
         RevWalk rw = new RevWalk(r);
         for (Entry<String, Ref> entry : map.entrySet()) {
            Ref ref = entry.getValue();
            RevObject object = rw.parseAny(ref.getObjectId());
            list.add(new RefModel(entry.getKey(), ref, object));
            String name = entry.getKey();
            if (fullName && !StringUtils.isEmpty(refs)) {
               name = refs + name;
            }
            list.add(new RefModel(name, ref, object));
         }
         rw.dispose();
         Collections.sort(list);
@@ -653,7 +712,7 @@
   public static List<GitNote> getNotesOnCommit(Repository repository, RevCommit commit) {
      List<GitNote> list = new ArrayList<GitNote>();
      List<RefModel> notesRefs = getNotesRefs(repository, -1);
      List<RefModel> notesRefs = getNotesRefs(repository, true, -1);
      for (RefModel notesRef : notesRefs) {
         RevTree notesTree = JGitUtils.getCommit(repository, notesRef.getName()).getTree();
         StringBuilder sb = new StringBuilder(commit.getName());