mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-08 22:45:51 +01:00
Add JGitUtil#withGit() and use it to release repository resources
certainly.
This commit is contained in:
@@ -5,28 +5,39 @@ import app.{RepositoryInfo, FileInfo, CommitInfo, DiffInfo, TagInfo}
|
||||
import util.Directory._
|
||||
import scala.collection.JavaConverters._
|
||||
import javax.servlet.ServletContext
|
||||
import org.eclipse.jgit.lib.Ref
|
||||
import org.eclipse.jgit.lib.ObjectId
|
||||
import org.eclipse.jgit.errors.MissingObjectException
|
||||
import org.eclipse.jgit.revwalk.RevCommit
|
||||
import org.eclipse.jgit.diff.DiffFormatter
|
||||
import org.eclipse.jgit.treewalk.TreeWalk
|
||||
import org.eclipse.jgit.revwalk.RevWalk
|
||||
import org.eclipse.jgit.diff.RawTextComparator
|
||||
import org.eclipse.jgit.util.io.DisabledOutputStream
|
||||
import org.eclipse.jgit.lib.Repository
|
||||
import org.eclipse.jgit.revwalk.RevSort
|
||||
import org.eclipse.jgit.diff.DiffEntry.ChangeType
|
||||
import org.eclipse.jgit.lib.FileMode
|
||||
import org.eclipse.jgit.treewalk.filter.PathFilter
|
||||
import org.eclipse.jgit.treewalk.CanonicalTreeParser
|
||||
import org.eclipse.jgit.lib._
|
||||
import org.eclipse.jgit.revwalk._
|
||||
import org.eclipse.jgit.revwalk.filter.RevFilter
|
||||
import org.eclipse.jgit.treewalk._
|
||||
import org.eclipse.jgit.treewalk.filter.PathFilter
|
||||
import org.eclipse.jgit.diff._
|
||||
import org.eclipse.jgit.diff.DiffEntry.ChangeType
|
||||
import org.eclipse.jgit.util.io.DisabledOutputStream
|
||||
import org.eclipse.jgit.errors.MissingObjectException
|
||||
|
||||
/**
|
||||
* Provides complex JGit operations.
|
||||
*/
|
||||
object JGitUtil {
|
||||
|
||||
/**
|
||||
* Use this method to use the Git object.
|
||||
* Repository resources are released certainly after processing.
|
||||
*/
|
||||
def withGit[T](dir: java.io.File)(f: Git => T): T = withGit(Git.open(dir))(f)
|
||||
|
||||
/**
|
||||
* Use this method to use the Git object.
|
||||
* Repository resources are released certainly after processing.
|
||||
*/
|
||||
def withGit[T](git: Git)(f: Git => T): T = {
|
||||
try {
|
||||
f(git)
|
||||
} finally {
|
||||
git.getRepository.close
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns RevCommit from the commit id.
|
||||
*
|
||||
@@ -45,19 +56,20 @@ object JGitUtil {
|
||||
* Returns the repository information. It contains branch names and tag names.
|
||||
*/
|
||||
def getRepositoryInfo(owner: String, repository: String, servletContext: ServletContext): RepositoryInfo = {
|
||||
val git = Git.open(getRepositoryDir(owner, repository))
|
||||
RepositoryInfo(
|
||||
owner, repository, "http://localhost:8080%s/git/%s/%s.git".format(servletContext.getContextPath, owner, repository),
|
||||
// branches
|
||||
git.branchList.call.asScala.map { ref =>
|
||||
ref.getName.replaceFirst("^refs/heads/", "")
|
||||
}.toList,
|
||||
// tags
|
||||
git.tagList.call.asScala.map { ref =>
|
||||
val revCommit = getRevCommitFromId(git, ref.getObjectId)
|
||||
TagInfo(ref.getName.replaceFirst("^refs/tags/", ""), revCommit.getCommitterIdent.getWhen, revCommit.getName)
|
||||
}.toList
|
||||
)
|
||||
withGit(getRepositoryDir(owner, repository)){ git =>
|
||||
RepositoryInfo(
|
||||
owner, repository, "http://localhost:8080%s/git/%s/%s.git".format(servletContext.getContextPath, owner, repository),
|
||||
// branches
|
||||
git.branchList.call.asScala.map { ref =>
|
||||
ref.getName.replaceFirst("^refs/heads/", "")
|
||||
}.toList,
|
||||
// tags
|
||||
git.tagList.call.asScala.map { ref =>
|
||||
val revCommit = getRevCommitFromId(git, ref.getObjectId)
|
||||
TagInfo(ref.getName.replaceFirst("^refs/tags/", ""), revCommit.getCommitterIdent.getWhen, revCommit.getName)
|
||||
}.toList
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -51,8 +51,12 @@ object WikiUtil {
|
||||
val dir = getWikiRepositoryDir(owner, repository)
|
||||
if(!dir.exists){
|
||||
val repo = new RepositoryBuilder().setGitDir(dir).setBare.build
|
||||
repo.create
|
||||
savePage(owner, repository, "Home", "Home", "Welcome to the %s wiki!!".format(repository), owner, "Initial Commit")
|
||||
try {
|
||||
repo.create
|
||||
savePage(owner, repository, "Home", "Home", "Welcome to the %s wiki!!".format(repository), owner, "Initial Commit")
|
||||
} finally {
|
||||
repo.close
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,14 +67,15 @@ object WikiUtil {
|
||||
// TODO create wiki repository in the repository setting changing.
|
||||
createWikiRepository(owner, repository)
|
||||
|
||||
val git = Git.open(getWikiRepositoryDir(owner, repository))
|
||||
try {
|
||||
JGitUtil.getFileList(git, "master", ".").find(_.name == pageName + ".md").map { file =>
|
||||
WikiPageInfo(file.name, new String(git.getRepository.open(file.id).getBytes, "UTF-8"), file.committer, file.time)
|
||||
JGitUtil.withGit(getWikiRepositoryDir(owner, repository)){ git =>
|
||||
try {
|
||||
JGitUtil.getFileList(git, "master", ".").find(_.name == pageName + ".md").map { file =>
|
||||
WikiPageInfo(file.name, new String(git.getRepository.open(file.id).getBytes, "UTF-8"), file.committer, file.time)
|
||||
}
|
||||
} catch {
|
||||
// TODO no commit, but it should not judge by exception.
|
||||
case e: NullPointerException => None
|
||||
}
|
||||
} catch {
|
||||
// TODO no commit, but it should not judge by exception.
|
||||
case e: NullPointerException => None
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,29 +104,30 @@ object WikiUtil {
|
||||
}
|
||||
|
||||
// write as file
|
||||
val cloned = Git.open(workDir)
|
||||
val file = new File(workDir, newPageName + ".md")
|
||||
val added = if(!file.exists || FileUtils.readFileToString(file, "UTF-8") != content){
|
||||
FileUtils.writeStringToFile(file, content, "UTF-8")
|
||||
cloned.add.addFilepattern(file.getName).call
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
JGitUtil.withGit(workDir){ git =>
|
||||
val file = new File(workDir, newPageName + ".md")
|
||||
val added = if(!file.exists || FileUtils.readFileToString(file, "UTF-8") != content){
|
||||
FileUtils.writeStringToFile(file, content, "UTF-8")
|
||||
git.add.addFilepattern(file.getName).call
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
|
||||
// delete file
|
||||
val deleted = if(currentPageName != "" && currentPageName != newPageName){
|
||||
cloned.rm.addFilepattern(currentPageName + ".md").call
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
// delete file
|
||||
val deleted = if(currentPageName != "" && currentPageName != newPageName){
|
||||
git.rm.addFilepattern(currentPageName + ".md").call
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
|
||||
// commit and push
|
||||
if(added || deleted){
|
||||
// TODO committer's mail address
|
||||
cloned.commit.setAuthor(committer, committer + "@devnull").setMessage(message).call
|
||||
cloned.push.call
|
||||
// commit and push
|
||||
if(added || deleted){
|
||||
// TODO committer's mail address
|
||||
git.commit.setAuthor(committer, committer + "@devnull").setMessage(message).call
|
||||
git.push.call
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,13 +148,14 @@ object WikiUtil {
|
||||
// delete file
|
||||
new File(workDir, pageName + ".md").delete
|
||||
|
||||
val cloned = Git.open(workDir)
|
||||
cloned.rm.addFilepattern(pageName + ".md").call
|
||||
JGitUtil.withGit(workDir){ git =>
|
||||
git.rm.addFilepattern(pageName + ".md").call
|
||||
|
||||
// commit and push
|
||||
// TODO committer's mail address
|
||||
cloned.commit.setAuthor(committer, committer + "@devnull").setMessage(message).call
|
||||
cloned.push.call
|
||||
// commit and push
|
||||
// TODO committer's mail address
|
||||
git.commit.setAuthor(committer, committer + "@devnull").setMessage(message).call
|
||||
git.push.call
|
||||
}
|
||||
}
|
||||
|
||||
def getDiffs(git: Git, commitId1: String, commitId2: String): List[DiffInfo] = {
|
||||
|
||||
Reference in New Issue
Block a user