aboutsummaryrefslogtreecommitdiff
path: root/internal/route/repo/editor.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/route/repo/editor.go')
-rw-r--r--internal/route/repo/editor.go53
1 files changed, 23 insertions, 30 deletions
diff --git a/internal/route/repo/editor.go b/internal/route/repo/editor.go
index 7b7a0935..17434dff 100644
--- a/internal/route/repo/editor.go
+++ b/internal/route/repo/editor.go
@@ -6,19 +6,18 @@ package repo
import (
"fmt"
- "io/ioutil"
"net/http"
"path"
"strings"
log "unknwon.dev/clog/v2"
- "github.com/gogs/git-module"
"gogs.io/gogs/internal/conf"
"gogs.io/gogs/internal/context"
"gogs.io/gogs/internal/db"
"gogs.io/gogs/internal/db/errors"
"gogs.io/gogs/internal/form"
+ "gogs.io/gogs/internal/gitutil"
"gogs.io/gogs/internal/pathutil"
"gogs.io/gogs/internal/template"
"gogs.io/gogs/internal/tool"
@@ -55,20 +54,20 @@ func editFile(c *context.Context, isNewFile bool) {
treeNames, treePaths := getParentTreeFields(c.Repo.TreePath)
if !isNewFile {
- entry, err := c.Repo.Commit.GetTreeEntryByPath(c.Repo.TreePath)
+ entry, err := c.Repo.Commit.TreeEntry(c.Repo.TreePath)
if err != nil {
- c.NotFoundOrServerError("GetTreeEntryByPath", git.IsErrNotExist, err)
+ c.NotFoundOrServerError("get tree entry", gitutil.IsErrRevisionNotExist, err)
return
}
// No way to edit a directory online.
- if entry.IsDir() {
+ if entry.IsTree() {
c.NotFound()
return
}
blob := entry.Blob()
- dataRc, err := blob.Data()
+ p, err := blob.Bytes()
if err != nil {
c.ServerError("blob.Data", err)
return
@@ -77,23 +76,17 @@ func editFile(c *context.Context, isNewFile bool) {
c.Data["FileSize"] = blob.Size()
c.Data["FileName"] = blob.Name()
- buf := make([]byte, 1024)
- n, _ := dataRc.Read(buf)
- buf = buf[:n]
-
// Only text file are editable online.
- if !tool.IsTextFile(buf) {
+ if !tool.IsTextFile(p) {
c.NotFound()
return
}
- d, _ := ioutil.ReadAll(dataRc)
- buf = append(buf, d...)
- if err, content := template.ToUTF8WithErr(buf); err != nil {
+ if err, content := template.ToUTF8WithErr(p); err != nil {
if err != nil {
log.Error("Failed to convert encoding to UTF-8: %v", err)
}
- c.Data["FileContent"] = string(buf)
+ c.Data["FileContent"] = string(p)
} else {
c.Data["FileContent"] = content
}
@@ -182,9 +175,9 @@ func editFilePost(c *context.Context, f form.EditRepoFile, isNewFile bool) {
var newTreePath string
for index, part := range treeNames {
newTreePath = path.Join(newTreePath, part)
- entry, err := c.Repo.Commit.GetTreeEntryByPath(newTreePath)
+ entry, err := c.Repo.Commit.TreeEntry(newTreePath)
if err != nil {
- if git.IsErrNotExist(err) {
+ if gitutil.IsErrRevisionNotExist(err) {
// Means there is no item with that name, so we're good
break
}
@@ -193,17 +186,17 @@ func editFilePost(c *context.Context, f form.EditRepoFile, isNewFile bool) {
return
}
if index != len(treeNames)-1 {
- if !entry.IsDir() {
+ if !entry.IsTree() {
c.FormErr("TreePath")
c.RenderWithErr(c.Tr("repo.editor.directory_is_a_file", part), EDIT_FILE, &f)
return
}
} else {
- if entry.IsLink() {
+ if entry.IsSymlink() {
c.FormErr("TreePath")
c.RenderWithErr(c.Tr("repo.editor.file_is_a_symlink", part), EDIT_FILE, &f)
return
- } else if entry.IsDir() {
+ } else if entry.IsTree() {
c.FormErr("TreePath")
c.RenderWithErr(c.Tr("repo.editor.filename_is_a_directory", part), EDIT_FILE, &f)
return
@@ -212,9 +205,9 @@ func editFilePost(c *context.Context, f form.EditRepoFile, isNewFile bool) {
}
if !isNewFile {
- _, err := c.Repo.Commit.GetTreeEntryByPath(oldTreePath)
+ _, err := c.Repo.Commit.TreeEntry(oldTreePath)
if err != nil {
- if git.IsErrNotExist(err) {
+ if gitutil.IsErrRevisionNotExist(err) {
c.FormErr("TreePath")
c.RenderWithErr(c.Tr("repo.editor.file_editing_no_longer_exists", oldTreePath), EDIT_FILE, &f)
} else {
@@ -223,7 +216,7 @@ func editFilePost(c *context.Context, f form.EditRepoFile, isNewFile bool) {
return
}
if lastCommit != c.Repo.CommitID {
- files, err := c.Repo.Commit.GetFilesChangedSinceCommit(lastCommit)
+ files, err := c.Repo.Commit.FilesChangedAfter(lastCommit)
if err != nil {
c.ServerError("GetFilesChangedSinceCommit", err)
return
@@ -240,9 +233,9 @@ func editFilePost(c *context.Context, f form.EditRepoFile, isNewFile bool) {
if oldTreePath != f.TreePath {
// We have a new filename (rename or completely new file) so we need to make sure it doesn't already exist, can't clobber.
- entry, err := c.Repo.Commit.GetTreeEntryByPath(f.TreePath)
+ entry, err := c.Repo.Commit.TreeEntry(f.TreePath)
if err != nil {
- if !git.IsErrNotExist(err) {
+ if !gitutil.IsErrRevisionNotExist(err) {
c.ServerError("GetTreeEntryByPath", err)
return
}
@@ -302,11 +295,11 @@ func NewFilePost(c *context.Context, f form.EditRepoFile) {
func DiffPreviewPost(c *context.Context, f form.EditPreviewDiff) {
treePath := c.Repo.TreePath
- entry, err := c.Repo.Commit.GetTreeEntryByPath(treePath)
+ entry, err := c.Repo.Commit.TreeEntry(treePath)
if err != nil {
c.Error(500, "GetTreeEntryByPath: "+err.Error())
return
- } else if entry.IsDir() {
+ } else if entry.IsTree() {
c.Error(422)
return
}
@@ -468,9 +461,9 @@ func UploadFilePost(c *context.Context, f form.UploadRepoFile) {
var newTreePath string
for _, part := range treeNames {
newTreePath = path.Join(newTreePath, part)
- entry, err := c.Repo.Commit.GetTreeEntryByPath(newTreePath)
+ entry, err := c.Repo.Commit.TreeEntry(newTreePath)
if err != nil {
- if git.IsErrNotExist(err) {
+ if gitutil.IsErrRevisionNotExist(err) {
// Means there is no item with that name, so we're good
break
}
@@ -480,7 +473,7 @@ func UploadFilePost(c *context.Context, f form.UploadRepoFile) {
}
// User can only upload files to a directory.
- if !entry.IsDir() {
+ if !entry.IsTree() {
c.FormErr("TreePath")
c.RenderWithErr(c.Tr("repo.editor.directory_is_a_file", part), UPLOAD_FILE, &f)
return