diff options
author | ᴜɴᴋɴᴡᴏɴ <u@gogs.io> | 2020-03-08 19:09:31 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-08 19:09:31 +0800 |
commit | 6437d0180b97a26319b50c2e22927dac7c94fcdd (patch) | |
tree | 3d0d097e7f498e4b970065096e7500876d365a8b /internal/route/repo/editor.go | |
parent | c65b5b9f84dee21dc362311b299694e8e00f6ac6 (diff) |
git: migrate to github.com/gogs/git-module@v1.0.0 (#5958)
* WIP
* Finish `internal/db/git_diff.go`
* FInish internal/db/mirror.go
* Finish internal/db/pull.go
* Finish internal/db/release.go
* Finish internal/db/repo.go
* Finish internal/db/repo_branch.go
* Finish internal/db/repo_editor.go
* Finish internal/db/update.go
* Save my work
* Add license header
* Compile!
* Merge master
* Finish internal/cmd/hook.go
* Finish internal/conf/static.go
* Finish internal/context/repo.go
* Finish internal/db/action.go
* Finish internal/db/git_diff.go
* Fix submodule URL inferring
* Finish internal/db/mirror.go
* Updat to beta.4
* css: update fonts
* Finish internal/db/pull.go
* Finish internal/db/release.go
* Finish internal/db/repo_branch.go
* Finish internal/db/wiki.go
* gitutil: enhance infer submodule UR
* Finish internal/route/api/v1/repo/commits.go
* mirror: only collect branch commits after sync
* mirror: fix tag support
* Finish internal/db/repo.go
* Finish internal/db/repo_editor.go
* Finish internal/db/update.go
* Finish internal/gitutil/pull_request.go
* Make it compile
* Finish internal/route/repo/setting.go
* Finish internal/route/repo/branch.go
* Finish internal/route/api/v1/repo/file.go
* Finish internal/route/repo/download.go
* Finish internal/route/repo/editor.go
* Use helper
* Finish internal/route/repo/issue.go
* Finish internal/route/repo/pull.go
* Finish internal/route/repo/release.go
* Finish internal/route/repo/repo.go
* Finish internal/route/repo/wiki.go
* Finish internal/route/repo/commit.go
* Finish internal/route/repo/view.go
* Finish internal/gitutil/tag.go
* go.sum
Diffstat (limited to 'internal/route/repo/editor.go')
-rw-r--r-- | internal/route/repo/editor.go | 53 |
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 |