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.

80 lines
2.5 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. "container/list"
  7. "fmt"
  8. "strings"
  9. "time"
  10. "github.com/Unknwon/com"
  11. )
  12. type PullRequestInfo struct {
  13. MergeBase string
  14. Commits *list.List
  15. // Diff *Diff
  16. NumFiles int
  17. }
  18. // GetPullRequestInfo generates and returns pull request information
  19. // between base and head branches of repositories.
  20. func (repo *Repository) GetPullRequestInfo(basePath, baseBranch, headBranch string) (*PullRequestInfo, error) {
  21. // Add a temporary remote.
  22. tmpRemote := com.ToStr(time.Now().UnixNano())
  23. _, stderr, err := com.ExecCmdDir(repo.Path, "git", "remote", "add", "-f", tmpRemote, basePath)
  24. if err != nil {
  25. return nil, fmt.Errorf("add base as remote: %v", concatenateError(err, stderr))
  26. }
  27. defer func() {
  28. com.ExecCmdDir(repo.Path, "git", "remote", "remove", tmpRemote)
  29. }()
  30. prInfo := new(PullRequestInfo)
  31. var stdout string
  32. remoteBranch := "remotes/" + tmpRemote + "/" + baseBranch
  33. // Get merge base commit.
  34. stdout, stderr, err = com.ExecCmdDir(repo.Path, "git", "merge-base", remoteBranch, headBranch)
  35. if err != nil {
  36. return nil, fmt.Errorf("get merge base: %v", concatenateError(err, stderr))
  37. }
  38. prInfo.MergeBase = strings.TrimSpace(stdout)
  39. stdout, stderr, err = com.ExecCmdDir(repo.Path, "git", "log", prInfo.MergeBase+"..."+headBranch, prettyLogFormat)
  40. if err != nil {
  41. return nil, fmt.Errorf("list diff logs: %v", concatenateError(err, stderr))
  42. }
  43. prInfo.Commits, err = parsePrettyFormatLog(repo, []byte(stdout))
  44. if err != nil {
  45. return nil, fmt.Errorf("parsePrettyFormatLog: %v", err)
  46. }
  47. // Count number of changed files.
  48. stdout, stderr, err = com.ExecCmdDir(repo.Path, "git", "diff", "--name-only", remoteBranch+"..."+headBranch)
  49. if err != nil {
  50. return nil, fmt.Errorf("list changed files: %v", concatenateError(err, stderr))
  51. }
  52. prInfo.NumFiles = len(strings.Split(stdout, "\n")) - 1
  53. return prInfo, nil
  54. }
  55. // GetPatch generates and returns patch data between given branches.
  56. func (repo *Repository) GetPatch(mergeBase, headBranch string) ([]byte, error) {
  57. stdout, stderr, err := com.ExecCmdDirBytes(repo.Path, "git", "diff", "-p", mergeBase, headBranch)
  58. if err != nil {
  59. return nil, concatenateError(err, string(stderr))
  60. }
  61. return stdout, nil
  62. }
  63. // Merge merges pull request from head repository and branch.
  64. func (repo *Repository) Merge(headRepoPath string, baseBranch, headBranch string) error {
  65. return nil
  66. }