Get all data from the bare repository.

Cloned repositories in backend is not necessary at last.
This commit is contained in:
takezoe
2013-04-30 01:07:31 +09:00
parent 1779d167ea
commit 35f0aa7b03
5 changed files with 47 additions and 126 deletions

View File

@@ -61,9 +61,6 @@ class CreateRepositoryServlet extends ServletBase {
FileUtils.deleteDirectory(tmpdir) FileUtils.deleteDirectory(tmpdir)
} }
// update all branches
updateAllBranches(LoginUser, form.name)
// redirect to the repository // redirect to the repository
redirect("/%s/%s".format(LoginUser, form.name)) redirect("/%s/%s".format(LoginUser, form.name))
} }

View File

@@ -36,22 +36,4 @@ class GitRepositoryServlet extends GitServlet {
}); });
} }
/**
* Override GitServlet#service() to pull pushed changes to cloned repositories for branch exploring.
*/
override def service(request: HttpServletRequest, response: HttpServletResponse): Unit = {
super.service(request, response)
logger.debug(request.getMethod + ": " + request.getRequestURI)
// update branches
if(request.getMethod == "POST" && request.getRequestURI.endsWith("/git-receive-pack")){
request.getRequestURI
.replaceFirst("^" + request.getServletContext.getContextPath + "/git/", "")
.replaceFirst("\\.git/git-receive-pack$", "").split("/") match {
case Array(owner, repository) => Directory.updateAllBranches(owner, repository)
}
}
}
} }

View File

