editor.go 10 KB

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