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)
|
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))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -99,31 +99,12 @@ class RepositoryViewerServlet extends ServletBase {
|
|||||||
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 {
|
|
||||||
// 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))
|
|
||||||
}
|
|
||||||
} 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
|
@scala.annotation.tailrec
|
||||||
def getPathObjectId(path: String, walk: TreeWalk): ObjectId = walk.next match {
|
def getPathObjectId(path: String, walk: TreeWalk): ObjectId = walk.next match {
|
||||||
@@ -131,10 +112,11 @@ class RepositoryViewerServlet extends ServletBase {
|
|||||||
case true => getPathObjectId(path, walk)
|
case true => getPathObjectId(path, walk)
|
||||||
}
|
}
|
||||||
|
|
||||||
val walk = new TreeWalk(git.getRepository)
|
val treeWalk = new TreeWalk(git.getRepository)
|
||||||
walk.addTree(rev.getTree)
|
treeWalk.addTree(revCommit.getTree)
|
||||||
walk.setRecursive(true)
|
treeWalk.setRecursive(true)
|
||||||
val objectId = getPathObjectId(path, walk)
|
val objectId = getPathObjectId(path, treeWalk)
|
||||||
|
treeWalk.release
|
||||||
|
|
||||||
if(raw){
|
if(raw){
|
||||||
// Download
|
// Download
|
||||||
@@ -147,8 +129,7 @@ class RepositoryViewerServlet extends ServletBase {
|
|||||||
val viewer = if(FileTypeUtil.isImage(path)) "image" else if(large) "large" else "text"
|
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)
|
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)
|
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)
|
||||||
|
|
||||||
|
|||||||
@@ -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.
|
||||||
@@ -45,33 +39,4 @@ 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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -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.
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user