git_diff.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. // fmt.Println(i, line)
  84. if strings.HasPrefix(line, "+++ ") || strings.HasPrefix(line, "--- ") {
  85. continue
  86. }
  87. if line == "" {
  88. continue
  89. }
  90. i = i + 1
  91. // Diff data too large, we only show the first about maxlines lines
  92. if i >= maxlines {
  93. log.Warn("Diff data too large")
  94. diff.Files = nil
  95. return diff, nil
  96. }
  97. switch {
  98. case line[0] == ' ':
  99. diffLine := &DiffLine{Type: DIFF_LINE_PLAIN, Content: line, LeftIdx: leftLine, RightIdx: rightLine}
  100. leftLine++
  101. rightLine++
  102. curSection.Lines = append(curSection.Lines, diffLine)
  103. continue
  104. case line[0] == '@':
  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. middle := -1
  142. // Note: In case file name is surrounded by double quotes(it happens only in git-shell).
  143. hasQuote := strings.Index(line, `\"`) > -1
  144. if hasQuote {
  145. line = strings.Replace(line, `\"`, `"`, -1)
  146. middle = strings.Index(line, ` "b/`)
  147. } else {
  148. middle = strings.Index(line, " b/")
  149. }
  150. beg := len(DIFF_HEAD)
  151. a := line[beg+2 : middle]
  152. b := line[middle+3:]
  153. if hasQuote {
  154. a = a[1 : len(a)-1]
  155. b = b[1 : len(b)-1]
  156. }
  157. fmt.Println(a, b)
  158. curFile = &DiffFile{
  159. Name: a,
  160. Index: len(diff.Files) + 1,
  161. Type: DIFF_FILE_CHANGE,
  162. Sections: make([]*DiffSection, 0, 10),
  163. }
  164. diff.Files = append(diff.Files, curFile)
  165. // Check file diff type.
  166. for scanner.Scan() {
  167. switch {
  168. case strings.HasPrefix(scanner.Text(), "new file"):
  169. curFile.Type = DIFF_FILE_ADD
  170. curFile.IsCreated = true
  171. case strings.HasPrefix(scanner.Text(), "deleted"):
  172. curFile.Type = DIFF_FILE_DEL
  173. curFile.IsDeleted = true
  174. case strings.HasPrefix(scanner.Text(), "index"):
  175. curFile.Type = DIFF_FILE_CHANGE
  176. case strings.HasPrefix(scanner.Text(), "similarity index 100%"):
  177. curFile.Type = DIFF_FILE_RENAME
  178. curFile.IsRenamed = true
  179. curFile.OldName = curFile.Name
  180. curFile.Name = b
  181. }
  182. if curFile.Type > 0 {
  183. break
  184. }
  185. }
  186. }
  187. }
  188. for _, f := range diff.Files {
  189. buf.Reset()
  190. for _, sec := range f.Sections {
  191. for _, l := range sec.Lines {
  192. buf.WriteString(l.Content)
  193. buf.WriteString("\n")
  194. }
  195. }
  196. charsetLabel, err := base.DetectEncoding(buf.Bytes())
  197. if charsetLabel != "UTF-8" && err == nil {
  198. encoding, _ := charset.Lookup(charsetLabel)
  199. if encoding != nil {
  200. d := encoding.NewDecoder()
  201. for _, sec := range f.Sections {
  202. for _, l := range sec.Lines {
  203. if c, _, err := transform.String(d, l.Content); err == nil {
  204. l.Content = c
  205. }
  206. }
  207. }
  208. }
  209. }
  210. }
  211. return diff, nil
  212. }
  213. func GetDiffRange(repoPath, beforeCommitId string, afterCommitId string, maxlines int) (*Diff, error) {
  214. repo, err := git.OpenRepository(repoPath)
  215. if err != nil {
  216. return nil, err
  217. }
  218. commit, err := repo.GetCommit(afterCommitId)
  219. if err != nil {
  220. return nil, err
  221. }
  222. rd, wr := io.Pipe()
  223. var cmd *exec.Cmd
  224. // if "after" commit given
  225. if beforeCommitId == "" {
  226. // First commit of repository.
  227. if commit.ParentCount() == 0 {
  228. cmd = exec.Command("git", "show", afterCommitId)
  229. } else {
  230. c, _ := commit.Parent(0)
  231. cmd = exec.Command("git", "diff", "-M", c.Id.String(), afterCommitId)
  232. }
  233. } else {
  234. cmd = exec.Command("git", "diff", "-M", beforeCommitId, afterCommitId)
  235. }
  236. cmd.Dir = repoPath
  237. cmd.Stdout = wr
  238. cmd.Stdin = os.Stdin
  239. cmd.Stderr = os.Stderr
  240. done := make(chan error)
  241. go func() {
  242. cmd.Start()
  243. done <- cmd.Wait()
  244. wr.Close()
  245. }()
  246. defer rd.Close()
  247. desc := fmt.Sprintf("GetDiffRange(%s)", repoPath)
  248. pid := process.Add(desc, cmd)
  249. go func() {
  250. // In case process became zombie.
  251. select {
  252. case <-time.After(5 * time.Minute):
  253. if errKill := process.Kill(pid); errKill != nil {
  254. log.Error(4, "git_diff.ParsePatch(Kill): %v", err)
  255. }
  256. <-done
  257. // return "", ErrExecTimeout.Error(), ErrExecTimeout
  258. case err = <-done:
  259. process.Remove(pid)
  260. }
  261. }()
  262. return ParsePatch(pid, maxlines, cmd, rd)
  263. }
  264. func GetDiffCommit(repoPath, commitId string, maxlines int) (*Diff, error) {
  265. return GetDiffRange(repoPath, "", commitId, maxlines)
  266. }