mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-07 05:55:51 +01:00
Synchronization of modification to the working repository for Wiki.
This commit is contained in:
@@ -8,6 +8,7 @@ import util.JGitUtil.DiffInfo
|
||||
import util.{Directory, JGitUtil}
|
||||
import org.eclipse.jgit.lib.RepositoryBuilder
|
||||
import org.eclipse.jgit.treewalk.CanonicalTreeParser
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
|
||||
object WikiService {
|
||||
|
||||
@@ -31,13 +32,40 @@ object WikiService {
|
||||
*/
|
||||
case class WikiPageHistoryInfo(name: String, committer: String, message: String, date: Date)
|
||||
|
||||
/**
|
||||
* lock objects
|
||||
*/
|
||||
private val locks = new ConcurrentHashMap[String, AnyRef]()
|
||||
|
||||
/**
|
||||
* Returns the lock object for the specified repository.
|
||||
*/
|
||||
private def getLockObject(owner: String, repository: String): AnyRef = synchronized {
|
||||
val key = owner + "/" + repository
|
||||
if(!locks.containsKey(key)){
|
||||
locks.put(key, new AnyRef())
|
||||
}
|
||||
locks.get(key)
|
||||
}
|
||||
|
||||
/**
|
||||
* Synchronizes a given function which modifies the working copy of the wiki repository.
|
||||
*
|
||||
* @param owner the repository owner
|
||||
* @param repository the repository name
|
||||
* @param f the function which modifies the working copy of the wiki repository
|
||||
* @tparam T the return type of the given function
|
||||
* @return the result of the given function
|
||||
*/
|
||||
def lock[T](owner: String, repository: String)(f: => T): T = getLockObject(owner, repository).synchronized(f)
|
||||
|
||||
}
|
||||
|
||||
trait WikiService {
|
||||
import WikiService._
|
||||
|
||||
// TODO synchronized?
|
||||
def createWikiRepository(owner: model.Account, repository: String): Unit = {
|
||||
lock(owner.userName, repository){
|
||||
val dir = Directory.getWikiRepositoryDir(owner.userName, repository)
|
||||
if(!dir.exists){
|
||||
val repo = new RepositoryBuilder().setGitDir(dir).setBare.build
|
||||
@@ -49,6 +77,7 @@ trait WikiService {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the wiki page.
|
||||
@@ -66,6 +95,36 @@ trait WikiService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the content of the specified file.
|
||||
*/
|
||||
def getFileContent(owner: String, repository: String, path: String): Option[Array[Byte]] = {
|
||||
JGitUtil.withGit(Directory.getWikiRepositoryDir(owner, repository)){ git =>
|
||||
try {
|
||||
val index = path.lastIndexOf('/')
|
||||
val parentPath = if(index < 0) "." else path.substring(0, index)
|
||||
val fileName = if(index < 0) path else path.substring(index + 1)
|
||||
|
||||
println("parentPath: " + parentPath)
|
||||
println("fileName: " + fileName)
|
||||
|
||||
JGitUtil.getFileList(git, "master", parentPath).foreach { file =>
|
||||
println("*" + file.name)
|
||||
}
|
||||
|
||||
JGitUtil.getFileList(git, "master", parentPath).find(_.name == fileName).map { file =>
|
||||
git.getRepository.open(file.id).getBytes
|
||||
}
|
||||
} catch {
|
||||
// TODO no commit, but it should not judge by exception.
|
||||
case e: NullPointerException => None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the list of wiki page names.
|
||||
*/
|
||||
def getWikiPageList(owner: String, repository: String): List[String] = {
|
||||
JGitUtil.getFileList(Git.open(Directory.getWikiRepositoryDir(owner, repository)), "master", ".")
|
||||
.filter(_.name.endsWith(".md"))
|
||||
@@ -73,22 +132,16 @@ trait WikiService {
|
||||
.sortBy(x => x)
|
||||
}
|
||||
|
||||
// TODO synchronized
|
||||
/**
|
||||
* Save the wiki page.
|
||||
*/
|
||||
def saveWikiPage(owner: String, repository: String, currentPageName: String, newPageName: String,
|
||||
content: String, committer: model.Account, message: String): Unit = {
|
||||
|
||||
lock(owner, repository){
|
||||
// clone working copy
|
||||
val workDir = Directory.getWikiWorkDir(owner, repository)
|
||||
|
||||
// clone
|
||||
if(!workDir.exists){
|
||||
Git.cloneRepository
|
||||
.setURI(Directory.getWikiRepositoryDir(owner, repository).toURI.toString)
|
||||
.setDirectory(workDir)
|
||||
.call
|
||||
}
|
||||
cloneOrPullWorkingCopy(workDir, owner, repository)
|
||||
|
||||
// write as file
|
||||
JGitUtil.withGit(workDir){ git =>
|
||||
@@ -116,21 +169,16 @@ trait WikiService {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the wiki page.
|
||||
*/
|
||||
def deleteWikiPage(owner: String, repository: String, pageName: String, committer: String, message: String): Unit = {
|
||||
|
||||
lock(owner, repository){
|
||||
// clone working copy
|
||||
val workDir = Directory.getWikiWorkDir(owner, repository)
|
||||
|
||||
// clone
|
||||
if(!workDir.exists){
|
||||
Git.cloneRepository
|
||||
.setURI(Directory.getWikiRepositoryDir(owner, repository).toURI.toString)
|
||||
.setDirectory(workDir)
|
||||
.call
|
||||
}
|
||||
cloneOrPullWorkingCopy(workDir, owner, repository)
|
||||
|
||||
// delete file
|
||||
new File(workDir, pageName + ".md").delete
|
||||
@@ -144,7 +192,11 @@ trait WikiService {
|
||||
git.push.call
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns differences between specified commits.
|
||||
*/
|
||||
def getWikiDiffs(git: Git, commitId1: String, commitId2: String): List[DiffInfo] = {
|
||||
// get diff between specified commit and its previous commit
|
||||
val reader = git.getRepository.newObjectReader
|
||||
@@ -162,4 +214,16 @@ trait WikiService {
|
||||
JGitUtil.getContent(git, diff.getNewId.toObjectId, false).map(new String(_, "UTF-8")))
|
||||
}.toList
|
||||
}
|
||||
|
||||
private def cloneOrPullWorkingCopy(workDir: File, owner: String, repository: String): Unit = {
|
||||
if(!workDir.exists){
|
||||
Git.cloneRepository
|
||||
.setURI(Directory.getWikiRepositoryDir(owner, repository).toURI.toString)
|
||||
.setDirectory(workDir)
|
||||
.call
|
||||
} else {
|
||||
val git = Git.open(workDir).pull()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user