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.

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. }
  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(CommitRepoActionOptions{
  136. PusherName: doer.Name,
  137. RepoOwnerID: repo.MustOwner().ID,
  138. RepoName: repo.Name,
  139. RefFullName: git.BRANCH_PREFIX + opts.NewBranch,
  140. OldCommitID: oldCommitID,
  141. NewCommitID: commit.ID.String(),
  142. Commits: pushCommits,
  143. }); err != nil {
  144. log.Error(4, "CommitRepoAction: %v", err)
  145. return nil
  146. }
  147. return nil
  148. }
  149. // GetDiffPreview produces and returns diff result of a file which is not yet committed.
  150. func (repo *Repository) GetDiffPreview(branch, treeName, content string) (diff *Diff, err error) {
  151. repoWorkingPool.CheckIn(com.ToStr(repo.ID))
  152. defer repoWorkingPool.CheckOut(com.ToStr(repo.ID))
  153. if err = repo.DiscardLocalRepoBranchChanges(branch); err != nil {
  154. return nil, fmt.Errorf("DiscardLocalRepoBranchChanges [branch: %s]: %v", branch, err)
  155. } else if err = repo.UpdateLocalCopyBranch(branch); err != nil {
  156. return nil, fmt.Errorf("UpdateLocalCopyBranch [branch: %s]: %v", branch, err)
  157. }
  158. localPath := repo.LocalCopyPath()
  159. filePath := path.Join(localPath, treeName)
  160. os.MkdirAll(filepath.Dir(filePath), os.ModePerm)
  161. if err = ioutil.WriteFile(filePath, []byte(content), 0666); err != nil {
  162. return nil, fmt.Errorf("WriteFile: %v", err)
  163. }
  164. cmd := exec.Command("git", "diff", treeName)
  165. cmd.Dir = localPath
  166. cmd.Stderr = os.Stderr
  167. stdout, err := cmd.StdoutPipe()
  168. if err != nil {
  169. return nil, fmt.Errorf("StdoutPipe: %v", err)
  170. }
  171. if err = cmd.Start(); err != nil {
  172. return nil, fmt.Errorf("Start: %v", err)
  173. }
  174. pid := process.Add(fmt.Sprintf("GetDiffPreview [repo_path: %s]", repo.RepoPath()), cmd)
  175. defer process.Remove(pid)
  176. diff, err = ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, stdout)
  177. if err != nil {
  178. return nil, fmt.Errorf("ParsePatch: %v", err)
  179. }
  180. if err = cmd.Wait(); err != nil {
  181. return nil, fmt.Errorf("Wait: %v", err)
  182. }
  183. return diff, nil
  184. }
  185. // ________ .__ __ ___________.__.__
  186. // \______ \ ____ | | _____/ |_ ____ \_ _____/|__| | ____
  187. // | | \_/ __ \| | _/ __ \ __\/ __ \ | __) | | | _/ __ \
  188. // | ` \ ___/| |_\ ___/| | \ ___/ | \ | | |_\ ___/
  189. // /_______ /\___ >____/\___ >__| \___ > \___ / |__|____/\___ >
  190. // \/ \/ \/ \/ \/ \/
  191. //
  192. type DeleteRepoFileOptions struct {
  193. LastCommitID string
  194. Branch string
  195. TreePath string
  196. Message string
  197. }
  198. func (repo *Repository) DeleteRepoFile(doer *User, opts DeleteRepoFileOptions) (err error) {
  199. repoWorkingPool.CheckIn(com.ToStr(repo.ID))
  200. defer repoWorkingPool.CheckOut(com.ToStr(repo.ID))
  201. localPath := repo.LocalCopyPath()
  202. if err = discardLocalRepoBranchChanges(localPath, opts.Branch); err != nil {
  203. return fmt.Errorf("discardLocalRepoBranchChanges [branch: %s]: %v", opts.Branch, err)
  204. } else if err = repo.UpdateLocalCopyBranch(opts.Branch); err != nil {
  205. return fmt.Errorf("UpdateLocalCopyBranch [branch: %s]: %v", opts.Branch, err)
  206. }
  207. if err = os.Remove(path.Join(localPath, opts.TreePath)); err != nil {
  208. return fmt.Errorf("Remove: %v", err)
  209. }
  210. if len(opts.Message) == 0 {
  211. opts.Message = "Delete file '" + opts.TreePath + "'"
  212. }
  213. if err = git.AddChanges(localPath, true); err != nil {
  214. return fmt.Errorf("git add --all: %v", err)
  215. }
  216. signaure := doer.NewGitSig()
  217. if err = git.CommitChanges(localPath, opts.Message, signaure); err != nil {
  218. return fmt.Errorf("git commit -m %s --author='%s <%s>': %v", opts.Message, signaure.Name, signaure.Email, err)
  219. } else if err = git.Push(localPath, "origin", opts.Branch); err != nil {
  220. return fmt.Errorf("git push origin %s: %v", opts.Branch, 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.Branch)
  228. if err != nil {
  229. log.Error(4, "GetBranchCommit [branch: %s]: %v", opts.Branch, 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.Branch,
  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. }