gitea源码

repo_compare.go 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package git
  5. import (
  6. "bufio"
  7. "bytes"
  8. "context"
  9. "errors"
  10. "fmt"
  11. "io"
  12. "os"
  13. "path/filepath"
  14. "regexp"
  15. "strconv"
  16. "strings"
  17. "code.gitea.io/gitea/modules/git/gitcmd"
  18. )
  19. // GetMergeBase checks and returns merge base of two branches and the reference used as base.
  20. func (repo *Repository) GetMergeBase(tmpRemote, base, head string) (string, string, error) {
  21. if tmpRemote == "" {
  22. tmpRemote = "origin"
  23. }
  24. if tmpRemote != "origin" {
  25. tmpBaseName := RemotePrefix + tmpRemote + "/tmp_" + base
  26. // Fetch commit into a temporary branch in order to be able to handle commits and tags
  27. _, _, err := gitcmd.NewCommand("fetch", "--no-tags").AddDynamicArguments(tmpRemote).AddDashesAndList(base+":"+tmpBaseName).RunStdString(repo.Ctx, &gitcmd.RunOpts{Dir: repo.Path})
  28. if err == nil {
  29. base = tmpBaseName
  30. }
  31. }
  32. stdout, _, err := gitcmd.NewCommand("merge-base").AddDashesAndList(base, head).RunStdString(repo.Ctx, &gitcmd.RunOpts{Dir: repo.Path})
  33. return strings.TrimSpace(stdout), base, err
  34. }
  35. type lineCountWriter struct {
  36. numLines int
  37. }
  38. // Write counts the number of newlines in the provided bytestream
  39. func (l *lineCountWriter) Write(p []byte) (n int, err error) {
  40. n = len(p)
  41. l.numLines += bytes.Count(p, []byte{'\000'})
  42. return n, err
  43. }
  44. // GetDiffNumChangedFiles counts the number of changed files
  45. // This is substantially quicker than shortstat but...
  46. func (repo *Repository) GetDiffNumChangedFiles(base, head string, directComparison bool) (int, error) {
  47. // Now there is git diff --shortstat but this appears to be slower than simply iterating with --nameonly
  48. w := &lineCountWriter{}
  49. stderr := new(bytes.Buffer)
  50. separator := "..."
  51. if directComparison {
  52. separator = ".."
  53. }
  54. // avoid: ambiguous argument 'refs/a...refs/b': unknown revision or path not in the working tree. Use '--': 'git <command> [<revision>...] -- [<file>...]'
  55. if err := gitcmd.NewCommand("diff", "-z", "--name-only").AddDynamicArguments(base+separator+head).AddArguments("--").
  56. Run(repo.Ctx, &gitcmd.RunOpts{
  57. Dir: repo.Path,
  58. Stdout: w,
  59. Stderr: stderr,
  60. }); err != nil {
  61. if strings.Contains(stderr.String(), "no merge base") {
  62. // git >= 2.28 now returns an error if base and head have become unrelated.
  63. // previously it would return the results of git diff -z --name-only base head so let's try that...
  64. w = &lineCountWriter{}
  65. stderr.Reset()
  66. if err = gitcmd.NewCommand("diff", "-z", "--name-only").AddDynamicArguments(base, head).AddArguments("--").Run(repo.Ctx, &gitcmd.RunOpts{
  67. Dir: repo.Path,
  68. Stdout: w,
  69. Stderr: stderr,
  70. }); err == nil {
  71. return w.numLines, nil
  72. }
  73. }
  74. return 0, fmt.Errorf("%w: Stderr: %s", err, stderr)
  75. }
  76. return w.numLines, nil
  77. }
  78. // GetDiffShortStatByCmdArgs counts number of changed files, number of additions and deletions
  79. // TODO: it can be merged with another "GetDiffShortStat" in the future
  80. func GetDiffShortStatByCmdArgs(ctx context.Context, repoPath string, trustedArgs gitcmd.TrustedCmdArgs, dynamicArgs ...string) (numFiles, totalAdditions, totalDeletions int, err error) {
  81. // Now if we call:
  82. // $ git diff --shortstat 1ebb35b98889ff77299f24d82da426b434b0cca0...788b8b1440462d477f45b0088875
  83. // we get:
  84. // " 9902 files changed, 2034198 insertions(+), 298800 deletions(-)\n"
  85. cmd := gitcmd.NewCommand("diff", "--shortstat").AddArguments(trustedArgs...).AddDynamicArguments(dynamicArgs...)
  86. stdout, _, err := cmd.RunStdString(ctx, &gitcmd.RunOpts{Dir: repoPath})
  87. if err != nil {
  88. return 0, 0, 0, err
  89. }
  90. return parseDiffStat(stdout)
  91. }
  92. var shortStatFormat = regexp.MustCompile(
  93. `\s*(\d+) files? changed(?:, (\d+) insertions?\(\+\))?(?:, (\d+) deletions?\(-\))?`)
  94. var patchCommits = regexp.MustCompile(`^From\s(\w+)\s`)
  95. func parseDiffStat(stdout string) (numFiles, totalAdditions, totalDeletions int, err error) {
  96. if len(stdout) == 0 || stdout == "\n" {
  97. return 0, 0, 0, nil
  98. }
  99. groups := shortStatFormat.FindStringSubmatch(stdout)
  100. if len(groups) != 4 {
  101. return 0, 0, 0, fmt.Errorf("unable to parse shortstat: %s groups: %s", stdout, groups)
  102. }
  103. numFiles, err = strconv.Atoi(groups[1])
  104. if err != nil {
  105. return 0, 0, 0, fmt.Errorf("unable to parse shortstat: %s. Error parsing NumFiles %w", stdout, err)
  106. }
  107. if len(groups[2]) != 0 {
  108. totalAdditions, err = strconv.Atoi(groups[2])
  109. if err != nil {
  110. return 0, 0, 0, fmt.Errorf("unable to parse shortstat: %s. Error parsing NumAdditions %w", stdout, err)
  111. }
  112. }
  113. if len(groups[3]) != 0 {
  114. totalDeletions, err = strconv.Atoi(groups[3])
  115. if err != nil {
  116. return 0, 0, 0, fmt.Errorf("unable to parse shortstat: %s. Error parsing NumDeletions %w", stdout, err)
  117. }
  118. }
  119. return numFiles, totalAdditions, totalDeletions, err
  120. }
  121. // GetDiff generates and returns patch data between given revisions, optimized for human readability
  122. func (repo *Repository) GetDiff(compareArg string, w io.Writer) error {
  123. stderr := new(bytes.Buffer)
  124. return gitcmd.NewCommand("diff", "-p").AddDynamicArguments(compareArg).
  125. Run(repo.Ctx, &gitcmd.RunOpts{
  126. Dir: repo.Path,
  127. Stdout: w,
  128. Stderr: stderr,
  129. })
  130. }
  131. // GetDiffBinary generates and returns patch data between given revisions, including binary diffs.
  132. func (repo *Repository) GetDiffBinary(compareArg string, w io.Writer) error {
  133. return gitcmd.NewCommand("diff", "-p", "--binary", "--histogram").AddDynamicArguments(compareArg).Run(repo.Ctx, &gitcmd.RunOpts{
  134. Dir: repo.Path,
  135. Stdout: w,
  136. })
  137. }
  138. // GetPatch generates and returns format-patch data between given revisions, able to be used with `git apply`
  139. func (repo *Repository) GetPatch(compareArg string, w io.Writer) error {
  140. stderr := new(bytes.Buffer)
  141. return gitcmd.NewCommand("format-patch", "--binary", "--stdout").AddDynamicArguments(compareArg).
  142. Run(repo.Ctx, &gitcmd.RunOpts{
  143. Dir: repo.Path,
  144. Stdout: w,
  145. Stderr: stderr,
  146. })
  147. }
  148. // GetFilesChangedBetween returns a list of all files that have been changed between the given commits
  149. // If base is undefined empty SHA (zeros), it only returns the files changed in the head commit
  150. // If base is the SHA of an empty tree (EmptyTreeSHA), it returns the files changes from the initial commit to the head commit
  151. func (repo *Repository) GetFilesChangedBetween(base, head string) ([]string, error) {
  152. objectFormat, err := repo.GetObjectFormat()
  153. if err != nil {
  154. return nil, err
  155. }
  156. cmd := gitcmd.NewCommand("diff-tree", "--name-only", "--root", "--no-commit-id", "-r", "-z")
  157. if base == objectFormat.EmptyObjectID().String() {
  158. cmd.AddDynamicArguments(head)
  159. } else {
  160. cmd.AddDynamicArguments(base, head)
  161. }
  162. stdout, _, err := cmd.RunStdString(repo.Ctx, &gitcmd.RunOpts{Dir: repo.Path})
  163. if err != nil {
  164. return nil, err
  165. }
  166. split := strings.Split(stdout, "\000")
  167. // Because Git will always emit filenames with a terminal NUL ignore the last entry in the split - which will always be empty.
  168. if len(split) > 0 {
  169. split = split[:len(split)-1]
  170. }
  171. return split, err
  172. }
  173. // ReadPatchCommit will check if a diff patch exists and return stats
  174. func (repo *Repository) ReadPatchCommit(prID int64) (commitSHA string, err error) {
  175. // Migrated repositories download patches to "pulls" location
  176. patchFile := fmt.Sprintf("pulls/%d.patch", prID)
  177. loadPatch, err := os.Open(filepath.Join(repo.Path, patchFile))
  178. if err != nil {
  179. return "", err
  180. }
  181. defer loadPatch.Close()
  182. // Read only the first line of the patch - usually it contains the first commit made in patch
  183. scanner := bufio.NewScanner(loadPatch)
  184. scanner.Scan()
  185. // Parse the Patch stats, sometimes Migration returns a 404 for the patch file
  186. commitSHAGroups := patchCommits.FindStringSubmatch(scanner.Text())
  187. if len(commitSHAGroups) != 0 {
  188. commitSHA = commitSHAGroups[1]
  189. } else {
  190. return "", errors.New("patch file doesn't contain valid commit ID")
  191. }
  192. return commitSHA, nil
  193. }