git_diff.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. "os"
  11. "os/exec"
  12. "strings"
  13. "time"
  14. "golang.org/x/net/html/charset"
  15. "golang.org/x/text/transform"
  16. "github.com/Unknwon/com"
  17. "github.com/gogits/gogs/modules/base"
  18. "github.com/gogits/gogs/modules/git"
  19. "github.com/gogits/gogs/modules/log"
  20. "github.com/gogits/gogs/modules/process"
  21. )
  22. // Diff line types.
  23. const (
  24. DIFF_LINE_PLAIN = iota + 1
  25. DIFF_LINE_ADD
  26. DIFF_LINE_DEL
  27. DIFF_LINE_SECTION
  28. )
  29. const (
  30. DIFF_FILE_ADD = iota + 1
  31. DIFF_FILE_CHANGE
  32. DIFF_FILE_DEL
  33. DIFF_FILE_RENAME
  34. )
  35. type DiffLine struct {
  36. LeftIdx int
  37. RightIdx int
  38. Type int
  39. Content string
  40. }
  41. func (d DiffLine) GetType() int {
  42. return d.Type
  43. }
  44. type DiffSection struct {
  45. Name string
  46. Lines []*DiffLine
  47. }
  48. type DiffFile struct {
  49. Name string
  50. OldName string
  51. Index int
  52. Addition, Deletion int
  53. Type int
  54. IsCreated bool
  55. IsDeleted bool
  56. IsBin bool
  57. IsRenamed bool
  58. Sections []*DiffSection
  59. }
  60. type Diff struct {
  61. TotalAddition, TotalDeletion int
  62. Files []*DiffFile
  63. }
  64. func (diff *Diff) NumFiles() int {
  65. return len(diff.Files)
  66. }
  67. const DIFF_HEAD = "diff --git "
  68. func ParsePatch(pid int64, maxlines int, cmd *exec.Cmd, reader io.Reader) (*Diff, error) {
  69. scanner := bufio.NewScanner(reader)
  70. var (
  71. curFile *DiffFile
  72. curSection = &DiffSection{
  73. Lines: make([]*DiffLine, 0, 10),
  74. }
  75. leftLine, rightLine int
  76. // FIXME: Should use cache in the future.
  77. buf bytes.Buffer
  78. )
  79. diff := &Diff{Files: make([]*DiffFile, 0)}
  80. var i int
  81. for scanner.Scan() {
  82. line := scanner.Text()
  83. if strings.HasPrefix(line, "+++ ") || strings.HasPrefix(line, "--- ") {
  84. continue
  85. }
  86. if line == "" {
  87. continue
  88. }
  89. i = i + 1
  90. // Diff data too large, we only show the first about maxlines lines
  91. if i >= maxlines {
  92. log.Warn("Diff data too large")
  93. diff.Files = nil
  94. return diff, nil
  95. }
  96. switch {
  97. case line[0] == ' ':
  98. diffLine := &DiffLine{Type: DIFF_LINE_PLAIN, Content: line, LeftIdx: leftLine, RightIdx: rightLine}
  99. leftLine++
  100. rightLine++
  101. curSection.Lines = append(curSection.Lines, diffLine)
  102. continue
  103. case line[0] == '@':
  104. curSection = &DiffSection{}
  105. curFile.Sections = append(curFile.Sections, curSection)
  106. ss := strings.Split(line, "@@")
  107. diffLine := &DiffLine{Type: DIFF_LINE_SECTION, Content: line}
  108. curSection.Lines = append(curSection.Lines, diffLine)
  109. // Parse line number.
  110. ranges := strings.Split(ss[1][1:], " ")
  111. leftLine, _ = com.StrTo(strings.Split(ranges[0], ",")[0][1:]).Int()
  112. if len(ranges) > 1 {
  113. rightLine, _ = com.StrTo(strings.Split(ranges[1], ",")[0]).Int()
  114. } else {
  115. log.Warn("Parse line number failed: %v", line)
  116. rightLine = leftLine
  117. }
  118. continue
  119. case line[0] == '+':
  120. curFile.Addition++
  121. diff.TotalAddition++
  122. diffLine := &DiffLine{Type: DIFF_LINE_ADD, Content: line, RightIdx: rightLine}
  123. rightLine++
  124. curSection.Lines = append(curSection.Lines, diffLine)
  125. continue
  126. case line[0] == '-':
  127. curFile.Deletion++
  128. diff.TotalDeletion++
  129. diffLine := &DiffLine{Type: DIFF_LINE_DEL, Content: line, LeftIdx: leftLine}
  130. if leftLine > 0 {
  131. leftLine++
  132. }
  133. curSection.Lines = append(curSection.Lines, diffLine)
  134. case strings.HasPrefix(line, "Binary"):
  135. curFile.IsBin = true
  136. continue
  137. }
  138. // Get new file.
  139. if strings.HasPrefix(line, DIFF_HEAD) {
  140. middle := -1
  141. // Note: In case file name is surrounded by double quotes(it happens only in git-shell).
  142. hasQuote := strings.Index(line, `\"`) > -1
  143. if hasQuote {
  144. line = strings.Replace(line, `\"`, `"`, -1)
  145. middle = strings.Index(line, ` "b/`)
  146. } else {
  147. middle = strings.Index(line, " b/")
  148. }
  149. beg := len(DIFF_HEAD)
  150. a := line[beg+2 : middle]
  151. b := line[middle+3:]
  152. if hasQuote {
  153. a = a[1 : len(a)-1]
  154. b = b[1 : len(b)-1]
  155. }
  156. curFile = &DiffFile{
  157. Name: a,
  158. Index: len(diff.Files) + 1,
  159. Type: DIFF_FILE_CHANGE,
  160. Sections: make([]*DiffSection, 0, 10),
  161. }
  162. diff.Files = append(diff.Files, curFile)
  163. // Check file diff type.
  164. for scanner.Scan() {
  165. switch {
  166. case strings.HasPrefix(scanner.Text(), "new file"):
  167. curFile.Type = DIFF_FILE_ADD
  168. curFile.IsCreated = true
  169. case strings.HasPrefix(scanner.Text(), "deleted"):
  170. curFile.Type = DIFF_FILE_DEL
  171. curFile.IsDeleted = true
  172. case strings.HasPrefix(scanner.Text(), "index"):
  173. curFile.Type = DIFF_FILE_CHANGE
  174. case strings.HasPrefix(scanner.Text(), "similarity index 100%"):
  175. curFile.Type = DIFF_FILE_RENAME
  176. curFile.IsRenamed = true
  177. curFile.OldName = curFile.Name
  178. curFile.Name = b
  179. }
  180. if curFile.Type > 0 {
  181. break
  182. }
  183. }
  184. }
  185. }
  186. for _, f := range diff.Files {
  187. buf.Reset()
  188. for _, sec := range f.Sections {
  189. for _, l := range sec.Lines {
  190. buf.WriteString(l.Content)
  191. buf.WriteString("\n")
  192. }
  193. }
  194. charsetLabel, err := base.DetectEncoding(buf.Bytes())
  195. if charsetLabel != "UTF-8" && err == nil {
  196. encoding, _ := charset.Lookup(charsetLabel)
  197. if encoding != nil {
  198. d := encoding.NewDecoder()
  199. for _, sec := range f.Sections {
  200. for _, l := range sec.Lines {
  201. if c, _, err := transform.String(d, l.Content); err == nil {
  202. l.Content = c
  203. }
  204. }
  205. }
  206. }
  207. }
  208. }
  209. return diff, nil
  210. }
  211. func GetDiffRange(repoPath, beforeCommitId string, afterCommitId string, maxlines int) (*Diff, error) {
  212. repo, err := git.OpenRepository(repoPath)
  213. if err != nil {
  214. return nil, err
  215. }
  216. commit, err := repo.GetCommit(afterCommitId)
  217. if err != nil {
  218. return nil, err
  219. }
  220. rd, wr := io.Pipe()
  221. var cmd *exec.Cmd
  222. // if "after" commit given
  223. if beforeCommitId == "" {
  224. // First commit of repository.
  225. if commit.ParentCount() == 0 {
  226. cmd = exec.Command("git", "show", afterCommitId)
  227. } else {
  228. c, _ := commit.Parent(0)
  229. cmd = exec.Command("git", "diff", "-M", c.ID.String(), afterCommitId)
  230. }
  231. } else {
  232. cmd = exec.Command("git", "diff", "-M", beforeCommitId, afterCommitId)
  233. }
  234. cmd.Dir = repoPath
  235. cmd.Stdout = wr
  236. cmd.Stdin = os.Stdin
  237. cmd.Stderr = os.Stderr
  238. done := make(chan error)
  239. go func() {
  240. cmd.Start()
  241. done <- cmd.Wait()
  242. wr.Close()
  243. }()
  244. defer rd.Close()
  245. desc := fmt.Sprintf("GetDiffRange(%s)", repoPath)
  246. pid := process.Add(desc, cmd)
  247. go func() {
  248. // In case process became zombie.
  249. select {
  250. case <-time.After(5 * time.Minute):
  251. if errKill := process.Kill(pid); errKill != nil {
  252. log.Error(4, "git_diff.ParsePatch(Kill): %v", err)
  253. }
  254. <-done
  255. // return "", ErrExecTimeout.Error(), ErrExecTimeout
  256. case err = <-done:
  257. process.Remove(pid)
  258. }
  259. }()
  260. return ParsePatch(pid, maxlines, cmd, rd)
  261. }
  262. func GetDiffCommit(repoPath, commitId string, maxlines int) (*Diff, error) {
  263. return GetDiffRange(repoPath, "", commitId, maxlines)
  264. }