editor.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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_FILE base.TplName = "repo/editor/edit"
  20. EDIT_DIFF_PREVIEW base.TplName = "repo/editor/diff_preview"
  21. DELETE_FILE base.TplName = "repo/editor/delete"
  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. if !base.IsTextFile(buf) {
  63. ctx.Handle(404, "", nil)
  64. return
  65. }
  66. d, _ := ioutil.ReadAll(dataRc)
  67. buf = append(buf, d...)
  68. if err, content := template.ToUTF8WithErr(buf); err != nil {
  69. if err != nil {
  70. log.Error(4, "ToUTF8WithErr: %v", err)
  71. }
  72. ctx.Data["FileContent"] = string(buf)
  73. } else {
  74. ctx.Data["FileContent"] = content
  75. }
  76. } else {
  77. treeNames = append(treeNames, "") // Append empty string to allow user name the new file.
  78. }
  79. ctx.Data["TreePath"] = ctx.Repo.TreePath
  80. ctx.Data["TreeNames"] = treeNames
  81. ctx.Data["BranchLink"] = branchLink
  82. ctx.Data["commit_summary"] = ""
  83. ctx.Data["commit_message"] = ""
  84. ctx.Data["commit_choice"] = "direct"
  85. ctx.Data["new_branch_name"] = ""
  86. ctx.Data["last_commit"] = ctx.Repo.Commit.ID
  87. ctx.Data["MarkdownFileExts"] = strings.Join(setting.Markdown.FileExtensions, ",")
  88. ctx.Data["LineWrapExtensions"] = strings.Join(setting.Repository.Editor.LineWrapExtensions, ",")
  89. ctx.Data["PreviewableFileModes"] = strings.Join(setting.Repository.Editor.PreviewableFileModes, ",")
  90. ctx.HTML(200, EDIT_FILE)
  91. }
  92. func EditFile(ctx *context.Context) {
  93. editFile(ctx, false)
  94. }
  95. func NewFile(ctx *context.Context) {
  96. editFile(ctx, true)
  97. }
  98. func editFilePost(ctx *context.Context, form auth.EditRepoFileForm, isNewFile bool) {
  99. ctx.Data["PageIsEdit"] = true
  100. ctx.Data["IsNewFile"] = isNewFile
  101. ctx.Data["RequireHighlightJS"] = true
  102. ctx.Data["RequireSimpleMDE"] = true
  103. oldBranchName := ctx.Repo.BranchName
  104. branchName := oldBranchName
  105. branchLink := ctx.Repo.RepoLink + "/src/" + branchName
  106. oldTreePath := ctx.Repo.TreePath
  107. lastCommit := form.LastCommit
  108. form.LastCommit = ctx.Repo.Commit.ID.String()
  109. if form.CommitChoice == "commit-to-new-branch" {
  110. branchName = form.NewBranchName
  111. }
  112. form.TreePath = strings.Trim(form.TreePath, " /")
  113. var treeNames []string
  114. if len(form.TreePath) > 0 {
  115. treeNames = strings.Split(form.TreePath, "/")
  116. }
  117. ctx.Data["TreePath"] = form.TreePath
  118. ctx.Data["TreeNames"] = treeNames
  119. ctx.Data["BranchLink"] = branchLink
  120. ctx.Data["FileContent"] = form.Content
  121. ctx.Data["commit_summary"] = form.CommitSummary
  122. ctx.Data["commit_message"] = form.CommitMessage
  123. ctx.Data["commit_choice"] = form.CommitChoice
  124. ctx.Data["new_branch_name"] = branchName
  125. ctx.Data["last_commit"] = form.LastCommit
  126. ctx.Data["MarkdownFileExts"] = strings.Join(setting.Markdown.FileExtensions, ",")
  127. ctx.Data["LineWrapExtensions"] = strings.Join(setting.Repository.Editor.LineWrapExtensions, ",")
  128. ctx.Data["PreviewableFileModes"] = strings.Join(setting.Repository.Editor.PreviewableFileModes, ",")
  129. if ctx.HasError() {
  130. ctx.HTML(200, EDIT_FILE)
  131. return
  132. }
  133. if len(form.TreePath) == 0 {
  134. ctx.Data["Err_TreePath"] = true
  135. ctx.RenderWithErr(ctx.Tr("repo.editor.filename_cannot_be_empty"), EDIT_FILE, &form)
  136. return
  137. }
  138. if oldBranchName != branchName {
  139. if _, err := ctx.Repo.Repository.GetBranch(branchName); err == nil {
  140. ctx.Data["Err_NewBranchName"] = true
  141. ctx.RenderWithErr(ctx.Tr("repo.editor.branch_already_exists", branchName), EDIT_FILE, &form)
  142. return
  143. }
  144. }
  145. var newTreePath string
  146. for index, part := range treeNames {
  147. newTreePath = path.Join(newTreePath, part)
  148. entry, err := ctx.Repo.Commit.GetTreeEntryByPath(newTreePath)
  149. if err != nil {
  150. if git.IsErrNotExist(err) {
  151. // Means there is no item with that name, so we're good
  152. break
  153. }
  154. ctx.Handle(500, "GetTreeEntryByPath", err)
  155. return
  156. }
  157. if index != len(treeNames)-1 {
  158. if !entry.IsDir() {
  159. ctx.Data["Err_TreePath"] = true
  160. ctx.RenderWithErr(ctx.Tr("repo.editor.directory_is_a_file", part), EDIT_FILE, &form)
  161. return
  162. }
  163. } else {
  164. if entry.IsDir() {
  165. ctx.Data["Err_TreePath"] = true
  166. ctx.RenderWithErr(ctx.Tr("repo.editor.filename_is_a_directory", part), EDIT_FILE, &form)
  167. return
  168. }
  169. }
  170. }
  171. if !isNewFile {
  172. _, err := ctx.Repo.Commit.GetTreeEntryByPath(oldTreePath)
  173. if err != nil {
  174. if git.IsErrNotExist(err) {
  175. ctx.Data["Err_TreePath"] = true
  176. ctx.RenderWithErr(ctx.Tr("repo.editor.file_editing_no_longer_exists", oldTreePath), EDIT_FILE, &form)
  177. } else {
  178. ctx.Handle(500, "GetTreeEntryByPath", err)
  179. }
  180. return
  181. }
  182. if lastCommit != ctx.Repo.CommitID {
  183. files, err := ctx.Repo.Commit.GetFilesChangedSinceCommit(lastCommit)
  184. if err != nil {
  185. ctx.Handle(500, "GetFilesChangedSinceCommit", err)
  186. return
  187. }
  188. for _, file := range files {
  189. if file == form.TreePath {
  190. ctx.RenderWithErr(ctx.Tr("repo.editor.file_changed_while_editing", ctx.Repo.RepoLink+"/compare/"+lastCommit+"..."+ctx.Repo.CommitID), EDIT_FILE, &form)
  191. return
  192. }
  193. }
  194. }
  195. }
  196. if oldTreePath != form.TreePath {
  197. // We have a new filename (rename or completely new file) so we need to make sure it doesn't already exist, can't clobber.
  198. entry, err := ctx.Repo.Commit.GetTreeEntryByPath(form.TreePath)
  199. if err != nil {
  200. if !git.IsErrNotExist(err) {
  201. ctx.Handle(500, "GetTreeEntryByPath", err)
  202. return
  203. }
  204. }
  205. if entry != nil {
  206. ctx.Data["Err_TreePath"] = true
  207. ctx.RenderWithErr(ctx.Tr("repo.editor.file_already_exists", form.TreePath), EDIT_FILE, &form)
  208. return
  209. }
  210. }
  211. message := strings.TrimSpace(form.CommitSummary)
  212. if len(message) == 0 {
  213. if isNewFile {
  214. message = ctx.Tr("repo.editor.add", form.TreePath)
  215. } else {
  216. message = ctx.Tr("repo.editor.update", form.TreePath)
  217. }
  218. }
  219. form.CommitMessage = strings.TrimSpace(form.CommitMessage)
  220. if len(form.CommitMessage) > 0 {
  221. message += "\n\n" + form.CommitMessage
  222. }
  223. if err := ctx.Repo.Repository.UpdateRepoFile(ctx.User, models.UpdateRepoFileOptions{
  224. LastCommitID: lastCommit,
  225. OldBranch: oldBranchName,
  226. NewBranch: branchName,
  227. OldTreeName: oldTreePath,
  228. NewTreeName: form.TreePath,
  229. Message: message,
  230. Content: strings.Replace(form.Content, "\r", "", -1),
  231. IsNewFile: isNewFile,
  232. }); err != nil {
  233. ctx.Data["Err_TreePath"] = true
  234. ctx.RenderWithErr(ctx.Tr("repo.editor.fail_to_update_file", form.TreePath, err), EDIT_FILE, &form)
  235. return
  236. }
  237. ctx.Redirect(ctx.Repo.RepoLink + "/src/" + branchName + "/" + form.TreePath)
  238. }
  239. func EditFilePost(ctx *context.Context, form auth.EditRepoFileForm) {
  240. editFilePost(ctx, form, false)
  241. }
  242. func NewFilePost(ctx *context.Context, form auth.EditRepoFileForm) {
  243. editFilePost(ctx, form, true)
  244. }
  245. func DiffPreviewPost(ctx *context.Context, form auth.EditPreviewDiffForm) {
  246. treePath := ctx.Repo.TreePath
  247. entry, err := ctx.Repo.Commit.GetTreeEntryByPath(treePath)
  248. if err != nil {
  249. ctx.Error(500, "GetTreeEntryByPath: "+err.Error())
  250. return
  251. } else if entry.IsDir() {
  252. ctx.Error(422)
  253. return
  254. }
  255. diff, err := ctx.Repo.Repository.GetDiffPreview(ctx.Repo.BranchName, treePath, form.Content)
  256. if err != nil {
  257. ctx.Error(500, "GetDiffPreview: "+err.Error())
  258. return
  259. }
  260. if diff.NumFiles() == 0 {
  261. ctx.PlainText(200, []byte(ctx.Tr("repo.editor.no_changes_to_show")))
  262. return
  263. }
  264. ctx.Data["File"] = diff.Files[0]
  265. ctx.HTML(200, EDIT_DIFF_PREVIEW)
  266. }
  267. func DeleteFile(ctx *context.Context) {
  268. ctx.Data["PageIsDelete"] = true
  269. ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchName
  270. ctx.Data["TreePath"] = ctx.Repo.TreePath
  271. ctx.Data["commit_summary"] = ""
  272. ctx.Data["commit_message"] = ""
  273. ctx.Data["commit_choice"] = "direct"
  274. ctx.Data["new_branch_name"] = ""
  275. ctx.HTML(200, DELETE_FILE)
  276. }
  277. func DeleteFilePost(ctx *context.Context, form auth.DeleteRepoFileForm) {
  278. ctx.Data["PageIsDelete"] = true
  279. ctx.Data["BranchLink"] = ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchName
  280. ctx.Data["TreePath"] = ctx.Repo.TreePath
  281. oldBranchName := ctx.Repo.BranchName
  282. branchName := oldBranchName
  283. treePath := ctx.Repo.TreePath
  284. if form.CommitChoice == "commit-to-new-branch" {
  285. branchName = form.NewBranchName
  286. }
  287. ctx.Data["commit_summary"] = form.CommitSummary
  288. ctx.Data["commit_message"] = form.CommitMessage
  289. ctx.Data["commit_choice"] = form.CommitChoice
  290. ctx.Data["new_branch_name"] = branchName
  291. if ctx.HasError() {
  292. ctx.HTML(200, DELETE_FILE)
  293. return
  294. }
  295. if oldBranchName != branchName {
  296. if _, err := ctx.Repo.Repository.GetBranch(branchName); err == nil {
  297. ctx.Data["Err_NewBranchName"] = true
  298. ctx.RenderWithErr(ctx.Tr("repo.editor.branch_already_exists", branchName), DELETE_FILE, &form)
  299. return
  300. }
  301. }
  302. message := strings.TrimSpace(form.CommitSummary)
  303. if len(message) == 0 {
  304. message = ctx.Tr("repo.editor.delete", treePath)
  305. }
  306. form.CommitMessage = strings.TrimSpace(form.CommitMessage)
  307. if len(form.CommitMessage) > 0 {
  308. message += "\n\n" + form.CommitMessage
  309. }
  310. if err := ctx.Repo.Repository.DeleteRepoFile(ctx.User, models.DeleteRepoFileOptions{
  311. LastCommitID: ctx.Repo.CommitID,
  312. OldBranch: oldBranchName,
  313. NewBranch: branchName,
  314. TreePath: treePath,
  315. Message: message,
  316. }); err != nil {
  317. ctx.Handle(500, "DeleteRepoFile", err)
  318. return
  319. }
  320. ctx.Flash.Success(ctx.Tr("repo.editor.file_delete_success", treePath))
  321. ctx.Redirect(ctx.Repo.RepoLink + "/src/" + branchName)
  322. }