mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-06 21:45:50 +01:00
Get all data from the bare repository.
Cloned repositories in backend is not necessary at last.
This commit is contained in:
@@ -61,9 +61,6 @@ class CreateRepositoryServlet extends ServletBase {
|
||||
FileUtils.deleteDirectory(tmpdir)
|
||||
}
|
||||
|
||||
// update all branches
|
||||
updateAllBranches(LoginUser, form.name)
|
||||
|
||||
// redirect to the repository
|
||||
redirect("/%s/%s".format(LoginUser, form.name))
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -98,57 +98,38 @@ class RepositoryViewerServlet extends ServletBase {
|
||||
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 @@ class RepositoryViewerServlet extends ServletBase {
|
||||
|
||||
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 @@ class RepositoryViewerServlet extends ServletBase {
|
||||
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 @@ class RepositoryViewerServlet extends ServletBase {
|
||||
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)
|
||||
|
||||
|
||||
@@ -31,12 +31,6 @@ object Directory {
|
||||
def getRepositoryDir(owner: String, repository: String): File =
|
||||
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.
|
||||
@@ -44,34 +38,5 @@ object Directory {
|
||||
*/
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user