repo_editor.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 models
  5. import (
  6. "fmt"
  7. "io/ioutil"
  8. "os"
  9. "os/exec"
  10. "path"
  11. "path/filepath"
  12. "time"
  13. "github.com/Unknwon/com"
  14. git "github.com/gogits/git-module"
  15. "github.com/gogits/gogs/modules/log"
  16. "github.com/gogits/gogs/modules/process"
  17. "github.com/gogits/gogs/modules/setting"
  18. )
  19. // ___________ .___.__ __ ___________.__.__
  20. // \_ _____/ __| _/|__|/ |_ \_ _____/|__| | ____
  21. // | __)_ / __ | | \ __\ | __) | | | _/ __ \
  22. // | \/ /_/ | | || | | \ | | |_\ ___/
  23. // /_______ /\____ | |__||__| \___ / |__|____/\___ >
  24. // \/ \/ \/ \/
  25. // discardLocalRepoBranchChanges discards local commits/changes of
  26. // given branch to make sure it is even to remote branch.
  27. func discardLocalRepoBranchChanges(localPath, branch string) error {
  28. if !com.IsExist(localPath) {
  29. return nil
  30. }
  31. // No need to check if nothing in the repository.
  32. if !git.IsBranchExist(localPath, branch) {
  33. return nil
  34. }
  35. refName := "origin/" + branch
  36. if err := git.ResetHEAD(localPath, true, refName); err != nil {
  37. return fmt.Errorf("git reset --hard %s: %v", refName, err)
  38. }
  39. return nil
  40. }
  41. func (repo *Repository) DiscardLocalRepoBranchChanges(branch string) error {
  42. return discardLocalRepoBranchChanges(repo.LocalCopyPath(), branch)
  43. }
  44. // checkoutNewBranch checks out to a new branch from the a branch name.
  45. func checkoutNewBranch(repoPath, localPath, oldBranch, newBranch string) error {
  46. if err := git.Checkout(localPath, git.CheckoutOptions{
  47. Timeout: time.Duration(setting.Git.Timeout.Pull) * time.Second,
  48. Branch: newBranch,
  49. OldBranch: oldBranch,
  50. }); err != nil {
  51. return fmt.Errorf("git checkout -b %s %s: %v", newBranch, oldBranch, err)
  52. }
  53. return nil
  54. }
  55. func (repo *Repository) CheckoutNewBranch(oldBranch, newBranch string) error {
  56. return checkoutNewBranch(repo.RepoPath(), repo.LocalCopyPath(), oldBranch, newBranch)
  57. }
  58. type UpdateRepoFileOptions struct {
  59. LastCommitID string
  60. OldBranch string
  61. NewBranch string
  62. OldTreeName string
  63. NewTreeName string
  64. Message string
  65. Content string
  66. IsNewFile bool
  67. }
  68. // UpdateRepoFile adds or updates a file in repository.
  69. func (repo *Repository) UpdateRepoFile(doer *User, opts UpdateRepoFileOptions) (err error) {
  70. repoWorkingPool.CheckIn(com.ToStr(repo.ID))
  71. defer repoWorkingPool.CheckOut(com.ToStr(repo.ID))
  72. if err = repo.DiscardLocalRepoBranchChanges(opts.OldBranch); err != nil {
  73. return fmt.Errorf("DiscardLocalRepoBranchChanges [branch: %s]: %v", opts.OldBranch, err)
  74. } else if err = repo.UpdateLocalCopyBranch(opts.OldBranch); err != nil {
  75. return fmt.Errorf("UpdateLocalCopyBranch [branch: %s]: %v", opts.OldBranch, err)
  76. }
  77. if opts.OldBranch != opts.NewBranch {
  78. if err := repo.CheckoutNewBranch(opts.OldBranch, opts.NewBranch); err != nil {
  79. return fmt.Errorf("CheckoutNewBranch [old_branch: %s, new_branch: %s]: %v", opts.OldBranch, opts.NewBranch, err)
  80. }
  81. }
  82. if len(opts.Message) == 0 {
  83. if opts.IsNewFile {
  84. opts.Message = "Add '" + opts.NewTreeName + "'"
  85. } else {
  86. opts.Message = "Update '" + opts.NewTreeName + "'"
  87. }
  88. }
  89. localPath := repo.LocalCopyPath()
  90. filePath := path.Join(localPath, opts.NewTreeName)
  91. os.MkdirAll(path.Dir(filePath), os.ModePerm)
  92. // If it's meant to be a new file, make sure it doesn't exist.
  93. if opts.IsNewFile {
  94. if com.IsExist(filePath) {
  95. return ErrRepoFileAlreadyExist{filePath}
  96. }
  97. }
  98. // If update a file, move if file name change.
  99. if len(opts.OldTreeName) > 0 && len(opts.NewTreeName) > 0 && opts.OldTreeName != opts.NewTreeName {
  100. if err = git.MoveFile(localPath, opts.OldTreeName, opts.NewTreeName); err != nil {
  101. return fmt.Errorf("git mv %s %s: %v", opts.OldTreeName, opts.NewTreeName, err)
  102. }
  103. }
  104. if err = ioutil.WriteFile(filePath, []byte(opts.Content), 0666); err != nil {
  105. return fmt.Errorf("WriteFile: %v", err)
  106. }
  107. if err = git.AddChanges(localPath, true); err != nil {
  108. return fmt.Errorf("git add --all: %v", err)
  109. } else if err = git.CommitChanges(localPath, git.CommitChangesOptions{
  110. Committer: doer.NewGitSig(),
  111. Message: opts.Message,
  112. }); err != nil {
  113. return fmt.Errorf("CommitChanges: %v", err)
  114. } else if err = git.Push(localPath, "origin", opts.NewBranch); err != nil {
  115. return fmt.Errorf("git push origin %s: %v", opts.NewBranch, err)
  116. }
  117. gitRepo, err := git.OpenRepository(repo.RepoPath())
  118. if err != nil {
  119. log.Error(4, "OpenRepository: %v", err)
  120. return nil
  121. }
  122. commit, err := gitRepo.GetBranchCommit(opts.NewBranch)
  123. if err != nil {
  124. log.Error(4, "GetBranchCommit [branch: %s]: %v", opts.NewBranch, err)
  125. return nil
  126. }
  127. // Simulate push event.
  128. pushCommits := &PushCommits{
  129. Len: 1,
  130. Commits: []*PushCommit{CommitToPushCommit(commit)},
  131. }
  132. oldCommitID := opts.LastCommitID
  133. if opts.NewBranch != opts.OldBranch {
  134. oldCommitID = git.EMPTY_SHA
  135. }
  136. if err := CommitRepoAction(CommitRepoActionOptions{
  137. PusherName: doer.Name,
  138. RepoOwnerID: repo.MustOwner().ID,
  139. RepoName: repo.Name,
  140. RefFullName: git.BRANCH_PREFIX + opts.NewBranch,
  141. OldCommitID: oldCommitID,
  142. NewCommitID: commit.ID.String(),
  143. Commits: pushCommits,
  144. }); err != nil {
  145. log.Error(4, "CommitRepoAction: %v", err)
  146. return nil
  147. }
  148. return nil
  149. }
  150. // GetDiffPreview produces and returns diff result of a file which is not yet committed.
  151. func (repo *Repository) GetDiffPreview(branch, treePath, content string) (diff *Diff, err error) {
  152. repoWorkingPool.CheckIn(com.ToStr(repo.ID))
  153. defer repoWorkingPool.CheckOut(com.ToStr(repo.ID))
  154. if err = repo.DiscardLocalRepoBranchChanges(branch); err != nil {
  155. return nil, fmt.Errorf("DiscardLocalRepoBranchChanges [branch: %s]: %v", branch, err)
  156. } else if err = repo.UpdateLocalCopyBranch(branch); err != nil {
  157. return nil, fmt.Errorf("UpdateLocalCopyBranch [branch: %s]: %v", branch, err)
  158. }
  159. localPath := repo.LocalCopyPath()
  160. filePath := path.Join(localPath, treePath)
  161. os.MkdirAll(filepath.Dir(filePath), os.ModePerm)
  162. if err = ioutil.WriteFile(filePath, []byte(content), 0666); err != nil {
  163. return nil, fmt.Errorf("WriteFile: %v", err)
  164. }
  165. cmd := exec.Command("git", "diff", treePath)
  166. cmd.Dir = localPath
  167. cmd.Stderr = os.Stderr
  168. stdout, err := cmd.StdoutPipe()
  169. if err != nil {
  170. return nil, fmt.Errorf("StdoutPipe: %v", err)
  171. }
  172. if err = cmd.Start(); err != nil {
  173. return nil, fmt.Errorf("Start: %v", err)
  174. }
  175. pid := process.Add(fmt.Sprintf("GetDiffPreview [repo_path: %s]", repo.RepoPath()), cmd)
  176. defer process.Remove(pid)
  177. diff, err = ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, stdout)
  178. if err != nil {
  179. return nil, fmt.Errorf("ParsePatch: %v", err)
  180. }
  181. if err = cmd.Wait(); err != nil {
  182. return nil, fmt.Errorf("Wait: %v", err)
  183. }
  184. return diff, nil
  185. }
  186. // ________ .__ __ ___________.__.__
  187. // \______ \ ____ | | _____/ |_ ____ \_ _____/|__| | ____
  188. // | | \_/ __ \| | _/ __ \ __\/ __ \ | __) | | | _/ __ \
  189. // | ` \ ___/| |_\ ___/| | \ ___/ | \ | | |_\ ___/
  190. // /_______ /\___ >____/\___ >__| \___ > \___ / |__|____/\___ >
  191. // \/ \/ \/ \/ \/ \/
  192. //
  193. type DeleteRepoFileOptions struct {
  194. LastCommitID string
  195. Branch string
  196. TreePath string
  197. Message string
  198. }
  199. func (repo *Repository) DeleteRepoFile(doer *User, opts DeleteRepoFileOptions) (err error) {
  200. repoWorkingPool.CheckIn(com.ToStr(repo.ID))
  201. defer repoWorkingPool.CheckOut(com.ToStr(repo.ID))
  202. localPath := repo.LocalCopyPath()
  203. if err = discardLocalRepoBranchChanges(localPath, opts.Branch); err != nil {
  204. return fmt.Errorf("discardLocalRepoBranchChanges [branch: %s]: %v", opts.Branch, err)
  205. } else if err = repo.UpdateLocalCopyBranch(opts.Branch); err != nil {
  206. return fmt.Errorf("UpdateLocalCopyBranch [branch: %s]: %v", opts.Branch, err)
  207. }
  208. if err = os.Remove(path.Join(localPath, opts.TreePath)); err != nil {
  209. return fmt.Errorf("Remove: %v", err)
  210. }
  211. if len(opts.Message) == 0 {
  212. opts.Message = "Delete file '" + opts.TreePath + "'"
  213. }
  214. if err = git.AddChanges(localPath, true); err != nil {
  215. return fmt.Errorf("git add --all: %v", err)
  216. } else if err = git.CommitChanges(localPath, git.CommitChangesOptions{
  217. Committer: doer.NewGitSig(),
  218. Message: opts.Message,
  219. }); err != nil {
  220. return fmt.Errorf("CommitChanges: %v", err)
  221. } else if err = git.Push(localPath, "origin", opts.Branch); err != nil {
  222. return fmt.Errorf("git push origin %s: %v", opts.Branch, err)
  223. }
  224. gitRepo, err := git.OpenRepository(repo.RepoPath())
  225. if err != nil {
  226. log.Error(4, "OpenRepository: %v", err)
  227. return nil
  228. }
  229. commit, err := gitRepo.GetBranchCommit(opts.Branch)
  230. if err != nil {
  231. log.Error(4, "GetBranchCommit [branch: %s]: %v", opts.Branch, err)
  232. return nil
  233. }
  234. // Simulate push event.
  235. pushCommits := &PushCommits{
  236. Len: 1,
  237. Commits: []*PushCommit{CommitToPushCommit(commit)},
  238. }
  239. if err := CommitRepoAction(CommitRepoActionOptions{
  240. PusherName: doer.Name,
  241. RepoOwnerID: repo.MustOwner().ID,
  242. RepoName: repo.Name,
  243. RefFullName: git.BRANCH_PREFIX + opts.Branch,
  244. OldCommitID: opts.LastCommitID,
  245. NewCommitID: commit.ID.String(),
  246. Commits: pushCommits,
  247. }); err != nil {
  248. log.Error(4, "CommitRepoAction: %v", err)
  249. return nil
  250. }
  251. return nil
  252. }