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 9.6KB

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