(refs #104)Zip file is exported from the bare repository directly.

This commit is contained in:
takezoe
2013-10-24 11:29:23 +09:00
parent 6dbfc12896
commit 60e7165983
2 changed files with 51 additions and 43 deletions

View File

@@ -11,7 +11,7 @@ import org.eclipse.jgit.api.Git
import org.eclipse.jgit.lib._ import org.eclipse.jgit.lib._
import org.apache.commons.io.FileUtils import org.apache.commons.io.FileUtils
import org.eclipse.jgit.treewalk._ import org.eclipse.jgit.treewalk._
import org.eclipse.jgit.api.errors.RefNotFoundException import java.util.zip.{ZipEntry, ZipOutputStream}
class RepositoryViewerController extends RepositoryViewerControllerBase class RepositoryViewerController extends RepositoryViewerControllerBase
with RepositoryService with AccountService with ReferrerAuthenticator with RepositoryService with AccountService with ReferrerAuthenticator
@@ -175,24 +175,33 @@ trait RepositoryViewerControllerBase extends ControllerBase {
} }
workDir.mkdirs workDir.mkdirs
// clone the repository
val cloneDir = new File(workDir, revision)
using(Git.cloneRepository
.setURI(getRepositoryDir(repository.owner, repository.name).toURI.toString)
.setDirectory(cloneDir)
.setBranch(revision)
.call){ git =>
// checkout the specified revision
git.checkout.setName(revision).call
}
// remove .git
FileUtils.deleteDirectory(new File(cloneDir, ".git"))
// create zip file
val zipFile = new File(workDir, (if(revision.length == 40) revision.substring(0, 10) else revision) + ".zip") val zipFile = new File(workDir, (if(revision.length == 40) revision.substring(0, 10) else revision) + ".zip")
FileUtil.createZipFile(zipFile, cloneDir)
using(Git.open(getRepositoryDir(repository.owner, repository.name))){ git =>
val revCommit = JGitUtil.getRevCommitFromId(git, git.getRepository.resolve(revision))
using(new TreeWalk(git.getRepository)){ walk =>
val reader = walk.getObjectReader
val objectId = new MutableObjectId
using(new ZipOutputStream(new java.io.FileOutputStream(zipFile))){ out =>
walk.addTree(revCommit.getTree)
walk.setRecursive(true)
while(walk.next){
val name = walk.getPathString
val mode = walk.getFileMode(0)
if(mode != FileMode.TREE){
walk.getObjectId(objectId, 0)
val entry = new ZipEntry(name)
val loader = reader.open(objectId)
entry.setSize(loader.getSize)
out.putNextEntry(entry)
loader.copyTo(out)
}
}
}
}
}
contentType = "application/octet-stream" contentType = "application/octet-stream"
zipFile zipFile

View File

@@ -1,9 +1,8 @@
package util package util
import org.apache.commons.io.{IOUtils, FileUtils} import org.apache.commons.io.FileUtils
import java.net.URLConnection import java.net.URLConnection
import java.io.File import java.io.File
import org.apache.commons.compress.archivers.zip.{ZipArchiveEntry, ZipArchiveOutputStream}
import util.ControlUtil._ import util.ControlUtil._
object FileUtil { object FileUtil {
@@ -32,23 +31,23 @@ object FileUtil {
def isText(content: Array[Byte]): Boolean = !content.contains(0) def isText(content: Array[Byte]): Boolean = !content.contains(0)
def createZipFile(dest: File, dir: File): Unit = { // def createZipFile(dest: File, dir: File): Unit = {
def addDirectoryToZip(out: ZipArchiveOutputStream, dir: File, path: String): Unit = { // def addDirectoryToZip(out: ZipArchiveOutputStream, dir: File, path: String): Unit = {
dir.listFiles.map { file => // dir.listFiles.map { file =>
if(file.isFile){ // if(file.isFile){
out.putArchiveEntry(new ZipArchiveEntry(path + "/" + file.getName)) // out.putArchiveEntry(new ZipArchiveEntry(path + "/" + file.getName))
out.write(FileUtils.readFileToByteArray(file)) // out.write(FileUtils.readFileToByteArray(file))
out.closeArchiveEntry // out.closeArchiveEntry
} else if(file.isDirectory){ // } else if(file.isDirectory){
addDirectoryToZip(out, file, path + "/" + file.getName) // addDirectoryToZip(out, file, path + "/" + file.getName)
} // }
} // }
} // }
//
using(new ZipArchiveOutputStream(dest)){ out => // using(new ZipArchiveOutputStream(dest)){ out =>
addDirectoryToZip(out, dir, dir.getName) // addDirectoryToZip(out, dir, dir.getName)
} // }
} // }
def getFileName(path: String): String = defining(path.lastIndexOf('/')){ i => def getFileName(path: String): String = defining(path.lastIndexOf('/')){ i =>
if(i >= 0) path.substring(i + 1) else path if(i >= 0) path.substring(i + 1) else path