repo_editor.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. localPath := repo.LocalCopyPath()
  83. filePath := path.Join(localPath, opts.NewTreeName)
  84. os.MkdirAll(path.Dir(filePath), os.ModePerm)
  85. // If it's meant to be a new file, make sure it doesn't exist.
  86. if opts.IsNewFile {
  87. if com.IsExist(filePath) {
  88. return ErrRepoFileAlreadyExist{filePath}
  89. }
  90. }
  91. // If update a file, move if file name change.
  92. if len(opts.OldTreeName) > 0 && len(opts.NewTreeName) > 0 && opts.OldTreeName != opts.NewTreeName {
  93. if err = git.MoveFile(localPath, opts.OldTreeName, opts.NewTreeName); err != nil {
  94. return fmt.Errorf("git mv %s %s: %v", opts.OldTreeName, opts.NewTreeName, err)
  95. }
  96. }
  97. if err = ioutil.WriteFile(filePath, []byte(opts.Content), 0666); err != nil {
  98. return fmt.Errorf("WriteFile: %v", err)
  99. }
  100. if err = git.AddChanges(localPath, true); err != nil {
  101. return fmt.Errorf("git add --all: %v", err)
  102. } else if err = git.CommitChanges(localPath, git.CommitChangesOptions{
  103. Committer: doer.NewGitSig(),
  104. Message: opts.Message,
  105. }); err != nil {
  106. return fmt.Errorf("CommitChanges: %v", err)
  107. } else if err = git.Push(localPath, "origin", opts.NewBranch); err != nil {
  108. return fmt.Errorf("git push origin %s: %v", opts.NewBranch, err)
  109. }
  110. gitRepo, err := git.OpenRepository(repo.RepoPath())
  111. if err != nil {
  112. log.Error(4, "OpenRepository: %v", err)
  113. return nil
  114. }
  115. commit, err := gitRepo.GetBranchCommit(opts.NewBranch)
  116. if err != nil {
  117. log.Error(4, "GetBranchCommit [branch: %s]: %v", opts.NewBranch, err)
  118. return nil
  119. }
  120. // Simulate push event.
  121. pushCommits := &PushCommits{
  122. Len: 1,
  123. Commits: []*PushCommit{CommitToPushCommit(commit)},
  124. }
  125. oldCommitID := opts.LastCommitID
  126. if opts.NewBranch != opts.OldBranch {
  127. oldCommitID = git.EMPTY_SHA
  128. }
  129. if err := CommitRepoAction(CommitRepoActionOptions{
  130. PusherName: doer.Name,
  131. RepoOwnerID: repo.MustOwner().ID,
  132. RepoName: repo.Name,
  133. RefFullName: git.BRANCH_PREFIX + opts.NewBranch,
  134. OldCommitID: oldCommitID,
  135. NewCommitID: commit.ID.String(),
  136. Commits: pushCommits,
  137. }); err != nil {
  138. log.Error(4, "CommitRepoAction: %v", err)
  139. return nil
  140. }
  141. return nil
  142. }
  143. // GetDiffPreview produces and returns diff result of a file which is not yet committed.
  144. func (repo *Repository) GetDiffPreview(branch, treePath, content string) (diff *Diff, err error) {
  145. repoWorkingPool.CheckIn(com.ToStr(repo.ID))
  146. defer repoWorkingPool.CheckOut(com.ToStr(repo.ID))
  147. if err = repo.DiscardLocalRepoBranchChanges(branch); err != nil {
  148. return nil, fmt.Errorf("DiscardLocalRepoBranchChanges [branch: %s]: %v", branch, err)
  149. } else if err = repo.UpdateLocalCopyBranch(branch); err != nil {
  150. return nil, fmt.Errorf("UpdateLocalCopyBranch [branch: %s]: %v", branch, err)
  151. }
  152. localPath := repo.LocalCopyPath()
  153. filePath := path.Join(localPath, treePath)
  154. os.MkdirAll(filepath.Dir(filePath), os.ModePerm)
  155. if err = ioutil.WriteFile(filePath, []byte(content), 0666); err != nil {
  156. return nil, fmt.Errorf("WriteFile: %v", err)
  157. }
  158. cmd := exec.Command("git", "diff", treePath)
  159. cmd.Dir = localPath
  160. cmd.Stderr = os.Stderr
  161. stdout, err := cmd.StdoutPipe()
  162. if err != nil {
  163. return nil, fmt.Errorf("StdoutPipe: %v", err)
  164. }
  165. if err = cmd.Start(); err != nil {
  166. return nil, fmt.Errorf("Start: %v", err)
  167. }
  168. pid := process.Add(fmt.Sprintf("GetDiffPreview [repo_path: %s]", repo.RepoPath()), cmd)
  169. defer process.Remove(pid)
  170. diff, err = ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, stdout)
  171. if err != nil {
  172. return nil, fmt.Errorf("ParsePatch: %v", err)
  173. }
  174. if err = cmd.Wait(); err != nil {
  175. return nil, fmt.Errorf("Wait: %v", err)
  176. }
  177. return diff, nil
  178. }
  179. // ________ .__ __ ___________.__.__
  180. // \______ \ ____ | | _____/ |_ ____ \_ _____/|__| | ____
  181. // | | \_/ __ \| | _/ __ \ __\/ __ \ | __) | | | _/ __ \
  182. // | ` \ ___/| |_\ ___/| | \ ___/ | \ | | |_\ ___/
  183. // /_______ /\___ >____/\___ >__| \___ > \___ / |__|____/\___ >
  184. // \/ \/ \/ \/ \/ \/
  185. //
  186. type DeleteRepoFileOptions struct {
  187. LastCommitID string
  188. OldBranch string
  189. NewBranch string
  190. TreePath string
  191. Message string
  192. }
  193. func (repo *Repository) DeleteRepoFile(doer *User, opts DeleteRepoFileOptions) (err error) {
  194. repoWorkingPool.CheckIn(com.ToStr(repo.ID))
  195. defer repoWorkingPool.CheckOut(com.ToStr(repo.ID))
  196. if err = repo.DiscardLocalRepoBranchChanges(opts.OldBranch); err != nil {
  197. return fmt.Errorf("DiscardLocalRepoBranchChanges [branch: %s]: %v", opts.OldBranch, err)
  198. } else if err = repo.UpdateLocalCopyBranch(opts.OldBranch); err != nil {
  199. return fmt.Errorf("UpdateLocalCopyBranch [branch: %s]: %v", opts.OldBranch, err)
  200. }
  201. if opts.OldBranch != opts.NewBranch {
  202. if err := repo.CheckoutNewBranch(opts.OldBranch, opts.NewBranch); err != nil {
  203. return fmt.Errorf("CheckoutNewBranch [old_branch: %s, new_branch: %s]: %v", opts.OldBranch, opts.NewBranch, err)
  204. }
  205. }
  206. localPath := repo.LocalCopyPath()
  207. if err = os.Remove(path.Join(localPath, opts.TreePath)); err != nil {
  208. return fmt.Errorf("Remove: %v", err)
  209. }
  210. if err = git.AddChanges(localPath, true); err != nil {
  211. return fmt.Errorf("git add --all: %v", err)
  212. } else if err = git.CommitChanges(localPath, git.CommitChangesOptions{
  213. Committer: doer.NewGitSig(),
  214. Message: opts.Message,
  215. }); err != nil {
  216. return fmt.Errorf("CommitChanges: %v", err)
  217. } else if err = git.Push(localPath, "origin", opts.NewBranch); err != nil {
  218. return fmt.Errorf("git push origin %s: %v", opts.NewBranch, err)
  219. }
  220. gitRepo, err := git.OpenRepository(repo.RepoPath())
  221. if err != nil {
  222. log.Error(4, "OpenRepository: %v", err)
  223. return nil
  224. }
  225. commit, err := gitRepo.GetBranchCommit(opts.NewBranch)
  226. if err != nil {
  227. log.Error(4, "GetBranchCommit [branch: %s]: %v", opts.NewBranch, err)
  228. return nil
  229. }
  230. // Simulate push event.
  231. pushCommits := &PushCommits{
  232. Len: 1,
  233. Commits: []*PushCommit{CommitToPushCommit(commit)},
  234. }
  235. if err := CommitRepoAction(CommitRepoActionOptions{
  236. PusherName: doer.Name,
  237. RepoOwnerID: repo.MustOwner().ID,
  238. RepoName: repo.Name,
  239. RefFullName: git.BRANCH_PREFIX + opts.NewBranch,
  240. OldCommitID: opts.LastCommitID,
  241. NewCommitID: commit.ID.String(),
  242. Commits: pushCommits,
  243. }); err != nil {
  244. log.Error(4, "CommitRepoAction: %v", err)
  245. return nil
  246. }
  247. return nil
  248. }