repo_pull.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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", remoteBranch+"..."+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(basePath, baseBranch, headBranch string) ([]byte, error) {
  57. // Add a temporary remote.
  58. tmpRemote := com.ToStr(time.Now().UnixNano())
  59. _, stderr, err := com.ExecCmdDirBytes(repo.Path, "git", "remote", "add", "-f", tmpRemote, basePath)
  60. if err != nil {
  61. return nil, fmt.Errorf("add base as remote: %v", concatenateError(err, string(stderr)))
  62. }
  63. defer func() {
  64. com.ExecCmdDir(repo.Path, "git", "remote", "remove", tmpRemote)
  65. }()
  66. var stdout []byte
  67. remoteBranch := "remotes/" + tmpRemote + "/" + baseBranch
  68. stdout, stderr, err = com.ExecCmdDirBytes(repo.Path, "git", "diff", "-p", remoteBranch, headBranch)
  69. if err != nil {
  70. return nil, concatenateError(err, string(stderr))
  71. }
  72. return stdout, nil
  73. }
  74. // Merge merges pull request from head repository and branch.
  75. func (repo *Repository) Merge(headRepoPath string, baseBranch, headBranch string) error {
  76. return nil
  77. }