Skip to content

Commit ddb9243

Browse files
committed
Display status at the Gist list and fix styles
1 parent f188481 commit ddb9243

File tree

6 files changed

+52
-57
lines changed

6 files changed

+52
-57
lines changed

src/main/resources/images/code.png

1.39 KB
Loading

src/main/scala/Plugin.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class Plugin extends gitbucket.core.plugin.Plugin {
4949
"images/menu-revisions.png" -> fromClassPath("images/menu-revisions.png"),
5050
"images/menu-forks-active.png" -> fromClassPath("images/menu-forks-active.png"),
5151
"images/menu-forks.png" -> fromClassPath("images/menu-forks.png"),
52+
"images/code.png" -> fromClassPath("images/code.png"),
5253
"images/snippet.png" -> fromClassPath("images/snippet.png")
5354
)
5455

src/main/scala/gitbucket/gist/controller/GistController.scala

Lines changed: 30 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ import gitbucket.core.util.ControlUtil._
1111
import gitbucket.core.util.Implicits._
1212
import gitbucket.core.view.helpers._
1313

14-
15-
import gitbucket.gist.model.{GistUser, Gist}
14+
import gitbucket.gist.model._
1615
import gitbucket.gist.service.GistService
1716
import gitbucket.gist.util._
1817
import gitbucket.gist.util.GistUtils._
@@ -41,19 +40,11 @@ trait GistControllerBase extends ControllerBase {
4140
val result = getPublicGists((page - 1) * Limit, Limit)
4241
val count = countPublicGists()
4342

44-
val gists: Seq[(Gist, String, String)] = result.map { gist =>
45-
val gitdir = new File(GistRepoDir, gist.userName + "/" + gist.repositoryName)
46-
if(gitdir.exists){
47-
using(Git.open(gitdir)){ git =>
48-
val (fileName, source) = JGitUtil.getFileList(git, "master", ".").map { file =>
49-
file.name -> StringUtil.convertFromByteArray(JGitUtil.getContentFromId(git, file.id, true).get).split("\n").take(9).mkString("\n")
50-
}.head
43+
val gists: Seq[(Gist, GistInfo)] = result.map { gist =>
44+
val files = getGistFiles(gist.userName, gist.repositoryName)
45+
val (fileName, source) = files.head
5146

52-
(gist, fileName, source)
53-
}
54-
} else {
55-
(gist, "", "Repository is not found!")
56-
}
47+
(gist, GistInfo(fileName, source, files.length, getForkedCount(gist.userName, gist.repositoryName)))
5748
}
5849

5950
html.list(None, gists, page, page * Limit < count)
@@ -340,48 +331,38 @@ trait GistControllerBase extends ControllerBase {
340331
countUserGists(userName, context.loginAccount.map(_.userName))
341332
)
342333

343-
val gists: Seq[(Gist, String, String)] = result._1.map { gist =>
334+
val gists: Seq[(Gist, GistInfo)] = result._1.map { gist =>
344335
val repoName = gist.repositoryName
345-
val gitdir = new File(GistRepoDir, userName + "/" + repoName)
346-
if(gitdir.exists){
347-
using(Git.open(gitdir)){ git =>
348-
val (fileName, source) = JGitUtil.getFileList(git, revision, ".").map { file =>
349-
file.name -> StringUtil.convertFromByteArray(JGitUtil.getContentFromId(git, file.id, true).get).split("\n").take(9).mkString("\n")
350-
}.head
351-
352-
(gist, fileName, source)
353-
}
354-
} else {
355-
(gist, "", "Repository is not found!")
356-
}
336+
val files = getGistFiles(userName, repoName, revision)
337+
val (fileName, source) = files.head
338+
(gist, GistInfo(fileName, source, files.length, getForkedCount(userName, repoName)))
357339
}
358340

359341
val fullName = getAccountByUserName(userName).get.fullName
360342
html.list(Some(GistUser(userName, fullName)), gists, page, page * Limit < result._2)
361343
}
362344
case Some(repoName) => {
363-
val gitdir = new File(GistRepoDir, userName + "/" + repoName)
364-
if(gitdir.exists){
365-
using(Git.open(gitdir)){ git =>
366-
val gist = getGist(userName, repoName).get
367-
val originUserName = gist.originUserName.getOrElse(userName)
368-
val originRepoName = gist.originRepositoryName.getOrElse(repoName)
369-
370-
//if(!gist.isPrivate || context.loginAccount.exists(x => x.isAdmin || x.userName == userName)){
371-
val files: Seq[(String, String)] = JGitUtil.getFileList(git, revision, ".").map { file =>
372-
file.name -> StringUtil.convertFromByteArray(JGitUtil.getContentFromId(git, file.id, true).get)
373-
}
374-
html.detail(
375-
gist,
376-
getForkedCount(originUserName, originRepoName),
377-
GistRepositoryURL(gist, baseUrl, context.settings),
378-
revision,
379-
files,
380-
isEditable(userName)
381-
)
382-
//} else Unauthorized
383-
}
384-
} else NotFound
345+
val gist = getGist(userName, repoName).get
346+
val originUserName = gist.originUserName.getOrElse(userName)
347+
val originRepoName = gist.originRepositoryName.getOrElse(repoName)
348+
349+
html.detail(
350+
gist,
351+
getForkedCount(originUserName, originRepoName),
352+
GistRepositoryURL(gist, baseUrl, context.settings),
353+
revision,
354+
getGistFiles(userName, repoName, revision),
355+
isEditable(userName)
356+
)
357+
}
358+
}
359+
}
360+
361+
private def getGistFiles(userName: String, repoName: String, revision: String = "master"): Seq[(String, String)] = {
362+
val gitdir = new File(GistRepoDir, userName + "/" + repoName)
363+
using(Git.open(gitdir)){ git =>
364+
JGitUtil.getFileList(git, revision, ".").map { file =>
365+
file.name -> StringUtil.convertFromByteArray(JGitUtil.getContentFromId(git, file.id, true).get)
385366
}
386367
}
387368
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package gitbucket.gist.model
2+
3+
case class GistInfo(fileName: String, source: String, fileCount: Int, forkedCount: Int)

src/main/twirl/gitbucket/gist/header.scala.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
}
3333
</div>
3434
</div>
35-
<div class="muted" style="margin-top: -10px; margin-left: 30px;">
35+
<div class="muted small" style="margin-top: -10px; margin-left: 30px;">
3636
Created at @gist.registeredDate
3737
@if(gist.originUserName.isDefined){
3838
- forked from <a href="@path/gist/@gist.originUserName/@gist.originRepositoryName">@gist.originUserName/@gist.originRepositoryName</a>

src/main/twirl/gitbucket/gist/list.scala.html

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
@(gistUser: Option[gitbucket.gist.model.GistUser],
2-
gists: Seq[(gitbucket.gist.model.Gist, String, String)],
2+
gists: Seq[(gitbucket.gist.model.Gist, gitbucket.gist.model.GistInfo)],
33
page: Int,
44
hasNext: Boolean)(implicit context: gitbucket.core.controller.Context)
55
@import context._
66
@import gitbucket.core.view.helpers._
77
@import gitbucket.core.service.RepositoryService.RepositoryInfo
8+
@import gitbucket.core.plugin.Images._
89
@gitbucket.core.html.main(gistUser.map(user => s"${user.userName}'s Snippets").getOrElse("")){
910
@gitbucket.gist.html.style()
1011
<div class="container">
@@ -25,7 +26,7 @@
2526
</div>
2627
<hr style="margin-bottom: 20px;"/>
2728
<div class="container body">
28-
@gists.map { case (gist, fileName, code) =>
29+
@gists.map { case (gist, gistInfo) =>
2930
<div>
3031
<div>
3132
@avatar(gist.userName, 20)
@@ -34,17 +35,26 @@
3435
@if(gist.isPrivate){
3536
<span class="label label-warning">Secret</span>
3637
}
38+
<div class="pull-right">
39+
<a href="@path/gist/@gist.userName/@gist.repositoryName" class="header-link">
40+
<img src="@dataURI("images/code.png")"> <strong>@gistInfo.fileCount @plural(gistInfo.fileCount, "file")</strong>
41+
</a>
42+
&nbsp;&nbsp;
43+
<a href="@path/gist/@gist.userName/@gist.repositoryName/forks" class="header-link">
44+
<img src="@dataURI("images/menu-forks.png")"> <strong>@gistInfo.forkedCount @plural(gistInfo.forkedCount, "fork")</strong>
45+
</a>
46+
</div>
3747
</div>
38-
<div class="muted" style="margin-left: 22px;">
48+
<div class="muted small" style="margin-left: 22px;">
3949
Created at @gist.registeredDate
4050
</div>
41-
<div style="margin-left: 22px; margin-top: 10px; margin-bottom: 10px;">
51+
<div class="small" style="margin-left: 22px; margin-top: 2px; margin-bottom: 2px;">
4252
@gist.description
4353
</div>
4454
<div>
45-
@if(isRenderable(fileName)){
55+
@if(isRenderable(gistInfo.fileName)){
4656
<div class="list-markup box-content markdown-body">
47-
@renderMarkup(List(fileName), code, "master", RepositoryInfo(
57+
@renderMarkup(List(gistInfo.fileName), gistInfo.source, "master", RepositoryInfo(
4858
owner = gist.userName,
4959
name = gist.repositoryName,
5060
httpUrl = "",
@@ -59,7 +69,7 @@
5969
), false, false, false)
6070
</div>
6171
} else {
62-
<pre class="list-code prettyprint linenums">@Html(code)</pre>
72+
<pre class="list-code prettyprint linenums">@Html(gistInfo.source)</pre>
6373
}
6474
</div>
6575
</div>

0 commit comments

Comments
 (0)