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. 184
      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" "github.com/gogits/gogs/pkg/setting"
) )
const APP_VER = "0.11.6.0407" const APP_VER = "0.11.7.0407"
func init() { func init() {
setting.AppVer = APP_VER setting.AppVer = APP_VER

13
pkg/context/context.go

@ -51,6 +51,19 @@ func (c *Context) PageIs(name string) {
c.Data["PageIs"+name] = true 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. // FormErr sets "Err_xxx" field in template data.
func (c *Context) FormErr(names ...string) { func (c *Context) FormErr(names ...string) {
for i := range names { for i := range names {

4
pkg/markup/markup.go

@ -14,8 +14,8 @@ import (
"github.com/Unknwon/com" "github.com/Unknwon/com"
"golang.org/x/net/html" "golang.org/x/net/html"
"github.com/gogits/gogs/pkg/tool"
"github.com/gogits/gogs/pkg/setting" "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. // 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)) 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 var rawHTML []byte
switch typ { switch typ {
case MARKDOWN: case MARKDOWN:

9
public/js/gogs.js

@ -64,7 +64,6 @@ function initEditDiffTab($form) {
var $this = $(this); var $this = $(this);
$.post($this.data('url'), { $.post($this.data('url'), {
"_csrf": csrf, "_csrf": csrf,
"context": $this.data('context'),
"content": $form.find('.tab.segment[data-tab="' + $tabMenu.data('write') + '"] textarea').val() "content": $form.find('.tab.segment[data-tab="' + $tabMenu.data('write') + '"] textarea').val()
}, },
function (data) { function (data) {
@ -704,9 +703,13 @@ function initEditor() {
parts.push(element.text()); parts.push(element.text());
} }
}); });
if ($(this).val()) if ($(this).val()) {
parts.push($(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'); }).trigger('keyup');
var $editArea = $('.repository.editor textarea#edit_area'); var $editArea = $('.repository.editor textarea#edit_area');

184
routers/repo/editor.go

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

Loading…
Cancel
Save