Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

git_diff.go 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. // Copyright 2014 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 models
  5. import (
  6. "bufio"
  7. "bytes"
  8. "fmt"
  9. "html"
  10. "html/template"
  11. "io"
  12. "io/ioutil"
  13. "os"
  14. "os/exec"
  15. "strings"
  16. "code.gitea.io/git"
  17. "code.gitea.io/gitea/modules/base"
  18. "code.gitea.io/gitea/modules/highlight"
  19. "code.gitea.io/gitea/modules/log"
  20. "code.gitea.io/gitea/modules/process"
  21. "code.gitea.io/gitea/modules/setting"
  22. "github.com/Unknwon/com"
  23. "github.com/sergi/go-diff/diffmatchpatch"
  24. "golang.org/x/net/html/charset"
  25. "golang.org/x/text/transform"
  26. )
  27. // DiffLineType represents the type of a DiffLine.
  28. type DiffLineType uint8
  29. // DiffLineType possible values.
  30. const (
  31. DiffLinePlain DiffLineType = iota + 1
  32. DiffLineAdd
  33. DiffLineDel
  34. DiffLineSection
  35. )
  36. // DiffFileType represents the type of a DiffFile.
  37. type DiffFileType uint8
  38. // DiffFileType possible values.
  39. const (
  40. DiffFileAdd DiffFileType = iota + 1
  41. DiffFileChange
  42. DiffFileDel
  43. DiffFileRename
  44. )
  45. // DiffLine represents a line difference in a DiffSection.
  46. type DiffLine struct {
  47. LeftIdx int
  48. RightIdx int
  49. Type DiffLineType
  50. Content string
  51. }
  52. // GetType returns the type of a DiffLine.
  53. func (d *DiffLine) GetType() int {
  54. return int(d.Type)
  55. }
  56. // DiffSection represents a section of a DiffFile.
  57. type DiffSection struct {
  58. Name string
  59. Lines []*DiffLine
  60. }
  61. var (
  62. addedCodePrefix = []byte("<span class=\"added-code\">")
  63. removedCodePrefix = []byte("<span class=\"removed-code\">")
  64. codeTagSuffix = []byte("</span>")
  65. )
  66. func diffToHTML(diffs []diffmatchpatch.Diff, lineType DiffLineType) template.HTML {
  67. buf := bytes.NewBuffer(nil)
  68. // Reproduce signs which are cut for inline diff before.
  69. switch lineType {
  70. case DiffLineAdd:
  71. buf.WriteByte('+')
  72. case DiffLineDel:
  73. buf.WriteByte('-')
  74. }
  75. for i := range diffs {
  76. switch {
  77. case diffs[i].Type == diffmatchpatch.DiffInsert && lineType == DiffLineAdd:
  78. buf.Write(addedCodePrefix)
  79. buf.WriteString(html.EscapeString(diffs[i].Text))
  80. buf.Write(codeTagSuffix)
  81. case diffs[i].Type == diffmatchpatch.DiffDelete && lineType == DiffLineDel:
  82. buf.Write(removedCodePrefix)
  83. buf.WriteString(html.EscapeString(diffs[i].Text))
  84. buf.Write(codeTagSuffix)
  85. case diffs[i].Type == diffmatchpatch.DiffEqual:
  86. buf.WriteString(html.EscapeString(diffs[i].Text))
  87. }
  88. }
  89. return template.HTML(buf.Bytes())
  90. }
  91. // GetLine gets a specific line by type (add or del) and file line number
  92. func (diffSection *DiffSection) GetLine(lineType DiffLineType, idx int) *DiffLine {
  93. var (
  94. difference = 0
  95. addCount = 0
  96. delCount = 0
  97. matchDiffLine *DiffLine
  98. )
  99. LOOP:
  100. for _, diffLine := range diffSection.Lines {
  101. switch diffLine.Type {
  102. case DiffLineAdd:
  103. addCount++
  104. case DiffLineDel:
  105. delCount++
  106. default:
  107. if matchDiffLine != nil {
  108. break LOOP
  109. }
  110. difference = diffLine.RightIdx - diffLine.LeftIdx
  111. addCount = 0
  112. delCount = 0
  113. }
  114. switch lineType {
  115. case DiffLineDel:
  116. if diffLine.RightIdx == 0 && diffLine.LeftIdx == idx-difference {
  117. matchDiffLine = diffLine
  118. }
  119. case DiffLineAdd:
  120. if diffLine.LeftIdx == 0 && diffLine.RightIdx == idx+difference {
  121. matchDiffLine = diffLine
  122. }
  123. }
  124. }
  125. if addCount == delCount {
  126. return matchDiffLine
  127. }
  128. return nil
  129. }
  130. var diffMatchPatch = diffmatchpatch.New()
  131. func init() {
  132. diffMatchPatch.DiffEditCost = 100
  133. }
  134. // GetComputedInlineDiffFor computes inline diff for the given line.
  135. func (diffSection *DiffSection) GetComputedInlineDiffFor(diffLine *DiffLine) template.HTML {
  136. if setting.Git.DisableDiffHighlight {
  137. return template.HTML(html.EscapeString(diffLine.Content[1:]))
  138. }
  139. var (
  140. compareDiffLine *DiffLine
  141. diff1 string
  142. diff2 string
  143. )
  144. // try to find equivalent diff line. ignore, otherwise
  145. switch diffLine.Type {
  146. case DiffLineAdd:
  147. compareDiffLine = diffSection.GetLine(DiffLineDel, diffLine.RightIdx)
  148. if compareDiffLine == nil {
  149. return template.HTML(html.EscapeString(diffLine.Content))
  150. }
  151. diff1 = compareDiffLine.Content
  152. diff2 = diffLine.Content
  153. case DiffLineDel:
  154. compareDiffLine = diffSection.GetLine(DiffLineAdd, diffLine.LeftIdx)
  155. if compareDiffLine == nil {
  156. return template.HTML(html.EscapeString(diffLine.Content))
  157. }
  158. diff1 = diffLine.Content
  159. diff2 = compareDiffLine.Content
  160. default:
  161. return template.HTML(html.EscapeString(diffLine.Content))
  162. }
  163. diffRecord := diffMatchPatch.DiffMain(diff1[1:], diff2[1:], true)
  164. diffRecord = diffMatchPatch.DiffCleanupEfficiency(diffRecord)
  165. return diffToHTML(diffRecord, diffLine.Type)
  166. }
  167. // DiffFile represents a file diff.
  168. type DiffFile struct {
  169. Name string
  170. OldName string
  171. Index int
  172. Addition, Deletion int
  173. Type DiffFileType
  174. IsCreated bool
  175. IsDeleted bool
  176. IsBin bool
  177. IsLFSFile bool
  178. IsRenamed bool
  179. IsSubmodule bool
  180. Sections []*DiffSection
  181. IsIncomplete bool
  182. }
  183. // GetType returns type of diff file.
  184. func (diffFile *DiffFile) GetType() int {
  185. return int(diffFile.Type)
  186. }
  187. // GetHighlightClass returns highlight class for a filename.
  188. func (diffFile *DiffFile) GetHighlightClass() string {
  189. return highlight.FileNameToHighlightClass(diffFile.Name)
  190. }
  191. // Diff represents a difference between two git trees.
  192. type Diff struct {
  193. TotalAddition, TotalDeletion int
  194. Files []*DiffFile
  195. IsIncomplete bool
  196. }
  197. // NumFiles returns number of files changes in a diff.
  198. func (diff *Diff) NumFiles() int {
  199. return len(diff.Files)
  200. }
  201. const cmdDiffHead = "diff --git "
  202. // ParsePatch builds a Diff object from a io.Reader and some
  203. // parameters.
  204. // TODO: move this function to gogits/git-module
  205. func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*Diff, error) {
  206. var (
  207. diff = &Diff{Files: make([]*DiffFile, 0)}
  208. curFile *DiffFile
  209. curSection = &DiffSection{
  210. Lines: make([]*DiffLine, 0, 10),
  211. }
  212. leftLine, rightLine int
  213. lineCount int
  214. curFileLinesCount int
  215. curFileLFSPrefix bool
  216. )
  217. input := bufio.NewReader(reader)
  218. isEOF := false
  219. for !isEOF {
  220. var linebuf bytes.Buffer
  221. for {
  222. b, err := input.ReadByte()
  223. if err != nil {
  224. if err == io.EOF {
  225. isEOF = true
  226. break
  227. } else {
  228. return nil, fmt.Errorf("ReadByte: %v", err)
  229. }
  230. }
  231. if b == '\n' {
  232. break
  233. }
  234. if linebuf.Len() < maxLineCharacters {
  235. linebuf.WriteByte(b)
  236. } else if linebuf.Len() == maxLineCharacters {
  237. curFile.IsIncomplete = true
  238. }
  239. }
  240. line := linebuf.String()
  241. if strings.HasPrefix(line, "+++ ") || strings.HasPrefix(line, "--- ") || len(line) == 0 {
  242. continue
  243. }
  244. trimLine := strings.Trim(line, "+- ")
  245. if trimLine == LFSMetaFileIdentifier {
  246. curFileLFSPrefix = true
  247. }
  248. if curFileLFSPrefix && strings.HasPrefix(trimLine, LFSMetaFileOidPrefix) {
  249. oid := strings.TrimPrefix(trimLine, LFSMetaFileOidPrefix)
  250. if len(oid) == 64 {
  251. m := &LFSMetaObject{Oid: oid}
  252. count, err := x.Count(m)
  253. if err == nil && count > 0 {
  254. curFile.IsBin = true
  255. curFile.IsLFSFile = true
  256. curSection.Lines = nil
  257. }
  258. }
  259. }
  260. curFileLinesCount++
  261. lineCount++
  262. // Diff data too large, we only show the first about maxLines lines
  263. if curFileLinesCount >= maxLines {
  264. curFile.IsIncomplete = true
  265. }
  266. switch {
  267. case line[0] == ' ':
  268. diffLine := &DiffLine{Type: DiffLinePlain, Content: line, LeftIdx: leftLine, RightIdx: rightLine}
  269. leftLine++
  270. rightLine++
  271. curSection.Lines = append(curSection.Lines, diffLine)
  272. continue
  273. case line[0] == '@':
  274. curSection = &DiffSection{}
  275. curFile.Sections = append(curFile.Sections, curSection)
  276. ss := strings.Split(line, "@@")
  277. diffLine := &DiffLine{Type: DiffLineSection, Content: line}
  278. curSection.Lines = append(curSection.Lines, diffLine)
  279. // Parse line number.
  280. ranges := strings.Split(ss[1][1:], " ")
  281. leftLine, _ = com.StrTo(strings.Split(ranges[0], ",")[0][1:]).Int()
  282. if len(ranges) > 1 {
  283. rightLine, _ = com.StrTo(strings.Split(ranges[1], ",")[0]).Int()
  284. } else {
  285. log.Warn("Parse line number failed: %v", line)
  286. rightLine = leftLine
  287. }
  288. continue
  289. case line[0] == '+':
  290. curFile.Addition++
  291. diff.TotalAddition++
  292. diffLine := &DiffLine{Type: DiffLineAdd, Content: line, RightIdx: rightLine}
  293. rightLine++
  294. curSection.Lines = append(curSection.Lines, diffLine)
  295. continue
  296. case line[0] == '-':
  297. curFile.Deletion++
  298. diff.TotalDeletion++
  299. diffLine := &DiffLine{Type: DiffLineDel, Content: line, LeftIdx: leftLine}
  300. if leftLine > 0 {
  301. leftLine++
  302. }
  303. curSection.Lines = append(curSection.Lines, diffLine)
  304. case strings.HasPrefix(line, "Binary"):
  305. curFile.IsBin = true
  306. continue
  307. }
  308. // Get new file.
  309. if strings.HasPrefix(line, cmdDiffHead) {
  310. middle := -1
  311. // Note: In case file name is surrounded by double quotes (it happens only in git-shell).
  312. // e.g. diff --git "a/xxx" "b/xxx"
  313. hasQuote := line[len(cmdDiffHead)] == '"'
  314. if hasQuote {
  315. middle = strings.Index(line, ` "b/`)
  316. } else {
  317. middle = strings.Index(line, " b/")
  318. }
  319. beg := len(cmdDiffHead)
  320. a := line[beg+2 : middle]
  321. b := line[middle+3:]
  322. if hasQuote {
  323. a = string(git.UnescapeChars([]byte(a[1 : len(a)-1])))
  324. b = string(git.UnescapeChars([]byte(b[1 : len(b)-1])))
  325. }
  326. curFile = &DiffFile{
  327. Name: b,
  328. OldName: a,
  329. Index: len(diff.Files) + 1,
  330. Type: DiffFileChange,
  331. Sections: make([]*DiffSection, 0, 10),
  332. IsRenamed: a != b,
  333. }
  334. diff.Files = append(diff.Files, curFile)
  335. if len(diff.Files) >= maxFiles {
  336. diff.IsIncomplete = true
  337. io.Copy(ioutil.Discard, reader)
  338. break
  339. }
  340. curFileLinesCount = 0
  341. curFileLFSPrefix = false
  342. // Check file diff type and is submodule.
  343. for {
  344. line, err := input.ReadString('\n')
  345. if err != nil {
  346. if err == io.EOF {
  347. isEOF = true
  348. } else {
  349. return nil, fmt.Errorf("ReadString: %v", err)
  350. }
  351. }
  352. switch {
  353. case strings.HasPrefix(line, "new file"):
  354. curFile.Type = DiffFileAdd
  355. curFile.IsCreated = true
  356. case strings.HasPrefix(line, "deleted"):
  357. curFile.Type = DiffFileDel
  358. curFile.IsDeleted = true
  359. case strings.HasPrefix(line, "index"):
  360. curFile.Type = DiffFileChange
  361. case strings.HasPrefix(line, "similarity index 100%"):
  362. curFile.Type = DiffFileRename
  363. }
  364. if curFile.Type > 0 {
  365. if strings.HasSuffix(line, " 160000\n") {
  366. curFile.IsSubmodule = true
  367. }
  368. break
  369. }
  370. }
  371. }
  372. }
  373. // FIXME: detect encoding while parsing.
  374. var buf bytes.Buffer
  375. for _, f := range diff.Files {
  376. buf.Reset()
  377. for _, sec := range f.Sections {
  378. for _, l := range sec.Lines {
  379. buf.WriteString(l.Content)
  380. buf.WriteString("\n")
  381. }
  382. }
  383. charsetLabel, err := base.DetectEncoding(buf.Bytes())
  384. if charsetLabel != "UTF-8" && err == nil {
  385. encoding, _ := charset.Lookup(charsetLabel)
  386. if encoding != nil {
  387. d := encoding.NewDecoder()
  388. for _, sec := range f.Sections {
  389. for _, l := range sec.Lines {
  390. if c, _, err := transform.String(d, l.Content); err == nil {
  391. l.Content = c
  392. }
  393. }
  394. }
  395. }
  396. }
  397. }
  398. return diff, nil
  399. }
  400. // GetDiffRange builds a Diff between two commits of a repository.
  401. // passing the empty string as beforeCommitID returns a diff from the
  402. // parent commit.
  403. func GetDiffRange(repoPath, beforeCommitID, afterCommitID string, maxLines, maxLineCharacters, maxFiles int) (*Diff, error) {
  404. gitRepo, err := git.OpenRepository(repoPath)
  405. if err != nil {
  406. return nil, err
  407. }
  408. commit, err := gitRepo.GetCommit(afterCommitID)
  409. if err != nil {
  410. return nil, err
  411. }
  412. var cmd *exec.Cmd
  413. // if "after" commit given
  414. if len(beforeCommitID) == 0 {
  415. // First commit of repository.
  416. if commit.ParentCount() == 0 {
  417. cmd = exec.Command("git", "show", afterCommitID)
  418. } else {
  419. c, _ := commit.Parent(0)
  420. cmd = exec.Command("git", "diff", "-M", c.ID.String(), afterCommitID)
  421. }
  422. } else {
  423. cmd = exec.Command("git", "diff", "-M", beforeCommitID, afterCommitID)
  424. }
  425. cmd.Dir = repoPath
  426. cmd.Stderr = os.Stderr
  427. stdout, err := cmd.StdoutPipe()
  428. if err != nil {
  429. return nil, fmt.Errorf("StdoutPipe: %v", err)
  430. }
  431. if err = cmd.Start(); err != nil {
  432. return nil, fmt.Errorf("Start: %v", err)
  433. }
  434. pid := process.GetManager().Add(fmt.Sprintf("GetDiffRange [repo_path: %s]", repoPath), cmd)
  435. defer process.GetManager().Remove(pid)
  436. diff, err := ParsePatch(maxLines, maxLineCharacters, maxFiles, stdout)
  437. if err != nil {
  438. return nil, fmt.Errorf("ParsePatch: %v", err)
  439. }
  440. if err = cmd.Wait(); err != nil {
  441. return nil, fmt.Errorf("Wait: %v", err)
  442. }
  443. return diff, nil
  444. }
  445. // RawDiffType type of a raw diff.
  446. type RawDiffType string
  447. // RawDiffType possible values.
  448. const (
  449. RawDiffNormal RawDiffType = "diff"
  450. RawDiffPatch RawDiffType = "patch"
  451. )
  452. // GetRawDiff dumps diff results of repository in given commit ID to io.Writer.
  453. // TODO: move this function to gogits/git-module
  454. func GetRawDiff(repoPath, commitID string, diffType RawDiffType, writer io.Writer) error {
  455. repo, err := git.OpenRepository(repoPath)
  456. if err != nil {
  457. return fmt.Errorf("OpenRepository: %v", err)
  458. }
  459. commit, err := repo.GetCommit(commitID)
  460. if err != nil {
  461. return fmt.Errorf("GetCommit: %v", err)
  462. }
  463. var cmd *exec.Cmd
  464. switch diffType {
  465. case RawDiffNormal:
  466. if commit.ParentCount() == 0 {
  467. cmd = exec.Command("git", "show", commitID)
  468. } else {
  469. c, _ := commit.Parent(0)
  470. cmd = exec.Command("git", "diff", "-M", c.ID.String(), commitID)
  471. }
  472. case RawDiffPatch:
  473. if commit.ParentCount() == 0 {
  474. cmd = exec.Command("git", "format-patch", "--no-signature", "--stdout", "--root", commitID)
  475. } else {
  476. c, _ := commit.Parent(0)
  477. query := fmt.Sprintf("%s...%s", commitID, c.ID.String())
  478. cmd = exec.Command("git", "format-patch", "--no-signature", "--stdout", query)
  479. }
  480. default:
  481. return fmt.Errorf("invalid diffType: %s", diffType)
  482. }
  483. stderr := new(bytes.Buffer)
  484. cmd.Dir = repoPath
  485. cmd.Stdout = writer
  486. cmd.Stderr = stderr
  487. if err = cmd.Run(); err != nil {
  488. return fmt.Errorf("Run: %v - %s", err, stderr)
  489. }
  490. return nil
  491. }
  492. // GetDiffCommit builds a Diff representing the given commitID.
  493. func GetDiffCommit(repoPath, commitID string, maxLines, maxLineCharacters, maxFiles int) (*Diff, error) {
  494. return GetDiffRange(repoPath, "", commitID, maxLines, maxLineCharacters, maxFiles)
  495. }