diff --git a/src/main/scala/app/CreateRepositoryController.scala b/src/main/scala/app/CreateRepositoryController.scala index 560b828..acd5185 100644 --- a/src/main/scala/app/CreateRepositoryController.scala +++ b/src/main/scala/app/CreateRepositoryController.scala @@ -9,7 +9,6 @@ import org.apache.commons.io._ import jp.sf.amateras.scalatra.forms._ import org.eclipse.jgit.lib.PersonIdent -import scala.Some class CreateRepositoryController extends CreateRepositoryControllerBase with RepositoryService with AccountService with WikiService with LabelsService with ActivityService @@ -74,8 +73,7 @@ JGitUtil.initRepository(gitdir) if(form.createReadme){ - val tmpdir = getInitRepositoryDir(form.owner, form.name) - try { + FileUtil.withTmpDir(getInitRepositoryDir(form.owner, form.name)){ tmpdir => // Clone the repository Git.cloneRepository.setURI(gitdir.toURI.toString).setDirectory(tmpdir).call @@ -97,9 +95,6 @@ .setCommitter(new PersonIdent(loginUserName, loginAccount.mailAddress)) .setMessage("Initial commit").call git.push.call - - } finally { - FileUtils.deleteDirectory(tmpdir) } } diff --git a/src/main/scala/app/PullRequestsController.scala b/src/main/scala/app/PullRequestsController.scala index dc4ee48..1b6f76f 100644 --- a/src/main/scala/app/PullRequestsController.scala +++ b/src/main/scala/app/PullRequestsController.scala @@ -4,6 +4,7 @@ import util.Directory._ import util.Implicits._ import util.ControlUtil._ +import util.FileUtil._ import service._ import org.eclipse.jgit.api.Git import jp.sf.amateras.scalatra.forms._ @@ -15,7 +16,6 @@ import service.IssuesService._ import service.PullRequestService._ import util.JGitUtil.DiffInfo -import scala.Some import service.RepositoryService.RepositoryTreeNode import util.JGitUtil.CommitInfo @@ -99,70 +99,66 @@ LockUtil.lock(s"${owner}/${name}/merge"){ getPullRequest(owner, name, issueId).map { case (issue, pullreq) => val remote = getRepositoryDir(owner, name) - val tmpdir = new java.io.File(getTemporaryDir(owner, name), s"merge-${issueId}") - val git = Git.cloneRepository.setDirectory(tmpdir).setURI(remote.toURI.toString).setBranch(pullreq.branch).call + withTmpDir(new java.io.File(getTemporaryDir(owner, name), s"merge-${issueId}")){ tmpdir => + using(Git.cloneRepository.setDirectory(tmpdir).setURI(remote.toURI.toString).setBranch(pullreq.branch).call){ git => - try { - // mark issue as merged and close. - val loginAccount = context.loginAccount.get - createComment(owner, name, loginAccount.userName, issueId, form.message, "merge") - createComment(owner, name, loginAccount.userName, issueId, "Close", "close") - updateClosed(owner, name, issueId, true) + // mark issue as merged and close. + val loginAccount = context.loginAccount.get + createComment(owner, name, loginAccount.userName, issueId, form.message, "merge") + createComment(owner, name, loginAccount.userName, issueId, "Close", "close") + updateClosed(owner, name, issueId, true) - // record activity - recordMergeActivity(owner, name, loginAccount.userName, issueId, form.message) + // record activity + recordMergeActivity(owner, name, loginAccount.userName, issueId, form.message) - // fetch pull request to temporary working repository - val pullRequestBranchName = s"gitbucket-pullrequest-${issueId}" + // fetch pull request to temporary working repository + val pullRequestBranchName = s"gitbucket-pullrequest-${issueId}" - git.fetch - .setRemote(getRepositoryDir(owner, name).toURI.toString) - .setRefSpecs(new RefSpec(s"refs/pull/${issueId}/head:refs/heads/${pullRequestBranchName}")).call + git.fetch + .setRemote(getRepositoryDir(owner, name).toURI.toString) + .setRefSpecs(new RefSpec(s"refs/pull/${issueId}/head:refs/heads/${pullRequestBranchName}")).call - // merge pull request - git.checkout.setName(pullreq.branch).call + // merge pull request + git.checkout.setName(pullreq.branch).call - val result = git.merge - .include(git.getRepository.resolve(pullRequestBranchName)) - .setFastForward(FastForwardMode.NO_FF) - .setCommit(false) - .call + val result = git.merge + .include(git.getRepository.resolve(pullRequestBranchName)) + .setFastForward(FastForwardMode.NO_FF) + .setCommit(false) + .call - if(result.getConflicts != null){ - throw new RuntimeException("This pull request can't merge automatically.") - } - - // merge commit - git.getRepository.writeMergeCommitMsg( - s"Merge pull request #${issueId} from ${pullreq.requestUserName}/${pullreq.requestRepositoryName}\n" - + form.message) - - git.commit - .setCommitter(new PersonIdent(loginAccount.userName, loginAccount.mailAddress)) - .call - - // push - git.push.call - - val (commits, _) = getRequestCompareInfo(owner, name, pullreq.commitIdFrom, - pullreq.requestUserName, pullreq.requestRepositoryName, pullreq.commitIdTo) - - commits.flatten.foreach { commit => - if(!existsCommitId(owner, name, commit.id)){ - insertCommitId(owner, name, commit.id) + if(result.getConflicts != null){ + throw new RuntimeException("This pull request can't merge automatically.") } + + // merge commit + git.getRepository.writeMergeCommitMsg( + s"Merge pull request #${issueId} from ${pullreq.requestUserName}/${pullreq.requestRepositoryName}\n" + + form.message) + + git.commit + .setCommitter(new PersonIdent(loginAccount.userName, loginAccount.mailAddress)) + .call + + // push + git.push.call + + val (commits, _) = getRequestCompareInfo(owner, name, pullreq.commitIdFrom, + pullreq.requestUserName, pullreq.requestRepositoryName, pullreq.commitIdTo) + + commits.flatten.foreach { commit => + if(!existsCommitId(owner, name, commit.id)){ + insertCommitId(owner, name, commit.id) + } + } + + // notifications + Notifier().toNotify(repository, issueId, "merge"){ + Notifier.msgStatus(s"${baseUrl}/${owner}/${name}/pull/${issueId}") + } + + redirect(s"/${owner}/${name}/pull/${issueId}") } - - // notifications - Notifier().toNotify(repository, issueId, "merge"){ - Notifier.msgStatus(s"${baseUrl}/${owner}/${name}/pull/${issueId}") - } - - redirect(s"/${owner}/${name}/pull/${issueId}") - - } finally { - git.getRepository.close - FileUtils.deleteDirectory(tmpdir) } } getOrElse NotFound } @@ -316,28 +312,21 @@ // TODO Are there more quick way? LockUtil.lock(s"${userName}/${repositoryName}/merge-check"){ val remote = getRepositoryDir(userName, repositoryName) - val tmpdir = new java.io.File(getTemporaryDir(userName, repositoryName), "merge-check") - if(tmpdir.exists()){ - FileUtils.deleteDirectory(tmpdir) - } + withTmpDir(new java.io.File(getTemporaryDir(userName, repositoryName), "merge-check")){ tmpdir => + using(Git.cloneRepository.setDirectory(tmpdir).setURI(remote.toURI.toString).setBranch(branch).call){ git => - val git = Git.cloneRepository.setDirectory(tmpdir).setURI(remote.toURI.toString).setBranch(branch).call - try { - git.checkout.setName(branch).call + git.checkout.setName(branch).call - git.fetch - .setRemote(getRepositoryDir(requestUserName, requestRepositoryName).toURI.toString) - .setRefSpecs(new RefSpec(s"refs/heads/${branch}:refs/heads/${requestBranch}")).call + git.fetch + .setRemote(getRepositoryDir(requestUserName, requestRepositoryName).toURI.toString) + .setRefSpecs(new RefSpec(s"refs/heads/${branch}:refs/heads/${requestBranch}")).call - val result = git.merge - .include(git.getRepository.resolve("FETCH_HEAD")) - .setCommit(false).call + val result = git.merge + .include(git.getRepository.resolve("FETCH_HEAD")) + .setCommit(false).call - result.getConflicts != null - - } finally { - git.getRepository.close - FileUtils.deleteDirectory(tmpdir) + result.getConflicts != null + } } } } diff --git a/src/main/scala/util/FileUtil.scala b/src/main/scala/util/FileUtil.scala index 0f6103b..1386a05 100644 --- a/src/main/scala/util/FileUtil.scala +++ b/src/main/scala/util/FileUtil.scala @@ -60,4 +60,14 @@ case _ => "" } -} \ No newline at end of file + def withTmpDir[A](dir: File)(action: File => A): A = { + if(dir.exists()){ + FileUtils.deleteDirectory(dir) + } + try{ + action(dir) + }finally{ + FileUtils.deleteDirectory(dir) + } + } +}