diff --git a/src/main/scala/app/PullRequestsController.scala b/src/main/scala/app/PullRequestsController.scala index d4787e1..b4cb72c 100644 --- a/src/main/scala/app/PullRequestsController.scala +++ b/src/main/scala/app/PullRequestsController.scala @@ -338,12 +338,12 @@ 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 @@ } 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 aaa88a8..704ce20 100644 --- a/src/main/scala/app/WikiController.scala +++ b/src/main/scala/app/WikiController.scala @@ -188,16 +188,16 @@ 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 4b8a05c..0988ad2 100644 --- a/src/main/scala/service/WikiService.scala +++ b/src/main/scala/service/WikiService.scala @@ -59,11 +59,11 @@ */ 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 @@ */ 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 @@ JGitUtil.getFileList(git, "master", parentPath).find(_.name == fileName).map { file => git.getRepository.open(file.id).getBytes } - } + } else None } /** @@ -239,7 +239,7 @@ } } - 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 @@ }) Some(newHeadId) - } + } else None } } } diff --git a/src/main/scala/servlet/GitRepositoryServlet.scala b/src/main/scala/servlet/GitRepositoryServlet.scala index a00a928..ed1dfd7 100644 --- a/src/main/scala/servlet/GitRepositoryServlet.scala +++ b/src/main/scala/servlet/GitRepositoryServlet.scala @@ -97,17 +97,17 @@ 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 17958c1..02d7fd1 100644 --- a/src/main/scala/util/ControlUtil.scala +++ b/src/main/scala/util/ControlUtil.scala @@ -40,22 +40,14 @@ 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 9286362..742641e 100644 --- a/src/main/scala/util/JGitUtil.scala +++ b/src/main/scala/util/JGitUtil.scala @@ -79,9 +79,9 @@ } 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 3f19b28..b6859d2 100644 --- a/src/main/scala/util/LDAPUtil.scala +++ b/src/main/scala/util/LDAPUtil.scala @@ -130,8 +130,8 @@ 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 } }