git_diff.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. }
  140. func (diffFile *DiffFile) GetType() int {
  141. return int(diffFile.Type)
  142. }
  143. func (diffFile *DiffFile) GetHighlightClass() string {
  144. return highlight.FileNameToHighlightClass(diffFile.Name)
  145. }
  146. type Diff struct {
  147. TotalAddition, TotalDeletion int
  148. Files []*DiffFile
  149. }
  150. func (diff *Diff) NumFiles() int {
  151. return len(diff.Files)
  152. }
  153. const DIFF_HEAD = "diff --git "
  154. func ParsePatch(maxlines int, reader io.Reader) (*Diff, error) {
  155. var (
  156. diff = &Diff{Files: make([]*DiffFile, 0)}
  157. curFile *DiffFile
  158. curSection = &DiffSection{
  159. Lines: make([]*DiffLine, 0, 10),
  160. }
  161. leftLine, rightLine int
  162. lineCount int
  163. )
  164. input := bufio.NewReader(reader)
  165. isEOF := false
  166. for {
  167. if isEOF {
  168. break
  169. }
  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, "--- ") {
  183. continue
  184. } else if len(line) == 0 {
  185. continue
  186. }
  187. lineCount++
  188. // Diff data too large, we only show the first about maxlines lines
  189. if lineCount >= maxlines {
  190. log.Warn("Diff data too large")
  191. io.Copy(ioutil.Discard, reader)
  192. diff.Files = nil
  193. return diff, nil
  194. }
  195. switch {
  196. case line[0] == ' ':
  197. diffLine := &DiffLine{Type: DIFF_LINE_PLAIN, Content: line, LeftIdx: leftLine, RightIdx: rightLine}
  198. leftLine++
  199. rightLine++
  200. curSection.Lines = append(curSection.Lines, diffLine)
  201. continue
  202. case line[0] == '@':
  203. curSection = &DiffSection{}
  204. curFile.Sections = append(curFile.Sections, curSection)
  205. ss := strings.Split(line, "@@")
  206. diffLine := &DiffLine{Type: DIFF_LINE_SECTION, Content: line}
  207. curSection.Lines = append(curSection.Lines, diffLine)
  208. // Parse line number.
  209. ranges := strings.Split(ss[1][1:], " ")
  210. leftLine, _ = com.StrTo(strings.Split(ranges[0], ",")[0][1:]).Int()
  211. if len(ranges) > 1 {
  212. rightLine, _ = com.StrTo(strings.Split(ranges[1], ",")[0]).Int()
  213. } else {
  214. log.Warn("Parse line number failed: %v", line)
  215. rightLine = leftLine
  216. }
  217. continue
  218. case line[0] == '+':
  219. curFile.Addition++
  220. diff.TotalAddition++
  221. diffLine := &DiffLine{Type: DIFF_LINE_ADD, Content: line, RightIdx: rightLine}
  222. rightLine++
  223. curSection.Lines = append(curSection.Lines, diffLine)
  224. continue
  225. case line[0] == '-':
  226. curFile.Deletion++
  227. diff.TotalDeletion++
  228. diffLine := &DiffLine{Type: DIFF_LINE_DEL, Content: line, LeftIdx: leftLine}
  229. if leftLine > 0 {
  230. leftLine++
  231. }
  232. curSection.Lines = append(curSection.Lines, diffLine)
  233. case strings.HasPrefix(line, "Binary"):
  234. curFile.IsBin = true
  235. continue
  236. }
  237. // Get new file.
  238. if strings.HasPrefix(line, DIFF_HEAD) {
  239. middle := -1
  240. // Note: In case file name is surrounded by double quotes (it happens only in git-shell).
  241. // e.g. diff --git "a/xxx" "b/xxx"
  242. hasQuote := line[len(DIFF_HEAD)] == '"'
  243. if hasQuote {
  244. middle = strings.Index(line, ` "b/`)
  245. } else {
  246. middle = strings.Index(line, " b/")
  247. }
  248. beg := len(DIFF_HEAD)
  249. a := line[beg+2 : middle]
  250. b := line[middle+3:]
  251. if hasQuote {
  252. a = string(git.UnescapeChars([]byte(a[1 : len(a)-1])))
  253. b = string(git.UnescapeChars([]byte(b[1 : len(b)-1])))
  254. }
  255. curFile = &DiffFile{
  256. Name: a,
  257. Index: len(diff.Files) + 1,
  258. Type: DIFF_FILE_CHANGE,
  259. Sections: make([]*DiffSection, 0, 10),
  260. }
  261. diff.Files = append(diff.Files, curFile)
  262. // Check file diff type.
  263. for {
  264. line, err := input.ReadString('\n')
  265. if err != nil {
  266. if err == io.EOF {
  267. isEOF = true
  268. } else {
  269. return nil, fmt.Errorf("ReadString: %v", err)
  270. }
  271. }
  272. switch {
  273. case strings.HasPrefix(line, "new file"):
  274. curFile.Type = DIFF_FILE_ADD
  275. curFile.IsCreated = true
  276. case strings.HasPrefix(line, "deleted"):
  277. curFile.Type = DIFF_FILE_DEL
  278. curFile.IsDeleted = true
  279. case strings.HasPrefix(line, "index"):
  280. curFile.Type = DIFF_FILE_CHANGE
  281. case strings.HasPrefix(line, "similarity index 100%"):
  282. curFile.Type = DIFF_FILE_RENAME
  283. curFile.IsRenamed = true
  284. curFile.OldName = curFile.Name
  285. curFile.Name = b
  286. }
  287. if curFile.Type > 0 {
  288. break
  289. }
  290. }
  291. }
  292. }
  293. // FIXME: detect encoding while parsing.
  294. var buf bytes.Buffer
  295. for _, f := range diff.Files {
  296. buf.Reset()
  297. for _, sec := range f.Sections {
  298. for _, l := range sec.Lines {
  299. buf.WriteString(l.Content)
  300. buf.WriteString("\n")
  301. }
  302. }
  303. charsetLabel, err := base.DetectEncoding(buf.Bytes())
  304. if charsetLabel != "UTF-8" && err == nil {
  305. encoding, _ := charset.Lookup(charsetLabel)
  306. if encoding != nil {
  307. d := encoding.NewDecoder()
  308. for _, sec := range f.Sections {
  309. for _, l := range sec.Lines {
  310. if c, _, err := transform.String(d, l.Content); err == nil {
  311. l.Content = c
  312. }
  313. }
  314. }
  315. }
  316. }
  317. }
  318. return diff, nil
  319. }
  320. func GetDiffRange(repoPath, beforeCommitID string, afterCommitID string, maxlines int) (*Diff, error) {
  321. repo, err := git.OpenRepository(repoPath)
  322. if err != nil {
  323. return nil, err
  324. }
  325. commit, err := repo.GetCommit(afterCommitID)
  326. if err != nil {
  327. return nil, err
  328. }
  329. var cmd *exec.Cmd
  330. // if "after" commit given
  331. if len(beforeCommitID) == 0 {
  332. // First commit of repository.
  333. if commit.ParentCount() == 0 {
  334. cmd = exec.Command("git", "show", afterCommitID)
  335. } else {
  336. c, _ := commit.Parent(0)
  337. cmd = exec.Command("git", "diff", "-M", c.ID.String(), afterCommitID)
  338. }
  339. } else {
  340. cmd = exec.Command("git", "diff", "-M", beforeCommitID, afterCommitID)
  341. }
  342. cmd.Dir = repoPath
  343. cmd.Stderr = os.Stderr
  344. stdout, err := cmd.StdoutPipe()
  345. if err != nil {
  346. return nil, fmt.Errorf("StdoutPipe: %v", err)
  347. }
  348. if err = cmd.Start(); err != nil {
  349. return nil, fmt.Errorf("Start: %v", err)
  350. }
  351. pid := process.Add(fmt.Sprintf("GetDiffRange (%s)", repoPath), cmd)
  352. defer process.Remove(pid)
  353. diff, err := ParsePatch(maxlines, stdout)
  354. if err != nil {
  355. return nil, fmt.Errorf("ParsePatch: %v", err)
  356. }
  357. if err = cmd.Wait(); err != nil {
  358. return nil, fmt.Errorf("Wait: %v", err)
  359. }
  360. return diff, nil
  361. }
  362. func GetDiffCommit(repoPath, commitId string, maxlines int) (*Diff, error) {
  363. return GetDiffRange(repoPath, "", commitId, maxlines)
  364. }