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.

git_diff.go 9.8KB

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