mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-07 22:15:51 +01:00
Fixed Scaladoc mistake.
This commit is contained in:
@@ -1,235 +1,235 @@
|
|||||||
package util
|
package util
|
||||||
|
|
||||||
import org.eclipse.jgit.api.Git
|
import org.eclipse.jgit.api.Git
|
||||||
import app.{RepositoryInfo, FileInfo, CommitInfo, DiffInfo}
|
import app.{RepositoryInfo, FileInfo, CommitInfo, DiffInfo}
|
||||||
import util.Directory._
|
import util.Directory._
|
||||||
import scala.collection.JavaConverters._
|
import scala.collection.JavaConverters._
|
||||||
import javax.servlet.ServletContext
|
import javax.servlet.ServletContext
|
||||||
import org.eclipse.jgit.lib.Ref
|
import org.eclipse.jgit.lib.Ref
|
||||||
import org.eclipse.jgit.lib.ObjectId
|
import org.eclipse.jgit.lib.ObjectId
|
||||||
import org.eclipse.jgit.errors.MissingObjectException
|
import org.eclipse.jgit.errors.MissingObjectException
|
||||||
import org.eclipse.jgit.revwalk.RevCommit
|
import org.eclipse.jgit.revwalk.RevCommit
|
||||||
import org.eclipse.jgit.diff.DiffFormatter
|
import org.eclipse.jgit.diff.DiffFormatter
|
||||||
import org.eclipse.jgit.treewalk.TreeWalk
|
import org.eclipse.jgit.treewalk.TreeWalk
|
||||||
import org.eclipse.jgit.revwalk.RevWalk
|
import org.eclipse.jgit.revwalk.RevWalk
|
||||||
import org.eclipse.jgit.diff.RawTextComparator
|
import org.eclipse.jgit.diff.RawTextComparator
|
||||||
import org.eclipse.jgit.util.io.DisabledOutputStream
|
import org.eclipse.jgit.util.io.DisabledOutputStream
|
||||||
import org.eclipse.jgit.lib.Repository
|
import org.eclipse.jgit.lib.Repository
|
||||||
import org.eclipse.jgit.revwalk.RevSort
|
import org.eclipse.jgit.revwalk.RevSort
|
||||||
import org.eclipse.jgit.diff.DiffEntry.ChangeType
|
import org.eclipse.jgit.diff.DiffEntry.ChangeType
|
||||||
import org.eclipse.jgit.lib.FileMode
|
import org.eclipse.jgit.lib.FileMode
|
||||||
import org.eclipse.jgit.treewalk.filter.PathFilter
|
import org.eclipse.jgit.treewalk.filter.PathFilter
|
||||||
import org.eclipse.jgit.treewalk.CanonicalTreeParser
|
import org.eclipse.jgit.treewalk.CanonicalTreeParser
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides complex JGit operations.
|
* Provides complex JGit operations.
|
||||||
*/
|
*/
|
||||||
object JGitUtil {
|
object JGitUtil {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the repository information. It contains branch names and tag names.
|
* Returns the repository information. It contains branch names and tag names.
|
||||||
*/
|
*/
|
||||||
def getRepositoryInfo(owner: String, repository: String, servletContext: ServletContext): RepositoryInfo = {
|
def getRepositoryInfo(owner: String, repository: String, servletContext: ServletContext): RepositoryInfo = {
|
||||||
val git = Git.open(getRepositoryDir(owner, repository))
|
val git = Git.open(getRepositoryDir(owner, repository))
|
||||||
RepositoryInfo(
|
RepositoryInfo(
|
||||||
owner, repository, "http://localhost:8080%s/git/%s/%s.git".format(servletContext.getContextPath, owner, repository),
|
owner, repository, "http://localhost:8080%s/git/%s/%s.git".format(servletContext.getContextPath, owner, repository),
|
||||||
// branches
|
// branches
|
||||||
git.branchList.call.toArray.map { ref =>
|
git.branchList.call.toArray.map { ref =>
|
||||||
ref.asInstanceOf[Ref].getName.replaceFirst("^refs/heads/", "")
|
ref.asInstanceOf[Ref].getName.replaceFirst("^refs/heads/", "")
|
||||||
}.toList,
|
}.toList,
|
||||||
// tags
|
// tags
|
||||||
git.tagList.call.toArray.map { ref =>
|
git.tagList.call.toArray.map { ref =>
|
||||||
ref.asInstanceOf[Ref].getName
|
ref.asInstanceOf[Ref].getName
|
||||||
}.toList
|
}.toList
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the file list of the specified path.
|
* Returns the file list of the specified path.
|
||||||
*
|
|
||||||
* @param git the Git object
|
|
||||||
* @param revision the branch name or commit id
|
|
||||||
* @param path the directory path (optional)
|
|
||||||
* @return HTML of the file list
|
|
||||||
*/
|
|
||||||
def getFileList(git: Git, revision: String, path: String = "."): List[FileInfo] = {
|
|
||||||
val revWalk = new RevWalk(git.getRepository)
|
|
||||||
val objectId = git.getRepository.resolve(revision)
|
|
||||||
val revCommit = revWalk.parseCommit(objectId)
|
|
||||||
|
|
||||||
val treeWalk = new TreeWalk(git.getRepository)
|
|
||||||
treeWalk.addTree(revCommit.getTree)
|
|
||||||
if(path != "."){
|
|
||||||
treeWalk.setRecursive(true)
|
|
||||||
treeWalk.setFilter(PathFilter.create(path))
|
|
||||||
}
|
|
||||||
|
|
||||||
val list = new scala.collection.mutable.ListBuffer[FileInfo]
|
|
||||||
|
|
||||||
while (treeWalk.next()) {
|
|
||||||
val fileCommit = JGitUtil.getLatestCommitFromPath(git, treeWalk.getPathString, revision)
|
|
||||||
list.append(FileInfo(
|
|
||||||
treeWalk.getObjectId(0),
|
|
||||||
treeWalk.getFileMode(0) == FileMode.TREE,
|
|
||||||
treeWalk.getNameString,
|
|
||||||
fileCommit.getCommitterIdent.getWhen,
|
|
||||||
fileCommit.getShortMessage,
|
|
||||||
fileCommit.getCommitterIdent.getName)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
treeWalk.release
|
|
||||||
revWalk.dispose
|
|
||||||
|
|
||||||
list.toList.sortWith { (file1, file2) => (file1.isDirectory, file2.isDirectory) match {
|
|
||||||
case (true , false) => true
|
|
||||||
case (false, true ) => false
|
|
||||||
case _ => file1.name.compareTo(file2.name) < 0
|
|
||||||
}}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the commit list of the specified branch.
|
|
||||||
*
|
|
||||||
* @param git the Git object
|
|
||||||
* @param revision the branch name or commit id
|
|
||||||
* @param page the page number (1-)
|
|
||||||
* @return a tuple of the commit list and whether has next
|
|
||||||
*/
|
|
||||||
def getCommitLog(git: Git, revision: String, page: Int): (List[CommitInfo], Boolean) = {
|
|
||||||
@scala.annotation.tailrec
|
|
||||||
def getCommitLog(i: java.util.Iterator[RevCommit], count: Int, logs: List[CommitInfo]): (List[CommitInfo], Boolean) =
|
|
||||||
i.hasNext match {
|
|
||||||
case true if(logs.size < 30) => getCommitLog(i, count + 1, if((page - 1) * 30 < count) logs :+ new CommitInfo(i.next) else logs)
|
|
||||||
case _ => (logs, i.hasNext)
|
|
||||||
}
|
|
||||||
|
|
||||||
val revWalk = new RevWalk(git.getRepository)
|
|
||||||
revWalk.markStart(revWalk.parseCommit(git.getRepository.resolve(revision)))
|
|
||||||
|
|
||||||
val commits = getCommitLog(revWalk.iterator, 0, Nil)
|
|
||||||
revWalk.release
|
|
||||||
|
|
||||||
commits
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the latest RevCommit of the specified path.
|
|
||||||
*
|
*
|
||||||
* @param git the Git object
|
* @param git the Git object
|
||||||
* @param path the path
|
* @param revision the branch name or commit id
|
||||||
* @param revision the branch name or commit id
|
* @param path the directory path (optional)
|
||||||
* @return the latest commit
|
* @return HTML of the file list
|
||||||
*/
|
*/
|
||||||
def getLatestCommitFromPath(git: Git, path: String, revision: String): RevCommit = {
|
def getFileList(git: Git, revision: String, path: String = "."): List[FileInfo] = {
|
||||||
val revWalk = new RevWalk(git.getRepository)
|
val revWalk = new RevWalk(git.getRepository)
|
||||||
revWalk.markStart(revWalk.parseCommit(git.getRepository.resolve(revision)))
|
val objectId = git.getRepository.resolve(revision)
|
||||||
revWalk.sort(RevSort.REVERSE);
|
val revCommit = revWalk.parseCommit(objectId)
|
||||||
val i = revWalk.iterator
|
|
||||||
|
val treeWalk = new TreeWalk(git.getRepository)
|
||||||
// TODO DON'T use var!
|
treeWalk.addTree(revCommit.getTree)
|
||||||
var result: RevCommit = null
|
if(path != "."){
|
||||||
|
treeWalk.setRecursive(true)
|
||||||
while(i.hasNext){
|
treeWalk.setFilter(PathFilter.create(path))
|
||||||
val commit = i.next
|
}
|
||||||
if(commit.getParentCount == 0){
|
|
||||||
// Initial commit
|
val list = new scala.collection.mutable.ListBuffer[FileInfo]
|
||||||
val treeWalk = new TreeWalk(git.getRepository)
|
|
||||||
treeWalk.reset()
|
while (treeWalk.next()) {
|
||||||
treeWalk.setRecursive(true)
|
val fileCommit = JGitUtil.getLatestCommitFromPath(git, treeWalk.getPathString, revision)
|
||||||
treeWalk.addTree(commit.getTree)
|
list.append(FileInfo(
|
||||||
while (treeWalk.next && result == null) {
|
treeWalk.getObjectId(0),
|
||||||
if(treeWalk.getPathString.startsWith(path)){
|
treeWalk.getFileMode(0) == FileMode.TREE,
|
||||||
result = commit
|
treeWalk.getNameString,
|
||||||
}
|
fileCommit.getCommitterIdent.getWhen,
|
||||||
}
|
fileCommit.getShortMessage,
|
||||||
treeWalk.release
|
fileCommit.getCommitterIdent.getName)
|
||||||
} else {
|
)
|
||||||
val parent = revWalk.parseCommit(commit.getParent(0).getId())
|
}
|
||||||
val df = new DiffFormatter(DisabledOutputStream.INSTANCE)
|
|
||||||
df.setRepository(git.getRepository)
|
treeWalk.release
|
||||||
df.setDiffComparator(RawTextComparator.DEFAULT)
|
revWalk.dispose
|
||||||
df.setDetectRenames(true)
|
|
||||||
val diffs = df.scan(parent.getTree(), commit.getTree)
|
list.toList.sortWith { (file1, file2) => (file1.isDirectory, file2.isDirectory) match {
|
||||||
val find = diffs.asScala.find { diff =>
|
case (true , false) => true
|
||||||
val objectId = diff.getNewId.name
|
case (false, true ) => false
|
||||||
(diff.getChangeType != ChangeType.DELETE && diff.getNewPath.startsWith(path))
|
case _ => file1.name.compareTo(file2.name) < 0
|
||||||
}
|
}}
|
||||||
if(find != None){
|
}
|
||||||
result = commit
|
|
||||||
}
|
/**
|
||||||
}
|
* Returns the commit list of the specified branch.
|
||||||
revWalk.release
|
*
|
||||||
}
|
* @param git the Git object
|
||||||
result
|
* @param revision the branch name or commit id
|
||||||
}
|
* @param page the page number (1-)
|
||||||
|
* @return a tuple of the commit list and whether has next
|
||||||
/**
|
*/
|
||||||
* Get object content of the given id as String from the Git repository.
|
def getCommitLog(git: Git, revision: String, page: Int): (List[CommitInfo], Boolean) = {
|
||||||
*
|
@scala.annotation.tailrec
|
||||||
* @param git the Git object
|
def getCommitLog(i: java.util.Iterator[RevCommit], count: Int, logs: List[CommitInfo]): (List[CommitInfo], Boolean) =
|
||||||
* @param id the object id
|
i.hasNext match {
|
||||||
* @param large if true then returns None for the large file
|
case true if(logs.size < 30) => getCommitLog(i, count + 1, if((page - 1) * 30 < count) logs :+ new CommitInfo(i.next) else logs)
|
||||||
* @return the object or None if object does not exist
|
case _ => (logs, i.hasNext)
|
||||||
*/
|
}
|
||||||
def getContent(git: Git, id: ObjectId, large: Boolean): Option[Array[Byte]] = try {
|
|
||||||
val loader = git.getRepository.getObjectDatabase.open(id)
|
val revWalk = new RevWalk(git.getRepository)
|
||||||
if(large == false && FileTypeUtil.isLarge(loader.getSize)){
|
revWalk.markStart(revWalk.parseCommit(git.getRepository.resolve(revision)))
|
||||||
None
|
|
||||||
} else {
|
val commits = getCommitLog(revWalk.iterator, 0, Nil)
|
||||||
Some(git.getRepository.getObjectDatabase.open(id).getBytes)
|
revWalk.release
|
||||||
}
|
|
||||||
} catch {
|
commits
|
||||||
case e: MissingObjectException => None
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
def getDiffs(git: Git, id: String): List[DiffInfo] = {
|
* Returns the latest RevCommit of the specified path.
|
||||||
@scala.annotation.tailrec
|
*
|
||||||
def getCommitLog(i: java.util.Iterator[RevCommit], logs: List[RevCommit]): List[RevCommit] =
|
* @param git the Git object
|
||||||
i.hasNext match {
|
* @param path the path
|
||||||
case true if(logs.size < 2) => getCommitLog(i, logs :+ i.next)
|
* @param revision the branch name or commit id
|
||||||
case _ => logs
|
* @return the latest commit
|
||||||
}
|
*/
|
||||||
|
def getLatestCommitFromPath(git: Git, path: String, revision: String): RevCommit = {
|
||||||
val revWalk = new RevWalk(git.getRepository)
|
val revWalk = new RevWalk(git.getRepository)
|
||||||
revWalk.markStart(revWalk.parseCommit(git.getRepository.resolve(id)))
|
revWalk.markStart(revWalk.parseCommit(git.getRepository.resolve(revision)))
|
||||||
|
revWalk.sort(RevSort.REVERSE);
|
||||||
val commits = getCommitLog(revWalk.iterator, Nil)
|
val i = revWalk.iterator
|
||||||
revWalk.release
|
|
||||||
|
// TODO DON'T use var!
|
||||||
val revCommit = commits(0)
|
var result: RevCommit = null
|
||||||
|
|
||||||
if(commits.length >= 2){
|
while(i.hasNext){
|
||||||
// not initial commit
|
val commit = i.next
|
||||||
val oldCommit = commits(1)
|
if(commit.getParentCount == 0){
|
||||||
|
// Initial commit
|
||||||
// get diff between specified commit and its previous commit
|
val treeWalk = new TreeWalk(git.getRepository)
|
||||||
val reader = git.getRepository.newObjectReader
|
treeWalk.reset()
|
||||||
|
treeWalk.setRecursive(true)
|
||||||
val oldTreeIter = new CanonicalTreeParser
|
treeWalk.addTree(commit.getTree)
|
||||||
oldTreeIter.reset(reader, git.getRepository.resolve(oldCommit.name + "^{tree}"))
|
while (treeWalk.next && result == null) {
|
||||||
|
if(treeWalk.getPathString.startsWith(path)){
|
||||||
val newTreeIter = new CanonicalTreeParser
|
result = commit
|
||||||
newTreeIter.reset(reader, git.getRepository.resolve(id + "^{tree}"))
|
}
|
||||||
|
}
|
||||||
import scala.collection.JavaConverters._
|
treeWalk.release
|
||||||
git.diff.setNewTree(newTreeIter).setOldTree(oldTreeIter).call.asScala.map { diff =>
|
} else {
|
||||||
DiffInfo(diff.getChangeType, diff.getOldPath, diff.getNewPath,
|
val parent = revWalk.parseCommit(commit.getParent(0).getId())
|
||||||
JGitUtil.getContent(git, diff.getOldId.toObjectId, false).map(new String(_, "UTF-8")),
|
val df = new DiffFormatter(DisabledOutputStream.INSTANCE)
|
||||||
JGitUtil.getContent(git, diff.getNewId.toObjectId, false).map(new String(_, "UTF-8")))
|
df.setRepository(git.getRepository)
|
||||||
}.toList
|
df.setDiffComparator(RawTextComparator.DEFAULT)
|
||||||
} else {
|
df.setDetectRenames(true)
|
||||||
// initial commit
|
val diffs = df.scan(parent.getTree(), commit.getTree)
|
||||||
val walk = new TreeWalk(git.getRepository)
|
val find = diffs.asScala.find { diff =>
|
||||||
walk.addTree(revCommit.getTree)
|
val objectId = diff.getNewId.name
|
||||||
val buffer = new scala.collection.mutable.ListBuffer[DiffInfo]()
|
(diff.getChangeType != ChangeType.DELETE && diff.getNewPath.startsWith(path))
|
||||||
while(walk.next){
|
}
|
||||||
buffer.append(DiffInfo(ChangeType.ADD, null, walk.getPathString, None,
|
if(find != None){
|
||||||
JGitUtil.getContent(git, walk.getObjectId(0), false).map(new String(_, "UTF-8"))))
|
result = commit
|
||||||
}
|
}
|
||||||
walk.release
|
}
|
||||||
buffer.toList
|
revWalk.release
|
||||||
}
|
}
|
||||||
}
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get object content of the given id as String from the Git repository.
|
||||||
|
*
|
||||||
|
* @param git the Git object
|
||||||
|
* @param id the object id
|
||||||
|
* @param large if false then returns None for the large file
|
||||||
|
* @return the object or None if object does not exist
|
||||||
|
*/
|
||||||
|
def getContent(git: Git, id: ObjectId, large: Boolean): Option[Array[Byte]] = try {
|
||||||
|
val loader = git.getRepository.getObjectDatabase.open(id)
|
||||||
|
if(large == false && FileTypeUtil.isLarge(loader.getSize)){
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(git.getRepository.getObjectDatabase.open(id).getBytes)
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
case e: MissingObjectException => None
|
||||||
|
}
|
||||||
|
|
||||||
|
def getDiffs(git: Git, id: String): List[DiffInfo] = {
|
||||||
|
@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 revCommit = commits(0)
|
||||||
|
|
||||||
|
if(commits.length >= 2){
|
||||||
|
// not initial commit
|
||||||
|
val oldCommit = commits(1)
|
||||||
|
|
||||||
|
// get diff between specified commit and its previous commit
|
||||||
|
val reader = git.getRepository.newObjectReader
|
||||||
|
|
||||||
|
val oldTreeIter = new CanonicalTreeParser
|
||||||
|
oldTreeIter.reset(reader, git.getRepository.resolve(oldCommit.name + "^{tree}"))
|
||||||
|
|
||||||
|
val newTreeIter = new CanonicalTreeParser
|
||||||
|
newTreeIter.reset(reader, git.getRepository.resolve(id + "^{tree}"))
|
||||||
|
|
||||||
|
import scala.collection.JavaConverters._
|
||||||
|
git.diff.setNewTree(newTreeIter).setOldTree(oldTreeIter).call.asScala.map { diff =>
|
||||||
|
DiffInfo(diff.getChangeType, diff.getOldPath, diff.getNewPath,
|
||||||
|
JGitUtil.getContent(git, diff.getOldId.toObjectId, false).map(new String(_, "UTF-8")),
|
||||||
|
JGitUtil.getContent(git, diff.getNewId.toObjectId, false).map(new String(_, "UTF-8")))
|
||||||
|
}.toList
|
||||||
|
} else {
|
||||||
|
// initial commit
|
||||||
|
val walk = new TreeWalk(git.getRepository)
|
||||||
|
walk.addTree(revCommit.getTree)
|
||||||
|
val buffer = new scala.collection.mutable.ListBuffer[DiffInfo]()
|
||||||
|
while(walk.next){
|
||||||
|
buffer.append(DiffInfo(ChangeType.ADD, null, walk.getPathString, None,
|
||||||
|
JGitUtil.getContent(git, walk.getObjectId(0), false).map(new String(_, "UTF-8"))))
|
||||||
|
}
|
||||||
|
walk.release
|
||||||
|
buffer.toList
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user