Browse Source

repo/editor: fix wrong context for subdirectory (#4368)

pull/4412/head
Unknwon 8 years ago
parent
commit
91cd350b63
No known key found for this signature in database
GPG Key ID: 25B575AE3213B2B3
  1. 2
      gogs.go
  2. 13
      pkg/context/context.go
  3. 4
      pkg/markup/markup.go
  4. 9
      public/js/gogs.js
  5. 180
      routers/repo/editor.go
  6. 2
      templates/.VERSION
  7. 4
      templates/repo/editor/edit.tmpl

2
gogs.go

@ -16,7 +16,7 @@ import (
"github.com/gogits/gogs/pkg/setting"
)
const APP_VER = "0.11.6.0407"
const APP_VER = "0.11.7.0407"
func init() {
setting.AppVer = APP_VER

13
pkg/context/context.go

@ -51,6 +51,19 @@ func (c *Context) PageIs(name string) {
c.Data["PageIs"+name] = true
}
// Require sets "Requirexxx" field in template data.
func (c *Context) Require(name string) {
c.Data["Require"+name] = true
}
func (c *Context) RequireHighlightJS() {
c.Require("HighlightJS")
}
func (c *Context) RequireSimpleMDE() {
c.Require("SimpleMDE")
}
// FormErr sets "Err_xxx" field in template data.
func (c *Context) FormErr(names ...string) {
for i := range names {

4
pkg/markup/markup.go

@ -14,8 +14,8 @@ import (
"github.com/Unknwon/com"
"golang.org/x/net/html"
"github.com/gogits/gogs/pkg/tool"
"github.com/gogits/gogs/pkg/setting"
"github.com/gogits/gogs/pkg/tool"
)
// IsReadmeFile reports whether name looks like a README file based on its extension.
@ -320,7 +320,7 @@ func Render(typ Type, input interface{}, urlPrefix string, metas map[string]stri
panic(fmt.Sprintf("unrecognized input content type: %T", input))
}
urlPrefix = strings.Replace(urlPrefix, " ", "%20", -1)
urlPrefix = strings.TrimRight(strings.Replace(urlPrefix, " ", "%20", -1), "/")
var rawHTML []byte
switch typ {
case MARKDOWN:

9
public/js/gogs.js

@ -64,7 +64,6 @@ function initEditDiffTab($form) {
var $this = $(this);
$.post($this.data('url'), {
"_csrf": csrf,
"context": $this.data('context'),
"content": $form.find('.tab.segment[data-tab="' + $tabMenu.data('write') + '"] textarea').val()
},
function (data) {
@ -704,9 +703,13 @@ function initEditor() {
parts.push(element.text());
}
});
if ($(this).val())
if ($(this).val()) {
parts.push($(this).val());
$('#tree_path').val(parts.join('/'));
}
var tree_path = parts.join('/');
$('#tree_path').val(tree_path);
$('#preview-tab').data('context', $('#preview-tab').data('root-context') + tree_path.substring(0, tree_path.lastIndexOf("/")+1));
}).trigger('keyup');
var $editArea = $('.repository.editor textarea#edit_area');

180
routers/repo/editor.go

@ -44,36 +44,36 @@ func getParentTreeFields(treePath string) (treeNames []string, treePaths []strin
return treeNames, treePaths
}
func editFile(ctx *context.Context, isNewFile bool) {
ctx.Data["PageIsEdit"] = true
ctx.Data["IsNewFile"] = isNewFile
ctx.Data["RequireHighlightJS"] = true
ctx.Data["RequireSimpleMDE"] = true
func editFile(c *context.Context, isNewFile bool) {
c.PageIs("Edit")
c.RequireHighlightJS()
c.RequireSimpleMDE()
c.Data["IsNewFile"] = isNewFile
treeNames, treePaths := getParentTreeFields(ctx.Repo.TreePath)
treeNames, treePaths := getParentTreeFields(c.Repo.TreePath)
if !isNewFile {
entry, err := ctx.Repo.Commit.GetTreeEntryByPath(ctx.Repo.TreePath)
entry, err := c.Repo.Commit.GetTreeEntryByPath(c.Repo.TreePath)
if err != nil {
ctx.NotFoundOrServerError("GetTreeEntryByPath", git.IsErrNotExist, err)
c.NotFoundOrServerError("GetTreeEntryByPath", git.IsErrNotExist, err)
return
}
// No way to edit a directory online.
if entry.IsDir() {
ctx.Handle(404, "", nil)
c.NotFound()
return
}
blob := entry.Blob()
dataRc, err := blob.Data()
if err != nil {
ctx.Handle(404, "blob.Data", err)
c.ServerError("blob.Data", err)
return
}
ctx.Data["FileSize"] = blob.Size()
ctx.Data["FileName"] = blob.Name()
c.Data["FileSize"] = blob.Size()
c.Data["FileName"] = blob.Name()
buf := make([]byte, 1024)
n, _ := dataRc.Read(buf)
@ -81,7 +81,7 @@ func editFile(ctx *context.Context, isNewFile bool) {
// Only text file are editable online.
if !tool.IsTextFile(buf) {
ctx.Handle(404, "", nil)
c.NotFound()
return
}
@ -89,51 +89,52 @@ func editFile(ctx *context.Context, isNewFile bool) {
buf = append(buf, d...)
if err, content := template.ToUTF8WithErr(buf); err != nil {
if err != nil {
log.Error(4, "ToUTF8WithErr: %v", err)
log.Error(2, "ToUTF8WithErr: %v", err)
}
ctx.Data["FileContent"] = string(buf)
c.Data["FileContent"] = string(buf)
} else {
ctx.Data["FileContent"] = content
c.Data["FileContent"] = content
}
} else {
treeNames = append(treeNames, "") // Append empty string to allow user name the new file.
}
ctx.Data["TreeNames"] = treeNames
ctx.Data["TreePaths"] = treePaths
ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchName
ctx.Data["commit_summary"] = ""
ctx.Data["commit_message"] = ""
ctx.Data["commit_choice"] = "direct"
ctx.Data["new_branch_name"] = ""
ctx.Data["last_commit"] = ctx.Repo.Commit.ID
ctx.Data["MarkdownFileExts"] = strings.Join(setting.Markdown.FileExtensions, ",")
ctx.Data["LineWrapExtensions"] = strings.Join(setting.Repository.Editor.LineWrapExtensions, ",")
ctx.Data["PreviewableFileModes"] = strings.Join(setting.Repository.Editor.PreviewableFileModes, ",")
ctx.Data["EditorconfigURLPrefix"] = fmt.Sprintf("%s/api/v1/repos/%s/editorconfig/", setting.AppSubURL, ctx.Repo.Repository.FullName())
c.Data["ParentTreePath"] = path.Dir(c.Repo.TreePath)
c.Data["TreeNames"] = treeNames
c.Data["TreePaths"] = treePaths
c.Data["BranchLink"] = c.Repo.RepoLink + "/src/" + c.Repo.BranchName
c.Data["commit_summary"] = ""
c.Data["commit_message"] = ""
c.Data["commit_choice"] = "direct"
c.Data["new_branch_name"] = ""
c.Data["last_commit"] = c.Repo.Commit.ID
c.Data["MarkdownFileExts"] = strings.Join(setting.Markdown.FileExtensions, ",")
c.Data["LineWrapExtensions"] = strings.Join(setting.Repository.Editor.LineWrapExtensions, ",")
c.Data["PreviewableFileModes"] = strings.Join(setting.Repository.Editor.PreviewableFileModes, ",")
c.Data["EditorconfigURLPrefix"] = fmt.Sprintf("%s/api/v1/repos/%s/editorconfig/", setting.AppSubURL, c.Repo.Repository.FullName())
ctx.HTML(200, EDIT_FILE)
c.Success(EDIT_FILE)
}
func EditFile(ctx *context.Context) {
editFile(ctx, false)
func EditFile(c *context.Context) {
editFile(c, false)
}
func NewFile(ctx *context.Context) {
editFile(ctx, true)
func NewFile(c *context.Context) {
editFile(c, true)
}
func editFilePost(ctx *context.Context, f form.EditRepoFile, isNewFile bool) {
ctx.Data["PageIsEdit"] = true
ctx.Data["IsNewFile"] = isNewFile
ctx.Data["RequireHighlightJS"] = true
ctx.Data["RequireSimpleMDE"] = true
func editFilePost(c *context.Context, f form.EditRepoFile, isNewFile bool) {
c.PageIs("Edit")
c.RequireHighlightJS()
c.RequireSimpleMDE()
c.Data["IsNewFile"] = isNewFile
oldBranchName := ctx.Repo.BranchName
oldBranchName := c.Repo.BranchName
branchName := oldBranchName
oldTreePath := ctx.Repo.TreePath
oldTreePath := c.Repo.TreePath
lastCommit := f.LastCommit
f.LastCommit = ctx.Repo.Commit.ID.String()
f.LastCommit = c.Repo.Commit.ID.String()
if f.IsNewBrnach() {
branchName = f.NewBranchName
@ -142,35 +143,36 @@ func editFilePost(ctx *context.Context, f form.EditRepoFile, isNewFile bool) {
f.TreePath = strings.Trim(f.TreePath, " /")
treeNames, treePaths := getParentTreeFields(f.TreePath)
ctx.Data["TreePath"] = f.TreePath
ctx.Data["TreeNames"] = treeNames
ctx.Data["TreePaths"] = treePaths
ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/" + branchName
ctx.Data["FileContent"] = f.Content
ctx.Data["commit_summary"] = f.CommitSummary
ctx.Data["commit_message"] = f.CommitMessage
ctx.Data["commit_choice"] = f.CommitChoice
ctx.Data["new_branch_name"] = branchName
ctx.Data["last_commit"] = f.LastCommit
ctx.Data["MarkdownFileExts"] = strings.Join(setting.Markdown.FileExtensions, ",")
ctx.Data["LineWrapExtensions"] = strings.Join(setting.Repository.Editor.LineWrapExtensions, ",")
ctx.Data["PreviewableFileModes"] = strings.Join(setting.Repository.Editor.PreviewableFileModes, ",")
c.Data["ParentTreePath"] = path.Dir(c.Repo.TreePath)
c.Data["TreePath"] = f.TreePath
c.Data["TreeNames"] = treeNames
c.Data["TreePaths"] = treePaths
c.Data["BranchLink"] = c.Repo.RepoLink + "/src/" + branchName
c.Data["FileContent"] = f.Content
c.Data["commit_summary"] = f.CommitSummary
c.Data["commit_message"] = f.CommitMessage
c.Data["commit_choice"] = f.CommitChoice
c.Data["new_branch_name"] = branchName
c.Data["last_commit"] = f.LastCommit
c.Data["MarkdownFileExts"] = strings.Join(setting.Markdown.FileExtensions, ",")
c.Data["LineWrapExtensions"] = strings.Join(setting.Repository.Editor.LineWrapExtensions, ",")
c.Data["PreviewableFileModes"] = strings.Join(setting.Repository.Editor.PreviewableFileModes, ",")
if ctx.HasError() {
ctx.HTML(200, EDIT_FILE)
if c.HasError() {
c.Success(EDIT_FILE)
return
}
if len(f.TreePath) == 0 {
ctx.Data["Err_TreePath"] = true
ctx.RenderWithErr(ctx.Tr("repo.editor.filename_cannot_be_empty"), EDIT_FILE, &f)
c.FormErr("TreePath")
c.RenderWithErr(c.Tr("repo.editor.filename_cannot_be_empty"), EDIT_FILE, &f)
return
}
if oldBranchName != branchName {
if _, err := ctx.Repo.Repository.GetBranch(branchName); err == nil {
ctx.Data["Err_NewBranchName"] = true
ctx.RenderWithErr(ctx.Tr("repo.editor.branch_already_exists", branchName), EDIT_FILE, &f)
if _, err := c.Repo.Repository.GetBranch(branchName); err == nil {
c.FormErr("NewBranchName")
c.RenderWithErr(c.Tr("repo.editor.branch_already_exists", branchName), EDIT_FILE, &f)
return
}
}
@ -178,56 +180,56 @@ func editFilePost(ctx *context.Context, f form.EditRepoFile, isNewFile bool) {
var newTreePath string
for index, part := range treeNames {
newTreePath = path.Join(newTreePath, part)
entry, err := ctx.Repo.Commit.GetTreeEntryByPath(newTreePath)
entry, err := c.Repo.Commit.GetTreeEntryByPath(newTreePath)
if err != nil {
if git.IsErrNotExist(err) {
// Means there is no item with that name, so we're good
break
}
ctx.Handle(500, "Repo.Commit.GetTreeEntryByPath", err)
c.ServerError("Repo.Commit.GetTreeEntryByPath", err)
return
}
if index != len(treeNames)-1 {
if !entry.IsDir() {
ctx.Data["Err_TreePath"] = true
ctx.RenderWithErr(ctx.Tr("repo.editor.directory_is_a_file", part), EDIT_FILE, &f)
c.FormErr("TreePath")
c.RenderWithErr(c.Tr("repo.editor.directory_is_a_file", part), EDIT_FILE, &f)
return
}
} else {
if entry.IsLink() {
ctx.Data["Err_TreePath"] = true
ctx.RenderWithErr(ctx.Tr("repo.editor.file_is_a_symlink", part), EDIT_FILE, &f)
c.FormErr("TreePath")
c.RenderWithErr(c.Tr("repo.editor.file_is_a_symlink", part), EDIT_FILE, &f)
return
} else if entry.IsDir() {
ctx.Data["Err_TreePath"] = true
ctx.RenderWithErr(ctx.Tr("repo.editor.filename_is_a_directory", part), EDIT_FILE, &f)
c.FormErr("TreePath")
c.RenderWithErr(c.Tr("repo.editor.filename_is_a_directory", part), EDIT_FILE, &f)
return
}
}
}
if !isNewFile {
_, err := ctx.Repo.Commit.GetTreeEntryByPath(oldTreePath)
_, err := c.Repo.Commit.GetTreeEntryByPath(oldTreePath)
if err != nil {
if git.IsErrNotExist(err) {
ctx.Data["Err_TreePath"] = true
ctx.RenderWithErr(ctx.Tr("repo.editor.file_editing_no_longer_exists", oldTreePath), EDIT_FILE, &f)
c.FormErr("TreePath")
c.RenderWithErr(c.Tr("repo.editor.file_editing_no_longer_exists", oldTreePath), EDIT_FILE, &f)
} else {
ctx.Handle(500, "GetTreeEntryByPath", err)
c.ServerError("GetTreeEntryByPath", err)
}
return
}
if lastCommit != ctx.Repo.CommitID {
files, err := ctx.Repo.Commit.GetFilesChangedSinceCommit(lastCommit)
if lastCommit != c.Repo.CommitID {
files, err := c.Repo.Commit.GetFilesChangedSinceCommit(lastCommit)
if err != nil {
ctx.Handle(500, "GetFilesChangedSinceCommit", err)
c.ServerError("GetFilesChangedSinceCommit", err)
return
}
for _, file := range files {
if file == f.TreePath {
ctx.RenderWithErr(ctx.Tr("repo.editor.file_changed_while_editing", ctx.Repo.RepoLink+"/compare/"+lastCommit+"..."+ctx.Repo.CommitID), EDIT_FILE, &f)
c.RenderWithErr(c.Tr("repo.editor.file_changed_while_editing", c.Repo.RepoLink+"/compare/"+lastCommit+"..."+c.Repo.CommitID), EDIT_FILE, &f)
return
}
}
@ -236,16 +238,16 @@ func editFilePost(ctx *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 := ctx.Repo.Commit.GetTreeEntryByPath(f.TreePath)
entry, err := c.Repo.Commit.GetTreeEntryByPath(f.TreePath)
if err != nil {
if !git.IsErrNotExist(err) {
ctx.Handle(500, "GetTreeEntryByPath", err)
c.ServerError("GetTreeEntryByPath", err)
return
}
}
if entry != nil {
ctx.Data["Err_TreePath"] = true
ctx.RenderWithErr(ctx.Tr("repo.editor.file_already_exists", f.TreePath), EDIT_FILE, &f)
c.FormErr("TreePath")
c.RenderWithErr(c.Tr("repo.editor.file_already_exists", f.TreePath), EDIT_FILE, &f)
return
}
}
@ -253,9 +255,9 @@ func editFilePost(ctx *context.Context, f form.EditRepoFile, isNewFile bool) {
message := strings.TrimSpace(f.CommitSummary)
if len(message) == 0 {
if isNewFile {
message = ctx.Tr("repo.editor.add", f.TreePath)
message = c.Tr("repo.editor.add", f.TreePath)
} else {
message = ctx.Tr("repo.editor.update", f.TreePath)
message = c.Tr("repo.editor.update", f.TreePath)
}
}
@ -264,7 +266,7 @@ func editFilePost(ctx *context.Context, f form.EditRepoFile, isNewFile bool) {
message += "\n\n" + f.CommitMessage
}
if err := ctx.Repo.Repository.UpdateRepoFile(ctx.User, models.UpdateRepoFileOptions{
if err := c.Repo.Repository.UpdateRepoFile(c.User, models.UpdateRepoFileOptions{
LastCommitID: lastCommit,
OldBranch: oldBranchName,
NewBranch: branchName,
@ -274,15 +276,15 @@ func editFilePost(ctx *context.Context, f form.EditRepoFile, isNewFile bool) {
Content: strings.Replace(f.Content, "\r", "", -1),
IsNewFile: isNewFile,
}); err != nil {
ctx.Data["Err_TreePath"] = true
ctx.RenderWithErr(ctx.Tr("repo.editor.fail_to_update_file", f.TreePath, err), EDIT_FILE, &f)
c.FormErr("TreePath")
c.RenderWithErr(c.Tr("repo.editor.fail_to_update_file", f.TreePath, err), EDIT_FILE, &f)
return
}
if f.IsNewBrnach() && ctx.Repo.PullRequest.Allowed {
ctx.Redirect(ctx.Repo.PullRequestURL(oldBranchName, f.NewBranchName))
if f.IsNewBrnach() && c.Repo.PullRequest.Allowed {
c.Redirect(c.Repo.PullRequestURL(oldBranchName, f.NewBranchName))
} else {
ctx.Redirect(ctx.Repo.RepoLink + "/src/" + branchName + "/" + template.EscapePound(f.TreePath))
c.Redirect(c.Repo.RepoLink + "/src/" + branchName + "/" + template.EscapePound(f.TreePath))
}
}

2
templates/.VERSION

@ -1 +1 @@
0.11.6.0407
0.11.7.0407

4
templates/repo/editor/edit.tmpl

@ -30,8 +30,8 @@
<div class="ui top attached tabular menu" data-write="write" data-preview="preview" data-diff="diff">
<a class="active item" data-tab="write"><i class="octicon octicon-code"></i> {{if .IsNewFile}}{{.i18n.Tr "repo.editor.new_file"}}{{else}}{{.i18n.Tr "repo.editor.edit_file"}}{{end}}</a>
{{if not .IsNewFile}}
<a class="item" data-tab="preview" data-url="{{AppSubURL}}/api/v1/markdown" data-context="{{.RepoLink}}/src/{{.BranchName}}" data-preview-file-modes="{{.PreviewableFileModes}}"><i class="octicon octicon-eye"></i> {{.i18n.Tr "repo.release.preview"}}</a>
<a class="item" data-tab="diff" data-url="{{.RepoLink}}/_preview/{{.BranchName}}/{{.TreePath}}" data-context="{{.BranchLink}}"><i class="octicon octicon-diff"></i> {{.i18n.Tr "repo.editor.preview_changes"}}</a>
<a class="item" id="preview-tab" data-tab="preview" data-url="{{AppSubURL}}/api/v1/markdown" data-root-context="{{.BranchLink}}/" data-context="{{.BranchLink}}/{{.ParentTreePath}}" data-preview-file-modes="{{.PreviewableFileModes}}"><i class="octicon octicon-eye"></i> {{.i18n.Tr "repo.release.preview"}}</a>
<a class="item" data-tab="diff" data-url="{{.RepoLink}}/_preview/{{.BranchName}}/{{.TreePath}}"><i class="octicon octicon-diff"></i> {{.i18n.Tr "repo.editor.preview_changes"}}</a>
{{end}}
</div>
<div class="ui bottom attached active tab segment" data-tab="write">

Loading…
Cancel
Save