diff --git a/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala b/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala index 07f706c..1ba4290 100644 --- a/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala +++ b/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala @@ -22,6 +22,7 @@ import org.eclipse.jgit.api.{ArchiveCommand, Git} import org.eclipse.jgit.archive.{TgzFormat, ZipFormat} import org.eclipse.jgit.dircache.DirCache +import org.eclipse.jgit.errors.MissingObjectException import org.eclipse.jgit.lib._ import org.eclipse.jgit.revwalk.RevCommit import org.eclipse.jgit.treewalk._ @@ -344,16 +345,21 @@ get("/:owner/:repository/commit/:id")(referrersOnly { repository => val id = params("id") - using(Git.open(getRepositoryDir(repository.owner, repository.name))){ git => - defining(JGitUtil.getRevCommitFromId(git, git.getRepository.resolve(id))){ revCommit => - JGitUtil.getDiffs(git, id) match { case (diffs, oldCommitId) => - html.commit(id, new JGitUtil.CommitInfo(revCommit), - JGitUtil.getBranchesOfCommit(git, revCommit.getName), - JGitUtil.getTagsOfCommit(git, revCommit.getName), - getCommitComments(repository.owner, repository.name, id, false), - repository, diffs, oldCommitId, hasWritePermission(repository.owner, repository.name, context.loginAccount)) + try { + using(Git.open(getRepositoryDir(repository.owner, repository.name))) { git => + defining(JGitUtil.getRevCommitFromId(git, git.getRepository.resolve(id))) { revCommit => + JGitUtil.getDiffs(git, id) match { + case (diffs, oldCommitId) => + html.commit(id, new JGitUtil.CommitInfo(revCommit), + JGitUtil.getBranchesOfCommit(git, revCommit.getName), + JGitUtil.getTagsOfCommit(git, revCommit.getName), + getCommitComments(repository.owner, repository.name, id, false), + repository, diffs, oldCommitId, hasWritePermission(repository.owner, repository.name, context.loginAccount)) + } } } + } catch { + case e:MissingObjectException => NotFound } }) diff --git a/src/main/scala/gitbucket/core/view/LinkConverter.scala b/src/main/scala/gitbucket/core/view/LinkConverter.scala index 639ae31..7792626 100644 --- a/src/main/scala/gitbucket/core/view/LinkConverter.scala +++ b/src/main/scala/gitbucket/core/view/LinkConverter.scala @@ -10,28 +10,64 @@ * Converts issue id, username and commit id to link. */ protected def convertRefsLinks(value: String, repository: RepositoryService.RepositoryInfo, - issueIdPrefix: String = "#", escapeHtml: Boolean = true)(implicit context: Context): String = { + issueIdPrefix: String = "#", escapeHtml: Boolean = true)(implicit context: Context): String = { // escape HTML tags val escaped = if(escapeHtml) value.replace("&", "&").replace("<", "<").replace(">", ">").replace("\"", """) else value escaped - // convert issue id to link - .replaceBy(("(?<=(^|\\W))" + issueIdPrefix + "([0-9]+)(?=(\\W|$))").r){ m => - getIssue(repository.owner, repository.name, m.group(2)) match { - case Some(issue) if(issue.isPullRequest) - => Some(s"""#${m.group(2)}""") - case Some(_) => Some(s"""#${m.group(2)}""") - case None => Some(s"""#${m.group(2)}""") - } + // convert username/project@SHA to link + .replaceBy("(?<=(^|\\W))([a-zA-Z0-9\\-_]+)/([a-zA-Z0-9\\-_\\.]+)@([a-f0-9]{40})(?=(\\W|$))".r){ m => + getAccountByUserName(m.group(2)).map { _ => + s"""${m.group(2)}/${m.group(3)}@${m.group(4).substring(0, 7)}""" } + } + + // convert username/project#Num to link + .replaceBy( ("(?<=(^|\\W))([a-zA-Z0-9\\-_]+)/([a-zA-Z0-9\\-_\\.]+)" + issueIdPrefix + "([0-9]+)(?=(\\W|$))").r){ m => + getIssue(m.group(2), m.group(3), m.group(4)) match { + case Some(issue) if (issue.isPullRequest) + => Some( s"""${m.group(2)}/${m.group(3)}#${m.group(4)}""") + case Some(_) => Some( s"""${m.group(2)}/${m.group(3)}#${m.group(4)}""") + case None => Some( s"""${m.group(2)}/${m.group(3)}#${m.group(4)}""") + } + } + + // convert username@SHA to link + .replaceBy( ("(?<=(^|\\W))([a-zA-Z0-9\\-_]+)@([a-f0-9]{40})(?=(\\W|$))").r ) { m => + getAccountByUserName(m.group(2)).map { _ => + s"""${m.group(2)}@${m.group(3).substring(0, 7)}""" + } + } + + // convert username#Num to link + .replaceBy( ("(?<=(^|\\W))([a-zA-Z0-9\\-_]+)" + issueIdPrefix + "([0-9]+)(?=(\\W|$))").r ) { m => + getIssue(m.group(2), repository.name, m.group(3)) match { + case Some(issue) if(issue.isPullRequest) + => Some(s"""${m.group(2)}#${m.group(3)}""") + case Some(_) => Some(s"""${m.group(2)}#${m.group(3)}""") + case None => Some(s"""${m.group(2)}#${m.group(3)}""") + } + } + + // convert issue id to link + .replaceBy(("(?<=(^|\\W))(GH-|" + issueIdPrefix + ")([0-9]+)(?=(\\W|$))").r){ m => + getIssue(repository.owner, repository.name, m.group(3)) match { + case Some(issue) if(issue.isPullRequest) + => Some(s"""${m.group(2)}${m.group(3)}""") + case Some(_) => Some(s"""${m.group(2)}${m.group(3)}""") + case None => Some(s"""${m.group(2)}${m.group(3)}""") + } + } + // convert @username to link .replaceBy("(?<=(^|\\W))@([a-zA-Z0-9\\-_\\.]+)(?=(\\W|$))".r){ m => getAccountByUserName(m.group(2)).map { _ => s"""@${m.group(2)}""" } } + // convert commit id to link - .replaceAll("(?<=(^|\\W))([a-f0-9]{40})(?=(\\W|$))", s"""$$2""") + .replaceAll("(?<=(^|[^\\w/@]))([a-f0-9]{40})(?=(\\W|$))", s"""$$2""") } }