James Moger
2011-05-25 f13c4c5a35a18d8478b276cc44570bbc3398aa73
src/com/gitblit/utils/JGitUtils.java
@@ -1,9 +1,25 @@
/*
 * Copyright 2011 gitblit.com.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.gitblit.utils;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.text.DateFormat;
import java.text.ParseException;
@@ -17,6 +33,8 @@
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.diff.DiffEntry;
@@ -80,6 +98,9 @@
   public static List<String> getNestedRepositories(File repositoriesFolder, File folder, boolean exportAll, boolean readNested) {
      String basefile = repositoriesFolder.getAbsolutePath();
      List<String> list = new ArrayList<String>();
      if (folder == null || !folder.exists()) {
         return list;
      }
      for (File file : folder.listFiles()) {
         if (file.isDirectory() && !file.getName().equalsIgnoreCase(Constants.DOT_GIT)) {
            // if this is a git repository add it to the list
@@ -805,10 +826,57 @@
      return null;
   }
   public static boolean zip(Repository r, String basePath, String objectId, OutputStream os) throws Exception {
      RevCommit commit = getCommit(r, objectId);
      if (commit == null) {
         return false;
      }
      final RevWalk rw = new RevWalk(r);
      final TreeWalk walk = new TreeWalk(r);
      try {
         walk.addTree(commit.getTree());
         ZipOutputStream zos = new ZipOutputStream(os);
         zos.setComment("Generated by Git:Blit");
         if (basePath != null && basePath.length() > 0) {
            PathFilter f = PathFilter.create(basePath);
            walk.setFilter(f);
         }
         walk.setRecursive(true);
         while (walk.next()) {
            ZipEntry entry = new ZipEntry(walk.getPathString());
            entry.setSize(walk.getObjectReader().getObjectSize(walk.getObjectId(0), Constants.OBJ_BLOB));
            entry.setComment(commit.getName());
            zos.putNextEntry(entry);
            ObjectId entid = walk.getObjectId(0);
            FileMode entmode = walk.getFileMode(0);
            RevBlob blob = (RevBlob) rw.lookupAny(entid, entmode.getObjectType());
            rw.parseBody(blob);
            ObjectLoader ldr = r.open(blob.getId(), Constants.OBJ_BLOB);
            byte[] tmp = new byte[4096];
            InputStream in = ldr.openStream();
            int n;
            while ((n = in.read(tmp)) > 0) {
               zos.write(tmp, 0, n);
            }
            in.close();
         }
         zos.finish();
         return true;
      } catch (IOException e) {
         LOGGER.error("Failed to zip files from commit " + commit.getName(), e);
      } finally {
         walk.release();
         rw.dispose();
      }
      return false;
   }
   public static List<Metric> getDateMetrics(Repository r) {
      Metric total = new Metric("TOTAL");
      final Map<String, Metric> metricMap = new HashMap<String, Metric>();
      if (hasCommits(r)) {
         final List<RefModel> tags = getTags(r, -1);
         final Map<ObjectId, RefModel> tagMap = new HashMap<ObjectId, RefModel>();