git_diff.go 6.6 KB

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