@@ -98,57 +98,38 @@ class RepositoryViewerServlet extends ServletBase {
val raw = params.get("raw").getOrElse("false").toBoolean val raw = params.get("raw").getOrElse("false").toBoolean
val path = multiParams("splat").head.replaceFirst("^tree/.+?/", "") val path = multiParams("splat").head.replaceFirst("^tree/.+?/", "")
val repositoryInfo = JGitUtil.getRepositoryInfo(owner, repository, servletContext) val repositoryInfo = JGitUtil.getRepositoryInfo(owner, repository, servletContext)
if(repositoryInfo.branchList.contains(id)){ val git = Git.open(getRepositoryDir(owner, repository))
// id is branch name val commitId = git.getRepository.resolve(id)
val dir = getBranchDir(owner, repository, id)
val git = Git.open(dir)
val rev = git.log.addPath(path).call.iterator.next
val file = new File(dir, path)
if(raw){ val revWalk = new RevWalk(git.getRepository)
// Download val revCommit = revWalk.parseCommit(commitId)
contentType = "application/octet-stream" revWalk.dispose
file
} else { @scala.annotation.tailrec
// Viewer def getPathObjectId(path: String, walk: TreeWalk): ObjectId = walk.next match {
val viewer = if(FileTypeUtil.isImage(file.getName)) "image" else if(FileTypeUtil.isLarge(file.length)) "large" else "text" case true if(walk.getPathString == path) => walk.getObjectId(0)
val content = ContentInfo( case true => getPathObjectId(path, walk)
viewer, if(viewer == "text") Some(FileUtils.readFileToString(file, "UTF-8")) else None }
)
html.blob(id, repositoryInfo, path.split("/").toList, content, new CommitInfo(rev)) val treeWalk = new TreeWalk(git.getRepository)
} treeWalk.addTree(revCommit.getTree)
treeWalk.setRecursive(true)
val objectId = getPathObjectId(path, treeWalk)
treeWalk.release
if(raw){
// Download
contentType = "application/octet-stream"
JGitUtil.getContent(git, objectId, false)
} else { } else {
// id is commit id // Viewer
val branch = JGitUtil.getBranchNameFromCommitId(id, repositoryInfo) val large = FileTypeUtil.isLarge(git.getRepository.getObjectDatabase.open(objectId).getSize)
val dir = getBranchDir(owner, repository, branch) val viewer = if(FileTypeUtil.isImage(path)) "image" else if(large) "large" else "text"
val git = Git.open(dir) val content = ContentInfo(viewer, if(viewer == "text") JGitUtil.getContent(git, objectId, false).map(new String(_, "UTF-8")) else None)
val rev = git.log.add(ObjectId.fromString(id)).call.iterator.next
@scala.annotation.tailrec
def getPathObjectId(path: String, walk: TreeWalk): ObjectId = walk.next match {
case true if(walk.getPathString == path) => walk.getObjectId(0)
case true => getPathObjectId(path, walk)
}
val walk = new TreeWalk(git.getRepository)
walk.addTree(rev.getTree)
walk.setRecursive(true)
val objectId = getPathObjectId(path, walk)
if(raw){
// Download
contentType = "application/octet-stream"
JGitUtil.getContent(git, objectId, false)
} else { html.blob(id, repositoryInfo, path.split("/").toList, content, new CommitInfo(revCommit))
// Viewer
val large = FileTypeUtil.isLarge(git.getRepository.getObjectDatabase.open(objectId).getSize)
val viewer = if(FileTypeUtil.isImage(path)) "image" else if(large) "large" else "text"
val content = ContentInfo(viewer, if(viewer == "text") JGitUtil.getContent(git, objectId, false).map(new String(_, "UTF-8")) else None)
html.blob(branch, repositoryInfo, path.split("/").toList, content, new CommitInfo(rev))
}
} }
} }
@@ -162,18 +143,23 @@ class RepositoryViewerServlet extends ServletBase {
val repositoryInfo = JGitUtil.getRepositoryInfo(owner, repository, servletContext) val repositoryInfo = JGitUtil.getRepositoryInfo(owner, repository, servletContext)
// get branch by commit id val git = Git.open(getRepositoryDir(owner, repository))
// TODO this does not work correctly...
val branch = repositoryInfo.branchList.find { branch =>
val git = Git.open(getBranchDir(owner, repository, branch))
git.log.add(ObjectId.fromString(id)).call.iterator.hasNext
}.get
val dir = getBranchDir(owner, repository, branch) @scala.annotation.tailrec
val git = Git.open(dir) def getCommitLog(i: java.util.Iterator[RevCommit], logs: List[RevCommit]): List[RevCommit] =
val ite = git.log.add(ObjectId.fromString(id)).call.iterator i.hasNext match {
val rev = ite.next case true if(logs.size < 2) => getCommitLog(i, logs :+ i.next)
val old = ite.next case _ => logs
}
val revWalk = new RevWalk(git.getRepository)
revWalk.markStart(revWalk.parseCommit(git.getRepository.resolve(id)))
val commits = getCommitLog(revWalk.iterator, Nil)
revWalk.release
val rev = commits(0)
val old = commits(1)
val diffs = if(old != null){ val diffs = if(old != null){
// get diff between specified commit and its previous commit // get diff between specified commit and its previous commit
@@ -204,7 +190,7 @@ class RepositoryViewerServlet extends ServletBase {
buffer.toList buffer.toList
} }
html.commit(branch, html.commit(id,
CommitInfo(rev.getName, rev.getCommitterIdent.getWhen, rev.getCommitterIdent.getName, rev.getFullMessage), CommitInfo(rev.getName, rev.getCommitterIdent.getWhen, rev.getCommitterIdent.getName, rev.getFullMessage),
repositoryInfo, diffs) repositoryInfo, diffs)
} }
@@ -231,6 +217,7 @@ class RepositoryViewerServlet extends ServletBase {
val revWalk = new RevWalk(git.getRepository) val revWalk = new RevWalk(git.getRepository)
val objectId = git.getRepository.resolve(revision) val objectId = git.getRepository.resolve(revision)
val revCommit = revWalk.parseCommit(objectId) val revCommit = revWalk.parseCommit(objectId)
revWalk.dispose
val files = JGitUtil.getFileList(owner, repository, revision, path) val files = JGitUtil.getFileList(owner, repository, revision, path)

View File

@@ -31,12 +31,6 @@ object Directory {
def getRepositoryDir(owner: String, repository: String): File = def getRepositoryDir(owner: String, repository: String): File =
new File("%s/%s/%s.git".format(RepositoryHome, owner, repository)) new File("%s/%s/%s.git".format(RepositoryHome, owner, repository))
/**
* Temporary directory which is used in the repository viewer.
*/
def getBranchDir(owner: String, repository: String, branch: String): File =
new File("%s/tmp/%s/branches/%s/%s".format(GitBucketHome, owner, repository, branch))
/** /**
* Temporary directory which is used in the repository creation. * Temporary directory which is used in the repository creation.
* GiyBucket generates initial repository contents in this directory and push them. * GiyBucket generates initial repository contents in this directory and push them.
@@ -44,34 +38,5 @@ object Directory {
*/ */
def getInitRepositoryDir(owner: String, repository: String): File = def getInitRepositoryDir(owner: String, repository: String): File =
new File("%s/tmp/%s/init-%s".format(GitBucketHome, owner, repository)) new File("%s/tmp/%s/init-%s".format(GitBucketHome, owner, repository))
def updateAllBranches(owner: String, repository: String): Unit = {
// TODO debug log
println("[pull]" + owner + "/" + repository)
val dir = Directory.getRepositoryDir(owner, repository)
val git = Git.open(dir)
val branchList = git.branchList.call.toArray.map { ref =>
ref.asInstanceOf[Ref].getName
}.toList
branchList.foreach { branch =>
val branchName = branch.replaceFirst("^refs/heads/", "")
val branchdir = Directory.getBranchDir(owner, repository, branchName)
if(!branchdir.exists){
branchdir.mkdirs()
Git.cloneRepository
.setURI(dir.toURL.toString)
.setBranch(branch)
.setDirectory(branchdir)
.call
Git.open(branchdir).checkout.setName(branchName).call
} else {
Git.open(branchdir).pull.call
}
}
}
} }

View File

@@ -43,16 +43,6 @@ object JGitUtil {
) )
} }
/**
* Get the branch name from the commit id.
*/
def getBranchNameFromCommitId(id: String, repositoryInfo: RepositoryInfo): String = {
repositoryInfo.branchList.find { branch =>
val git = Git.open(getBranchDir(repositoryInfo.owner, repositoryInfo.name, branch))
git.log.add(ObjectId.fromString(id)).call.iterator.hasNext
}.get
}
/** /**
* Returns the file list of the specified path. * Returns the file list of the specified path.
* *