@@ -11,8 +11,7 @@ import gitbucket.core.util.ControlUtil._
1111import gitbucket .core .util .Implicits ._
1212import gitbucket .core .view .helpers ._
1313
14-
15- import gitbucket .gist .model .{GistUser , Gist }
14+ import gitbucket .gist .model ._
1615import gitbucket .gist .service .GistService
1716import gitbucket .gist .util ._
1817import 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 }
0 commit comments