mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-09 15:05:50 +01:00
use FileUtil.withTmpDir and FileUtil.using
This commit is contained in:
@@ -9,7 +9,6 @@ import org.eclipse.jgit.api.Git
|
|||||||
import org.apache.commons.io._
|
import org.apache.commons.io._
|
||||||
import jp.sf.amateras.scalatra.forms._
|
import jp.sf.amateras.scalatra.forms._
|
||||||
import org.eclipse.jgit.lib.PersonIdent
|
import org.eclipse.jgit.lib.PersonIdent
|
||||||
import scala.Some
|
|
||||||
|
|
||||||
class CreateRepositoryController extends CreateRepositoryControllerBase
|
class CreateRepositoryController extends CreateRepositoryControllerBase
|
||||||
with RepositoryService with AccountService with WikiService with LabelsService with ActivityService
|
with RepositoryService with AccountService with WikiService with LabelsService with ActivityService
|
||||||
@@ -74,8 +73,7 @@ trait CreateRepositoryControllerBase extends ControllerBase {
|
|||||||
JGitUtil.initRepository(gitdir)
|
JGitUtil.initRepository(gitdir)
|
||||||
|
|
||||||
if(form.createReadme){
|
if(form.createReadme){
|
||||||
val tmpdir = getInitRepositoryDir(form.owner, form.name)
|
FileUtil.withTmpDir(getInitRepositoryDir(form.owner, form.name)){ tmpdir =>
|
||||||
try {
|
|
||||||
// Clone the repository
|
// Clone the repository
|
||||||
Git.cloneRepository.setURI(gitdir.toURI.toString).setDirectory(tmpdir).call
|
Git.cloneRepository.setURI(gitdir.toURI.toString).setDirectory(tmpdir).call
|
||||||
|
|
||||||
@@ -97,9 +95,6 @@ trait CreateRepositoryControllerBase extends ControllerBase {
|
|||||||
.setCommitter(new PersonIdent(loginUserName, loginAccount.mailAddress))
|
.setCommitter(new PersonIdent(loginUserName, loginAccount.mailAddress))
|
||||||
.setMessage("Initial commit").call
|
.setMessage("Initial commit").call
|
||||||
git.push.call
|
git.push.call
|
||||||
|
|
||||||
} finally {
|
|
||||||
FileUtils.deleteDirectory(tmpdir)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import util.{LockUtil, CollaboratorsAuthenticator, JGitUtil, ReferrerAuthenticat
|
|||||||
import util.Directory._
|
import util.Directory._
|
||||||
import util.Implicits._
|
import util.Implicits._
|
||||||
import util.ControlUtil._
|
import util.ControlUtil._
|
||||||
|
import util.FileUtil._
|
||||||
import service._
|
import service._
|
||||||
import org.eclipse.jgit.api.Git
|
import org.eclipse.jgit.api.Git
|
||||||
import jp.sf.amateras.scalatra.forms._
|
import jp.sf.amateras.scalatra.forms._
|
||||||
@@ -15,7 +16,6 @@ import org.eclipse.jgit.api.MergeCommand.FastForwardMode
|
|||||||
import service.IssuesService._
|
import service.IssuesService._
|
||||||
import service.PullRequestService._
|
import service.PullRequestService._
|
||||||
import util.JGitUtil.DiffInfo
|
import util.JGitUtil.DiffInfo
|
||||||
import scala.Some
|
|
||||||
import service.RepositoryService.RepositoryTreeNode
|
import service.RepositoryService.RepositoryTreeNode
|
||||||
import util.JGitUtil.CommitInfo
|
import util.JGitUtil.CommitInfo
|
||||||
|
|
||||||
@@ -99,70 +99,66 @@ trait PullRequestsControllerBase extends ControllerBase {
|
|||||||
LockUtil.lock(s"${owner}/${name}/merge"){
|
LockUtil.lock(s"${owner}/${name}/merge"){
|
||||||
getPullRequest(owner, name, issueId).map { case (issue, pullreq) =>
|
getPullRequest(owner, name, issueId).map { case (issue, pullreq) =>
|
||||||
val remote = getRepositoryDir(owner, name)
|
val remote = getRepositoryDir(owner, name)
|
||||||
val tmpdir = new java.io.File(getTemporaryDir(owner, name), s"merge-${issueId}")
|
withTmpDir(new java.io.File(getTemporaryDir(owner, name), s"merge-${issueId}")){ tmpdir =>
|
||||||
val git = Git.cloneRepository.setDirectory(tmpdir).setURI(remote.toURI.toString).setBranch(pullreq.branch).call
|
using(Git.cloneRepository.setDirectory(tmpdir).setURI(remote.toURI.toString).setBranch(pullreq.branch).call){ git =>
|
||||||
|
|
||||||
try {
|
// mark issue as merged and close.
|
||||||
// mark issue as merged and close.
|
val loginAccount = context.loginAccount.get
|
||||||
val loginAccount = context.loginAccount.get
|
createComment(owner, name, loginAccount.userName, issueId, form.message, "merge")
|
||||||
createComment(owner, name, loginAccount.userName, issueId, form.message, "merge")
|
createComment(owner, name, loginAccount.userName, issueId, "Close", "close")
|
||||||
createComment(owner, name, loginAccount.userName, issueId, "Close", "close")
|
updateClosed(owner, name, issueId, true)
|
||||||
updateClosed(owner, name, issueId, true)
|
|
||||||
|
|
||||||
// record activity
|
// record activity
|
||||||
recordMergeActivity(owner, name, loginAccount.userName, issueId, form.message)
|
recordMergeActivity(owner, name, loginAccount.userName, issueId, form.message)
|
||||||
|
|
||||||
// fetch pull request to temporary working repository
|
// fetch pull request to temporary working repository
|
||||||
val pullRequestBranchName = s"gitbucket-pullrequest-${issueId}"
|
val pullRequestBranchName = s"gitbucket-pullrequest-${issueId}"
|
||||||
|
|
||||||
git.fetch
|
git.fetch
|
||||||
.setRemote(getRepositoryDir(owner, name).toURI.toString)
|
.setRemote(getRepositoryDir(owner, name).toURI.toString)
|
||||||
.setRefSpecs(new RefSpec(s"refs/pull/${issueId}/head:refs/heads/${pullRequestBranchName}")).call
|
.setRefSpecs(new RefSpec(s"refs/pull/${issueId}/head:refs/heads/${pullRequestBranchName}")).call
|
||||||
|
|
||||||
// merge pull request
|
// merge pull request
|
||||||
git.checkout.setName(pullreq.branch).call
|
git.checkout.setName(pullreq.branch).call
|
||||||
|
|
||||||
val result = git.merge
|
val result = git.merge
|
||||||
.include(git.getRepository.resolve(pullRequestBranchName))
|
.include(git.getRepository.resolve(pullRequestBranchName))
|
||||||
.setFastForward(FastForwardMode.NO_FF)
|
.setFastForward(FastForwardMode.NO_FF)
|
||||||
.setCommit(false)
|
.setCommit(false)
|
||||||
.call
|
.call
|
||||||
|
|
||||||
if(result.getConflicts != null){
|
if(result.getConflicts != null){
|
||||||
throw new RuntimeException("This pull request can't merge automatically.")
|
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
} getOrElse NotFound
|
||||||
}
|
}
|
||||||
@@ -316,28 +312,21 @@ trait PullRequestsControllerBase extends ControllerBase {
|
|||||||
// TODO Are there more quick way?
|
// TODO Are there more quick way?
|
||||||
LockUtil.lock(s"${userName}/${repositoryName}/merge-check"){
|
LockUtil.lock(s"${userName}/${repositoryName}/merge-check"){
|
||||||
val remote = getRepositoryDir(userName, repositoryName)
|
val remote = getRepositoryDir(userName, repositoryName)
|
||||||
val tmpdir = new java.io.File(getTemporaryDir(userName, repositoryName), "merge-check")
|
withTmpDir(new java.io.File(getTemporaryDir(userName, repositoryName), "merge-check")){ tmpdir =>
|
||||||
if(tmpdir.exists()){
|
using(Git.cloneRepository.setDirectory(tmpdir).setURI(remote.toURI.toString).setBranch(branch).call){ git =>
|
||||||
FileUtils.deleteDirectory(tmpdir)
|
|
||||||
}
|
|
||||||
|
|
||||||
val git = Git.cloneRepository.setDirectory(tmpdir).setURI(remote.toURI.toString).setBranch(branch).call
|
git.checkout.setName(branch).call
|
||||||
try {
|
|
||||||
git.checkout.setName(branch).call
|
|
||||||
|
|
||||||
git.fetch
|
git.fetch
|
||||||
.setRemote(getRepositoryDir(requestUserName, requestRepositoryName).toURI.toString)
|
.setRemote(getRepositoryDir(requestUserName, requestRepositoryName).toURI.toString)
|
||||||
.setRefSpecs(new RefSpec(s"refs/heads/${branch}:refs/heads/${requestBranch}")).call
|
.setRefSpecs(new RefSpec(s"refs/heads/${branch}:refs/heads/${requestBranch}")).call
|
||||||
|
|
||||||
val result = git.merge
|
val result = git.merge
|
||||||
.include(git.getRepository.resolve("FETCH_HEAD"))
|
.include(git.getRepository.resolve("FETCH_HEAD"))
|
||||||
.setCommit(false).call
|
.setCommit(false).call
|
||||||
|
|
||||||
result.getConflicts != null
|
result.getConflicts != null
|
||||||
|
}
|
||||||
} finally {
|
|
||||||
git.getRepository.close
|
|
||||||
FileUtils.deleteDirectory(tmpdir)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user