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_pull.go 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2015 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 git
  5. import (
  6. "bytes"
  7. "container/list"
  8. "fmt"
  9. "io"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. // PullRequestInfo represents needed information for a pull request.
  15. type PullRequestInfo struct {
  16. MergeBase string
  17. Commits *list.List
  18. NumFiles int
  19. }
  20. // GetMergeBase checks and returns merge base of two branches.
  21. func (repo *Repository) GetMergeBase(base, head string) (string, error) {
  22. stdout, err := NewCommand("merge-base", base, head).RunInDir(repo.Path)
  23. return strings.TrimSpace(stdout), err
  24. }
  25. // GetPullRequestInfo generates and returns pull request information
  26. // between base and head branches of repositories.
  27. func (repo *Repository) GetPullRequestInfo(basePath, baseBranch, headBranch string) (_ *PullRequestInfo, err error) {
  28. var remoteBranch string
  29. // We don't need a temporary remote for same repository.
  30. if repo.Path != basePath {
  31. // Add a temporary remote
  32. tmpRemote := strconv.FormatInt(time.Now().UnixNano(), 10)
  33. if err = repo.AddRemote(tmpRemote, basePath, true); err != nil {
  34. return nil, fmt.Errorf("AddRemote: %v", err)
  35. }
  36. defer repo.RemoveRemote(tmpRemote)
  37. remoteBranch = "remotes/" + tmpRemote + "/" + baseBranch
  38. } else {
  39. remoteBranch = baseBranch
  40. }
  41. prInfo := new(PullRequestInfo)
  42. prInfo.MergeBase, err = repo.GetMergeBase(remoteBranch, headBranch)
  43. if err == nil {
  44. // We have a common base
  45. logs, err := NewCommand("log", prInfo.MergeBase+"..."+headBranch, prettyLogFormat).RunInDirBytes(repo.Path)
  46. if err != nil {
  47. return nil, err
  48. }
  49. prInfo.Commits, err = repo.parsePrettyFormatLogToList(logs)
  50. if err != nil {
  51. return nil, fmt.Errorf("parsePrettyFormatLogToList: %v", err)
  52. }
  53. } else {
  54. prInfo.Commits = list.New()
  55. prInfo.MergeBase, err = GetFullCommitID(repo.Path, remoteBranch)
  56. if err != nil {
  57. prInfo.MergeBase = remoteBranch
  58. }
  59. }
  60. // Count number of changed files.
  61. stdout, err := NewCommand("diff", "--name-only", remoteBranch+"..."+headBranch).RunInDir(repo.Path)
  62. if err != nil {
  63. return nil, err
  64. }
  65. prInfo.NumFiles = len(strings.Split(stdout, "\n")) - 1
  66. return prInfo, nil
  67. }
  68. // GetPatch generates and returns patch data between given revisions.
  69. func (repo *Repository) GetPatch(base, head string) ([]byte, error) {
  70. return NewCommand("diff", "-p", "--binary", base, head).RunInDirBytes(repo.Path)
  71. }
  72. // GetFormatPatch generates and returns format-patch data between given revisions.
  73. func (repo *Repository) GetFormatPatch(base, head string) (io.Reader, error) {
  74. stdout := new(bytes.Buffer)
  75. stderr := new(bytes.Buffer)
  76. if err := NewCommand("format-patch", "--binary", "--stdout", base+"..."+head).
  77. RunInDirPipeline(repo.Path, stdout, stderr); err != nil {
  78. return nil, concatenateError(err, stderr.String())
  79. }
  80. return stdout, nil
  81. }