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.

diff.go 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // Copyright 2020 The Gitea 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. "bufio"
  7. "bytes"
  8. "context"
  9. "fmt"
  10. "io"
  11. "os/exec"
  12. "regexp"
  13. "strconv"
  14. "strings"
  15. "code.gitea.io/gitea/modules/process"
  16. )
  17. // RawDiffType type of a raw diff.
  18. type RawDiffType string
  19. // RawDiffType possible values.
  20. const (
  21. RawDiffNormal RawDiffType = "diff"
  22. RawDiffPatch RawDiffType = "patch"
  23. )
  24. // GetRawDiff dumps diff results of repository in given commit ID to io.Writer.
  25. func GetRawDiff(repoPath, commitID string, diffType RawDiffType, writer io.Writer) error {
  26. return GetRawDiffForFile(repoPath, "", commitID, diffType, "", writer)
  27. }
  28. // GetRawDiffForFile dumps diff results of file in given commit ID to io.Writer.
  29. func GetRawDiffForFile(repoPath, startCommit, endCommit string, diffType RawDiffType, file string, writer io.Writer) error {
  30. repo, err := OpenRepository(repoPath)
  31. if err != nil {
  32. return fmt.Errorf("OpenRepository: %v", err)
  33. }
  34. defer repo.Close()
  35. return GetRepoRawDiffForFile(repo, startCommit, endCommit, diffType, file, writer)
  36. }
  37. // GetRepoRawDiffForFile dumps diff results of file in given commit ID to io.Writer according given repository
  38. func GetRepoRawDiffForFile(repo *Repository, startCommit, endCommit string, diffType RawDiffType, file string, writer io.Writer) error {
  39. commit, err := repo.GetCommit(endCommit)
  40. if err != nil {
  41. return fmt.Errorf("GetCommit: %v", err)
  42. }
  43. fileArgs := make([]string, 0)
  44. if len(file) > 0 {
  45. fileArgs = append(fileArgs, "--", file)
  46. }
  47. // FIXME: graceful: These commands should have a timeout
  48. ctx, cancel := context.WithCancel(DefaultContext)
  49. defer cancel()
  50. var cmd *exec.Cmd
  51. switch diffType {
  52. case RawDiffNormal:
  53. if len(startCommit) != 0 {
  54. cmd = exec.CommandContext(ctx, GitExecutable, append([]string{"diff", "-M", startCommit, endCommit}, fileArgs...)...)
  55. } else if commit.ParentCount() == 0 {
  56. cmd = exec.CommandContext(ctx, GitExecutable, append([]string{"show", endCommit}, fileArgs...)...)
  57. } else {
  58. c, _ := commit.Parent(0)
  59. cmd = exec.CommandContext(ctx, GitExecutable, append([]string{"diff", "-M", c.ID.String(), endCommit}, fileArgs...)...)
  60. }
  61. case RawDiffPatch:
  62. if len(startCommit) != 0 {
  63. query := fmt.Sprintf("%s...%s", endCommit, startCommit)
  64. cmd = exec.CommandContext(ctx, GitExecutable, append([]string{"format-patch", "--no-signature", "--stdout", "--root", query}, fileArgs...)...)
  65. } else if commit.ParentCount() == 0 {
  66. cmd = exec.CommandContext(ctx, GitExecutable, append([]string{"format-patch", "--no-signature", "--stdout", "--root", endCommit}, fileArgs...)...)
  67. } else {
  68. c, _ := commit.Parent(0)
  69. query := fmt.Sprintf("%s...%s", endCommit, c.ID.String())
  70. cmd = exec.CommandContext(ctx, GitExecutable, append([]string{"format-patch", "--no-signature", "--stdout", query}, fileArgs...)...)
  71. }
  72. default:
  73. return fmt.Errorf("invalid diffType: %s", diffType)
  74. }
  75. stderr := new(bytes.Buffer)
  76. cmd.Dir = repo.Path
  77. cmd.Stdout = writer
  78. cmd.Stderr = stderr
  79. pid := process.GetManager().Add(fmt.Sprintf("GetRawDiffForFile: [repo_path: %s]", repo.Path), cancel)
  80. defer process.GetManager().Remove(pid)
  81. if err = cmd.Run(); err != nil {
  82. return fmt.Errorf("Run: %v - %s", err, stderr)
  83. }
  84. return nil
  85. }
  86. // ParseDiffHunkString parse the diffhunk content and return
  87. func ParseDiffHunkString(diffhunk string) (leftLine, leftHunk, rightLine, righHunk int) {
  88. ss := strings.Split(diffhunk, "@@")
  89. ranges := strings.Split(ss[1][1:], " ")
  90. leftRange := strings.Split(ranges[0], ",")
  91. leftLine, _ = strconv.Atoi(leftRange[0][1:])
  92. if len(leftRange) > 1 {
  93. leftHunk, _ = strconv.Atoi(leftRange[1])
  94. }
  95. if len(ranges) > 1 {
  96. rightRange := strings.Split(ranges[1], ",")
  97. rightLine, _ = strconv.Atoi(rightRange[0])
  98. if len(rightRange) > 1 {
  99. righHunk, _ = strconv.Atoi(rightRange[1])
  100. }
  101. } else {
  102. log("Parse line number failed: %v", diffhunk)
  103. rightLine = leftLine
  104. righHunk = leftHunk
  105. }
  106. return
  107. }
  108. // Example: @@ -1,8 +1,9 @@ => [..., 1, 8, 1, 9]
  109. var hunkRegex = regexp.MustCompile(`^@@ -(?P<beginOld>[0-9]+)(,(?P<endOld>[0-9]+))? \+(?P<beginNew>[0-9]+)(,(?P<endNew>[0-9]+))? @@`)
  110. const cmdDiffHead = "diff --git "
  111. func isHeader(lof string) bool {
  112. return strings.HasPrefix(lof, cmdDiffHead) || strings.HasPrefix(lof, "---") || strings.HasPrefix(lof, "+++")
  113. }
  114. // CutDiffAroundLine cuts a diff of a file in way that only the given line + numberOfLine above it will be shown
  115. // it also recalculates hunks and adds the appropriate headers to the new diff.
  116. // Warning: Only one-file diffs are allowed.
  117. func CutDiffAroundLine(originalDiff io.Reader, line int64, old bool, numbersOfLine int) string {
  118. if line == 0 || numbersOfLine == 0 {
  119. // no line or num of lines => no diff
  120. return ""
  121. }
  122. scanner := bufio.NewScanner(originalDiff)
  123. hunk := make([]string, 0)
  124. // begin is the start of the hunk containing searched line
  125. // end is the end of the hunk ...
  126. // currentLine is the line number on the side of the searched line (differentiated by old)
  127. // otherLine is the line number on the opposite side of the searched line (differentiated by old)
  128. var begin, end, currentLine, otherLine int64
  129. var headerLines int
  130. for scanner.Scan() {
  131. lof := scanner.Text()
  132. // Add header to enable parsing
  133. if isHeader(lof) {
  134. hunk = append(hunk, lof)
  135. headerLines++
  136. }
  137. if currentLine > line {
  138. break
  139. }
  140. // Detect "hunk" with contains commented lof
  141. if strings.HasPrefix(lof, "@@") {
  142. // Already got our hunk. End of hunk detected!
  143. if len(hunk) > headerLines {
  144. break
  145. }
  146. // A map with named groups of our regex to recognize them later more easily
  147. submatches := hunkRegex.FindStringSubmatch(lof)
  148. groups := make(map[string]string)
  149. for i, name := range hunkRegex.SubexpNames() {
  150. if i != 0 && name != "" {
  151. groups[name] = submatches[i]
  152. }
  153. }
  154. if old {
  155. begin, _ = strconv.ParseInt(groups["beginOld"], 10, 64)
  156. end, _ = strconv.ParseInt(groups["endOld"], 10, 64)
  157. // init otherLine with begin of opposite side
  158. otherLine, _ = strconv.ParseInt(groups["beginNew"], 10, 64)
  159. } else {
  160. begin, _ = strconv.ParseInt(groups["beginNew"], 10, 64)
  161. if groups["endNew"] != "" {
  162. end, _ = strconv.ParseInt(groups["endNew"], 10, 64)
  163. } else {
  164. end = 0
  165. }
  166. // init otherLine with begin of opposite side
  167. otherLine, _ = strconv.ParseInt(groups["beginOld"], 10, 64)
  168. }
  169. end += begin // end is for real only the number of lines in hunk
  170. // lof is between begin and end
  171. if begin <= line && end >= line {
  172. hunk = append(hunk, lof)
  173. currentLine = begin
  174. continue
  175. }
  176. } else if len(hunk) > headerLines {
  177. hunk = append(hunk, lof)
  178. // Count lines in context
  179. switch lof[0] {
  180. case '+':
  181. if !old {
  182. currentLine++
  183. } else {
  184. otherLine++
  185. }
  186. case '-':
  187. if old {
  188. currentLine++
  189. } else {
  190. otherLine++
  191. }
  192. default:
  193. currentLine++
  194. otherLine++
  195. }
  196. }
  197. }
  198. // No hunk found
  199. if currentLine == 0 {
  200. return ""
  201. }
  202. // headerLines + hunkLine (1) = totalNonCodeLines
  203. if len(hunk)-headerLines-1 <= numbersOfLine {
  204. // No need to cut the hunk => return existing hunk
  205. return strings.Join(hunk, "\n")
  206. }
  207. var oldBegin, oldNumOfLines, newBegin, newNumOfLines int64
  208. if old {
  209. oldBegin = currentLine
  210. newBegin = otherLine
  211. } else {
  212. oldBegin = otherLine
  213. newBegin = currentLine
  214. }
  215. // headers + hunk header
  216. newHunk := make([]string, headerLines)
  217. // transfer existing headers
  218. copy(newHunk, hunk[:headerLines])
  219. // transfer last n lines
  220. newHunk = append(newHunk, hunk[len(hunk)-numbersOfLine-1:]...)
  221. // calculate newBegin, ... by counting lines
  222. for i := len(hunk) - 1; i >= len(hunk)-numbersOfLine; i-- {
  223. switch hunk[i][0] {
  224. case '+':
  225. newBegin--
  226. newNumOfLines++
  227. case '-':
  228. oldBegin--
  229. oldNumOfLines++
  230. default:
  231. oldBegin--
  232. newBegin--
  233. newNumOfLines++
  234. oldNumOfLines++
  235. }
  236. }
  237. // construct the new hunk header
  238. newHunk[headerLines] = fmt.Sprintf("@@ -%d,%d +%d,%d @@",
  239. oldBegin, oldNumOfLines, newBegin, newNumOfLines)
  240. return strings.Join(newHunk, "\n")
  241. }