repo_editor.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. }
  110. signaure := doer.NewGitSig()
  111. if err = git.CommitChanges(localPath, opts.Message, signaure); err != nil {
  112. return fmt.Errorf("git commit -m %s --author='%s <%s>': %v", opts.Message, signaure.Name, signaure.Email, err)
  113. } else if err = git.Push(localPath, "origin", opts.NewBranch); err != nil {
  114. return fmt.Errorf("git push origin %s: %v", opts.NewBranch, err)
  115. }
  116. gitRepo, err := git.OpenRepository(repo.RepoPath())
  117. if err != nil {
  118. log.Error(4, "OpenRepository: %v", err)
  119. return nil
  120. }
  121. commit, err := gitRepo.GetBranchCommit(opts.NewBranch)
  122. if err != nil {
  123. log.Error(4, "GetBranchCommit [branch: %s]: %v", opts.NewBranch, err)
  124. return nil
  125. }
  126. // Simulate push event.
  127. pushCommits := &PushCommits{
  128. Len: 1,
  129. Commits: []*PushCommit{CommitToPushCommit(commit)},
  130. }
  131. oldCommitID := opts.LastCommitID
  132. if opts.NewBranch != opts.OldBranch {
  133. oldCommitID = git.EMPTY_SHA
  134. }
  135. if err := CommitRepoAction(doer.ID, repo.MustOwner().ID, doer.Name, doer.Email,
  136. repo.ID, repo.MustOwner().Name, repo.Name, git.BRANCH_PREFIX+opts.NewBranch,
  137. pushCommits, oldCommitID, commit.ID.String()); err != nil {
  138. log.Error(4, "CommitRepoAction: %v", err)
  139. return nil
  140. }
  141. go HookQueue.Add(repo.ID)
  142. return nil
  143. }
  144. // GetDiffPreview produces and returns diff result of a file which is not yet committed.
  145. func (repo *Repository) GetDiffPreview(branch, treeName, content string) (diff *Diff, err error) {
  146. repoWorkingPool.CheckIn(com.ToStr(repo.ID))
  147. defer repoWorkingPool.CheckOut(com.ToStr(repo.ID))
  148. if err = repo.DiscardLocalRepoBranchChanges(branch); err != nil {
  149. return nil, fmt.Errorf("DiscardLocalRepoBranchChanges [branch: %s]: %v", branch, err)
  150. } else if err = repo.UpdateLocalCopyBranch(branch); err != nil {
  151. return nil, fmt.Errorf("UpdateLocalCopyBranch [branch: %s]: %v", branch, err)
  152. }
  153. localPath := repo.LocalCopyPath()
  154. filePath := path.Join(localPath, treeName)
  155. os.MkdirAll(filepath.Dir(filePath), os.ModePerm)
  156. if err = ioutil.WriteFile(filePath, []byte(content), 0666); err != nil {
  157. return nil, fmt.Errorf("WriteFile: %v", err)
  158. }
  159. cmd := exec.Command("git", "diff", treeName)
  160. cmd.Dir = localPath
  161. cmd.Stderr = os.Stderr
  162. stdout, err := cmd.StdoutPipe()
  163. if err != nil {
  164. return nil, fmt.Errorf("StdoutPipe: %v", err)
  165. }
  166. if err = cmd.Start(); err != nil {
  167. return nil, fmt.Errorf("Start: %v", err)
  168. }
  169. pid := process.Add(fmt.Sprintf("GetDiffPreview [repo_path: %s]", repo.RepoPath()), cmd)
  170. defer process.Remove(pid)
  171. diff, err = ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, stdout)
  172. if err != nil {
  173. return nil, fmt.Errorf("ParsePatch: %v", err)
  174. }
  175. if err = cmd.Wait(); err != nil {
  176. return nil, fmt.Errorf("Wait: %v", err)
  177. }
  178. return diff, nil
  179. }
  180. // ________ .__ __ ___________.__.__
  181. // \______ \ ____ | | _____/ |_ ____ \_ _____/|__| | ____
  182. // | | \_/ __ \| | _/ __ \ __\/ __ \ | __) | | | _/ __ \
  183. // | ` \ ___/| |_\ ___/| | \ ___/ | \ | | |_\ ___/
  184. // /_______ /\___ >____/\___ >__| \___ > \___ / |__|____/\___ >
  185. // \/ \/ \/ \/ \/ \/
  186. //
  187. func (repo *Repository) DeleteRepoFile(doer *User, oldCommitID, branch, treeName, message string) (err error) {
  188. repoWorkingPool.CheckIn(com.ToStr(repo.ID))
  189. defer repoWorkingPool.CheckOut(com.ToStr(repo.ID))
  190. localPath := repo.LocalCopyPath()
  191. if err = discardLocalRepoBranchChanges(localPath, branch); err != nil {
  192. return fmt.Errorf("discardLocalRepoChanges: %v", err)
  193. } else if err = repo.UpdateLocalCopyBranch(branch); err != nil {
  194. return fmt.Errorf("UpdateLocalCopyBranch: %v", err)
  195. }
  196. filePath := path.Join(localPath, treeName)
  197. os.Remove(filePath)
  198. if len(message) == 0 {
  199. message = "Delete file '" + treeName + "'"
  200. }
  201. if err = git.AddChanges(localPath, true); err != nil {
  202. return fmt.Errorf("AddChanges: %v", err)
  203. } else if err = git.CommitChanges(localPath, message, doer.NewGitSig()); err != nil {
  204. return fmt.Errorf("CommitChanges: %v", err)
  205. } else if err = git.Push(localPath, "origin", branch); err != nil {
  206. return fmt.Errorf("Push: %v", err)
  207. }
  208. gitRepo, err := git.OpenRepository(repo.RepoPath())
  209. if err != nil {
  210. log.Error(4, "OpenRepository: %v", err)
  211. return nil
  212. }
  213. commit, err := gitRepo.GetBranchCommit(branch)
  214. if err != nil {
  215. log.Error(4, "GetBranchCommit [branch: %s]: %v", branch, err)
  216. return nil
  217. }
  218. pushCommits := &PushCommits{
  219. Len: 1,
  220. Commits: []*PushCommit{CommitToPushCommit(commit)},
  221. }
  222. if err := CommitRepoAction(doer.ID, repo.MustOwner().ID, doer.Name, doer.Email,
  223. repo.ID, repo.MustOwner().Name, repo.Name, git.BRANCH_PREFIX+branch,
  224. pushCommits, oldCommitID, commit.ID.String()); err != nil {
  225. log.Error(4, "CommitRepoAction: %v", err)
  226. return nil
  227. }
  228. go HookQueue.Add(repo.ID)
  229. return nil
  230. }