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.

edit.go 8.7KB

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