mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-06 21:45:50 +01:00
Use ControlUtil.
This commit is contained in:
@@ -208,9 +208,9 @@ trait PullRequestsControllerBase extends ControllerBase {
|
|||||||
(forkedRepository.repository.originUserName, forkedRepository.repository.originRepositoryName) match {
|
(forkedRepository.repository.originUserName, forkedRepository.repository.originRepositoryName) match {
|
||||||
case (Some(originUserName), Some(originRepositoryName)) => {
|
case (Some(originUserName), Some(originRepositoryName)) => {
|
||||||
getRepository(originUserName, originRepositoryName, baseUrl).map { originRepository =>
|
getRepository(originUserName, originRepositoryName, baseUrl).map { originRepository =>
|
||||||
withGit(
|
using(
|
||||||
getRepositoryDir(originUserName, originRepositoryName),
|
Git.open(getRepositoryDir(originUserName, originRepositoryName)),
|
||||||
getRepositoryDir(forkedRepository.owner, forkedRepository.name)
|
Git.open(getRepositoryDir(forkedRepository.owner, forkedRepository.name))
|
||||||
){ (oldGit, newGit) =>
|
){ (oldGit, newGit) =>
|
||||||
val oldBranch = JGitUtil.getDefaultBranch(oldGit, originRepository).get._2
|
val oldBranch = JGitUtil.getDefaultBranch(oldGit, originRepository).get._2
|
||||||
val newBranch = JGitUtil.getDefaultBranch(newGit, forkedRepository).get._2
|
val newBranch = JGitUtil.getDefaultBranch(newGit, forkedRepository).get._2
|
||||||
@@ -236,9 +236,9 @@ trait PullRequestsControllerBase extends ControllerBase {
|
|||||||
(getRepository(originOwner, repository.name, baseUrl),
|
(getRepository(originOwner, repository.name, baseUrl),
|
||||||
getRepository(forkedOwner, repository.name, baseUrl)) match {
|
getRepository(forkedOwner, repository.name, baseUrl)) match {
|
||||||
case (Some(originRepository), Some(forkedRepository)) => {
|
case (Some(originRepository), Some(forkedRepository)) => {
|
||||||
withGit(
|
using(
|
||||||
getRepositoryDir(originOwner, repository.name),
|
Git.open(getRepositoryDir(originOwner, repository.name)),
|
||||||
getRepositoryDir(forkedOwner, repository.name)
|
Git.open(getRepositoryDir(forkedOwner, repository.name))
|
||||||
){ case (oldGit, newGit) =>
|
){ case (oldGit, newGit) =>
|
||||||
val originBranch = JGitUtil.getDefaultBranch(oldGit, originRepository, tmpOriginBranch).get._2
|
val originBranch = JGitUtil.getDefaultBranch(oldGit, originRepository, tmpOriginBranch).get._2
|
||||||
val forkedBranch = JGitUtil.getDefaultBranch(newGit, forkedRepository, tmpForkedBranch).get._2
|
val forkedBranch = JGitUtil.getDefaultBranch(newGit, forkedRepository, tmpForkedBranch).get._2
|
||||||
@@ -318,20 +318,6 @@ trait PullRequestsControllerBase extends ControllerBase {
|
|||||||
redirect(s"/${repository.owner}/${repository.name}/pull/${issueId}")
|
redirect(s"/${repository.owner}/${repository.name}/pull/${issueId}")
|
||||||
})
|
})
|
||||||
|
|
||||||
/**
|
|
||||||
* Handles w Git object simultaneously.
|
|
||||||
*/
|
|
||||||
private def withGit[T](oldDir: java.io.File, newDir: java.io.File)(action: (Git, Git) => T): T = {
|
|
||||||
val oldGit = Git.open(oldDir)
|
|
||||||
val newGit = Git.open(newDir)
|
|
||||||
try {
|
|
||||||
action(oldGit, newGit)
|
|
||||||
} finally {
|
|
||||||
oldGit.getRepository.close
|
|
||||||
newGit.getRepository.close
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses branch identifier and extracts owner and branch name as tuple.
|
* Parses branch identifier and extracts owner and branch name as tuple.
|
||||||
*
|
*
|
||||||
@@ -365,9 +351,9 @@ trait PullRequestsControllerBase extends ControllerBase {
|
|||||||
private def getRequestCompareInfo(userName: String, repositoryName: String, branch: String,
|
private def getRequestCompareInfo(userName: String, repositoryName: String, branch: String,
|
||||||
requestUserName: String, requestRepositoryName: String, requestCommitId: String): (Seq[Seq[CommitInfo]], Seq[DiffInfo]) = {
|
requestUserName: String, requestRepositoryName: String, requestCommitId: String): (Seq[Seq[CommitInfo]], Seq[DiffInfo]) = {
|
||||||
|
|
||||||
withGit(
|
using(
|
||||||
getRepositoryDir(userName, repositoryName),
|
Git.open(getRepositoryDir(userName, repositoryName)),
|
||||||
getRepositoryDir(requestUserName, requestRepositoryName)
|
Git.open(getRepositoryDir(requestUserName, requestRepositoryName))
|
||||||
){ (oldGit, newGit) =>
|
){ (oldGit, newGit) =>
|
||||||
val oldId = oldGit.getRepository.resolve(branch)
|
val oldId = oldGit.getRepository.resolve(branch)
|
||||||
val newId = newGit.getRepository.resolve(requestCommitId)
|
val newId = newGit.getRepository.resolve(requestCommitId)
|
||||||
@@ -384,31 +370,30 @@ trait PullRequestsControllerBase extends ControllerBase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private def searchPullRequests(userName: Option[String], repository: RepositoryService.RepositoryInfo) = {
|
private def searchPullRequests(userName: Option[String], repository: RepositoryService.RepositoryInfo) =
|
||||||
val owner = repository.owner
|
defining(repository.owner, repository.name){ case (owner, repoName) =>
|
||||||
val repoName = repository.name
|
val filterUser = userName.map { x => Map("created_by" -> x) } getOrElse Map("all" -> "")
|
||||||
val filterUser = userName.map { x => Map("created_by" -> x) } getOrElse Map("all" -> "")
|
val page = IssueSearchCondition.page(request)
|
||||||
val page = IssueSearchCondition.page(request)
|
val sessionKey = s"${owner}/${repoName}/pulls"
|
||||||
val sessionKey = s"${owner}/${repoName}/pulls"
|
|
||||||
|
|
||||||
// retrieve search condition
|
// retrieve search condition
|
||||||
val condition = if(request.getQueryString == null){
|
val condition = if(request.getQueryString == null){
|
||||||
session.get(sessionKey).getOrElse(IssueSearchCondition()).asInstanceOf[IssueSearchCondition]
|
session.get(sessionKey).getOrElse(IssueSearchCondition()).asInstanceOf[IssueSearchCondition]
|
||||||
} else IssueSearchCondition(request)
|
} else IssueSearchCondition(request)
|
||||||
|
|
||||||
session.put(sessionKey, condition)
|
session.put(sessionKey, condition)
|
||||||
|
|
||||||
pulls.html.list(
|
pulls.html.list(
|
||||||
searchIssue(condition, filterUser, true, (page - 1) * PullRequestLimit, PullRequestLimit, owner -> repoName),
|
searchIssue(condition, filterUser, true, (page - 1) * PullRequestLimit, PullRequestLimit, owner -> repoName),
|
||||||
getPullRequestCountGroupByUser(condition.state == "closed", owner, Some(repoName)),
|
getPullRequestCountGroupByUser(condition.state == "closed", owner, Some(repoName)),
|
||||||
userName,
|
userName,
|
||||||
page,
|
page,
|
||||||
countIssue(condition.copy(state = "open"), filterUser, true, owner -> repoName),
|
countIssue(condition.copy(state = "open"), filterUser, true, owner -> repoName),
|
||||||
countIssue(condition.copy(state = "closed"), filterUser, true, owner -> repoName),
|
countIssue(condition.copy(state = "closed"), filterUser, true, owner -> repoName),
|
||||||
countIssue(condition, Map.empty, true, owner -> repoName),
|
countIssue(condition, Map.empty, true, owner -> repoName),
|
||||||
condition,
|
condition,
|
||||||
repository,
|
repository,
|
||||||
hasWritePermission(owner, repoName, context.loginAccount))
|
hasWritePermission(owner, repoName, context.loginAccount))
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -180,25 +180,25 @@ trait RepositorySettingsControllerBase extends ControllerBase with FlashMapSuppo
|
|||||||
* Provides duplication check for web hook url.
|
* Provides duplication check for web hook url.
|
||||||
*/
|
*/
|
||||||
private def webHook: Constraint = new Constraint(){
|
private def webHook: Constraint = new Constraint(){
|
||||||
override def validate(name: String, value: String): Option[String] = {
|
override def validate(name: String, value: String): Option[String] =
|
||||||
val paths = request.getRequestURI.split("/")
|
defining(request.getRequestURI.split("/")){ paths =>
|
||||||
getWebHookURLs(paths(1), paths(2)).map(_.url).find(_ == value).map(_ => "URL had been registered already.")
|
getWebHookURLs(paths(1), paths(2)).map(_.url).find(_ == value).map(_ => "URL had been registered already.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides Constraint to validate the collaborator name.
|
* Provides Constraint to validate the collaborator name.
|
||||||
*/
|
*/
|
||||||
private def collaborator: Constraint = new Constraint(){
|
private def collaborator: Constraint = new Constraint(){
|
||||||
override def validate(name: String, value: String): Option[String] = {
|
override def validate(name: String, value: String): Option[String] =
|
||||||
val paths = request.getRequestURI.split("/")
|
defining(request.getRequestURI.split("/")){ paths =>
|
||||||
getAccountByUserName(value) match {
|
getAccountByUserName(value) match {
|
||||||
case None => Some("User does not exist.")
|
case None => Some("User does not exist.")
|
||||||
case Some(x) if(x.userName == paths(1) || getCollaborators(paths(1), paths(2)).contains(x.userName))
|
case Some(x) if(x.userName == paths(1) || getCollaborators(paths(1), paths(2)).contains(x.userName))
|
||||||
=> Some("User can access this repository already.")
|
=> Some("User can access this repository already.")
|
||||||
case _ => None
|
case _ => None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -85,13 +85,10 @@ trait RepositoryViewerControllerBase extends ControllerBase {
|
|||||||
case true => getPathObjectId(path, walk)
|
case true => getPathObjectId(path, walk)
|
||||||
}
|
}
|
||||||
|
|
||||||
val treeWalk = new TreeWalk(git.getRepository)
|
val objectId = using(new TreeWalk(git.getRepository)){ treeWalk =>
|
||||||
val objectId = try {
|
|
||||||
treeWalk.addTree(revCommit.getTree)
|
treeWalk.addTree(revCommit.getTree)
|
||||||
treeWalk.setRecursive(true)
|
treeWalk.setRecursive(true)
|
||||||
getPathObjectId(path, treeWalk)
|
getPathObjectId(path, treeWalk)
|
||||||
} finally {
|
|
||||||
treeWalk.release
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(raw){
|
if(raw){
|
||||||
@@ -129,13 +126,13 @@ trait RepositoryViewerControllerBase extends ControllerBase {
|
|||||||
val id = params("id")
|
val id = params("id")
|
||||||
|
|
||||||
using(Git.open(getRepositoryDir(repository.owner, repository.name))){ git =>
|
using(Git.open(getRepositoryDir(repository.owner, repository.name))){ git =>
|
||||||
val revCommit = JGitUtil.getRevCommitFromId(git, git.getRepository.resolve(id))
|
defining(JGitUtil.getRevCommitFromId(git, git.getRepository.resolve(id))){ revCommit =>
|
||||||
|
JGitUtil.getDiffs(git, id) match { case (diffs, oldCommitId) =>
|
||||||
JGitUtil.getDiffs(git, id) match { case (diffs, oldCommitId) =>
|
repo.html.commit(id, new JGitUtil.CommitInfo(revCommit),
|
||||||
repo.html.commit(id, new JGitUtil.CommitInfo(revCommit),
|
JGitUtil.getBranchesOfCommit(git, revCommit.getName),
|
||||||
JGitUtil.getBranchesOfCommit(git, revCommit.getName),
|
JGitUtil.getTagsOfCommit(git, revCommit.getName),
|
||||||
JGitUtil.getTagsOfCommit(git, revCommit.getName),
|
repository, diffs, oldCommitId)
|
||||||
repository, diffs, oldCommitId)
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -238,19 +235,19 @@ trait RepositoryViewerControllerBase extends ControllerBase {
|
|||||||
val revisions = Seq(if(revstr.isEmpty) repository.repository.defaultBranch else revstr, repository.branchList.head)
|
val revisions = Seq(if(revstr.isEmpty) repository.repository.defaultBranch else revstr, repository.branchList.head)
|
||||||
// get specified commit
|
// get specified commit
|
||||||
JGitUtil.getDefaultBranch(git, repository, revstr).map { case (objectId, revision) =>
|
JGitUtil.getDefaultBranch(git, repository, revstr).map { case (objectId, revision) =>
|
||||||
val revCommit = JGitUtil.getRevCommitFromId(git, objectId)
|
defining(JGitUtil.getRevCommitFromId(git, objectId)){ revCommit =>
|
||||||
|
|
||||||
// get files
|
// get files
|
||||||
val files = JGitUtil.getFileList(git, revision, path)
|
val files = JGitUtil.getFileList(git, revision, path)
|
||||||
// process README.md
|
// process README.md
|
||||||
val readme = files.find(_.name == "README.md").map { file =>
|
val readme = files.find(_.name == "README.md").map { file =>
|
||||||
StringUtil.convertFromByteArray(JGitUtil.getContent(Git.open(getRepositoryDir(repository.owner, repository.name)), file.id, true).get)
|
StringUtil.convertFromByteArray(JGitUtil.getContent(Git.open(getRepositoryDir(repository.owner, repository.name)), file.id, true).get)
|
||||||
}
|
}
|
||||||
|
|
||||||
repo.html.files(revision, repository,
|
repo.html.files(revision, repository,
|
||||||
if(path == ".") Nil else path.split("/").toList, // current path
|
if(path == ".") Nil else path.split("/").toList, // current path
|
||||||
new JGitUtil.CommitInfo(revCommit), // latest commit
|
new JGitUtil.CommitInfo(revCommit), // latest commit
|
||||||
files, readme)
|
files, readme)
|
||||||
|
}
|
||||||
} getOrElse NotFound
|
} getOrElse NotFound
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
package util
|
package util
|
||||||
|
|
||||||
import org.eclipse.jgit.api.Git
|
import org.eclipse.jgit.api.Git
|
||||||
import org.eclipse.jgit.revwalk.DepthWalk.RevWalk
|
import org.eclipse.jgit.revwalk.RevWalk
|
||||||
|
import org.eclipse.jgit.treewalk.TreeWalk
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides control facilities.
|
* Provides control facilities.
|
||||||
@@ -11,9 +12,7 @@ object ControlUtil {
|
|||||||
def defining[A, B](value: A)(f: A => B): B = f(value)
|
def defining[A, B](value: A)(f: A => B): B = f(value)
|
||||||
|
|
||||||
def using[A <% { def close(): Unit }, B](resource: A)(f: A => B): B =
|
def using[A <% { def close(): Unit }, B](resource: A)(f: A => B): B =
|
||||||
try {
|
try f(resource) finally {
|
||||||
f(resource)
|
|
||||||
} finally {
|
|
||||||
if(resource != null){
|
if(resource != null){
|
||||||
try {
|
try {
|
||||||
resource.close()
|
resource.close()
|
||||||
@@ -24,31 +23,26 @@ object ControlUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
def using[T](git: Git)(f: Git => T): T =
|
def using[T](git: Git)(f: Git => T): T =
|
||||||
try {
|
try f(git) finally git.getRepository.close
|
||||||
f(git)
|
|
||||||
} finally {
|
def using[T](git1: Git, git2: Git)(f: (Git, Git) => T): T =
|
||||||
git.getRepository.close
|
try f(git1, git2) finally {
|
||||||
|
git1.getRepository.close
|
||||||
|
git2.getRepository.close
|
||||||
}
|
}
|
||||||
|
|
||||||
def using[T](revWalk: RevWalk)(f: RevWalk => T): T =
|
def using[T](revWalk: RevWalk)(f: RevWalk => T): T =
|
||||||
try {
|
try f(revWalk) finally revWalk.release()
|
||||||
f(revWalk)
|
|
||||||
} finally {
|
def using[T](treeWalk: TreeWalk)(f: TreeWalk => T): T =
|
||||||
revWalk.release()
|
try f(treeWalk) finally treeWalk.release()
|
||||||
}
|
|
||||||
|
|
||||||
def executeIf(condition: => Boolean)(action: => Unit): Boolean =
|
def executeIf(condition: => Boolean)(action: => Unit): Boolean =
|
||||||
if(condition){
|
if(condition){
|
||||||
action
|
action
|
||||||
true
|
true
|
||||||
} else {
|
} else false
|
||||||
false
|
|
||||||
}
|
|
||||||
|
|
||||||
def optionIf[T](condition: => Boolean)(action: => Option[T]): Option[T] =
|
def optionIf[T](condition: => Boolean)(action: => Option[T]): Option[T] =
|
||||||
if(condition){
|
if(condition) action else None
|
||||||
action
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -159,45 +159,43 @@ object JGitUtil {
|
|||||||
* @return HTML of the file list
|
* @return HTML of the file list
|
||||||
*/
|
*/
|
||||||
def getFileList(git: Git, revision: String, path: String = "."): List[FileInfo] = {
|
def getFileList(git: Git, revision: String, path: String = "."): List[FileInfo] = {
|
||||||
val revWalk = new RevWalk(git.getRepository)
|
|
||||||
val objectId = git.getRepository.resolve(revision)
|
|
||||||
val revCommit = revWalk.parseCommit(objectId)
|
|
||||||
|
|
||||||
val treeWalk = new TreeWalk(git.getRepository)
|
|
||||||
treeWalk.addTree(revCommit.getTree)
|
|
||||||
if(path != "."){
|
|
||||||
treeWalk.setRecursive(true)
|
|
||||||
treeWalk.setFilter(new TreeFilter(){
|
|
||||||
|
|
||||||
var stopRecursive = false
|
|
||||||
|
|
||||||
def include(walker: TreeWalk): Boolean = {
|
|
||||||
val targetPath = walker.getPathString
|
|
||||||
if((path + "/").startsWith(targetPath)){
|
|
||||||
true
|
|
||||||
} else if(targetPath.startsWith(path + "/") && targetPath.substring(path.length + 1).indexOf("/") < 0){
|
|
||||||
stopRecursive = true
|
|
||||||
treeWalk.setRecursive(false)
|
|
||||||
true
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
def shouldBeRecursive(): Boolean = !stopRecursive
|
|
||||||
|
|
||||||
override def clone: TreeFilter = return this
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
val list = new scala.collection.mutable.ListBuffer[(ObjectId, FileMode, String, String)]
|
val list = new scala.collection.mutable.ListBuffer[(ObjectId, FileMode, String, String)]
|
||||||
|
|
||||||
while (treeWalk.next()) {
|
using(new RevWalk(git.getRepository)){ revWalk =>
|
||||||
list.append((treeWalk.getObjectId(0), treeWalk.getFileMode(0), treeWalk.getPathString, treeWalk.getNameString))
|
val objectId = git.getRepository.resolve(revision)
|
||||||
|
val revCommit = revWalk.parseCommit(objectId)
|
||||||
|
|
||||||
|
using(new TreeWalk(git.getRepository)){ treeWalk =>
|
||||||
|
treeWalk.addTree(revCommit.getTree)
|
||||||
|
if(path != "."){
|
||||||
|
treeWalk.setRecursive(true)
|
||||||
|
treeWalk.setFilter(new TreeFilter(){
|
||||||
|
|
||||||
|
var stopRecursive = false
|
||||||
|
|
||||||
|
def include(walker: TreeWalk): Boolean = {
|
||||||
|
val targetPath = walker.getPathString
|
||||||
|
if((path + "/").startsWith(targetPath)){
|
||||||
|
true
|
||||||
|
} else if(targetPath.startsWith(path + "/") && targetPath.substring(path.length + 1).indexOf("/") < 0){
|
||||||
|
stopRecursive = true
|
||||||
|
treeWalk.setRecursive(false)
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
def shouldBeRecursive(): Boolean = !stopRecursive
|
||||||
|
|
||||||
|
override def clone: TreeFilter = return this
|
||||||
|
})
|
||||||
|
}
|
||||||
|
while (treeWalk.next()) {
|
||||||
|
list.append((treeWalk.getObjectId(0), treeWalk.getFileMode(0), treeWalk.getPathString, treeWalk.getNameString))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
treeWalk.release
|
|
||||||
revWalk.dispose
|
|
||||||
|
|
||||||
val commits = getLatestCommitFromPaths(git, list.toList.map(_._3), revision)
|
val commits = getLatestCommitFromPaths(git, list.toList.map(_._3), revision)
|
||||||
list.map { case (objectId, fileMode, path, name) =>
|
list.map { case (objectId, fileMode, path, name) =>
|
||||||
@@ -367,19 +365,19 @@ object JGitUtil {
|
|||||||
|
|
||||||
} else {
|
} else {
|
||||||
// initial commit
|
// initial commit
|
||||||
val treeWalk = new TreeWalk(git.getRepository)
|
using(new TreeWalk(git.getRepository)){ treeWalk =>
|
||||||
treeWalk.addTree(revCommit.getTree)
|
treeWalk.addTree(revCommit.getTree)
|
||||||
val buffer = new scala.collection.mutable.ListBuffer[DiffInfo]()
|
val buffer = new scala.collection.mutable.ListBuffer[DiffInfo]()
|
||||||
while(treeWalk.next){
|
while(treeWalk.next){
|
||||||
buffer.append((if(!fetchContent){
|
buffer.append((if(!fetchContent){
|
||||||
DiffInfo(ChangeType.ADD, null, treeWalk.getPathString, None, None)
|
DiffInfo(ChangeType.ADD, null, treeWalk.getPathString, None, None)
|
||||||
} else {
|
} else {
|
||||||
DiffInfo(ChangeType.ADD, null, treeWalk.getPathString, None,
|
DiffInfo(ChangeType.ADD, null, treeWalk.getPathString, None,
|
||||||
JGitUtil.getContent(git, treeWalk.getObjectId(0), false).filter(FileUtil.isText).map(convertFromByteArray))
|
JGitUtil.getContent(git, treeWalk.getObjectId(0), false).filter(FileUtil.isText).map(convertFromByteArray))
|
||||||
}))
|
}))
|
||||||
|
}
|
||||||
|
(buffer.toList, None)
|
||||||
}
|
}
|
||||||
treeWalk.release
|
|
||||||
(buffer.toList, None)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user