mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-12 08:25:50 +01:00
Merge pull request #2229 from gitbucket/jgitutil-specs
Add Testcases for JGitUtil
This commit is contained in:
@@ -21,6 +21,8 @@ scalaVersion := "2.12.8"
|
|||||||
|
|
||||||
scalafmtOnCompile := true
|
scalafmtOnCompile := true
|
||||||
|
|
||||||
|
coverageExcludedPackages := ".*\\.html\\..*"
|
||||||
|
|
||||||
// dependency settings
|
// dependency settings
|
||||||
resolvers ++= Seq(
|
resolvers ++= Seq(
|
||||||
Classpaths.typesafeReleases,
|
Classpaths.typesafeReleases,
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ addSbtPlugin("com.typesafe.sbt" % "sbt-twirl" % "1.3.15")
|
|||||||
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.9")
|
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.9")
|
||||||
addSbtPlugin("org.scalatra.sbt" % "sbt-scalatra" % "1.0.3")
|
addSbtPlugin("org.scalatra.sbt" % "sbt-scalatra" % "1.0.3")
|
||||||
addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.1.2")
|
addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.1.2")
|
||||||
addSbtCoursier
|
|
||||||
addSbtPlugin("com.typesafe.sbt" % "sbt-license-report" % "1.2.0")
|
addSbtPlugin("com.typesafe.sbt" % "sbt-license-report" % "1.2.0")
|
||||||
addSbtPlugin("net.virtual-void" % "sbt-dependency-graph" % "0.9.2")
|
addSbtPlugin("net.virtual-void" % "sbt-dependency-graph" % "0.9.2")
|
||||||
|
addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.5.1")
|
||||||
|
|
||||||
|
addSbtCoursier
|
||||||
|
|||||||
@@ -589,10 +589,12 @@ trait PullRequestsControllerBase extends ControllerBase {
|
|||||||
val mailAddresses =
|
val mailAddresses =
|
||||||
context.loginAccount.map(x => Seq(x.mailAddress) ++ getAccountExtraMailAddresses(x.userName)).getOrElse(Nil)
|
context.loginAccount.map(x => Seq(x.mailAddress) ++ getAccountExtraMailAddresses(x.userName)).getOrElse(Nil)
|
||||||
|
|
||||||
val branches = JGitUtil
|
val branches =
|
||||||
|
using(Git.open(getRepositoryDir(repository.owner, repository.name))) {
|
||||||
|
git =>
|
||||||
|
JGitUtil
|
||||||
.getBranches(
|
.getBranches(
|
||||||
owner = repository.owner,
|
git = git,
|
||||||
name = repository.name,
|
|
||||||
defaultBranch = repository.repository.defaultBranch,
|
defaultBranch = repository.repository.defaultBranch,
|
||||||
origin = repository.repository.originUserName.isEmpty
|
origin = repository.repository.originUserName.isEmpty
|
||||||
)
|
)
|
||||||
@@ -606,6 +608,7 @@ trait PullRequestsControllerBase extends ControllerBase {
|
|||||||
}
|
}
|
||||||
.map(_.name)
|
.map(_.name)
|
||||||
.reverse
|
.reverse
|
||||||
|
}
|
||||||
|
|
||||||
val targetRepository = (for {
|
val targetRepository = (for {
|
||||||
parentUserName <- repository.repository.parentUserName
|
parentUserName <- repository.repository.parentUserName
|
||||||
|
|||||||
@@ -723,10 +723,11 @@ trait RepositoryViewerControllerBase extends ControllerBase {
|
|||||||
*/
|
*/
|
||||||
get("/:owner/:repository/branches")(referrersOnly { repository =>
|
get("/:owner/:repository/branches")(referrersOnly { repository =>
|
||||||
val protectedBranches = getProtectedBranchList(repository.owner, repository.name).toSet
|
val protectedBranches = getProtectedBranchList(repository.owner, repository.name).toSet
|
||||||
val branches = JGitUtil
|
val branches = using(Git.open(getRepositoryDir(repository.owner, repository.name))) {
|
||||||
|
git =>
|
||||||
|
JGitUtil
|
||||||
.getBranches(
|
.getBranches(
|
||||||
owner = repository.owner,
|
git = git,
|
||||||
name = repository.name,
|
|
||||||
defaultBranch = repository.repository.defaultBranch,
|
defaultBranch = repository.repository.defaultBranch,
|
||||||
origin = repository.repository.originUserName.isEmpty
|
origin = repository.repository.originUserName.isEmpty
|
||||||
)
|
)
|
||||||
@@ -746,6 +747,7 @@ trait RepositoryViewerControllerBase extends ControllerBase {
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
.reverse
|
.reverse
|
||||||
|
}
|
||||||
|
|
||||||
html.branches(branches, hasDeveloperRole(repository.owner, repository.name, context.loginAccount), repository)
|
html.branches(branches, hasDeveloperRole(repository.owner, repository.name, context.loginAccount), repository)
|
||||||
})
|
})
|
||||||
@@ -920,7 +922,7 @@ trait RepositoryViewerControllerBase extends ControllerBase {
|
|||||||
repository,
|
repository,
|
||||||
if (path == ".") Nil else path.split("/").toList, // current path
|
if (path == ".") Nil else path.split("/").toList, // current path
|
||||||
new JGitUtil.CommitInfo(lastModifiedCommit), // last modified commit
|
new JGitUtil.CommitInfo(lastModifiedCommit), // last modified commit
|
||||||
JGitUtil.getCommitCount(repository.owner, repository.name, lastModifiedCommit.getName),
|
JGitUtil.getCommitCount(git, lastModifiedCommit.getName),
|
||||||
files,
|
files,
|
||||||
readme,
|
readme,
|
||||||
hasDeveloperRole(repository.owner, repository.name, context.loginAccount),
|
hasDeveloperRole(repository.owner, repository.name, context.loginAccount),
|
||||||
@@ -967,7 +969,7 @@ trait RepositoryViewerControllerBase extends ControllerBase {
|
|||||||
val entryPath =
|
val entryPath =
|
||||||
if (path.isEmpty) baseName + "/" + treeWalk.getPathString
|
if (path.isEmpty) baseName + "/" + treeWalk.getPathString
|
||||||
else path.split("/").last + treeWalk.getPathString.substring(path.length)
|
else path.split("/").last + treeWalk.getPathString.substring(path.length)
|
||||||
val size = JGitUtil.getFileSize(git, repository, treeWalk)
|
val size = JGitUtil.getContentSize(git.getRepository.open(treeWalk.getObjectId(0)))
|
||||||
val mode = treeWalk.getFileMode.getBits
|
val mode = treeWalk.getFileMode.getBits
|
||||||
val entry: ArchiveEntry = entryCreator(entryPath, size, date, mode)
|
val entry: ArchiveEntry = entryCreator(entryPath, size, date, mode)
|
||||||
JGitUtil.openFile(git, repository, commit.getTree, treeWalk.getPathString) { in =>
|
JGitUtil.openFile(git, repository, commit.getTree, treeWalk.getPathString) { in =>
|
||||||
|
|||||||
@@ -3,8 +3,11 @@ import gitbucket.core.api._
|
|||||||
import gitbucket.core.controller.ControllerBase
|
import gitbucket.core.controller.ControllerBase
|
||||||
import gitbucket.core.service.{AccountService, ProtectedBranchService, RepositoryService}
|
import gitbucket.core.service.{AccountService, ProtectedBranchService, RepositoryService}
|
||||||
import gitbucket.core.util._
|
import gitbucket.core.util._
|
||||||
|
import gitbucket.core.util.SyntaxSugars._
|
||||||
|
import gitbucket.core.util.Directory._
|
||||||
import gitbucket.core.util.Implicits._
|
import gitbucket.core.util.Implicits._
|
||||||
import gitbucket.core.util.JGitUtil.getBranches
|
import gitbucket.core.util.JGitUtil.getBranches
|
||||||
|
import org.eclipse.jgit.api.Git
|
||||||
|
|
||||||
trait ApiRepositoryBranchControllerBase extends ControllerBase {
|
trait ApiRepositoryBranchControllerBase extends ControllerBase {
|
||||||
self: RepositoryService
|
self: RepositoryService
|
||||||
@@ -22,11 +25,11 @@ trait ApiRepositoryBranchControllerBase extends ControllerBase {
|
|||||||
* https://developer.github.com/v3/repos/branches/#list-branches
|
* https://developer.github.com/v3/repos/branches/#list-branches
|
||||||
*/
|
*/
|
||||||
get("/api/v3/repos/:owner/:repository/branches")(referrersOnly { repository =>
|
get("/api/v3/repos/:owner/:repository/branches")(referrersOnly { repository =>
|
||||||
|
using(Git.open(getRepositoryDir(repository.owner, repository.name))) { git =>
|
||||||
JsonFormat(
|
JsonFormat(
|
||||||
JGitUtil
|
JGitUtil
|
||||||
.getBranches(
|
.getBranches(
|
||||||
owner = repository.owner,
|
git = git,
|
||||||
name = repository.name,
|
|
||||||
defaultBranch = repository.repository.defaultBranch,
|
defaultBranch = repository.repository.defaultBranch,
|
||||||
origin = repository.repository.originUserName.isEmpty
|
origin = repository.repository.originUserName.isEmpty
|
||||||
)
|
)
|
||||||
@@ -34,6 +37,7 @@ trait ApiRepositoryBranchControllerBase extends ControllerBase {
|
|||||||
ApiBranchForList(br.name, ApiBranchCommit(br.commitId))
|
ApiBranchForList(br.name, ApiBranchCommit(br.commitId))
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -41,12 +45,12 @@ trait ApiRepositoryBranchControllerBase extends ControllerBase {
|
|||||||
* https://developer.github.com/v3/repos/branches/#get-branch
|
* https://developer.github.com/v3/repos/branches/#get-branch
|
||||||
*/
|
*/
|
||||||
get("/api/v3/repos/:owner/:repository/branches/*")(referrersOnly { repository =>
|
get("/api/v3/repos/:owner/:repository/branches/*")(referrersOnly { repository =>
|
||||||
//import gitbucket.core.api._
|
using(Git.open(getRepositoryDir(repository.owner, repository.name))) {
|
||||||
|
git =>
|
||||||
(for {
|
(for {
|
||||||
branch <- params.get("splat") if repository.branchList.contains(branch)
|
branch <- params.get("splat") if repository.branchList.contains(branch)
|
||||||
br <- getBranches(
|
br <- getBranches(
|
||||||
repository.owner,
|
git,
|
||||||
repository.name,
|
|
||||||
repository.repository.defaultBranch,
|
repository.repository.defaultBranch,
|
||||||
repository.repository.originUserName.isEmpty
|
repository.repository.originUserName.isEmpty
|
||||||
).find(_.name == branch)
|
).find(_.name == branch)
|
||||||
@@ -56,6 +60,7 @@ trait ApiRepositoryBranchControllerBase extends ControllerBase {
|
|||||||
ApiBranch(branch, ApiBranchCommit(br.commitId), ApiBranchProtection(protection))(RepositoryName(repository))
|
ApiBranch(branch, ApiBranchCommit(br.commitId), ApiBranchProtection(protection))(RepositoryName(repository))
|
||||||
)
|
)
|
||||||
}) getOrElse NotFound()
|
}) getOrElse NotFound()
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -209,12 +214,13 @@ trait ApiRepositoryBranchControllerBase extends ControllerBase {
|
|||||||
*/
|
*/
|
||||||
patch("/api/v3/repos/:owner/:repository/branches/*")(ownerOnly { repository =>
|
patch("/api/v3/repos/:owner/:repository/branches/*")(ownerOnly { repository =>
|
||||||
import gitbucket.core.api._
|
import gitbucket.core.api._
|
||||||
|
using(Git.open(getRepositoryDir(repository.owner, repository.name))) {
|
||||||
|
git =>
|
||||||
(for {
|
(for {
|
||||||
branch <- params.get("splat") if repository.branchList.contains(branch)
|
branch <- params.get("splat") if repository.branchList.contains(branch)
|
||||||
protection <- extractFromJsonBody[ApiBranchProtection.EnablingAndDisabling].map(_.protection)
|
protection <- extractFromJsonBody[ApiBranchProtection.EnablingAndDisabling].map(_.protection)
|
||||||
br <- getBranches(
|
br <- getBranches(
|
||||||
repository.owner,
|
git,
|
||||||
repository.name,
|
|
||||||
repository.repository.defaultBranch,
|
repository.repository.defaultBranch,
|
||||||
repository.repository.originUserName.isEmpty
|
repository.repository.originUserName.isEmpty
|
||||||
).find(_.name == branch)
|
).find(_.name == branch)
|
||||||
@@ -232,5 +238,6 @@ trait ApiRepositoryBranchControllerBase extends ControllerBase {
|
|||||||
}
|
}
|
||||||
JsonFormat(ApiBranch(branch, ApiBranchCommit(br.commitId), protection)(RepositoryName(repository)))
|
JsonFormat(ApiBranch(branch, ApiBranchCommit(br.commitId), protection)(RepositoryName(repository)))
|
||||||
}) getOrElse NotFound()
|
}) getOrElse NotFound()
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -240,18 +240,16 @@ object JGitUtil {
|
|||||||
* Returns the number of commits in the specified branch or commit.
|
* Returns the number of commits in the specified branch or commit.
|
||||||
* If the specified branch has over 10000 commits, this method returns 100001.
|
* If the specified branch has over 10000 commits, this method returns 100001.
|
||||||
*/
|
*/
|
||||||
def getCommitCount(owner: String, repository: String, branch: String): Int = {
|
def getCommitCount(git: Git, branch: String, max: Int = 10001): Int = {
|
||||||
val dir = getRepositoryDir(owner, repository)
|
val dir = git.getRepository.getDirectory
|
||||||
val key = dir.getAbsolutePath + "@" + branch
|
val key = dir.getAbsolutePath + "@" + branch
|
||||||
val entry = cache.getEntry(key)
|
val entry = cache.getEntry(key)
|
||||||
|
|
||||||
if (entry == null) {
|
if (entry == null) {
|
||||||
using(Git.open(dir)) { git =>
|
|
||||||
val commitId = git.getRepository.resolve(branch)
|
val commitId = git.getRepository.resolve(branch)
|
||||||
val commitCount = git.log.add(commitId).call.iterator.asScala.take(10001).size
|
val commitCount = git.log.add(commitId).call.iterator.asScala.take(max).size
|
||||||
cache.put(key, commitCount)
|
cache.put(key, commitCount)
|
||||||
commitCount
|
commitCount
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
entry.getValue
|
entry.getValue
|
||||||
}
|
}
|
||||||
@@ -1110,6 +1108,7 @@ object JGitUtil {
|
|||||||
/**
|
/**
|
||||||
* Fetch pull request contents into refs/pull/${issueId}/head and return (commitIdTo, commitIdFrom)
|
* Fetch pull request contents into refs/pull/${issueId}/head and return (commitIdTo, commitIdFrom)
|
||||||
*/
|
*/
|
||||||
|
// TODO should take Git instead of owner and username for testability
|
||||||
def updatePullRequest(
|
def updatePullRequest(
|
||||||
userName: String,
|
userName: String,
|
||||||
repositoryName: String,
|
repositoryName: String,
|
||||||
@@ -1154,8 +1153,7 @@ object JGitUtil {
|
|||||||
git.log.add(startCommit).addPath(path).setMaxCount(1).call.iterator.next
|
git.log.add(startCommit).addPath(path).setMaxCount(1).call.iterator.next
|
||||||
}
|
}
|
||||||
|
|
||||||
def getBranches(owner: String, name: String, defaultBranch: String, origin: Boolean): Seq[BranchInfo] = {
|
def getBranches(git: Git, defaultBranch: String, origin: Boolean): Seq[BranchInfo] = {
|
||||||
using(Git.open(getRepositoryDir(owner, name))) { git =>
|
|
||||||
val repo = git.getRepository
|
val repo = git.getRepository
|
||||||
val defaultObject = repo.resolve(defaultBranch)
|
val defaultObject = repo.resolve(defaultBranch)
|
||||||
|
|
||||||
@@ -1192,7 +1190,6 @@ object JGitUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
def getBlame(git: Git, id: String, path: String): Iterable[BlameInfo] = {
|
def getBlame(git: Git, id: String, path: String): Iterable[BlameInfo] = {
|
||||||
Option(git.getRepository.resolve(id))
|
Option(git.getRepository.resolve(id))
|
||||||
@@ -1203,7 +1200,7 @@ object JGitUtil {
|
|||||||
val blame = blamer.call()
|
val blame = blamer.call()
|
||||||
var blameMap = Map[String, JGitUtil.BlameInfo]()
|
var blameMap = Map[String, JGitUtil.BlameInfo]()
|
||||||
var idLine = List[(String, Int)]()
|
var idLine = List[(String, Int)]()
|
||||||
val commits = 0.to(blame.getResultContents().size() - 1).map { i =>
|
0.to(blame.getResultContents().size() - 1).map { i =>
|
||||||
val c = blame.getSourceCommit(i)
|
val c = blame.getSourceCommit(i)
|
||||||
if (!blameMap.contains(c.name)) {
|
if (!blameMap.contains(c.name)) {
|
||||||
blameMap += c.name -> JGitUtil.BlameInfo(
|
blameMap += c.name -> JGitUtil.BlameInfo(
|
||||||
@@ -1243,24 +1240,7 @@ object JGitUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
def getFileSize(git: Git, repository: RepositoryService.RepositoryInfo, treeWalk: TreeWalk): Long = {
|
private def openFile[T](git: Git, repository: RepositoryService.RepositoryInfo, treeWalk: TreeWalk)(
|
||||||
val attrs = treeWalk.getAttributes
|
|
||||||
val loader = git.getRepository.open(treeWalk.getObjectId(0))
|
|
||||||
if (attrs.containsKey("filter") && attrs.get("filter").getValue == "lfs") {
|
|
||||||
val lfsAttrs = getLfsAttributes(loader)
|
|
||||||
lfsAttrs.get("size").map(_.toLong).get
|
|
||||||
} else {
|
|
||||||
loader.getSize
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
def getFileSize(git: Git, repository: RepositoryService.RepositoryInfo, tree: RevTree, path: String): Long = {
|
|
||||||
using(TreeWalk.forPath(git.getRepository, path, tree)) { treeWalk =>
|
|
||||||
getFileSize(git, repository, treeWalk)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
def openFile[T](git: Git, repository: RepositoryService.RepositoryInfo, treeWalk: TreeWalk)(
|
|
||||||
f: InputStream => T
|
f: InputStream => T
|
||||||
): T = {
|
): T = {
|
||||||
val attrs = treeWalk.getAttributes
|
val attrs = treeWalk.getAttributes
|
||||||
@@ -1270,7 +1250,6 @@ object JGitUtil {
|
|||||||
if (lfsAttrs.nonEmpty) {
|
if (lfsAttrs.nonEmpty) {
|
||||||
val oid = lfsAttrs("oid").split(":")(1)
|
val oid = lfsAttrs("oid").split(":")(1)
|
||||||
|
|
||||||
val file = new File(FileUtil.getLfsFilePath(repository.owner, repository.name, oid))
|
|
||||||
using(new FileInputStream(FileUtil.getLfsFilePath(repository.owner, repository.name, oid))) { in =>
|
using(new FileInputStream(FileUtil.getLfsFilePath(repository.owner, repository.name, oid))) { in =>
|
||||||
f(in)
|
f(in)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -133,5 +133,11 @@
|
|||||||
});
|
});
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
<style type="text/css">
|
||||||
|
a.tag {
|
||||||
|
color: #888888;
|
||||||
|
margin-right: 4px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import java.nio.file._
|
|||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
object GitSpecUtil {
|
object GitSpecUtil {
|
||||||
|
|
||||||
def withTestFolder[U](f: File => U): U = {
|
def withTestFolder[U](f: File => U): U = {
|
||||||
val folder = new File(System.getProperty("java.io.tmpdir"), "test-" + System.nanoTime)
|
val folder = new File(System.getProperty("java.io.tmpdir"), "test-" + System.nanoTime)
|
||||||
if (!folder.mkdirs()) {
|
if (!folder.mkdirs()) {
|
||||||
@@ -26,7 +27,9 @@ object GitSpecUtil {
|
|||||||
FileUtils.deleteQuietly(folder)
|
FileUtils.deleteQuietly(folder)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
def withTestRepository[U](f: Git => U): U = withTestFolder(folder => using(Git.open(createTestRepository(folder)))(f))
|
def withTestRepository[U](f: Git => U): U = withTestFolder(folder => using(Git.open(createTestRepository(folder)))(f))
|
||||||
|
|
||||||
def createTestRepository(dir: File): File = {
|
def createTestRepository(dir: File): File = {
|
||||||
RepositoryCache.clear()
|
RepositoryCache.clear()
|
||||||
FileUtils.deleteQuietly(dir)
|
FileUtils.deleteQuietly(dir)
|
||||||
@@ -34,13 +37,14 @@ object GitSpecUtil {
|
|||||||
JGitUtil.initRepository(dir)
|
JGitUtil.initRepository(dir)
|
||||||
dir
|
dir
|
||||||
}
|
}
|
||||||
|
|
||||||
def createFile(
|
def createFile(
|
||||||
git: Git,
|
git: Git,
|
||||||
branch: String,
|
branch: String,
|
||||||
name: String,
|
name: String,
|
||||||
content: String,
|
content: String,
|
||||||
autorName: String = "dummy",
|
authorName: String = "dummy",
|
||||||
autorEmail: String = "dummy@example.com",
|
authorEmail: String = "dummy@example.com",
|
||||||
message: String = "test commit"
|
message: String = "test commit"
|
||||||
): Unit = {
|
): Unit = {
|
||||||
val builder = DirCache.newInCore.builder()
|
val builder = DirCache.newInCore.builder()
|
||||||
@@ -67,13 +71,14 @@ object GitSpecUtil {
|
|||||||
headId,
|
headId,
|
||||||
builder.getDirCache.writeTree(inserter),
|
builder.getDirCache.writeTree(inserter),
|
||||||
branch,
|
branch,
|
||||||
autorName,
|
authorName,
|
||||||
autorEmail,
|
authorEmail,
|
||||||
message
|
message
|
||||||
)
|
)
|
||||||
inserter.flush()
|
inserter.flush()
|
||||||
inserter.close()
|
inserter.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
def getFile(git: Git, branch: String, path: String) = {
|
def getFile(git: Git, branch: String, path: String) = {
|
||||||
val revCommit = JGitUtil.getRevCommitFromId(git, git.getRepository.resolve(branch))
|
val revCommit = JGitUtil.getRevCommitFromId(git, git.getRepository.resolve(branch))
|
||||||
val objectId = using(new TreeWalk(git.getRepository)) { walk =>
|
val objectId = using(new TreeWalk(git.getRepository)) { walk =>
|
||||||
@@ -89,6 +94,7 @@ object GitSpecUtil {
|
|||||||
}
|
}
|
||||||
JGitUtil.getContentInfo(git, path, objectId)
|
JGitUtil.getContentInfo(git, path, objectId)
|
||||||
}
|
}
|
||||||
|
|
||||||
def mergeAndCommit(git: Git, into: String, branch: String, message: String = null): Unit = {
|
def mergeAndCommit(git: Git, into: String, branch: String, message: String = null): Unit = {
|
||||||
val repository = git.getRepository
|
val repository = git.getRepository
|
||||||
val merger = MergeStrategy.RECURSIVE.newMerger(repository, true)
|
val merger = MergeStrategy.RECURSIVE.newMerger(repository, true)
|
||||||
|
|||||||
@@ -1,10 +1,270 @@
|
|||||||
package gitbucket.core.util
|
package gitbucket.core.util
|
||||||
|
|
||||||
import GitSpecUtil._
|
import GitSpecUtil._
|
||||||
|
import gitbucket.core.util.SyntaxSugars.using
|
||||||
|
import org.apache.commons.io.IOUtils
|
||||||
|
import org.eclipse.jgit.diff.DiffEntry
|
||||||
|
import org.eclipse.jgit.diff.DiffEntry.ChangeType
|
||||||
|
import org.eclipse.jgit.lib.Constants
|
||||||
|
import org.eclipse.jgit.treewalk.TreeWalk
|
||||||
import org.scalatest.FunSuite
|
import org.scalatest.FunSuite
|
||||||
|
|
||||||
|
import scala.collection.JavaConverters._
|
||||||
|
|
||||||
class JGitUtilSpec extends FunSuite {
|
class JGitUtilSpec extends FunSuite {
|
||||||
|
|
||||||
|
test("isEmpty") {
|
||||||
|
withTestRepository { git =>
|
||||||
|
assert(JGitUtil.isEmpty(git) == true)
|
||||||
|
|
||||||
|
createFile(git, Constants.HEAD, "README.md", "body1", message = "commit1")
|
||||||
|
assert(JGitUtil.isEmpty(git) == false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test("getDiffs") {
|
||||||
|
withTestRepository { git =>
|
||||||
|
createFile(git, Constants.HEAD, "README.md", "body1", message = "commit1")
|
||||||
|
|
||||||
|
val branchId = git.getRepository.resolve("master")
|
||||||
|
val commit = JGitUtil.getRevCommitFromId(git, branchId)
|
||||||
|
|
||||||
|
createFile(git, Constants.HEAD, "LICENSE", "Apache License", message = "commit1")
|
||||||
|
createFile(git, Constants.HEAD, "README.md", "body1\nbody2", message = "commit1")
|
||||||
|
|
||||||
|
// latest commit
|
||||||
|
val diff1 = JGitUtil.getDiffs(git, None, "master", false, true)
|
||||||
|
assert(diff1.size == 1)
|
||||||
|
assert(diff1(0).changeType == ChangeType.MODIFY)
|
||||||
|
assert(diff1(0).oldPath == "README.md")
|
||||||
|
assert(diff1(0).newPath == "README.md")
|
||||||
|
assert(diff1(0).tooLarge == false)
|
||||||
|
assert(diff1(0).patch == Some("""@@ -1 +1,2 @@
|
||||||
|
|-body1
|
||||||
|
|\ No newline at end of file
|
||||||
|
|+body1
|
||||||
|
|+body2
|
||||||
|
|\ No newline at end of file""".stripMargin))
|
||||||
|
|
||||||
|
// from specified commit
|
||||||
|
val diff2 = JGitUtil.getDiffs(git, Some(commit.getName), "master", false, true)
|
||||||
|
assert(diff2.size == 2)
|
||||||
|
assert(diff2(0).changeType == ChangeType.ADD)
|
||||||
|
assert(diff2(0).oldPath == "/dev/null")
|
||||||
|
assert(diff2(0).newPath == "LICENSE")
|
||||||
|
assert(diff2(0).tooLarge == false)
|
||||||
|
assert(diff2(0).patch == Some("""+++ b/LICENSE
|
||||||
|
|@@ -0,0 +1 @@
|
||||||
|
|+Apache License
|
||||||
|
|\ No newline at end of file""".stripMargin))
|
||||||
|
|
||||||
|
assert(diff2(1).changeType == ChangeType.MODIFY)
|
||||||
|
assert(diff2(1).oldPath == "README.md")
|
||||||
|
assert(diff2(1).newPath == "README.md")
|
||||||
|
assert(diff2(1).tooLarge == false)
|
||||||
|
assert(diff2(1).patch == Some("""@@ -1 +1,2 @@
|
||||||
|
|-body1
|
||||||
|
|\ No newline at end of file
|
||||||
|
|+body1
|
||||||
|
|+body2
|
||||||
|
|\ No newline at end of file""".stripMargin))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test("getRevCommitFromId") {
|
||||||
|
withTestRepository { git =>
|
||||||
|
createFile(git, Constants.HEAD, "README.md", "body1", message = "commit1")
|
||||||
|
|
||||||
|
// branch name
|
||||||
|
val branchId = git.getRepository.resolve("master")
|
||||||
|
val commit1 = JGitUtil.getRevCommitFromId(git, branchId)
|
||||||
|
|
||||||
|
// commit id
|
||||||
|
val commitName = commit1.getName
|
||||||
|
val commitId = git.getRepository.resolve(commitName)
|
||||||
|
val commit2 = JGitUtil.getRevCommitFromId(git, commitId)
|
||||||
|
|
||||||
|
// tag name
|
||||||
|
JGitUtil.createTag(git, "1.0", None, commitName)
|
||||||
|
val tagId = git.getRepository.resolve("1.0")
|
||||||
|
val commit3 = JGitUtil.getRevCommitFromId(git, tagId)
|
||||||
|
|
||||||
|
// all refer same commit
|
||||||
|
assert(commit1 == commit2)
|
||||||
|
assert(commit1 == commit3)
|
||||||
|
assert(commit2 == commit3)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test("getCommitCount and getAllCommitIds") {
|
||||||
|
withTestRepository { git =>
|
||||||
|
// getCommitCount
|
||||||
|
createFile(git, Constants.HEAD, "README.md", "body1", message = "commit1")
|
||||||
|
assert(JGitUtil.getCommitCount(git, "master") == 1)
|
||||||
|
|
||||||
|
createFile(git, Constants.HEAD, "README.md", "body2", message = "commit2")
|
||||||
|
assert(JGitUtil.getCommitCount(git, "master") == 2)
|
||||||
|
|
||||||
|
// maximum limit
|
||||||
|
(3 to 10).foreach { i =>
|
||||||
|
createFile(git, Constants.HEAD, "README.md", "body" + i, message = "commit" + i)
|
||||||
|
}
|
||||||
|
assert(JGitUtil.getCommitCount(git, "master", 5) == 5)
|
||||||
|
|
||||||
|
// actual commit count
|
||||||
|
val gitLog = git.log.add(git.getRepository.resolve("master")).all
|
||||||
|
assert(gitLog.call.asScala.toSeq.size == 10)
|
||||||
|
|
||||||
|
// getAllCommitIds
|
||||||
|
val allCommits = JGitUtil.getAllCommitIds(git)
|
||||||
|
assert(allCommits.size == 10)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test("createBranch, branchesOfCommit and getBranches") {
|
||||||
|
withTestRepository { git =>
|
||||||
|
createFile(git, Constants.HEAD, "README.md", "body1", message = "commit1")
|
||||||
|
|
||||||
|
// createBranch
|
||||||
|
assert(JGitUtil.createBranch(git, "master", "test1") == Right("Branch created."))
|
||||||
|
assert(JGitUtil.createBranch(git, "master", "test2") == Right("Branch created."))
|
||||||
|
assert(JGitUtil.createBranch(git, "master", "test2") == Left("Sorry, that branch already exists."))
|
||||||
|
|
||||||
|
// verify
|
||||||
|
val branches = git.branchList.call()
|
||||||
|
assert(branches.size == 3)
|
||||||
|
assert(branches.get(0).getName == "refs/heads/master")
|
||||||
|
assert(branches.get(1).getName == "refs/heads/test1")
|
||||||
|
assert(branches.get(2).getName == "refs/heads/test2")
|
||||||
|
|
||||||
|
// getBranchesOfCommit
|
||||||
|
val commit = JGitUtil.getRevCommitFromId(git, git.getRepository.resolve("master"))
|
||||||
|
val branchesOfCommit = JGitUtil.getBranchesOfCommit(git, commit.getName)
|
||||||
|
assert(branchesOfCommit.size == 3)
|
||||||
|
assert(branchesOfCommit(0) == "master")
|
||||||
|
assert(branchesOfCommit(1) == "test1")
|
||||||
|
assert(branchesOfCommit(2) == "test2")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test("getBranches") {
|
||||||
|
withTestRepository { git =>
|
||||||
|
createFile(git, Constants.HEAD, "README.md", "body1", message = "commit1")
|
||||||
|
JGitUtil.createBranch(git, "master", "test1")
|
||||||
|
|
||||||
|
createFile(git, Constants.HEAD, "README.md", "body2", message = "commit2")
|
||||||
|
JGitUtil.createBranch(git, "master", "test2")
|
||||||
|
|
||||||
|
// getBranches
|
||||||
|
val branches = JGitUtil.getBranches(git, "master", true)
|
||||||
|
assert(branches.size == 3)
|
||||||
|
|
||||||
|
assert(branches(0).name == "master")
|
||||||
|
assert(branches(0).committerName == "dummy")
|
||||||
|
assert(branches(0).committerEmailAddress == "dummy@example.com")
|
||||||
|
|
||||||
|
assert(branches(1).name == "test1")
|
||||||
|
assert(branches(1).committerName == "dummy")
|
||||||
|
assert(branches(1).committerEmailAddress == "dummy@example.com")
|
||||||
|
|
||||||
|
assert(branches(2).name == "test2")
|
||||||
|
assert(branches(2).committerName == "dummy")
|
||||||
|
assert(branches(2).committerEmailAddress == "dummy@example.com")
|
||||||
|
|
||||||
|
assert(branches(0).commitId != branches(1).commitId)
|
||||||
|
assert(branches(0).commitId == branches(2).commitId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test("createTag, getTagsOfCommit and getTagsOnCommit") {
|
||||||
|
withTestRepository { git =>
|
||||||
|
createFile(git, Constants.HEAD, "README.md", "body1", message = "commit1")
|
||||||
|
|
||||||
|
// createTag
|
||||||
|
assert(JGitUtil.createTag(git, "1.0", Some("test1"), "master") == Right("Tag added."))
|
||||||
|
assert(
|
||||||
|
JGitUtil.createTag(git, "1.0", Some("test2"), "master") == Left("Sorry, some Git operation error occurs.")
|
||||||
|
)
|
||||||
|
|
||||||
|
// record current commit
|
||||||
|
val commit = JGitUtil.getRevCommitFromId(git, git.getRepository.resolve("master"))
|
||||||
|
|
||||||
|
// createTag
|
||||||
|
createFile(git, Constants.HEAD, "LICENSE", "Apache License", message = "commit2")
|
||||||
|
assert(JGitUtil.createTag(git, "1.1", Some("test3"), "master") == Right("Tag added."))
|
||||||
|
|
||||||
|
// verify
|
||||||
|
val allTags = git.tagList().call().asScala
|
||||||
|
assert(allTags.size == 2)
|
||||||
|
assert(allTags(0).getName == "refs/tags/1.0")
|
||||||
|
assert(allTags(1).getName == "refs/tags/1.1")
|
||||||
|
|
||||||
|
// getTagsOfCommit
|
||||||
|
val tagsOfCommit = JGitUtil.getTagsOfCommit(git, commit.getName)
|
||||||
|
assert(tagsOfCommit.size == 2)
|
||||||
|
assert(tagsOfCommit(0) == "1.1")
|
||||||
|
assert(tagsOfCommit(1) == "1.0")
|
||||||
|
|
||||||
|
// getTagsOnCommit
|
||||||
|
val tagsOnCommit = JGitUtil.getTagsOnCommit(git, "master")
|
||||||
|
assert(tagsOnCommit.size == 1)
|
||||||
|
assert(tagsOnCommit(0) == "1.1")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test("openFile for non-LFS file") {
|
||||||
|
withTestRepository { git =>
|
||||||
|
createFile(git, Constants.HEAD, "README.md", "body1", message = "commit1")
|
||||||
|
createFile(git, Constants.HEAD, "LICENSE", "Apache License", message = "commit2")
|
||||||
|
|
||||||
|
val objectId = git.getRepository.resolve("master")
|
||||||
|
val commit = JGitUtil.getRevCommitFromId(git, objectId)
|
||||||
|
|
||||||
|
// Since Non-LFS file doesn't need RepositoryInfo give null
|
||||||
|
assert(JGitUtil.openFile(git, null, commit.getTree, "README.md") { in =>
|
||||||
|
IOUtils.toString(in, "UTF-8")
|
||||||
|
} == "body1")
|
||||||
|
|
||||||
|
assert(JGitUtil.openFile(git, null, commit.getTree, "LICENSE") { in =>
|
||||||
|
IOUtils.toString(in, "UTF-8")
|
||||||
|
} == "Apache License")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test("getContentFromPath") {
|
||||||
|
withTestRepository { git =>
|
||||||
|
createFile(git, Constants.HEAD, "README.md", "body1", message = "commit1")
|
||||||
|
createFile(git, Constants.HEAD, "LARGE_FILE", "body1" * 1000000, message = "commit1")
|
||||||
|
|
||||||
|
val objectId = git.getRepository.resolve("master")
|
||||||
|
val commit = JGitUtil.getRevCommitFromId(git, objectId)
|
||||||
|
|
||||||
|
val content1 = JGitUtil.getContentFromPath(git, commit.getTree, "README.md", true)
|
||||||
|
assert(content1.map(x => new String(x, "UTF-8")) == Some("body1"))
|
||||||
|
|
||||||
|
val content2 = JGitUtil.getContentFromPath(git, commit.getTree, "LARGE_FILE", false)
|
||||||
|
assert(content2.isEmpty)
|
||||||
|
|
||||||
|
val content3 = JGitUtil.getContentFromPath(git, commit.getTree, "LARGE_FILE", true)
|
||||||
|
assert(content3.map(x => new String(x, "UTF-8")) == Some("body1" * 1000000))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test("getBlame") {
|
||||||
|
withTestRepository { git =>
|
||||||
|
createFile(git, Constants.HEAD, "README.md", "body1\nbody2\nbody3", message = "commit1")
|
||||||
|
createFile(git, Constants.HEAD, "README.md", "body0\nbody2\nbody3", message = "commit2")
|
||||||
|
|
||||||
|
val blames = JGitUtil.getBlame(git, "master", "README.md").toSeq
|
||||||
|
|
||||||
|
assert(blames.size == 2)
|
||||||
|
assert(blames(0).message == "commit2")
|
||||||
|
assert(blames(0).lines == Set(0))
|
||||||
|
assert(blames(1).message == "commit1")
|
||||||
|
assert(blames(1).lines == Set(1, 2))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
test("getFileList(git: Git, revision: String, path)") {
|
test("getFileList(git: Git, revision: String, path)") {
|
||||||
withTestRepository { git =>
|
withTestRepository { git =>
|
||||||
def list(branch: String, path: String) =
|
def list(branch: String, path: String) =
|
||||||
@@ -170,4 +430,19 @@ class JGitUtilSpec extends FunSuite {
|
|||||||
assert(list("master", "test") == List(("text2.txt", "commit2", false)))
|
assert(list("master", "test") == List(("text2.txt", "commit2", false)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test("getLfsObjects") {
|
||||||
|
val str = """version https://git-lfs.github.com/spec/v1
|
||||||
|
|oid sha256:aa8a7a4903572ccd1571c03f442661a983d79b53bbb7bcdd50769429f0b24ab8
|
||||||
|
|size 178643""".stripMargin
|
||||||
|
|
||||||
|
val attrs = JGitUtil.getLfsObjects(str)
|
||||||
|
assert(attrs("oid") == "sha256:aa8a7a4903572ccd1571c03f442661a983d79b53bbb7bcdd50769429f0b24ab8")
|
||||||
|
assert(attrs("size") == "178643")
|
||||||
|
}
|
||||||
|
|
||||||
|
test("getContentInfo") {
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user