MCPcopy Create free account
hub / github.com/gogs/gogs / editFilePost

Function editFilePost

internal/route/repo/editor.go:120–292  ·  view source on GitHub ↗
(c *context.Context, f form.EditRepoFile, isNewFile bool)

Source from the content-addressed store, hash-verified

118}
119
120func 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)

Callers 2

EditFilePostFunction · 0.85
NewFilePostFunction · 0.85

Calls 15

CleanFunction · 0.92
IsErrRevisionNotExistFunction · 0.92
getParentTreeFieldsFunction · 0.85
PageIsMethod · 0.80
RequireHighlightJSMethod · 0.80
RequireSimpleMDEMethod · 0.80
HasErrorMethod · 0.80
HTMLMethod · 0.80
FormErrMethod · 0.80
RenderWithErrMethod · 0.80
TrMethod · 0.80
UpdateRepoFileMethod · 0.80

Tested by

no test coverage detected