(c *context.Context, f form.EditRepoFile, isNewFile bool)
| 118 | } |
| 119 | |
| 120 | func editFilePost(c *context.Context, f form.EditRepoFile, isNewFile bool) { |
| 121 | c.PageIs("Edit") |
| 122 | c.RequireHighlightJS() |
| 123 | c.RequireSimpleMDE() |
| 124 | c.Data["IsNewFile"] = isNewFile |
| 125 | |
| 126 | oldBranchName := c.Repo.BranchName |
| 127 | branchName := oldBranchName |
| 128 | oldTreePath := c.Repo.TreePath |
| 129 | lastCommit := f.LastCommit |
| 130 | f.LastCommit = c.Repo.Commit.ID.String() |
| 131 | |
| 132 | if f.IsNewBrnach() { |
| 133 | branchName = f.NewBranchName |
| 134 | } |
| 135 | |
| 136 | // 🚨 SECURITY: Prevent path traversal. |
| 137 | f.TreePath = pathutil.Clean(f.TreePath) |
| 138 | treeNames, treePaths := getParentTreeFields(f.TreePath) |
| 139 | |
| 140 | c.Data["ParentTreePath"] = path.Dir(c.Repo.TreePath) |
| 141 | c.Data["TreePath"] = f.TreePath |
| 142 | c.Data["TreeNames"] = treeNames |
| 143 | c.Data["TreePaths"] = treePaths |
| 144 | c.Data["BranchLink"] = c.Repo.RepoLink + "/src/" + branchName |
| 145 | c.Data["FileContent"] = f.Content |
| 146 | c.Data["commit_summary"] = f.CommitSummary |
| 147 | c.Data["commit_message"] = f.CommitMessage |
| 148 | c.Data["commit_choice"] = f.CommitChoice |
| 149 | c.Data["new_branch_name"] = branchName |
| 150 | c.Data["last_commit"] = f.LastCommit |
| 151 | c.Data["MarkdownFileExts"] = strings.Join(conf.Markdown.FileExtensions, ",") |
| 152 | c.Data["LineWrapExtensions"] = strings.Join(conf.Repository.Editor.LineWrapExtensions, ",") |
| 153 | c.Data["PreviewableFileModes"] = strings.Join(conf.Repository.Editor.PreviewableFileModes, ",") |
| 154 | |
| 155 | if c.HasError() { |
| 156 | c.HTML(http.StatusBadRequest, tmplEditorEdit) |
| 157 | return |
| 158 | } |
| 159 | |
| 160 | if f.TreePath == "" { |
| 161 | c.FormErr("TreePath") |
| 162 | c.RenderWithErr(c.Tr("repo.editor.filename_cannot_be_empty"), http.StatusBadRequest, tmplEditorEdit, &f) |
| 163 | return |
| 164 | } |
| 165 | |
| 166 | if oldBranchName != branchName { |
| 167 | if _, err := c.Repo.Repository.GetBranch(branchName); err == nil { |
| 168 | c.FormErr("NewBranchName") |
| 169 | c.RenderWithErr(c.Tr("repo.editor.branch_already_exists", branchName), http.StatusUnprocessableEntity, tmplEditorEdit, &f) |
| 170 | return |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | var newTreePath string |
| 175 | for index, part := range treeNames { |
| 176 | newTreePath = path.Join(newTreePath, part) |
| 177 | entry, err := c.Repo.Commit.TreeEntry(newTreePath) |
no test coverage detected