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.

repo_editor.go 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 of given branch
  26. // to make sure it is even to remote branch when local copy exists.
  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. if err := git.ResetHEAD(localPath, true, "origin/"+branch); err != nil {
  36. return fmt.Errorf("ResetHEAD: %v", err)
  37. }
  38. return nil
  39. }
  40. func (repo *Repository) DiscardLocalRepoBranchChanges(branch string) error {
  41. return discardLocalRepoBranchChanges(repo.LocalCopyPath(), branch)
  42. }
  43. func checkoutNewBranch(repoPath, localPath, oldBranch, newBranch string) error {
  44. if !com.IsExist(localPath) {
  45. if err := UpdateLocalCopyBranch(repoPath, localPath, oldBranch); err != nil {
  46. return err
  47. }
  48. }
  49. if err := git.Checkout(localPath, git.CheckoutOptions{
  50. Branch: newBranch,
  51. OldBranch: oldBranch,
  52. Timeout: time.Duration(setting.Git.Timeout.Pull) * time.Second,
  53. }); err != nil {
  54. return fmt.Errorf("Checkout: %v", err)
  55. }
  56. return nil
  57. }
  58. // CheckoutNewBranch checks out a new branch from the given branch name.
  59. func (repo *Repository) CheckoutNewBranch(oldBranch, newBranch string) error {
  60. return checkoutNewBranch(repo.RepoPath(), repo.LocalCopyPath(), oldBranch, newBranch)
  61. }
  62. type UpdateRepoFileOptions struct {
  63. LastCommitID string
  64. OldBranch string
  65. NewBranch string
  66. OldTreeName string
  67. NewTreeName string
  68. Message string
  69. Content string
  70. IsNewFile bool
  71. }
  72. // updateRepoFile adds new file to repository.
  73. func (repo *Repository) UpdateRepoFile(doer *User, opts *UpdateRepoFileOptions) (err error) {
  74. repoWorkingPool.CheckIn(com.ToStr(repo.ID))
  75. defer repoWorkingPool.CheckOut(com.ToStr(repo.ID))
  76. if err = repo.DiscardLocalRepoBranchChanges(opts.OldBranch); err != nil {
  77. return fmt.Errorf("DiscardLocalRepoBranchChanges [branch: %s]: %v", opts.OldBranch, err)
  78. } else if err = repo.UpdateLocalCopyBranch(opts.OldBranch); err != nil {
  79. return fmt.Errorf("UpdateLocalCopyBranch [branch: %s]: %v", opts.OldBranch, err)
  80. }
  81. if opts.OldBranch != opts.NewBranch {
  82. if err := repo.CheckoutNewBranch(opts.OldBranch, opts.NewBranch); err != nil {
  83. return fmt.Errorf("CheckoutNewBranch [old_branch: %s, new_branch: %s]: %v", opts.OldBranch, opts.NewBranch, err)
  84. }
  85. }
  86. localPath := repo.LocalCopyPath()
  87. filePath := path.Join(localPath, opts.NewTreeName)
  88. if len(opts.Message) == 0 {
  89. if opts.IsNewFile {
  90. opts.Message = "Add '" + opts.NewTreeName + "'"
  91. } else {
  92. opts.Message = "Update '" + opts.NewTreeName + "'"
  93. }
  94. }
  95. os.MkdirAll(path.Dir(filePath), os.ModePerm)
  96. // If new file, make sure it doesn't exist; if old file, move if file name change.
  97. if opts.IsNewFile {
  98. if com.IsExist(filePath) {
  99. return ErrRepoFileAlreadyExist{filePath}
  100. }
  101. } else if len(opts.OldTreeName) > 0 && len(opts.NewTreeName) > 0 && opts.NewTreeName != opts.OldTreeName {
  102. if err = git.MoveFile(localPath, opts.OldTreeName, opts.NewTreeName); err != nil {
  103. return fmt.Errorf("MoveFile [old_tree_name: %s, new_tree_name: %s]: %v", opts.OldTreeName, opts.NewTreeName, err)
  104. }
  105. }
  106. if err = ioutil.WriteFile(filePath, []byte(opts.Content), 0666); err != nil {
  107. return fmt.Errorf("WriteFile: %v", err)
  108. }
  109. if err = git.AddChanges(localPath, true); err != nil {
  110. return fmt.Errorf("AddChanges: %v", err)
  111. } else if err = git.CommitChanges(localPath, opts.Message, doer.NewGitSig()); err != nil {
  112. return fmt.Errorf("CommitChanges: %v", err)
  113. } else if err = git.Push(localPath, "origin", opts.NewBranch); err != nil {
  114. return fmt.Errorf("Push: %v", 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. pushCommits := &PushCommits{
  127. Len: 1,
  128. Commits: []*PushCommit{CommitToPushCommit(commit)},
  129. }
  130. oldCommitID := opts.LastCommitID
  131. if opts.NewBranch != opts.OldBranch {
  132. oldCommitID = "0000000000000000000000000000000000000000" // New Branch so we use all 0s
  133. }
  134. if err := CommitRepoAction(doer.ID, repo.MustOwner().ID, doer.Name, doer.Email,
  135. repo.ID, repo.MustOwner().Name, repo.Name, git.BRANCH_PREFIX+opts.NewBranch,
  136. pushCommits, oldCommitID, commit.ID.String()); err != nil {
  137. log.Error(4, "CommitRepoAction: %v", err)
  138. return nil
  139. }
  140. go HookQueue.Add(repo.ID)
  141. return nil
  142. }
  143. func (repo *Repository) GetDiffPreview(branch, treeName, content string) (diff *Diff, err error) {
  144. repoWorkingPool.CheckIn(com.ToStr(repo.ID))
  145. defer repoWorkingPool.CheckOut(com.ToStr(repo.ID))
  146. if err = repo.DiscardLocalRepoBranchChanges(branch); err != nil {
  147. return nil, fmt.Errorf("discardLocalRepoChanges: %s - %v", branch, err)
  148. } else if err = repo.UpdateLocalCopyBranch(branch); err != nil {
  149. return nil, fmt.Errorf("UpdateLocalCopyBranch: %s - %v", branch, err)
  150. }
  151. localPath := repo.LocalCopyPath()
  152. filePath := path.Join(localPath, treeName)
  153. os.MkdirAll(filepath.Dir(filePath), os.ModePerm)
  154. if err = ioutil.WriteFile(filePath, []byte(content), 0666); err != nil {
  155. return nil, fmt.Errorf("WriteFile: %v", err)
  156. }
  157. cmd := exec.Command("git", "diff", treeName)
  158. cmd.Dir = localPath
  159. cmd.Stderr = os.Stderr
  160. stdout, err := cmd.StdoutPipe()
  161. if err != nil {
  162. return nil, fmt.Errorf("StdoutPipe: %v", err)
  163. }
  164. if err = cmd.Start(); err != nil {
  165. return nil, fmt.Errorf("Start: %v", err)
  166. }
  167. pid := process.Add(fmt.Sprintf("GetDiffRange [repo_path: %s]", repo.RepoPath()), cmd)
  168. defer process.Remove(pid)
  169. diff, err = ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, stdout)
  170. if err != nil {
  171. return nil, fmt.Errorf("ParsePatch: %v", err)
  172. }
  173. if err = cmd.Wait(); err != nil {
  174. return nil, fmt.Errorf("Wait: %v", err)
  175. }
  176. return diff, nil
  177. }