diff --git a/src/main/scala/app/CreateRepositoryServlet.scala b/src/main/scala/app/CreateRepositoryServlet.scala index ef25436..42fede5 100644 --- a/src/main/scala/app/CreateRepositoryServlet.scala +++ b/src/main/scala/app/CreateRepositoryServlet.scala @@ -61,9 +61,6 @@ FileUtils.deleteDirectory(tmpdir) } - // update all branches - updateAllBranches(LoginUser, form.name) - // redirect to the repository redirect("/%s/%s".format(LoginUser, form.name)) } diff --git a/src/main/scala/app/GitRepositoryServlet.scala b/src/main/scala/app/GitRepositoryServlet.scala index 8fecbe2..2294e56 100644 --- a/src/main/scala/app/GitRepositoryServlet.scala +++ b/src/main/scala/app/GitRepositoryServlet.scala @@ -36,22 +36,4 @@ }); } - /** - * 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) - } - } - } - } diff --git a/src/main/scala/app/RepositoryViewerServlet.scala b/src/main/scala/app/RepositoryViewerServlet.scala index 26d2806..65ee7ab 100644 --- a/src/main/scala/app/RepositoryViewerServlet.scala +++ b/src/main/scala/app/RepositoryViewerServlet.scala @@ -98,57 +98,38 @@ val raw = params.get("raw").getOrElse("false").toBoolean val path = multiParams("splat").head.replaceFirst("^tree/.+?/", "") val repositoryInfo = JGitUtil.getRepositoryInfo(owner, repository, servletContext) - - if(repositoryInfo.branchList.contains(id)){ - // id is branch name - 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) + + val git = Git.open(getRepositoryDir(owner, repository)) + val commitId = git.getRepository.resolve(id) - if(raw){ - // Download - contentType = "application/octet-stream" - file - } else { - // Viewer - val viewer = if(FileTypeUtil.isImage(file.getName)) "image" else if(FileTypeUtil.isLarge(file.length)) "large" else "text" - val content = ContentInfo( - viewer, if(viewer == "text") Some(FileUtils.readFileToString(file, "UTF-8")) else None - ) - html.blob(id, repositoryInfo, path.split("/").toList, content, new CommitInfo(rev)) - } + val revWalk = new RevWalk(git.getRepository) + val revCommit = revWalk.parseCommit(commitId) + revWalk.dispose + + @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 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 { - // id is commit id - val branch = JGitUtil.getBranchNameFromCommitId(id, repositoryInfo) - val dir = getBranchDir(owner, repository, branch) - val git = Git.open(dir) - 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) + // 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) - } else { - // 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)) - } + html.blob(id, repositoryInfo, path.split("/").toList, content, new CommitInfo(revCommit)) } } @@ -162,18 +143,23 @@ val repositoryInfo = JGitUtil.getRepositoryInfo(owner, repository, servletContext) - // get branch by commit id - // 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 git = Git.open(getRepositoryDir(owner, repository)) - val dir = getBranchDir(owner, repository, branch) - val git = Git.open(dir) - val ite = git.log.add(ObjectId.fromString(id)).call.iterator - val rev = ite.next - val old = ite.next + @scala.annotation.tailrec + def getCommitLog(i: java.util.Iterator[RevCommit], logs: List[RevCommit]): List[RevCommit] = + i.hasNext match { + case true if(logs.size < 2) => getCommitLog(i, logs :+ i.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){ // get diff between specified commit and its previous commit @@ -204,7 +190,7 @@ buffer.toList } - html.commit(branch, + html.commit(id, CommitInfo(rev.getName, rev.getCommitterIdent.getWhen, rev.getCommitterIdent.getName, rev.getFullMessage), repositoryInfo, diffs) } @@ -231,6 +217,7 @@ val revWalk = new RevWalk(git.getRepository) val objectId = git.getRepository.resolve(revision) val revCommit = revWalk.parseCommit(objectId) + revWalk.dispose val files = JGitUtil.getFileList(owner, repository, revision, path) diff --git a/src/main/scala/util/Directory.scala b/src/main/scala/util/Directory.scala index 7cbe3fe..57a4613 100644 --- a/src/main/scala/util/Directory.scala +++ b/src/main/scala/util/Directory.scala @@ -32,46 +32,11 @@ 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. * GiyBucket generates initial repository contents in this directory and push them. * This directory is removed after the repository creation. */ def getInitRepositoryDir(owner: String, repository: String): File = 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 - } - } - } - } \ No newline at end of file diff --git a/src/main/scala/util/JGitUtil.scala b/src/main/scala/util/JGitUtil.scala index 876c412..bd8c050 100644 --- a/src/main/scala/util/JGitUtil.scala +++ b/src/main/scala/util/JGitUtil.scala @@ -44,16 +44,6 @@ } /** - * 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. * * @param owner the repository owner