diff --git a/src/main/scala/app/PullRequestsController.scala b/src/main/scala/app/PullRequestsController.scala index d4787e187..b4cb72c31 100644 --- a/src/main/scala/app/PullRequestsController.scala +++ b/src/main/scala/app/PullRequestsController.scala @@ -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() } } } diff --git a/src/main/scala/app/WikiController.scala b/src/main/scala/app/WikiController.scala index aaa88a896..704ce20a8 100644 --- a/src/main/scala/app/WikiController.scala +++ b/src/main/scala/app/WikiController.scala @@ -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." } } } diff --git a/src/main/scala/service/WikiService.scala b/src/main/scala/service/WikiService.scala index 4b8a05c76..0988ad2df 100644 --- a/src/main/scala/service/WikiService.scala +++ b/src/main/scala/service/WikiService.scala @@ -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 } } } diff --git a/src/main/scala/servlet/GitRepositoryServlet.scala b/src/main/scala/servlet/GitRepositoryServlet.scala index a00a92811..ed1dfd706 100644 --- a/src/main/scala/servlet/GitRepositoryServlet.scala +++ b/src/main/scala/servlet/GitRepositoryServlet.scala @@ -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 } } diff --git a/src/main/scala/util/ControlUtil.scala b/src/main/scala/util/ControlUtil.scala index 17958c1b6..02d7fd189 100644 --- a/src/main/scala/util/ControlUtil.scala +++ b/src/main/scala/util/ControlUtil.scala @@ -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 } diff --git a/src/main/scala/util/JGitUtil.scala b/src/main/scala/util/JGitUtil.scala index 928636266..742641e62 100644 --- a/src/main/scala/util/JGitUtil.scala +++ b/src/main/scala/util/JGitUtil.scala @@ -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 } } diff --git a/src/main/scala/util/LDAPUtil.scala b/src/main/scala/util/LDAPUtil.scala index 3f19b2830..b6859d2fb 100644 --- a/src/main/scala/util/LDAPUtil.scala +++ b/src/main/scala/util/LDAPUtil.scala @@ -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 } }