editor.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repo
  5. import (
  6. "io/ioutil"
  7. "path"
  8. "strings"
  9. "github.com/gogits/git-module"
  10. "github.com/gogits/gogs/models"
  11. "github.com/gogits/gogs/modules/auth"
  12. "github.com/gogits/gogs/modules/base"
  13. "github.com/gogits/gogs/modules/context"
  14. "github.com/gogits/gogs/modules/log"
  15. "github.com/gogits/gogs/modules/setting"
  16. "github.com/gogits/gogs/modules/template"
  17. )
  18. const (
  19. EDIT base.TplName = "repo/editor/edit"
  20. DIFF_PREVIEW base.TplName = "repo/editor/diff_preview"
  21. DIFF_PREVIEW_NEW base.TplName = "repo/editor/diff_preview_new"
  22. )
  23. func editFile(ctx *context.Context, isNewFile bool) {
  24. ctx.Data["PageIsEdit"] = true
  25. ctx.Data["IsNewFile"] = isNewFile
  26. ctx.Data["RequireHighlightJS"] = true
  27. ctx.Data["RequireSimpleMDE"] = true
  28. branchLink := ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchName
  29. var treeNames []string
  30. if len(ctx.Repo.TreePath) > 0 {
  31. treeNames = strings.Split(ctx.Repo.TreePath, "/")
  32. }
  33. if !isNewFile {
  34. entry, err := ctx.Repo.Commit.GetTreeEntryByPath(ctx.Repo.TreePath)
  35. if err != nil {
  36. if git.IsErrNotExist(err) {
  37. ctx.Handle(404, "GetTreeEntryByPath", err)
  38. } else {
  39. ctx.Handle(500, "GetTreeEntryByPath", err)
  40. }
  41. return
  42. }
  43. // No way to edit a directory online.
  44. if entry.IsDir() {
  45. ctx.Handle(404, "", nil)
  46. return
  47. }
  48. blob := entry.Blob()
  49. dataRc, err := blob.Data()
  50. if err != nil {
  51. ctx.Handle(404, "blob.Data", err)
  52. return
  53. }
  54. ctx.Data["FileSize"] = blob.Size()
  55. ctx.Data["FileName"] = blob.Name()
  56. buf := make([]byte, 1024)
  57. n, _ := dataRc.Read(buf)
  58. if n > 0 {
  59. buf = buf[:n]
  60. }
  61. // Only text file are editable online.
  62. _, isTextFile := base.IsTextFile(buf)
  63. if !isTextFile {
  64. ctx.Handle(404, "", nil)
  65. return
  66. }
  67. d, _ := ioutil.ReadAll(dataRc)
  68. buf = append(buf, d...)
  69. if err, content := template.ToUTF8WithErr(buf); err != nil {
  70. if err != nil {
  71. log.Error(4, "ToUTF8WithErr: %v", err)
  72. }
  73. ctx.Data["FileContent"] = string(buf)
  74. } else {
  75. ctx.Data["FileContent"] = content
  76. }
  77. } else {
  78. treeNames = append(treeNames, "") // Append empty string to allow user name the new file.
  79. }
  80. ctx.Data["TreePath"] = ctx.Repo.TreePath
  81. ctx.Data["TreeNames"] = treeNames
  82. ctx.Data["BranchLink"] = branchLink
  83. ctx.Data["commit_summary"] = ""
  84. ctx.Data["commit_message"] = ""
  85. ctx.Data["commit_choice"] = "direct"
  86. ctx.Data["new_branch_name"] = ""
  87. ctx.Data["last_commit"] = ctx.Repo.Commit.ID
  88. ctx.Data["MarkdownFileExts"] = strings.Join(setting.Markdown.FileExtensions, ",")
  89. ctx.Data["LineWrapExtensions"] = strings.Join(setting.Repository.Editor.LineWrapExtensions, ",")
  90. ctx.Data["PreviewableFileModes"] = strings.Join(setting.Repository.Editor.PreviewableFileModes, ",")
  91. ctx.HTML(200, EDIT)
  92. }
  93. func EditFile(ctx *context.Context) {
  94. editFile(ctx, false)
  95. }
  96. func NewFile(ctx *context.Context) {
  97. editFile(ctx, true)
  98. }
  99. func editFilePost(ctx *context.Context, form auth.EditRepoFileForm, isNewFile bool) {
  100. ctx.Data["PageIsEdit"] = true
  101. ctx.Data["IsNewFile"] = isNewFile
  102. ctx.Data["RequireHighlightJS"] = true
  103. ctx.Data["RequireSimpleMDE"] = true
  104. oldBranchName := ctx.Repo.BranchName
  105. branchName := oldBranchName
  106. branchLink := ctx.Repo.RepoLink + "/src/" + branchName
  107. oldTreePath := ctx.Repo.TreePath
  108. content := form.Content
  109. commitChoice := form.CommitChoice
  110. lastCommit := form.LastCommit
  111. form.LastCommit = ctx.Repo.Commit.ID.String()
  112. if commitChoice == "commit-to-new-branch" {
  113. branchName = form.NewBranchName
  114. }
  115. form.TreePath = strings.Trim(form.TreePath, " /")
  116. var treeNames []string
  117. if len(form.TreePath) > 0 {
  118. treeNames = strings.Split(form.TreePath, "/")
  119. }
  120. ctx.Data["TreePath"] = form.TreePath
  121. ctx.Data["TreeNames"] = treeNames
  122. ctx.Data["BranchLink"] = branchLink
  123. ctx.Data["FileContent"] = content
  124. ctx.Data["commit_summary"] = form.CommitSummary
  125. ctx.Data["commit_message"] = form.CommitMessage
  126. ctx.Data["commit_choice"] = commitChoice
  127. ctx.Data["new_branch_name"] = branchName
  128. ctx.Data["last_commit"] = form.LastCommit
  129. ctx.Data["MarkdownFileExts"] = strings.Join(setting.Markdown.FileExtensions, ",")
  130. ctx.Data["LineWrapExtensions"] = strings.Join(setting.Repository.Editor.LineWrapExtensions, ",")
  131. ctx.Data["PreviewableFileModes"] = strings.Join(setting.Repository.Editor.PreviewableFileModes, ",")
  132. if ctx.HasError() {
  133. ctx.HTML(200, EDIT)
  134. return
  135. }
  136. if len(form.TreePath) == 0 {
  137. ctx.Data["Err_TreePath"] = true
  138. ctx.RenderWithErr(ctx.Tr("repo.editor.filename_cannot_be_empty"), EDIT, &form)
  139. return
  140. }
  141. if oldBranchName != branchName {
  142. if _, err := ctx.Repo.Repository.GetBranch(branchName); err == nil {
  143. ctx.Data["Err_NewBranchName"] = true
  144. ctx.RenderWithErr(ctx.Tr("repo.editor.branch_already_exists", branchName), EDIT, &form)
  145. return
  146. }
  147. }
  148. var newTreePath string
  149. for index, part := range treeNames {
  150. newTreePath = path.Join(newTreePath, part)
  151. entry, err := ctx.Repo.Commit.GetTreeEntryByPath(newTreePath)
  152. if err != nil {
  153. if git.IsErrNotExist(err) {
  154. // Means there is no item with that name, so we're good
  155. break
  156. }
  157. ctx.Handle(500, "GetTreeEntryByPath", err)
  158. return
  159. }
  160. if index != len(treeNames)-1 {
  161. if !entry.IsDir() {
  162. ctx.Data["Err_TreePath"] = true
  163. ctx.RenderWithErr(ctx.Tr("repo.editor.directory_is_a_file", part), EDIT, &form)
  164. return
  165. }
  166. } else {
  167. if entry.IsDir() {
  168. ctx.Data["Err_TreePath"] = true
  169. ctx.RenderWithErr(ctx.Tr("repo.editor.filename_is_a_directory", part), EDIT, &form)
  170. return
  171. }
  172. }
  173. }
  174. if !isNewFile {
  175. _, err := ctx.Repo.Commit.GetTreeEntryByPath(oldTreePath)
  176. if err != nil {
  177. if git.IsErrNotExist(err) {
  178. ctx.Data["Err_TreePath"] = true
  179. ctx.RenderWithErr(ctx.Tr("repo.editor.file_editing_no_longer_exists", oldTreePath), EDIT, &form)
  180. } else {
  181. ctx.Handle(500, "GetTreeEntryByPath", err)
  182. }
  183. return
  184. }
  185. if lastCommit != ctx.Repo.CommitID {
  186. files, err := ctx.Repo.Commit.GetFilesChangedSinceCommit(lastCommit)
  187. if err != nil {
  188. ctx.Handle(500, "GetFilesChangedSinceCommit", err)
  189. return
  190. }
  191. for _, file := range files {
  192. if file == form.TreePath {
  193. ctx.RenderWithErr(ctx.Tr("repo.editor.file_changed_while_editing", ctx.Repo.RepoLink+"/compare/"+lastCommit+"..."+ctx.Repo.CommitID), EDIT, &form)
  194. return
  195. }
  196. }
  197. }
  198. }
  199. if oldTreePath != form.TreePath {
  200. // We have a new filename (rename or completely new file) so we need to make sure it doesn't already exist, can't clobber.
  201. entry, err := ctx.Repo.Commit.GetTreeEntryByPath(form.TreePath)
  202. if err != nil {
  203. if !git.IsErrNotExist(err) {
  204. ctx.Handle(500, "GetTreeEntryByPath", err)
  205. return
  206. }
  207. }
  208. if entry != nil {
  209. ctx.Data["Err_TreePath"] = true
  210. ctx.RenderWithErr(ctx.Tr("repo.editor.file_already_exists", form.TreePath), EDIT, &form)
  211. return
  212. }
  213. }
  214. var message string
  215. if len(form.CommitSummary) > 0 {
  216. message = strings.TrimSpace(form.CommitSummary)
  217. } else {
  218. if isNewFile {
  219. message = ctx.Tr("repo.editor.add", form.TreePath)
  220. } else {
  221. message = ctx.Tr("repo.editor.update", form.TreePath)
  222. }
  223. }
  224. form.CommitMessage = strings.TrimSpace(form.CommitMessage)
  225. if len(form.CommitMessage) > 0 {
  226. message += "\n\n" + form.CommitMessage
  227. }
  228. if err := ctx.Repo.Repository.UpdateRepoFile(ctx.User, models.UpdateRepoFileOptions{
  229. LastCommitID: lastCommit,
  230. OldBranch: oldBranchName,
  231. NewBranch: branchName,
  232. OldTreeName: oldTreePath,
  233. NewTreeName: form.TreePath,
  234. Message: message,
  235. Content: content,
  236. IsNewFile: isNewFile,
  237. }); err != nil {
  238. ctx.Data["Err_TreePath"] = true
  239. ctx.RenderWithErr(ctx.Tr("repo.editor.failed_to_update_file", err), EDIT, &form)
  240. return
  241. }
  242. ctx.Redirect(ctx.Repo.RepoLink + "/src/" + branchName + "/" + form.TreePath)
  243. }
  244. func EditFilePost(ctx *context.Context, form auth.EditRepoFileForm) {
  245. editFilePost(ctx, form, false)
  246. }
  247. func NewFilePost(ctx *context.Context, form auth.EditRepoFileForm) {
  248. editFilePost(ctx, form, true)
  249. }
  250. func DiffPreviewPost(ctx *context.Context, form auth.EditPreviewDiffForm) {
  251. treeName := ctx.Repo.TreePath
  252. content := form.Content
  253. entry, err := ctx.Repo.Commit.GetTreeEntryByPath(treeName)
  254. if err != nil {
  255. if git.IsErrNotExist(err) {
  256. ctx.Data["FileContent"] = content
  257. ctx.HTML(200, DIFF_PREVIEW_NEW)
  258. } else {
  259. ctx.Error(500, "GetTreeEntryByPath: "+err.Error())
  260. }
  261. return
  262. }
  263. if entry.IsDir() {
  264. ctx.Error(422)
  265. return
  266. }
  267. diff, err := ctx.Repo.Repository.GetDiffPreview(ctx.Repo.BranchName, treeName, content)
  268. if err != nil {
  269. ctx.Error(500, "GetDiffPreview: "+err.Error())
  270. return
  271. }
  272. if diff.NumFiles() == 0 {
  273. ctx.PlainText(200, []byte(ctx.Tr("repo.editor.no_changes_to_show")))
  274. return
  275. }
  276. ctx.Data["File"] = diff.Files[0]
  277. ctx.HTML(200, DIFF_PREVIEW)
  278. }
  279. func DeleteFilePost(ctx *context.Context, form auth.DeleteRepoFileForm) {
  280. branchName := ctx.Repo.BranchName
  281. treeName := ctx.Repo.TreePath
  282. if ctx.HasError() {
  283. ctx.Redirect(ctx.Repo.RepoLink + "/src/" + branchName + "/" + treeName)
  284. return
  285. }
  286. if err := ctx.Repo.Repository.DeleteRepoFile(ctx.User, models.DeleteRepoFileOptions{
  287. LastCommitID: ctx.Repo.CommitID,
  288. Branch: branchName,
  289. TreePath: treeName,
  290. Message: form.CommitSummary,
  291. }); err != nil {
  292. ctx.Handle(500, "DeleteRepoFile", err)
  293. return
  294. }
  295. ctx.Flash.Success(ctx.Tr("repo.editor.file_delete_success", treeName))
  296. ctx.Redirect(ctx.Repo.RepoLink + "/src/" + branchName)
  297. }