repo_editor.go 9.3 KB

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