mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-06 13:35:50 +01:00
Refactoring
This commit is contained in:
@@ -126,19 +126,6 @@ trait RepositoryViewerControllerBase extends ControllerBase {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// TODO Share this method with WikiService
|
|
||||||
private def processTree(git: Git, id: ObjectId)(f: (String, CanonicalTreeParser) => Unit) = {
|
|
||||||
using(new RevWalk(git.getRepository)){ revWalk =>
|
|
||||||
using(new TreeWalk(git.getRepository)){ treeWalk =>
|
|
||||||
val index = treeWalk.addTree(revWalk.parseTree(id))
|
|
||||||
treeWalk.setRecursive(true)
|
|
||||||
while(treeWalk.next){
|
|
||||||
f(treeWalk.getPathString, treeWalk.getTree(index, classOf[CanonicalTreeParser]))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
post("/:owner/:repository/edit/*", editorForm)(collaboratorsOnly { (form, repository) =>
|
post("/:owner/:repository/edit/*", editorForm)(collaboratorsOnly { (form, repository) =>
|
||||||
val (id, path) = splitPath(repository, multiParams("splat").head)
|
val (id, path) = splitPath(repository, multiParams("splat").head)
|
||||||
|
|
||||||
@@ -150,7 +137,7 @@ trait RepositoryViewerControllerBase extends ControllerBase {
|
|||||||
val headName = s"refs/heads/${id}"
|
val headName = s"refs/heads/${id}"
|
||||||
val headTip = git.getRepository.resolve(s"refs/heads/${id}")
|
val headTip = git.getRepository.resolve(s"refs/heads/${id}")
|
||||||
|
|
||||||
processTree(git, headTip){ (treePath, tree) =>
|
JGitUtil.processTree(git, headTip){ (treePath, tree) =>
|
||||||
if(treePath != path){
|
if(treePath != path){
|
||||||
builder.add(JGitUtil.createDirCacheEntry(treePath, tree.getEntryFileMode, tree.getEntryObjectId))
|
builder.add(JGitUtil.createDirCacheEntry(treePath, tree.getEntryFileMode, tree.getEntryObjectId))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -175,17 +175,9 @@ trait WikiService {
|
|||||||
val inserter = git.getRepository.newObjectInserter()
|
val inserter = git.getRepository.newObjectInserter()
|
||||||
val headId = git.getRepository.resolve(Constants.HEAD + "^{commit}")
|
val headId = git.getRepository.resolve(Constants.HEAD + "^{commit}")
|
||||||
|
|
||||||
using(new RevWalk(git.getRepository)){ revWalk =>
|
JGitUtil.processTree(git, headId){ (path, tree) =>
|
||||||
using(new TreeWalk(git.getRepository)){ treeWalk =>
|
if(revertInfo.find(x => x.filePath == path).isEmpty){
|
||||||
val index = treeWalk.addTree(revWalk.parseTree(headId))
|
builder.add(JGitUtil.createDirCacheEntry(path, tree.getEntryFileMode, tree.getEntryObjectId))
|
||||||
treeWalk.setRecursive(true)
|
|
||||||
while(treeWalk.next){
|
|
||||||
val path = treeWalk.getPathString
|
|
||||||
val tree = treeWalk.getTree(index, classOf[CanonicalTreeParser])
|
|
||||||
if(revertInfo.find(x => x.filePath == path).isEmpty){
|
|
||||||
builder.add(JGitUtil.createDirCacheEntry(path, tree.getEntryFileMode, tree.getEntryObjectId))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -226,22 +218,14 @@ trait WikiService {
|
|||||||
var removed = false
|
var removed = false
|
||||||
|
|
||||||
if(headId != null){
|
if(headId != null){
|
||||||
using(new RevWalk(git.getRepository)){ revWalk =>
|
JGitUtil.processTree(git, headId){ (path, tree) =>
|
||||||
using(new TreeWalk(git.getRepository)){ treeWalk =>
|
if(path == currentPageName + ".md" && currentPageName != newPageName){
|
||||||
val index = treeWalk.addTree(revWalk.parseTree(headId))
|
removed = true
|
||||||
treeWalk.setRecursive(true)
|
} else if(path != newPageName + ".md"){
|
||||||
while(treeWalk.next){
|
builder.add(JGitUtil.createDirCacheEntry(path, tree.getEntryFileMode, tree.getEntryObjectId))
|
||||||
val path = treeWalk.getPathString
|
} else {
|
||||||
val tree = treeWalk.getTree(index, classOf[CanonicalTreeParser])
|
created = false
|
||||||
if(path == currentPageName + ".md" && currentPageName != newPageName){
|
updated = JGitUtil.getContentFromId(git, tree.getEntryObjectId, true).map(new String(_, "UTF-8") != content).getOrElse(false)
|
||||||
removed = true
|
|
||||||
} else if(path != newPageName + ".md"){
|
|
||||||
builder.add(JGitUtil.createDirCacheEntry(path, tree.getEntryFileMode, tree.getEntryObjectId))
|
|
||||||
} else {
|
|
||||||
created = false
|
|
||||||
updated = JGitUtil.getContentFromId(git, tree.getEntryObjectId, true).map(new String(_, "UTF-8") != content).getOrElse(false)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -280,26 +264,17 @@ trait WikiService {
|
|||||||
val headId = git.getRepository.resolve(Constants.HEAD + "^{commit}")
|
val headId = git.getRepository.resolve(Constants.HEAD + "^{commit}")
|
||||||
var removed = false
|
var removed = false
|
||||||
|
|
||||||
using(new RevWalk(git.getRepository)){ revWalk =>
|
JGitUtil.processTree(git, headId){ (path, tree) =>
|
||||||
using(new TreeWalk(git.getRepository)){ treeWalk =>
|
if(path != pageName + ".md"){
|
||||||
val index = treeWalk.addTree(revWalk.parseTree(headId))
|
builder.add(JGitUtil.createDirCacheEntry(path, tree.getEntryFileMode, tree.getEntryObjectId))
|
||||||
treeWalk.setRecursive(true)
|
} else {
|
||||||
while(treeWalk.next){
|
removed = true
|
||||||
val path = treeWalk.getPathString
|
|
||||||
val tree = treeWalk.getTree(index, classOf[CanonicalTreeParser])
|
|
||||||
if(path != pageName + ".md"){
|
|
||||||
builder.add(JGitUtil.createDirCacheEntry(path, tree.getEntryFileMode, tree.getEntryObjectId))
|
|
||||||
} else {
|
|
||||||
removed = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(removed){
|
|
||||||
builder.finish()
|
|
||||||
JGitUtil.createNewCommit(git, inserter, headId, builder.getDirCache.writeTree(inserter), committer, mailAddress, message)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if(removed){
|
||||||
|
builder.finish()
|
||||||
|
JGitUtil.createNewCommit(git, inserter, headId, builder.getDirCache.writeTree(inserter), committer, mailAddress, message)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,15 +37,4 @@ object ControlUtil {
|
|||||||
def using[T](treeWalk: TreeWalk)(f: TreeWalk => T): T =
|
def using[T](treeWalk: TreeWalk)(f: TreeWalk => T): T =
|
||||||
try f(treeWalk) finally treeWalk.release()
|
try f(treeWalk) finally treeWalk.release()
|
||||||
|
|
||||||
|
|
||||||
// def withTmpRefSpec[T](ref: RefSpec, git: Git)(f: RefSpec => T): T = {
|
|
||||||
// try {
|
|
||||||
// f(ref)
|
|
||||||
// } finally {
|
|
||||||
// val refUpdate = git.getRepository.updateRef(ref.getDestination)
|
|
||||||
// refUpdate.setForceUpdate(true)
|
|
||||||
// refUpdate.delete()
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,24 +31,6 @@ object FileUtil {
|
|||||||
|
|
||||||
def isText(content: Array[Byte]): Boolean = !content.contains(0)
|
def isText(content: Array[Byte]): Boolean = !content.contains(0)
|
||||||
|
|
||||||
// def createZipFile(dest: File, dir: File): Unit = {
|
|
||||||
// def addDirectoryToZip(out: ZipArchiveOutputStream, dir: File, path: String): Unit = {
|
|
||||||
// dir.listFiles.map { file =>
|
|
||||||
// if(file.isFile){
|
|
||||||
// out.putArchiveEntry(new ZipArchiveEntry(path + "/" + file.getName))
|
|
||||||
// out.write(FileUtils.readFileToByteArray(file))
|
|
||||||
// out.closeArchiveEntry
|
|
||||||
// } else if(file.isDirectory){
|
|
||||||
// addDirectoryToZip(out, file, path + "/" + file.getName)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// using(new ZipArchiveOutputStream(dest)){ out =>
|
|
||||||
// addDirectoryToZip(out, dir, dir.getName)
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
def getFileName(path: String): String = defining(path.lastIndexOf('/')){ i =>
|
def getFileName(path: String): String = defining(path.lastIndexOf('/')){ i =>
|
||||||
if(i >= 0) path.substring(i + 1) else path
|
if(i >= 0) path.substring(i + 1) else path
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -585,4 +585,17 @@ object JGitUtil {
|
|||||||
existIds.toSeq
|
existIds.toSeq
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def processTree(git: Git, id: ObjectId)(f: (String, CanonicalTreeParser) => Unit) = {
|
||||||
|
using(new RevWalk(git.getRepository)){ revWalk =>
|
||||||
|
using(new TreeWalk(git.getRepository)){ treeWalk =>
|
||||||
|
val index = treeWalk.addTree(revWalk.parseTree(id))
|
||||||
|
treeWalk.setRecursive(true)
|
||||||
|
while(treeWalk.next){
|
||||||
|
f(treeWalk.getPathString, treeWalk.getTree(index, classOf[CanonicalTreeParser]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user