mirror of https://github.com/gogits/gogs.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
368 lines
9.6 KiB
368 lines
9.6 KiB
10 years ago
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
||
|
// Use of this source code is governed by a MIT-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
|
package repo
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
8 years ago
|
"fmt"
|
||
|
gotemplate "html/template"
|
||
10 years ago
|
"io/ioutil"
|
||
|
"path"
|
||
|
"strings"
|
||
|
|
||
9 years ago
|
"github.com/Unknwon/paginater"
|
||
8 years ago
|
log "gopkg.in/clog.v1"
|
||
9 years ago
|
|
||
9 years ago
|
"github.com/gogits/git-module"
|
||
9 years ago
|
|
||
10 years ago
|
"github.com/gogits/gogs/models"
|
||
8 years ago
|
"github.com/gogits/gogs/pkg/context"
|
||
|
"github.com/gogits/gogs/pkg/markup"
|
||
|
"github.com/gogits/gogs/pkg/setting"
|
||
|
"github.com/gogits/gogs/pkg/template"
|
||
|
"github.com/gogits/gogs/pkg/template/highlight"
|
||
8 years ago
|
"github.com/gogits/gogs/pkg/tool"
|
||
10 years ago
|
)
|
||
|
|
||
|
const (
|
||
8 years ago
|
BARE = "repo/bare"
|
||
|
HOME = "repo/home"
|
||
|
WATCHERS = "repo/watchers"
|
||
|
FORKS = "repo/forks"
|
||
10 years ago
|
)
|
||
|
|
||
8 years ago
|
func renderDirectory(c *context.Context, treeLink string) {
|
||
|
tree, err := c.Repo.Commit.SubTree(c.Repo.TreePath)
|
||
8 years ago
|
if err != nil {
|
||
8 years ago
|
c.NotFoundOrServerError("Repo.Commit.SubTree", git.IsErrNotExist, err)
|
||
8 years ago
|
return
|
||
8 years ago
|
}
|
||
10 years ago
|
|
||
8 years ago
|
entries, err := tree.ListEntries()
|
||
|
if err != nil {
|
||
8 years ago
|
c.ServerError("ListEntries", err)
|
||
8 years ago
|
return
|
||
10 years ago
|
}
|
||
8 years ago
|
entries.Sort()
|
||
10 years ago
|
|
||
8 years ago
|
c.Data["Files"], err = entries.GetCommitsInfoWithCustomConcurrency(c.Repo.Commit, c.Repo.TreePath, setting.Repository.CommitsFetchConcurrency)
|
||
8 years ago
|
if err != nil {
|
||
8 years ago
|
c.ServerError("GetCommitsInfoWithCustomConcurrency", err)
|
||
10 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
var readmeFile *git.Blob
|
||
|
for _, entry := range entries {
|
||
8 years ago
|
if entry.IsDir() || !markup.IsReadmeFile(entry.Name()) {
|
||
8 years ago
|
continue
|
||
|
}
|
||
|
|
||
|
// TODO: collect all possible README files and show with priority.
|
||
|
readmeFile = entry.Blob()
|
||
|
break
|
||
|
}
|
||
|
|
||
|
if readmeFile != nil {
|
||
8 years ago
|
c.Data["RawFileLink"] = ""
|
||
|
c.Data["ReadmeInList"] = true
|
||
|
c.Data["ReadmeExist"] = true
|
||
8 years ago
|
|
||
|
dataRc, err := readmeFile.Data()
|
||
8 years ago
|
if err != nil {
|
||
8 years ago
|
c.ServerError("readmeFile.Data", err)
|
||
10 years ago
|
return
|
||
8 years ago
|
}
|
||
10 years ago
|
|
||
8 years ago
|
buf := make([]byte, 1024)
|
||
|
n, _ := dataRc.Read(buf)
|
||
8 years ago
|
buf = buf[:n]
|
||
8 years ago
|
|
||
8 years ago
|
isTextFile := tool.IsTextFile(buf)
|
||
8 years ago
|
c.Data["IsTextFile"] = isTextFile
|
||
|
c.Data["FileName"] = readmeFile.Name()
|
||
8 years ago
|
if isTextFile {
|
||
|
d, _ := ioutil.ReadAll(dataRc)
|
||
|
buf = append(buf, d...)
|
||
8 years ago
|
|
||
|
switch markup.Detect(readmeFile.Name()) {
|
||
|
case markup.MARKDOWN:
|
||
8 years ago
|
c.Data["IsMarkdown"] = true
|
||
|
buf = markup.Markdown(buf, treeLink, c.Repo.Repository.ComposeMetas())
|
||
8 years ago
|
case markup.ORG_MODE:
|
||
8 years ago
|
c.Data["IsMarkdown"] = true
|
||
|
buf = markup.OrgMode(buf, treeLink, c.Repo.Repository.ComposeMetas())
|
||
8 years ago
|
case markup.IPYTHON_NOTEBOOK:
|
||
8 years ago
|
c.Data["IsIPythonNotebook"] = true
|
||
|
c.Data["RawFileLink"] = c.Repo.RepoLink + "/raw/" + path.Join(c.Repo.BranchName, c.Repo.TreePath, readmeFile.Name())
|
||
8 years ago
|
default:
|
||
|
buf = bytes.Replace(buf, []byte("\n"), []byte(`<br>`), -1)
|
||
10 years ago
|
}
|
||
8 years ago
|
c.Data["FileContent"] = string(buf)
|
||
8 years ago
|
}
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
// Show latest commit info of repository in table header,
|
||
|
// or of directory if not in root directory.
|
||
8 years ago
|
latestCommit := c.Repo.Commit
|
||
|
if len(c.Repo.TreePath) > 0 {
|
||
|
latestCommit, err = c.Repo.Commit.GetCommitByPath(c.Repo.TreePath)
|
||
10 years ago
|
if err != nil {
|
||
8 years ago
|
c.ServerError("GetCommitByPath", err)
|
||
10 years ago
|
return
|
||
|
}
|
||
8 years ago
|
}
|
||
8 years ago
|
c.Data["LatestCommit"] = latestCommit
|
||
|
c.Data["LatestCommitUser"] = models.ValidateCommitWithEmail(latestCommit)
|
||
8 years ago
|
|
||
8 years ago
|
if c.Repo.CanEnableEditor() {
|
||
|
c.Data["CanAddFile"] = true
|
||
|
c.Data["CanUploadFile"] = setting.Repository.Upload.Enabled
|
||
8 years ago
|
}
|
||
|
}
|
||
10 years ago
|
|
||
8 years ago
|
func renderFile(c *context.Context, entry *git.TreeEntry, treeLink, rawLink string) {
|
||
|
c.Data["IsViewFile"] = true
|
||
10 years ago
|
|
||
8 years ago
|
blob := entry.Blob()
|
||
|
dataRc, err := blob.Data()
|
||
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "Data", err)
|
||
8 years ago
|
return
|
||
|
}
|
||
10 years ago
|
|
||
8 years ago
|
c.Data["FileSize"] = blob.Size()
|
||
|
c.Data["FileName"] = blob.Name()
|
||
|
c.Data["HighlightClass"] = highlight.FileNameToHighlightClass(blob.Name())
|
||
|
c.Data["RawFileLink"] = rawLink + "/" + c.Repo.TreePath
|
||
8 years ago
|
|
||
|
buf := make([]byte, 1024)
|
||
|
n, _ := dataRc.Read(buf)
|
||
8 years ago
|
buf = buf[:n]
|
||
8 years ago
|
|
||
8 years ago
|
isTextFile := tool.IsTextFile(buf)
|
||
8 years ago
|
c.Data["IsTextFile"] = isTextFile
|
||
8 years ago
|
|
||
|
// Assume file is not editable first.
|
||
|
if !isTextFile {
|
||
8 years ago
|
c.Data["EditFileTooltip"] = c.Tr("repo.editor.cannot_edit_non_text_files")
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
canEnableEditor := c.Repo.CanEnableEditor()
|
||
8 years ago
|
switch {
|
||
|
case isTextFile:
|
||
|
if blob.Size() >= setting.UI.MaxDisplayFileSize {
|
||
8 years ago
|
c.Data["IsFileTooLarge"] = true
|
||
8 years ago
|
break
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
c.Data["ReadmeExist"] = markup.IsReadmeFile(blob.Name())
|
||
8 years ago
|
|
||
8 years ago
|
d, _ := ioutil.ReadAll(dataRc)
|
||
|
buf = append(buf, d...)
|
||
10 years ago
|
|
||
8 years ago
|
switch markup.Detect(blob.Name()) {
|
||
|
case markup.MARKDOWN:
|
||
8 years ago
|
c.Data["IsMarkdown"] = true
|
||
|
c.Data["FileContent"] = string(markup.Markdown(buf, path.Dir(treeLink), c.Repo.Repository.ComposeMetas()))
|
||
8 years ago
|
case markup.ORG_MODE:
|
||
8 years ago
|
c.Data["IsMarkdown"] = true
|
||
|
c.Data["FileContent"] = string(markup.OrgMode(buf, path.Dir(treeLink), c.Repo.Repository.ComposeMetas()))
|
||
8 years ago
|
case markup.IPYTHON_NOTEBOOK:
|
||
8 years ago
|
c.Data["IsIPythonNotebook"] = true
|
||
8 years ago
|
default:
|
||
8 years ago
|
// Building code view blocks with line number on server side.
|
||
|
var fileContent string
|
||
|
if err, content := template.ToUTF8WithErr(buf); err != nil {
|
||
|
if err != nil {
|
||
|
log.Error(4, "ToUTF8WithErr: %s", err)
|
||
10 years ago
|
}
|
||
8 years ago
|
fileContent = string(buf)
|
||
|
} else {
|
||
|
fileContent = content
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
8 years ago
|
var output bytes.Buffer
|
||
|
lines := strings.Split(fileContent, "\n")
|
||
|
for index, line := range lines {
|
||
8 years ago
|
output.WriteString(fmt.Sprintf(`<li class="L%d" rel="L%d">%s</li>`, index+1, index+1, gotemplate.HTMLEscapeString(strings.TrimRight(line, "\r"))) + "\n")
|
||
10 years ago
|
}
|
||
8 years ago
|
c.Data["FileContent"] = gotemplate.HTML(output.String())
|
||
8 years ago
|
|
||
|
output.Reset()
|
||
|
for i := 0; i < len(lines); i++ {
|
||
|
output.WriteString(fmt.Sprintf(`<span id="L%d">%d</span>`, i+1, i+1))
|
||
|
}
|
||
8 years ago
|
c.Data["LineNums"] = gotemplate.HTML(output.String())
|
||
10 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
if canEnableEditor {
|
||
8 years ago
|
c.Data["CanEditFile"] = true
|
||
|
c.Data["EditFileTooltip"] = c.Tr("repo.editor.edit_this_file")
|
||
|
} else if !c.Repo.IsViewBranch {
|
||
|
c.Data["EditFileTooltip"] = c.Tr("repo.editor.must_be_on_a_branch")
|
||
|
} else if !c.Repo.IsWriter() {
|
||
|
c.Data["EditFileTooltip"] = c.Tr("repo.editor.fork_before_edit")
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
8 years ago
|
case tool.IsPDFFile(buf):
|
||
8 years ago
|
c.Data["IsPDFFile"] = true
|
||
8 years ago
|
case tool.IsVideoFile(buf):
|
||
8 years ago
|
c.Data["IsVideoFile"] = true
|
||
8 years ago
|
case tool.IsImageFile(buf):
|
||
8 years ago
|
c.Data["IsImageFile"] = true
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
if canEnableEditor {
|
||
8 years ago
|
c.Data["CanDeleteFile"] = true
|
||
|
c.Data["DeleteFileTooltip"] = c.Tr("repo.editor.delete_this_file")
|
||
|
} else if !c.Repo.IsViewBranch {
|
||
|
c.Data["DeleteFileTooltip"] = c.Tr("repo.editor.must_be_on_a_branch")
|
||
|
} else if !c.Repo.IsWriter() {
|
||
|
c.Data["DeleteFileTooltip"] = c.Tr("repo.editor.must_have_write_access")
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
func setEditorconfigIfExists(c *context.Context) {
|
||
|
ec, err := c.Repo.GetEditorconfig()
|
||
8 years ago
|
if err != nil && !git.IsErrNotExist(err) {
|
||
8 years ago
|
log.Trace("setEditorconfigIfExists.GetEditorconfig [%d]: %v", c.Repo.Repository.ID, err)
|
||
8 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.Data["Editorconfig"] = ec
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
func Home(c *context.Context) {
|
||
|
c.Data["PageIsViewFiles"] = true
|
||
8 years ago
|
|
||
8 years ago
|
if c.Repo.Repository.IsBare {
|
||
|
c.HTML(200, BARE)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
title := c.Repo.Repository.Owner.Name + "/" + c.Repo.Repository.Name
|
||
|
if len(c.Repo.Repository.Description) > 0 {
|
||
|
title += ": " + c.Repo.Repository.Description
|
||
8 years ago
|
}
|
||
8 years ago
|
c.Data["Title"] = title
|
||
|
if c.Repo.BranchName != c.Repo.Repository.DefaultBranch {
|
||
|
c.Data["Title"] = title + " @ " + c.Repo.BranchName
|
||
8 years ago
|
}
|
||
8 years ago
|
c.Data["RequireHighlightJS"] = true
|
||
8 years ago
|
|
||
8 years ago
|
branchLink := c.Repo.RepoLink + "/src/" + c.Repo.BranchName
|
||
8 years ago
|
treeLink := branchLink
|
||
8 years ago
|
rawLink := c.Repo.RepoLink + "/raw/" + c.Repo.BranchName
|
||
8 years ago
|
|
||
8 years ago
|
isRootDir := false
|
||
8 years ago
|
if len(c.Repo.TreePath) > 0 {
|
||
|
treeLink += "/" + c.Repo.TreePath
|
||
8 years ago
|
} else {
|
||
|
isRootDir = true
|
||
|
|
||
|
// Only show Git stats panel when view root directory
|
||
|
var err error
|
||
8 years ago
|
c.Repo.CommitsCount, err = c.Repo.Commit.CommitsCount()
|
||
8 years ago
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "CommitsCount", err)
|
||
8 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.Data["CommitsCount"] = c.Repo.CommitsCount
|
||
8 years ago
|
}
|
||
8 years ago
|
c.Data["PageIsRepoHome"] = isRootDir
|
||
8 years ago
|
|
||
|
// Get current entry user currently looking at.
|
||
8 years ago
|
entry, err := c.Repo.Commit.GetTreeEntryByPath(c.Repo.TreePath)
|
||
8 years ago
|
if err != nil {
|
||
8 years ago
|
c.NotFoundOrServerError("Repo.Commit.GetTreeEntryByPath", git.IsErrNotExist, err)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
|
if entry.IsDir() {
|
||
8 years ago
|
renderDirectory(c, treeLink)
|
||
8 years ago
|
} else {
|
||
8 years ago
|
renderFile(c, entry, treeLink, rawLink)
|
||
8 years ago
|
}
|
||
8 years ago
|
if c.Written() {
|
||
8 years ago
|
return
|
||
|
}
|
||
10 years ago
|
|
||
8 years ago
|
setEditorconfigIfExists(c)
|
||
|
if c.Written() {
|
||
8 years ago
|
return
|
||
|
}
|
||
10 years ago
|
|
||
8 years ago
|
var treeNames []string
|
||
|
paths := make([]string, 0, 5)
|
||
8 years ago
|
if len(c.Repo.TreePath) > 0 {
|
||
|
treeNames = strings.Split(c.Repo.TreePath, "/")
|
||
8 years ago
|
for i := range treeNames {
|
||
|
paths = append(paths, strings.Join(treeNames[:i+1], "/"))
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
c.Data["HasParentPath"] = true
|
||
8 years ago
|
if len(paths)-2 >= 0 {
|
||
8 years ago
|
c.Data["ParentPath"] = "/" + paths[len(paths)-2]
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
c.Data["Paths"] = paths
|
||
|
c.Data["TreeLink"] = treeLink
|
||
|
c.Data["TreeNames"] = treeNames
|
||
|
c.Data["BranchLink"] = branchLink
|
||
|
c.HTML(200, HOME)
|
||
10 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
func RenderUserCards(c *context.Context, total int, getter func(page int) ([]*models.User, error), tpl string) {
|
||
|
page := c.QueryInt("page")
|
||
9 years ago
|
if page <= 0 {
|
||
|
page = 1
|
||
|
}
|
||
|
pager := paginater.New(total, models.ItemsPerPage, page, 5)
|
||
8 years ago
|
c.Data["Page"] = pager
|
||
9 years ago
|
|
||
|
items, err := getter(pager.Current())
|
||
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "getter", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.Data["Cards"] = items
|
||
9 years ago
|
|
||
8 years ago
|
c.HTML(200, tpl)
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func Watchers(c *context.Context) {
|
||
|
c.Data["Title"] = c.Tr("repo.watchers")
|
||
|
c.Data["CardsTitle"] = c.Tr("repo.watchers")
|
||
|
c.Data["PageIsWatchers"] = true
|
||
|
RenderUserCards(c, c.Repo.Repository.NumWatches, c.Repo.Repository.GetWatchers, WATCHERS)
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func Stars(c *context.Context) {
|
||
|
c.Data["Title"] = c.Tr("repo.stargazers")
|
||
|
c.Data["CardsTitle"] = c.Tr("repo.stargazers")
|
||
|
c.Data["PageIsStargazers"] = true
|
||
|
RenderUserCards(c, c.Repo.Repository.NumStars, c.Repo.Repository.GetStargazers, WATCHERS)
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
func Forks(c *context.Context) {
|
||
|
c.Data["Title"] = c.Tr("repos.forks")
|
||
9 years ago
|
|
||
8 years ago
|
forks, err := c.Repo.Repository.GetForks()
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "GetForks", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
|
for _, fork := range forks {
|
||
|
if err = fork.GetOwner(); err != nil {
|
||
8 years ago
|
c.Handle(500, "GetOwner", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
}
|
||
8 years ago
|
c.Data["Forks"] = forks
|
||
9 years ago
|
|
||
8 years ago
|
c.HTML(200, FORKS)
|
||
9 years ago
|
}
|