git_diff.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. "github.com/Unknwon/com"
  17. "github.com/sergi/go-diff/diffmatchpatch"
  18. "golang.org/x/net/html/charset"
  19. "golang.org/x/text/transform"
  20. "github.com/gogits/git-module"
  21. "github.com/gogits/gogs/modules/base"
  22. "github.com/gogits/gogs/modules/log"
  23. "github.com/gogits/gogs/modules/process"
  24. "github.com/gogits/gogs/modules/template/highlight"
  25. )
  26. type DiffLineType uint8
  27. const (
  28. DIFF_LINE_PLAIN DiffLineType = iota + 1
  29. DIFF_LINE_ADD
  30. DIFF_LINE_DEL
  31. DIFF_LINE_SECTION
  32. )
  33. type DiffFileType uint8
  34. const (
  35. DIFF_FILE_ADD DiffFileType = iota + 1
  36. DIFF_FILE_CHANGE
  37. DIFF_FILE_DEL
  38. DIFF_FILE_RENAME
  39. )
  40. type DiffLine struct {
  41. LeftIdx int
  42. RightIdx int
  43. Type DiffLineType
  44. Content string
  45. }
  46. func (d *DiffLine) GetType() int {
  47. return int(d.Type)
  48. }
  49. type DiffSection struct {
  50. Name string
  51. Lines []*DiffLine
  52. }
  53. var (
  54. addedCodePrefix = []byte("<span class=\"added-code\">")
  55. removedCodePrefix = []byte("<span class=\"removed-code\">")
  56. codeTagSuffix = []byte("</span>")
  57. )
  58. func diffToHTML(diffs []diffmatchpatch.Diff, lineType DiffLineType) template.HTML {
  59. var buf bytes.Buffer
  60. for i := range diffs {
  61. if diffs[i].Type == diffmatchpatch.DiffInsert && lineType == DIFF_LINE_ADD {
  62. buf.Write(addedCodePrefix)
  63. buf.WriteString(html.EscapeString(diffs[i].Text))
  64. buf.Write(codeTagSuffix)
  65. } else if diffs[i].Type == diffmatchpatch.DiffDelete && lineType == DIFF_LINE_DEL {
  66. buf.Write(removedCodePrefix)
  67. buf.WriteString(html.EscapeString(diffs[i].Text))
  68. buf.Write(codeTagSuffix)
  69. } else if diffs[i].Type == diffmatchpatch.DiffEqual {
  70. buf.WriteString(html.EscapeString(diffs[i].Text))
  71. }
  72. }
  73. return template.HTML(buf.Bytes())
  74. }
  75. // get an specific line by type (add or del) and file line number
  76. func (diffSection *DiffSection) GetLine(lineType DiffLineType, idx int) *DiffLine {
  77. difference := 0
  78. for _, diffLine := range diffSection.Lines {
  79. if diffLine.Type == DIFF_LINE_PLAIN {
  80. // get the difference of line numbers between ADD and DEL versions
  81. difference = diffLine.RightIdx - diffLine.LeftIdx
  82. continue
  83. }
  84. if lineType == DIFF_LINE_DEL {
  85. if diffLine.RightIdx == 0 && diffLine.LeftIdx == idx-difference {
  86. return diffLine
  87. }
  88. } else if lineType == DIFF_LINE_ADD {
  89. if diffLine.LeftIdx == 0 && diffLine.RightIdx == idx+difference {
  90. return diffLine
  91. }
  92. }
  93. }
  94. return nil
  95. }
  96. // computes inline diff for the given line
  97. func (diffSection *DiffSection) GetComputedInlineDiffFor(diffLine *DiffLine) template.HTML {
  98. var compareDiffLine *DiffLine
  99. var diff1, diff2 string
  100. getDefaultReturn := func() template.HTML {
  101. return template.HTML(html.EscapeString(diffLine.Content[1:]))
  102. }
  103. // just compute diff for adds and removes
  104. if diffLine.Type != DIFF_LINE_ADD && diffLine.Type != DIFF_LINE_DEL {
  105. return getDefaultReturn()
  106. }
  107. // try to find equivalent diff line. ignore, otherwise
  108. if diffLine.Type == DIFF_LINE_ADD {
  109. compareDiffLine = diffSection.GetLine(DIFF_LINE_DEL, diffLine.RightIdx)
  110. if compareDiffLine == nil {
  111. return getDefaultReturn()
  112. }
  113. diff1 = compareDiffLine.Content
  114. diff2 = diffLine.Content
  115. } else {
  116. compareDiffLine = diffSection.GetLine(DIFF_LINE_ADD, diffLine.LeftIdx)
  117. if compareDiffLine == nil {
  118. return getDefaultReturn()
  119. }
  120. diff1 = diffLine.Content
  121. diff2 = compareDiffLine.Content
  122. }
  123. dmp := diffmatchpatch.New()
  124. diffRecord := dmp.DiffMain(diff1[1:], diff2[1:], true)
  125. diffRecord = dmp.DiffCleanupSemantic(diffRecord)
  126. return diffToHTML(diffRecord, diffLine.Type)
  127. }
  128. type DiffFile struct {
  129. Name string
  130. OldName string
  131. Index int
  132. Addition, Deletion int
  133. Type DiffFileType
  134. IsCreated bool
  135. IsDeleted bool
  136. IsBin bool
  137. IsRenamed bool
  138. Sections []*DiffSection
  139. IsIncomplete bool
  140. }
  141. func (diffFile *DiffFile) GetType() int {
  142. return int(diffFile.Type)
  143. }
  144. func (diffFile *DiffFile) GetHighlightClass() string {
  145. return highlight.FileNameToHighlightClass(diffFile.Name)
  146. }
  147. type Diff struct {
  148. TotalAddition, TotalDeletion int
  149. Files []*DiffFile
  150. IsIncomplete bool
  151. }
  152. func (diff *Diff) NumFiles() int {
  153. return len(diff.Files)
  154. }
  155. const DIFF_HEAD = "diff --git "
  156. func ParsePatch(maxLines, maxLineCharacteres, maxFiles int, reader io.Reader) (*Diff, error) {
  157. var (
  158. diff = &Diff{Files: make([]*DiffFile, 0)}
  159. curFile *DiffFile
  160. curSection = &DiffSection{
  161. Lines: make([]*DiffLine, 0, 10),
  162. }
  163. leftLine, rightLine int
  164. lineCount int
  165. curFileLinesCount int
  166. )
  167. input := bufio.NewReader(reader)
  168. isEOF := false
  169. for !isEOF {
  170. line, err := input.ReadString('\n')
  171. if err != nil {
  172. if err == io.EOF {
  173. isEOF = true
  174. } else {
  175. return nil, fmt.Errorf("ReadString: %v", err)
  176. }
  177. }
  178. if len(line) > 0 && line[len(line)-1] == '\n' {
  179. // Remove line break.
  180. line = line[:len(line)-1]
  181. }
  182. if strings.HasPrefix(line, "+++ ") || strings.HasPrefix(line, "--- ") || len(line) == 0 {
  183. continue
  184. }
  185. curFileLinesCount++
  186. lineCount++
  187. // Diff data too large, we only show the first about maxlines lines
  188. if curFileLinesCount >= maxLines || len(line) >= maxLineCharacteres {
  189. curFile.IsIncomplete = true
  190. }
  191. switch {
  192. case line[0] == ' ':
  193. diffLine := &DiffLine{Type: DIFF_LINE_PLAIN, Content: line, LeftIdx: leftLine, RightIdx: rightLine}
  194. leftLine++
  195. rightLine++
  196. curSection.Lines = append(curSection.Lines, diffLine)
  197. continue
  198. case line[0] == '@':
  199. curSection = &DiffSection{}
  200. curFile.Sections = append(curFile.Sections, curSection)
  201. ss := strings.Split(line, "@@")
  202. diffLine := &DiffLine{Type: DIFF_LINE_SECTION, Content: line}
  203. curSection.Lines = append(curSection.Lines, diffLine)
  204. // Parse line number.
  205. ranges := strings.Split(ss[1][1:], " ")
  206. leftLine, _ = com.StrTo(strings.Split(ranges[0], ",")[0][1:]).Int()
  207. if len(ranges) > 1 {
  208. rightLine, _ = com.StrTo(strings.Split(ranges[1], ",")[0]).Int()
  209. } else {
  210. log.Warn("Parse line number failed: %v", line)
  211. rightLine = leftLine
  212. }
  213. continue
  214. case line[0] == '+':
  215. curFile.Addition++
  216. diff.TotalAddition++
  217. diffLine := &DiffLine{Type: DIFF_LINE_ADD, Content: line, RightIdx: rightLine}
  218. rightLine++
  219. curSection.Lines = append(curSection.Lines, diffLine)
  220. continue
  221. case line[0] == '-':
  222. curFile.Deletion++
  223. diff.TotalDeletion++
  224. diffLine := &DiffLine{Type: DIFF_LINE_DEL, Content: line, LeftIdx: leftLine}
  225. if leftLine > 0 {
  226. leftLine++
  227. }
  228. curSection.Lines = append(curSection.Lines, diffLine)
  229. case strings.HasPrefix(line, "Binary"):
  230. curFile.IsBin = true
  231. continue
  232. }
  233. // Get new file.
  234. if strings.HasPrefix(line, DIFF_HEAD) {
  235. middle := -1
  236. // Note: In case file name is surrounded by double quotes (it happens only in git-shell).
  237. // e.g. diff --git "a/xxx" "b/xxx"
  238. hasQuote := line[len(DIFF_HEAD)] == '"'
  239. if hasQuote {
  240. middle = strings.Index(line, ` "b/`)
  241. } else {
  242. middle = strings.Index(line, " b/")
  243. }
  244. beg := len(DIFF_HEAD)
  245. a := line[beg+2 : middle]
  246. b := line[middle+3:]
  247. if hasQuote {
  248. a = string(git.UnescapeChars([]byte(a[1 : len(a)-1])))
  249. b = string(git.UnescapeChars([]byte(b[1 : len(b)-1])))
  250. }
  251. curFile = &DiffFile{
  252. Name: a,
  253. Index: len(diff.Files) + 1,
  254. Type: DIFF_FILE_CHANGE,
  255. Sections: make([]*DiffSection, 0, 10),
  256. }
  257. diff.Files = append(diff.Files, curFile)
  258. if len(diff.Files) >= maxFiles {
  259. diff.IsIncomplete = true
  260. io.Copy(ioutil.Discard, reader)
  261. break
  262. }
  263. curFileLinesCount = 0
  264. // Check file diff type.
  265. for {
  266. line, err := input.ReadString('\n')
  267. if err != nil {
  268. if err == io.EOF {
  269. isEOF = true
  270. } else {
  271. return nil, fmt.Errorf("ReadString: %v", err)
  272. }
  273. }
  274. switch {
  275. case strings.HasPrefix(line, "new file"):
  276. curFile.Type = DIFF_FILE_ADD
  277. curFile.IsCreated = true
  278. case strings.HasPrefix(line, "deleted"):
  279. curFile.Type = DIFF_FILE_DEL
  280. curFile.IsDeleted = true
  281. case strings.HasPrefix(line, "index"):
  282. curFile.Type = DIFF_FILE_CHANGE
  283. case strings.HasPrefix(line, "similarity index 100%"):
  284. curFile.Type = DIFF_FILE_RENAME
  285. curFile.IsRenamed = true
  286. curFile.OldName = curFile.Name
  287. curFile.Name = b
  288. }
  289. if curFile.Type > 0 {
  290. break
  291. }
  292. }
  293. }
  294. }
  295. // FIXME: detect encoding while parsing.
  296. var buf bytes.Buffer
  297. for _, f := range diff.Files {
  298. buf.Reset()
  299. for _, sec := range f.Sections {
  300. for _, l := range sec.Lines {
  301. buf.WriteString(l.Content)
  302. buf.WriteString("\n")
  303. }
  304. }
  305. charsetLabel, err := base.DetectEncoding(buf.Bytes())
  306. if charsetLabel != "UTF-8" && err == nil {
  307. encoding, _ := charset.Lookup(charsetLabel)
  308. if encoding != nil {
  309. d := encoding.NewDecoder()
  310. for _, sec := range f.Sections {
  311. for _, l := range sec.Lines {
  312. if c, _, err := transform.String(d, l.Content); err == nil {
  313. l.Content = c
  314. }
  315. }
  316. }
  317. }
  318. }
  319. }
  320. return diff, nil
  321. }
  322. func GetDiffRange(repoPath, beforeCommitID string, afterCommitID string, maxLines, maxLineCharacteres, maxFiles int) (*Diff, error) {
  323. repo, err := git.OpenRepository(repoPath)
  324. if err != nil {
  325. return nil, err
  326. }
  327. commit, err := repo.GetCommit(afterCommitID)
  328. if err != nil {
  329. return nil, err
  330. }
  331. var cmd *exec.Cmd
  332. // if "after" commit given
  333. if len(beforeCommitID) == 0 {
  334. // First commit of repository.
  335. if commit.ParentCount() == 0 {
  336. cmd = exec.Command("git", "show", afterCommitID)
  337. } else {
  338. c, _ := commit.Parent(0)
  339. cmd = exec.Command("git", "diff", "-M", c.ID.String(), afterCommitID)
  340. }
  341. } else {
  342. cmd = exec.Command("git", "diff", "-M", beforeCommitID, afterCommitID)
  343. }
  344. cmd.Dir = repoPath
  345. cmd.Stderr = os.Stderr
  346. stdout, err := cmd.StdoutPipe()
  347. if err != nil {
  348. return nil, fmt.Errorf("StdoutPipe: %v", err)
  349. }
  350. if err = cmd.Start(); err != nil {
  351. return nil, fmt.Errorf("Start: %v", err)
  352. }
  353. pid := process.Add(fmt.Sprintf("GetDiffRange (%s)", repoPath), cmd)
  354. defer process.Remove(pid)
  355. diff, err := ParsePatch(maxLines, maxLineCharacteres, maxFiles, stdout)
  356. if err != nil {
  357. return nil, fmt.Errorf("ParsePatch: %v", err)
  358. }
  359. if err = cmd.Wait(); err != nil {
  360. return nil, fmt.Errorf("Wait: %v", err)
  361. }
  362. return diff, nil
  363. }
  364. func GetDiffCommit(repoPath, commitId string, maxLines, maxLineCharacteres, maxFiles int) (*Diff, error) {
  365. return GetDiffRange(repoPath, "", commitId, maxLines, maxLineCharacteres, maxFiles)
  366. }