Save diff fragment as a file instead of database

This commit is contained in:
Naoki Takezoe
2018-04-27 14:57:59 +09:00
parent dd1dbd429c
commit 86903a7a22
8 changed files with 108 additions and 28 deletions

View File

@@ -7,8 +7,4 @@
<addPrimaryKey constraintName="IDX_ACCOUNT_EXTRA_MAIL_ADDRESS_PK" tableName="ACCOUNT_EXTRA_MAIL_ADDRESS" columnNames="USER_NAME, EXTRA_MAIL_ADDRESS"/>
<addUniqueConstraint constraintName="IDX_ACCOUNT_EXTRA_MAIL_ADDRESS_1" tableName="ACCOUNT_EXTRA_MAIL_ADDRESS" columnNames="EXTRA_MAIL_ADDRESS"/>
<addColumn tableName="COMMIT_COMMENT">
<column name="DIFF" type="text" nullable="true" />
</addColumn>
</changeSet>

View File

@@ -562,9 +562,24 @@ trait RepositoryViewerControllerBase extends ControllerBase {
form.fileName,
form.oldLineNumber,
form.newLineNumber,
form.issueId,
form.diff
form.issueId
)
for {
fileName <- form.fileName
diff <- form.diff
} {
saveCommitCommentDiff(
repository.owner,
repository.name,
id,
fileName,
form.oldLineNumber,
form.newLineNumber,
diff
)
}
form.issueId match {
case Some(issueId) =>
recordCommentPullRequestActivity(
@@ -614,10 +629,24 @@ trait RepositoryViewerControllerBase extends ControllerBase {
form.fileName,
form.oldLineNumber,
form.newLineNumber,
form.issueId,
form.diff
form.issueId
)
for {
fileName <- form.fileName
diff <- form.diff
} {
saveCommitCommentDiff(
repository.owner,
repository.name,
id,
fileName,
form.oldLineNumber,
form.newLineNumber,
diff
)
}
val comment = getCommitComment(repository.owner, repository.name, commentId.toString).get
form.issueId match {
case Some(issueId) =>

View File

@@ -54,7 +54,6 @@ trait CommitCommentComponent extends TemplateComponent { self: Profile =>
val registeredDate = column[java.util.Date]("REGISTERED_DATE")
val updatedDate = column[java.util.Date]("UPDATED_DATE")
val issueId = column[Option[Int]]("ISSUE_ID")
val diff = column[Option[String]]("DIFF")
def * =
(
userName,
@@ -68,8 +67,7 @@ trait CommitCommentComponent extends TemplateComponent { self: Profile =>
newLine,
registeredDate,
updatedDate,
issueId,
diff
issueId
) <> (CommitComment.tupled, CommitComment.unapply)
def byPrimaryKey(commentId: Int) = this.commentId === commentId.bind
@@ -88,13 +86,13 @@ case class CommitComment(
newLine: Option[Int],
registeredDate: java.util.Date,
updatedDate: java.util.Date,
issueId: Option[Int],
diff: Option[String]
issueId: Option[Int]
) extends Comment
case class CommitComments(
fileName: String,
commentedUserName: String,
registeredDate: Date,
comments: Seq[CommitComment]
comments: Seq[CommitComment],
diff: Option[String]
) extends Comment

View File

@@ -1,9 +1,14 @@
package gitbucket.core.service
import java.io.File
import gitbucket.core.model.CommitComment
import gitbucket.core.model.Profile._
import gitbucket.core.model.Profile.profile.blockingApi._
import gitbucket.core.model.Profile.dateColumnType
import gitbucket.core.util.Directory._
import gitbucket.core.util.StringUtil
import org.apache.commons.io.FileUtils
trait CommitsService {
@@ -31,8 +36,7 @@ trait CommitsService {
fileName: Option[String],
oldLine: Option[Int],
newLine: Option[Int],
issueId: Option[Int],
diff: Option[String]
issueId: Option[Int]
)(implicit s: Session): Int =
CommitComments returning CommitComments.map(_.commentId) insert CommitComment(
userName = owner,
@@ -45,8 +49,7 @@ trait CommitsService {
newLine = newLine,
registeredDate = currentDate,
updatedDate = currentDate,
issueId = issueId,
diff = diff
issueId = issueId
)
def updateCommitCommentPosition(commentId: Int, commitId: String, oldLine: Option[Int], newLine: Option[Int])(
@@ -70,4 +73,48 @@ trait CommitsService {
def deleteCommitComment(commentId: Int)(implicit s: Session) =
CommitComments filter (_.byPrimaryKey(commentId)) delete
def saveCommitCommentDiff(
owner: String,
repository: String,
commitId: String,
fileName: String,
oldLine: Option[Int],
newLine: Option[Int],
diffJson: String
): Unit = {
val dir = new java.io.File(getDiffDir(owner, repository), commitId)
if (!dir.exists) {
dir.mkdirs()
}
val file = diffFile(dir, fileName, oldLine, newLine)
FileUtils.write(file, diffJson, "UTF-8")
}
def loadCommitCommentDiff(
owner: String,
repository: String,
commitId: String,
fileName: String,
oldLine: Option[Int],
newLine: Option[Int]
): Option[String] = {
val dir = new java.io.File(getDiffDir(owner, repository), commitId)
val file = diffFile(dir, fileName, oldLine, newLine)
if (file.exists) {
Option(FileUtils.readFileToString(file, "UTF-8"))
} else None
}
private def diffFile(dir: java.io.File, fileName: String, oldLine: Option[Int], newLine: Option[Int]): File = {
new File(
dir,
StringUtil.sha1(
fileName +
"_oldLine:" + oldLine.map(_.toString).getOrElse("") +
"_newLine:" + newLine.map(_.toString).getOrElse("")
)
)
}
}

View File

@@ -163,9 +163,9 @@ trait PullRequestService { self: IssuesService with CommitsService =>
// Collect comment positions
val positions = getCommitComments(pullreq.userName, pullreq.repositoryName, pullreq.commitIdTo, true)
.collect {
case CommitComment(_, _, _, commentId, _, _, Some(file), None, Some(newLine), _, _, _, _) =>
case CommitComment(_, _, _, commentId, _, _, Some(file), None, Some(newLine), _, _, _) =>
(file, commentId, Right(newLine))
case CommitComment(_, _, _, commentId, _, _, Some(file), Some(oldLine), None, _, _, _, _) =>
case CommitComment(_, _, _, commentId, _, _, Some(file), Some(oldLine), None, _, _, _) =>
(file, commentId, Left(oldLine))
}
.groupBy { case (file, _, _) => file }
@@ -338,12 +338,20 @@ trait PullRequestService { self: IssuesService with CommitsService =>
case ((Some(_), _, _, _), comments) =>
comments.head
// Comment on a specific line of a commit
case ((None, Some(fileName), _, _), comments) =>
case ((None, Some(fileName), oldLine, newLine), comments) =>
gitbucket.core.model.CommitComments(
fileName = fileName,
commentedUserName = comments.head.commentedUserName,
registeredDate = comments.head.registeredDate,
comments = comments.map(_.asInstanceOf[CommitComment])
comments = comments.map(_.asInstanceOf[CommitComment]),
diff = loadCommitCommentDiff(
userName,
repositoryName,
comments.head.asInstanceOf[CommitComment].commitId,
fileName,
oldLine,
newLine
)
)
// Comment on a specific commit
case (_, comments) =>

View File

@@ -67,6 +67,12 @@ object Directory {
def getLfsDir(owner: String, repository: String): File =
new File(getRepositoryFilesDir(owner, repository), "lfs")
/**
* Directory for files which store diff fragment
*/
def getDiffDir(owner: String, repository: String): File =
new File(getRepositoryFilesDir(owner, repository), "diff")
/**
* Directory for uploaded files by the specified user.
*/

View File

@@ -10,11 +10,7 @@
<a href="@helpers.url(repository)/commit/@comments.comments.head.commitId" class="monospace">@comments.comments.head.commitId.substring(0, 7)</a>
</span>
</div>
@comments.comments.headOption.map { comment =>
@comment.diff.map { diff =>
@helpers.diff(diff)
}
}
@comments.diff.map(helpers.diff)
<div class="panel-body">
@comments.comments.map { comment =>
@gitbucket.core.helper.html.commitcomment(comment, hasWritePermission, repository, latestCommitId)

View File

@@ -237,7 +237,7 @@
}
case comment: gitbucket.core.model.CommitComment => {
@gitbucket.core.helper.html.commitcomments(gitbucket.core.model.CommitComments(
comment.fileName.getOrElse(""), comment.commentedUserName, comment.registeredDate, Seq(comment)
comment.fileName.getOrElse(""), comment.commentedUserName, comment.registeredDate, Seq(comment), None
), isManageable, repository, pullreq.map(_.commitIdTo))
}
}