diff --git a/src/main/resources/update/gitbucket-core_4.24.xml b/src/main/resources/update/gitbucket-core_4.24.xml index 5faaac3..03249f5 100644 --- a/src/main/resources/update/gitbucket-core_4.24.xml +++ b/src/main/resources/update/gitbucket-core_4.24.xml @@ -7,8 +7,4 @@ - - - - diff --git a/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala b/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala index 3207e51..b3b7685 100644 --- a/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala +++ b/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala @@ -562,9 +562,24 @@ 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 @@ 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) => diff --git a/src/main/scala/gitbucket/core/model/Comment.scala b/src/main/scala/gitbucket/core/model/Comment.scala index b4f081e..8e5044c 100644 --- a/src/main/scala/gitbucket/core/model/Comment.scala +++ b/src/main/scala/gitbucket/core/model/Comment.scala @@ -54,7 +54,6 @@ 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 @@ newLine, registeredDate, updatedDate, - issueId, - diff + issueId ) <> (CommitComment.tupled, CommitComment.unapply) def byPrimaryKey(commentId: Int) = this.commentId === commentId.bind @@ -88,13 +86,13 @@ 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 diff --git a/src/main/scala/gitbucket/core/service/CommitsService.scala b/src/main/scala/gitbucket/core/service/CommitsService.scala index f4969ac..237762d 100644 --- a/src/main/scala/gitbucket/core/service/CommitsService.scala +++ b/src/main/scala/gitbucket/core/service/CommitsService.scala @@ -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 @@ 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 @@ 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 @@ 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("") + ) + ) + } + } diff --git a/src/main/scala/gitbucket/core/service/PullRequestService.scala b/src/main/scala/gitbucket/core/service/PullRequestService.scala index 9dd3d15..f50a068 100644 --- a/src/main/scala/gitbucket/core/service/PullRequestService.scala +++ b/src/main/scala/gitbucket/core/service/PullRequestService.scala @@ -163,9 +163,9 @@ // 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 @@ 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) => diff --git a/src/main/scala/gitbucket/core/util/Directory.scala b/src/main/scala/gitbucket/core/util/Directory.scala index 6e8a0b8..416b7a5 100644 --- a/src/main/scala/gitbucket/core/util/Directory.scala +++ b/src/main/scala/gitbucket/core/util/Directory.scala @@ -68,6 +68,12 @@ 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. */ def getUserUploadDir(userName: String): File = diff --git a/src/main/twirl/gitbucket/core/helper/commitcomments.scala.html b/src/main/twirl/gitbucket/core/helper/commitcomments.scala.html index 60f7b81..cbfbc8a 100644 --- a/src/main/twirl/gitbucket/core/helper/commitcomments.scala.html +++ b/src/main/twirl/gitbucket/core/helper/commitcomments.scala.html @@ -10,11 +10,7 @@ @comments.comments.head.commitId.substring(0, 7) - @comments.comments.headOption.map { comment => - @comment.diff.map { diff => - @helpers.diff(diff) - } - } + @comments.diff.map(helpers.diff)
@comments.comments.map { comment => @gitbucket.core.helper.html.commitcomment(comment, hasWritePermission, repository, latestCommitId) diff --git a/src/main/twirl/gitbucket/core/issues/commentlist.scala.html b/src/main/twirl/gitbucket/core/issues/commentlist.scala.html index 05fa53e..4957327 100644 --- a/src/main/twirl/gitbucket/core/issues/commentlist.scala.html +++ b/src/main/twirl/gitbucket/core/issues/commentlist.scala.html @@ -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)) } }