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