You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

editor.go 11KB

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