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.

89 lines
2.7 KiB

  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. return nil, fmt.Errorf("GetMergeBase: %v", err)
  45. }
  46. logs, err := NewCommand("log", prInfo.MergeBase+"..."+headBranch, prettyLogFormat).RunInDirBytes(repo.Path)
  47. if err != nil {
  48. return nil, err
  49. }
  50. prInfo.Commits, err = repo.parsePrettyFormatLogToList(logs)
  51. if err != nil {
  52. return nil, fmt.Errorf("parsePrettyFormatLogToList: %v", err)
  53. }
  54. // Count number of changed files.
  55. stdout, err := NewCommand("diff", "--name-only", remoteBranch+"..."+headBranch).RunInDir(repo.Path)
  56. if err != nil {
  57. return nil, err
  58. }
  59. prInfo.NumFiles = len(strings.Split(stdout, "\n")) - 1
  60. return prInfo, nil
  61. }
  62. // GetPatch generates and returns patch data between given revisions.
  63. func (repo *Repository) GetPatch(base, head string) ([]byte, error) {
  64. return NewCommand("diff", "-p", "--binary", base, head).RunInDirBytes(repo.Path)
  65. }
  66. // GetFormatPatch generates and returns format-patch data between given revisions.
  67. func (repo *Repository) GetFormatPatch(base, head string) (io.Reader, error) {
  68. stdout := new(bytes.Buffer)
  69. stderr := new(bytes.Buffer)
  70. if err := NewCommand("format-patch", "--binary", "--stdout", base+"..."+head).
  71. RunInDirPipeline(repo.Path, stdout, stderr); err != nil {
  72. return nil, concatenateError(err, stderr.String())
  73. }
  74. return stdout, nil
  75. }