Remove some functions from ControlUtil.

This commit is contained in:
takezoe
2013-12-15 04:21:39 +09:00
parent ac884bd7c3
commit 0e9bf59c0f
7 changed files with 34 additions and 38 deletions

View File

@@ -338,12 +338,12 @@ trait PullRequestsControllerBase extends ControllerBase {
using(Git.open(getRepositoryDir(requestUserName, requestRepositoryName))) { git =>
val remoteRefName = s"refs/heads/${branch}"
val tmpRefName = s"refs/merge-check/${userName}/${branch}"
withTmpRefSpec(new RefSpec(s"${remoteRefName}:${tmpRefName}").setForceUpdate(true), git) { ref =>
val refSpec = new RefSpec(s"${remoteRefName}:${tmpRefName}").setForceUpdate(true)
try {
// fetch objects from origin repository branch
git.fetch
.setRemote(getRepositoryDir(userName, repositoryName).toURI.toString)
.setRefSpecs(ref)
.setRefSpecs(refSpec)
.call
// merge conflict check
@@ -355,6 +355,10 @@ trait PullRequestsControllerBase extends ControllerBase {
} catch {
case e: NoMergeBaseException => true
}
} finally {
val refUpdate = git.getRepository.updateRef(refSpec.getDestination)
refUpdate.setForceUpdate(true)
refUpdate.delete()
}
}
}

View File

@@ -188,16 +188,16 @@ trait WikiControllerBase extends ControllerBase with FlashMapSupport {
private def conflictForNew: Constraint = new Constraint(){
override def validate(name: String, value: String, messages: Messages): Option[String] = {
optionIf(targetWikiPage.nonEmpty){
Some("Someone has created the wiki since you started. Please reload this page and re-apply your changes.")
targetWikiPage.map { _ =>
"Someone has created the wiki since you started. Please reload this page and re-apply your changes."
}
}
}
private def conflictForEdit: Constraint = new Constraint(){
override def validate(name: String, value: String, messages: Messages): Option[String] = {
optionIf(targetWikiPage.map(_.id != params("id")).getOrElse(false)){
Some("Someone has edited the wiki since you started. Please reload this page and re-apply your changes.")
targetWikiPage.filter(_.id != params("id")).map{ _ =>
"Someone has edited the wiki since you started. Please reload this page and re-apply your changes."
}
}
}

View File

@@ -59,11 +59,11 @@ trait WikiService {
*/
def getWikiPage(owner: String, repository: String, pageName: String): Option[WikiPageInfo] = {
using(Git.open(Directory.getWikiRepositoryDir(owner, repository))){ git =>
optionIf(!JGitUtil.isEmpty(git)){
if(!JGitUtil.isEmpty(git)){
JGitUtil.getFileList(git, "master", ".").find(_.name == pageName + ".md").map { file =>
WikiPageInfo(file.name, new String(git.getRepository.open(file.id).getBytes, "UTF-8"), file.committer, file.time, file.commitId)
}
}
} else None
}
}
@@ -72,7 +72,7 @@ trait WikiService {
*/
def getFileContent(owner: String, repository: String, path: String): Option[Array[Byte]] =
using(Git.open(Directory.getWikiRepositoryDir(owner, repository))){ git =>
optionIf(!JGitUtil.isEmpty(git)){
if(!JGitUtil.isEmpty(git)){
val index = path.lastIndexOf('/')
val parentPath = if(index < 0) "." else path.substring(0, index)
val fileName = if(index < 0) path else path.substring(index + 1)
@@ -80,7 +80,7 @@ trait WikiService {
JGitUtil.getFileList(git, "master", parentPath).find(_.name == fileName).map { file =>
git.getRepository.open(file.id).getBytes
}
}
} else None
}
/**
@@ -239,7 +239,7 @@ trait WikiService {
}
}
optionIf(created || updated || removed){
if(created || updated || removed){
builder.add(JGitUtil.createDirCacheEntry(newPageName + ".md", FileMode.REGULAR_FILE, inserter.insert(Constants.OBJ_BLOB, content.getBytes("UTF-8"))))
builder.finish()
val newHeadId = JGitUtil.createNewCommit(git, inserter, headId, builder.getDirCache.writeTree(inserter), committer.fullName, committer.mailAddress,
@@ -256,7 +256,7 @@ trait WikiService {
})
Some(newHeadId)
}
} else None
}
}
}

View File

@@ -97,17 +97,17 @@ class CommitLogHook(owner: String, repository: String, userName: String, baseURL
val newCommits = if(commits.size > 1000){
val existIds = getAllCommitIds(owner, repository)
commits.flatMap { commit =>
optionIf(!existIds.contains(commit.id)){
if(!existIds.contains(commit.id)){
createIssueComment(commit)
Some(commit)
}
} else None
}
} else {
commits.flatMap { commit =>
optionIf(!existsCommitId(owner, repository, commit.id)){
if(!existsCommitId(owner, repository, commit.id)){
createIssueComment(commit)
Some(commit)
}
} else None
}
}

View File

@@ -40,22 +40,14 @@ object ControlUtil {
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()
}
}
// 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()
// }
// }
def executeIf(condition: => Boolean)(action: => Unit): Boolean =
if(condition){
action
true
} else false
def optionIf[T](condition: => Boolean)(action: => Option[T]): Option[T] =
if(condition) action else None
}

View File

@@ -79,9 +79,9 @@ object JGitUtil {
}
val description = defining(fullMessage.trim.indexOf("\n")){ i =>
optionIf(i >= 0){
if(i >= 0){
Some(fullMessage.trim.substring(i).trim)
}
} else None
}
}

View File

@@ -130,8 +130,8 @@ object LDAPUtil {
private def findMailAddress(conn: LDAPConnection, userDN: String, mailAttribute: String): Option[String] =
defining(conn.search(userDN, LDAPConnection.SCOPE_BASE, null, Array[String](mailAttribute), false)){ results =>
optionIf (results.hasMore) {
if(results.hasMore) {
Option(results.next.getAttribute(mailAttribute)).map(_.getStringValue)
}
} else None
}
}