mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-05 21:15:49 +01:00
Use ControlUtil#using() to handle RevWalk.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package util
|
||||
|
||||
import org.eclipse.jgit.api.Git
|
||||
import org.eclipse.jgit.revwalk.DepthWalk.RevWalk
|
||||
|
||||
/**
|
||||
* Provides control facilities.
|
||||
@@ -22,10 +23,6 @@ object ControlUtil {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this method to use the Git object.
|
||||
* Repository resources are released certainly after processing.
|
||||
*/
|
||||
def using[T](git: Git)(f: Git => T): T =
|
||||
try {
|
||||
f(git)
|
||||
@@ -33,6 +30,13 @@ object ControlUtil {
|
||||
git.getRepository.close
|
||||
}
|
||||
|
||||
def using[T](revWalk: RevWalk)(f: RevWalk => T): T =
|
||||
try {
|
||||
f(revWalk)
|
||||
} finally {
|
||||
revWalk.release()
|
||||
}
|
||||
|
||||
def executeIf(condition: => Boolean)(action: => Unit): Boolean =
|
||||
if(condition){
|
||||
action
|
||||
|
||||
@@ -241,8 +241,8 @@ object JGitUtil {
|
||||
case _ => (logs, i.hasNext)
|
||||
}
|
||||
|
||||
val revWalk = new RevWalk(git.getRepository)
|
||||
val objectId = git.getRepository.resolve(revision)
|
||||
using(new RevWalk(git.getRepository)){ revWalk =>
|
||||
defining(git.getRepository.resolve(revision)){ objectId =>
|
||||
if(objectId == null){
|
||||
Left(s"${revision} can't be resolved.")
|
||||
} else {
|
||||
@@ -255,11 +255,9 @@ object JGitUtil {
|
||||
override def clone(): RevFilter = this
|
||||
})
|
||||
}
|
||||
|
||||
val commits = getCommitLog(revWalk.iterator, 0, Nil)
|
||||
revWalk.release
|
||||
|
||||
Right(commits)
|
||||
Right(getCommitLog(revWalk.iterator, 0, Nil))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -279,13 +277,10 @@ object JGitUtil {
|
||||
case false => logs
|
||||
}
|
||||
|
||||
val revWalk = new RevWalk(git.getRepository)
|
||||
using(new RevWalk(git.getRepository)){ revWalk =>
|
||||
revWalk.markStart(revWalk.parseCommit(git.getRepository.resolve(begin)))
|
||||
|
||||
val commits = getCommitLog(revWalk.iterator, Nil)
|
||||
revWalk.release
|
||||
|
||||
commits.reverse
|
||||
getCommitLog(revWalk.iterator, Nil).reverse
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -360,12 +355,9 @@ object JGitUtil {
|
||||
case _ => logs
|
||||
}
|
||||
|
||||
val revWalk = new RevWalk(git.getRepository)
|
||||
using(new RevWalk(git.getRepository)){ revWalk =>
|
||||
revWalk.markStart(revWalk.parseCommit(git.getRepository.resolve(id)))
|
||||
|
||||
val commits = getCommitLog(revWalk.iterator, Nil)
|
||||
revWalk.release
|
||||
|
||||
val revCommit = commits(0)
|
||||
|
||||
if(commits.length >= 2){
|
||||
@@ -375,21 +367,22 @@ object JGitUtil {
|
||||
|
||||
} else {
|
||||
// initial commit
|
||||
val walk = new TreeWalk(git.getRepository)
|
||||
walk.addTree(revCommit.getTree)
|
||||
val treeWalk = new TreeWalk(git.getRepository)
|
||||
treeWalk.addTree(revCommit.getTree)
|
||||
val buffer = new scala.collection.mutable.ListBuffer[DiffInfo]()
|
||||
while(walk.next){
|
||||
while(treeWalk.next){
|
||||
buffer.append((if(!fetchContent){
|
||||
DiffInfo(ChangeType.ADD, null, walk.getPathString, None, None)
|
||||
DiffInfo(ChangeType.ADD, null, treeWalk.getPathString, None, None)
|
||||
} else {
|
||||
DiffInfo(ChangeType.ADD, null, walk.getPathString, None,
|
||||
JGitUtil.getContent(git, walk.getObjectId(0), false).filter(FileUtil.isText).map(convertFromByteArray))
|
||||
DiffInfo(ChangeType.ADD, null, treeWalk.getPathString, None,
|
||||
JGitUtil.getContent(git, treeWalk.getObjectId(0), false).filter(FileUtil.isText).map(convertFromByteArray))
|
||||
}))
|
||||
}
|
||||
walk.release
|
||||
treeWalk.release
|
||||
(buffer.toList, None)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def getDiffs(git: Git, from: String, to: String, fetchContent: Boolean): List[DiffInfo] = {
|
||||
val reader = git.getRepository.newObjectReader
|
||||
@@ -415,38 +408,28 @@ object JGitUtil {
|
||||
/**
|
||||
* Returns the list of branch names of the specified commit.
|
||||
*/
|
||||
def getBranchesOfCommit(git: Git, commitId: String): List[String] = {
|
||||
val walk = new org.eclipse.jgit.revwalk.RevWalk(git.getRepository)
|
||||
try {
|
||||
val commit = walk.parseCommit(git.getRepository.resolve(commitId + "^0"))
|
||||
|
||||
def getBranchesOfCommit(git: Git, commitId: String): List[String] =
|
||||
using(new RevWalk(git.getRepository)){ revWalk =>
|
||||
defining(revWalk.parseCommit(git.getRepository.resolve(commitId + "^0"))){ commit =>
|
||||
git.getRepository.getAllRefs.entrySet.asScala.filter { e =>
|
||||
(e.getKey.startsWith(Constants.R_HEADS) && walk.isMergedInto(commit, walk.parseCommit(e.getValue.getObjectId)))
|
||||
(e.getKey.startsWith(Constants.R_HEADS) && revWalk.isMergedInto(commit, revWalk.parseCommit(e.getValue.getObjectId)))
|
||||
}.map { e =>
|
||||
e.getValue.getName.substring(org.eclipse.jgit.lib.Constants.R_HEADS.length)
|
||||
}.toList.sorted
|
||||
|
||||
} finally {
|
||||
walk.release
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the list of tags of the specified commit.
|
||||
*/
|
||||
def getTagsOfCommit(git: Git, commitId: String): List[String] = {
|
||||
val walk = new org.eclipse.jgit.revwalk.RevWalk(git.getRepository)
|
||||
try {
|
||||
val commit = walk.parseCommit(git.getRepository.resolve(commitId + "^0"))
|
||||
|
||||
def getTagsOfCommit(git: Git, commitId: String): List[String] =
|
||||
using(new RevWalk(git.getRepository)){ revWalk =>
|
||||
defining(revWalk.parseCommit(git.getRepository.resolve(commitId + "^0"))){ commit =>
|
||||
git.getRepository.getAllRefs.entrySet.asScala.filter { e =>
|
||||
(e.getKey.startsWith(Constants.R_TAGS) && walk.isMergedInto(commit, walk.parseCommit(e.getValue.getObjectId)))
|
||||
(e.getKey.startsWith(Constants.R_TAGS) && revWalk.isMergedInto(commit, revWalk.parseCommit(e.getValue.getObjectId)))
|
||||
}.map { e =>
|
||||
e.getValue.getName.substring(org.eclipse.jgit.lib.Constants.R_TAGS.length)
|
||||
}.toList.sorted.reverse
|
||||
|
||||
} finally {
|
||||
walk.release
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user