Use ControlUtil#using() to handle RevWalk.

This commit is contained in:
takezoe
2013-09-21 22:21:59 +09:00
parent e89b2020a3
commit c00b704843
2 changed files with 69 additions and 82 deletions

View File

@@ -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

View File

@@ -241,25 +241,23 @@ 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 {
revWalk.markStart(revWalk.parseCommit(objectId)) revWalk.markStart(revWalk.parseCommit(objectId))
if(path.nonEmpty){ if(path.nonEmpty){
revWalk.setRevFilter(new RevFilter(){ revWalk.setRevFilter(new RevFilter(){
def include(walk: RevWalk, commit: RevCommit): Boolean = { def include(walk: RevWalk, commit: RevCommit): Boolean = {
getDiffs(git, commit.getName, false)._1.find(_.newPath == path).nonEmpty getDiffs(git, commit.getName, false)._1.find(_.newPath == path).nonEmpty
}
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,34 +355,32 @@ 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) val revCommit = commits(0)
revWalk.release
val revCommit = commits(0)
if(commits.length >= 2){
// not initial commit
val oldCommit = commits(1)
(getDiffs(git, oldCommit.getName, id, fetchContent), Some(oldCommit.getName))
} else { if(commits.length >= 2){
// initial commit // not initial commit
val walk = new TreeWalk(git.getRepository) val oldCommit = commits(1)
walk.addTree(revCommit.getTree) (getDiffs(git, oldCommit.getName, id, fetchContent), Some(oldCommit.getName))
val buffer = new scala.collection.mutable.ListBuffer[DiffInfo]()
while(walk.next){ } else {
buffer.append((if(!fetchContent){ // initial commit
DiffInfo(ChangeType.ADD, null, walk.getPathString, None, None) val treeWalk = new TreeWalk(git.getRepository)
} else { treeWalk.addTree(revCommit.getTree)
DiffInfo(ChangeType.ADD, null, walk.getPathString, None, val buffer = new scala.collection.mutable.ListBuffer[DiffInfo]()
JGitUtil.getContent(git, walk.getObjectId(0), false).filter(FileUtil.isText).map(convertFromByteArray)) while(treeWalk.next){
})) buffer.append((if(!fetchContent){
DiffInfo(ChangeType.ADD, null, treeWalk.getPathString, None, None)
} else {
DiffInfo(ChangeType.ADD, null, treeWalk.getPathString, None,
JGitUtil.getContent(git, treeWalk.getObjectId(0), false).filter(FileUtil.isText).map(convertFromByteArray))
}))
}
treeWalk.release
(buffer.toList, None)
} }
walk.release
(buffer.toList, None)
} }
} }
@@ -415,40 +408,30 @@ 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 =>
(e.getKey.startsWith(Constants.R_HEADS) && revWalk.isMergedInto(commit, revWalk.parseCommit(e.getValue.getObjectId)))
git.getRepository.getAllRefs.entrySet.asScala.filter { e => }.map { e =>
(e.getKey.startsWith(Constants.R_HEADS) && walk.isMergedInto(commit, walk.parseCommit(e.getValue.getObjectId))) e.getValue.getName.substring(org.eclipse.jgit.lib.Constants.R_HEADS.length)
}.map { e => }.toList.sorted
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. * 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 =>
(e.getKey.startsWith(Constants.R_TAGS) && revWalk.isMergedInto(commit, revWalk.parseCommit(e.getValue.getObjectId)))
git.getRepository.getAllRefs.entrySet.asScala.filter { e => }.map { e =>
(e.getKey.startsWith(Constants.R_TAGS) && walk.isMergedInto(commit, walk.parseCommit(e.getValue.getObjectId))) e.getValue.getName.substring(org.eclipse.jgit.lib.Constants.R_TAGS.length)
}.map { e => }.toList.sorted.reverse
e.getValue.getName.substring(org.eclipse.jgit.lib.Constants.R_TAGS.length) }
}.toList.sorted.reverse
} finally {
walk.release
} }
}
def initRepository(dir: java.io.File): Unit = def initRepository(dir: java.io.File): Unit =
using(new RepositoryBuilder().setGitDir(dir).setBare.build){ repository => using(new RepositoryBuilder().setGitDir(dir).setBare.build){ repository =>