Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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