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 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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/edit"
  20. DIFF_PREVIEW base.TplName = "repo/diff_preview"
  21. DIFF_PREVIEW_NEW base.TplName = "repo/diff_preview_new"
  22. )
  23. func EditFile(ctx *context.Context) {
  24. editFile(ctx, false)
  25. }
  26. func NewFile(ctx *context.Context) {
  27. editFile(ctx, true)
  28. }
  29. func editFile(ctx *context.Context, isNewFile bool) {
  30. ctx.Data["PageIsEdit"] = true
  31. ctx.Data["IsNewFile"] = isNewFile
  32. ctx.Data["RequireHighlightJS"] = true
  33. userName := ctx.Repo.Owner.Name
  34. repoName := ctx.Repo.Repository.Name
  35. branchName := ctx.Repo.BranchName
  36. branchLink := ctx.Repo.RepoLink + "/src/" + branchName
  37. treeName := ctx.Repo.TreeName
  38. var treeNames []string
  39. if len(treeName) > 0 {
  40. treeNames = strings.Split(treeName, "/")
  41. }
  42. if !isNewFile {
  43. entry, err := ctx.Repo.Commit.GetTreeEntryByPath(treeName)
  44. if err != nil && git.IsErrNotExist(err) {
  45. ctx.Handle(404, "GetTreeEntryByPath", err)
  46. return
  47. }
  48. if (ctx.Repo.IsViewCommit) || entry == nil || entry.IsDir() {
  49. ctx.Handle(404, "repo.Home", nil)
  50. return
  51. }
  52. blob := entry.Blob()
  53. dataRc, err := blob.Data()
  54. if err != nil {
  55. ctx.Handle(404, "blob.Data", err)
  56. return
  57. }
  58. ctx.Data["FileSize"] = blob.Size()
  59. ctx.Data["FileName"] = blob.Name()
  60. buf := make([]byte, 1024)
  61. n, _ := dataRc.Read(buf)
  62. if n > 0 {
  63. buf = buf[:n]
  64. }
  65. _, isTextFile := base.IsTextFile(buf)
  66. if !isTextFile {
  67. ctx.Handle(404, "repo.Home", nil)
  68. return
  69. }
  70. d, _ := ioutil.ReadAll(dataRc)
  71. buf = append(buf, d...)
  72. if err, content := template.ToUTF8WithErr(buf); err != nil {
  73. if err != nil {
  74. log.Error(4, "Convert content encoding: %s", err)
  75. }
  76. ctx.Data["FileContent"] = string(buf)
  77. } else {
  78. ctx.Data["FileContent"] = content
  79. }
  80. } else {
  81. treeNames = append(treeNames, "")
  82. }
  83. ctx.Data["RequireSimpleMDE"] = true
  84. ctx.Data["UserName"] = userName
  85. ctx.Data["RepoName"] = repoName
  86. ctx.Data["BranchName"] = branchName
  87. ctx.Data["TreeName"] = treeName
  88. ctx.Data["TreeNames"] = treeNames
  89. ctx.Data["BranchLink"] = branchLink
  90. ctx.Data["CommitSummary"] = ""
  91. ctx.Data["CommitMessage"] = ""
  92. ctx.Data["CommitChoice"] = "direct"
  93. ctx.Data["NewBranchName"] = ""
  94. ctx.Data["CommitDirectlyToThisBranch"] = ctx.Tr("repo.commit_directly_to_this_branch", "<strong class=\"branch-name\">"+branchName+"</strong>")
  95. ctx.Data["CreateNewBranch"] = ctx.Tr("repo.create_new_branch", "<strong>"+ctx.Tr("repo.new_branch")+"</strong>")
  96. ctx.Data["LastCommit"] = ctx.Repo.Commit.ID
  97. ctx.Data["MarkdownFileExts"] = strings.Join(setting.Markdown.FileExtensions, ",")
  98. ctx.Data["LineWrapExtensions"] = strings.Join(setting.Repository.Editor.LineWrapExtensions, ",")
  99. ctx.Data["PreviewableFileModes"] = strings.Join(setting.Repository.Editor.PreviewableFileModes, ",")
  100. ctx.Data["PreviewDiffURL"] = ctx.Repo.RepoLink + "/preview/" + branchName + "/" + treeName
  101. ctx.HTML(200, EDIT)
  102. }
  103. func EditFilePost(ctx *context.Context, form auth.EditRepoFileForm) {
  104. editFilePost(ctx, form, false)
  105. }
  106. func NewFilePost(ctx *context.Context, form auth.EditRepoFileForm) {
  107. editFilePost(ctx, form, true)
  108. }
  109. func editFilePost(ctx *context.Context, form auth.EditRepoFileForm, isNewFile bool) {
  110. ctx.Data["PageIsEdit"] = true
  111. ctx.Data["IsNewFile"] = isNewFile
  112. ctx.Data["RequireHighlightJS"] = true
  113. userName := ctx.Repo.Owner.Name
  114. repoName := ctx.Repo.Repository.Name
  115. oldBranchName := ctx.Repo.BranchName
  116. branchName := oldBranchName
  117. branchLink := ctx.Repo.RepoLink + "/src/" + branchName
  118. oldTreeName := ctx.Repo.TreeName
  119. content := form.Content
  120. commitChoice := form.CommitChoice
  121. lastCommit := form.LastCommit
  122. if commitChoice == "commit-to-new-branch" {
  123. branchName = form.NewBranchName
  124. }
  125. treeName := form.TreeName
  126. treeName = strings.Trim(treeName, " ")
  127. treeName = strings.Trim(treeName, "/")
  128. var treeNames []string
  129. if len(treeName) > 0 {
  130. treeNames = strings.Split(treeName, "/")
  131. }
  132. ctx.Data["RequireSimpleMDE"] = true
  133. ctx.Data["UserName"] = userName
  134. ctx.Data["RepoName"] = repoName
  135. ctx.Data["BranchName"] = branchName
  136. ctx.Data["TreeName"] = treeName
  137. ctx.Data["TreeNames"] = treeNames
  138. ctx.Data["BranchLink"] = branchLink
  139. ctx.Data["FileContent"] = content
  140. ctx.Data["CommitSummary"] = form.CommitSummary
  141. ctx.Data["CommitMessage"] = form.CommitMessage
  142. ctx.Data["CommitChoice"] = commitChoice
  143. ctx.Data["NewBranchName"] = branchName
  144. ctx.Data["CommitDirectlyToThisBranch"] = ctx.Tr("repo.commit_directly_to_this_branch", "<strong class=\"branch-name\">"+oldBranchName+"</strong>")
  145. ctx.Data["CreateNewBranch"] = ctx.Tr("repo.create_new_branch", "<strong>"+ctx.Tr("repo.new_branch")+"</strong>")
  146. ctx.Data["LastCommit"] = ctx.Repo.Commit.ID
  147. ctx.Data["MarkdownFileExts"] = strings.Join(setting.Markdown.FileExtensions, ",")
  148. ctx.Data["LineWrapExtensions"] = strings.Join(setting.Repository.Editor.LineWrapExtensions, ",")
  149. ctx.Data["PreviewableFileModes"] = strings.Join(setting.Repository.Editor.PreviewableFileModes, ",")
  150. ctx.Data["PreviewDiffURL"] = ctx.Repo.RepoLink + "/preview/" + branchName + "/" + treeName
  151. if ctx.HasError() {
  152. ctx.HTML(200, EDIT)
  153. return
  154. }
  155. if len(treeName) == 0 {
  156. ctx.Data["Err_Filename"] = true
  157. ctx.RenderWithErr(ctx.Tr("repo.filename_cannot_be_empty"), EDIT, &form)
  158. log.Error(4, "%s: %s", "EditFile", "Filename can't be empty")
  159. return
  160. }
  161. if oldBranchName != branchName {
  162. if _, err := ctx.Repo.Repository.GetBranch(branchName); err == nil {
  163. ctx.Data["Err_Branchname"] = true
  164. ctx.RenderWithErr(ctx.Tr("repo.branch_already_exists"), EDIT, &form)
  165. log.Error(4, "%s: %s - %s", "BranchName", branchName, "Branch already exists")
  166. return
  167. }
  168. }
  169. treepath := ""
  170. for index, part := range treeNames {
  171. treepath = path.Join(treepath, part)
  172. entry, err := ctx.Repo.Commit.GetTreeEntryByPath(treepath)
  173. if err != nil {
  174. // Means there is no item with that name, so we're good
  175. break
  176. }
  177. if index != len(treeNames)-1 {
  178. if !entry.IsDir() {
  179. ctx.Data["Err_Filename"] = true
  180. ctx.RenderWithErr(ctx.Tr("repo.directory_is_a_file"), EDIT, &form)
  181. log.Error(4, "%s: %s - %s", "EditFile", treeName, "Directory given is a file")
  182. return
  183. }
  184. } else {
  185. if entry.IsDir() {
  186. ctx.Data["Err_Filename"] = true
  187. ctx.RenderWithErr(ctx.Tr("repo.filename_is_a_directory"), EDIT, &form)
  188. log.Error(4, "%s: %s - %s", "EditFile", treeName, "Filename given is a dirctory")
  189. return
  190. }
  191. }
  192. }
  193. if !isNewFile {
  194. _, err := ctx.Repo.Commit.GetTreeEntryByPath(oldTreeName)
  195. if err != nil && git.IsErrNotExist(err) {
  196. ctx.Data["Err_Filename"] = true
  197. ctx.RenderWithErr(ctx.Tr("repo.file_editing_no_longer_exists"), EDIT, &form)
  198. log.Error(4, "%s: %s / %s - %s", "EditFile", branchName, oldTreeName, "File doesn't exist for editing")
  199. return
  200. }
  201. if lastCommit != ctx.Repo.CommitID {
  202. if files, err := ctx.Repo.Commit.GetFilesChangedSinceCommit(lastCommit); err == nil {
  203. for _, file := range files {
  204. if file == treeName {
  205. name := ctx.Repo.Commit.Author.Name
  206. if u, err := models.GetUserByEmail(ctx.Repo.Commit.Author.Email); err == nil {
  207. name = `<a href="` + setting.AppSubUrl + "/" + u.Name + `" target="_blank">` + u.Name + `</a>`
  208. }
  209. message := ctx.Tr("repo.user_has_committed_since_you_started_editing", name) +
  210. ` <a href="` + ctx.Repo.RepoLink + "/commit/" + ctx.Repo.CommitID + `" target="_blank">` + ctx.Tr("repo.see_what_changed") + `</a>` +
  211. " " + ctx.Tr("repo.pressing_commit_again_will_overwrite_those_changes", "<em>"+ctx.Tr("repo.commit_changes")+"</em>")
  212. log.Error(4, "%s: %s / %s - %s", "EditFile", branchName, oldTreeName, "File updated by another user")
  213. ctx.RenderWithErr(message, EDIT, &form)
  214. return
  215. }
  216. }
  217. }
  218. }
  219. }
  220. if oldTreeName != treeName {
  221. // We have a new filename (rename or completely new file) so we need to make sure it doesn't already exist, can't clobber
  222. _, err := ctx.Repo.Commit.GetTreeEntryByPath(treeName)
  223. if err == nil {
  224. ctx.Data["Err_Filename"] = true
  225. ctx.RenderWithErr(ctx.Tr("repo.file_already_exists"), EDIT, &form)
  226. log.Error(4, "%s: %s - %s", "NewFile", treeName, "File already exists, can't create new")
  227. return
  228. }
  229. }
  230. message := ""
  231. if form.CommitSummary != "" {
  232. message = strings.Trim(form.CommitSummary, " ")
  233. } else {
  234. if isNewFile {
  235. message = ctx.Tr("repo.add") + " '" + treeName + "'"
  236. } else {
  237. message = ctx.Tr("repo.update") + " '" + treeName + "'"
  238. }
  239. }
  240. if strings.Trim(form.CommitMessage, " ") != "" {
  241. message += "\n\n" + strings.Trim(form.CommitMessage, " ")
  242. }
  243. if err := ctx.Repo.Repository.UpdateRepoFile(ctx.User, oldBranchName, branchName, oldTreeName, treeName, content, message, isNewFile); err != nil {
  244. ctx.Data["Err_Filename"] = true
  245. ctx.RenderWithErr(ctx.Tr("repo.unable_to_update_file"), EDIT, &form)
  246. log.Error(4, "%s: %v", "EditFile", err)
  247. return
  248. }
  249. if branch, err := ctx.Repo.Repository.GetBranch(branchName); err != nil {
  250. log.Error(4, "repo.Repository.GetBranch(%s): %v", branchName, err)
  251. } else if commit, err := branch.GetCommit(); err != nil {
  252. log.Error(4, "branch.GetCommit(): %v", err)
  253. } else {
  254. pc := &models.PushCommits{
  255. Len: 1,
  256. Commits: []*models.PushCommit{models.CommitToPushCommit(commit)},
  257. }
  258. oldCommitID := ctx.Repo.CommitID
  259. newCommitID := commit.ID.String()
  260. if branchName != oldBranchName {
  261. oldCommitID = "0000000000000000000000000000000000000000" // New Branch so we use all 0s
  262. }
  263. if err := models.CommitRepoAction(ctx.User.ID, ctx.Repo.Owner.ID, ctx.User.LowerName, ctx.Repo.Owner.Email,
  264. ctx.Repo.Repository.ID, ctx.Repo.Owner.LowerName, ctx.Repo.Repository.Name, "refs/heads/"+branchName, pc,
  265. oldCommitID, newCommitID); err != nil {
  266. log.Error(4, "models.CommitRepoAction(branch = %s): %v", branchName, err)
  267. }
  268. models.HookQueue.Add(ctx.Repo.Repository.ID)
  269. }
  270. // Leaving this off until forked repos that get a branch can compare with forks master and not upstream
  271. //if oldBranchName != branchName {
  272. // ctx.Redirect(EscapeUrl(ctx.Repo.RepoLink + "/compare/" + oldBranchName + "..." + branchName))
  273. //} else {
  274. ctx.Redirect(EscapeUrl(ctx.Repo.RepoLink + "/src/" + branchName + "/" + treeName))
  275. //}
  276. }
  277. func DiffPreviewPost(ctx *context.Context, form auth.EditPreviewDiffForm) {
  278. userName := ctx.Repo.Owner.Name
  279. repoName := ctx.Repo.Repository.Name
  280. branchName := ctx.Repo.BranchName
  281. treeName := ctx.Repo.TreeName
  282. content := form.Content
  283. entry, err := ctx.Repo.Commit.GetTreeEntryByPath(treeName)
  284. if (err != nil && git.IsErrNotExist(err)) || entry.IsDir() {
  285. ctx.Data["FileContent"] = content
  286. ctx.HTML(200, DIFF_PREVIEW_NEW)
  287. return
  288. }
  289. diff, err := ctx.Repo.Repository.GetPreviewDiff(models.RepoPath(userName, repoName), branchName, treeName, content, setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
  290. if err != nil {
  291. ctx.Error(404, err.Error())
  292. log.Error(4, "%s: %v", "GetPreviewDiff", err)
  293. return
  294. }
  295. if diff.NumFiles() == 0 {
  296. ctx.Error(200, ctx.Tr("repo.no_changes_to_show"))
  297. return
  298. }
  299. ctx.Data["IsSplitStyle"] = ctx.Query("style") == "split"
  300. ctx.Data["File"] = diff.Files[0]
  301. ctx.HTML(200, DIFF_PREVIEW)
  302. }
  303. func EscapeUrl(str string) string {
  304. return strings.NewReplacer("?", "%3F", "%", "%25", "#", "%23", " ", "%20", "^", "%5E", "\\", "%5C", "{", "%7B", "}", "%7D", "|", "%7C").Replace(str)
  305. }