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.

228 lines
4.8 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  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. "fmt"
  8. "io"
  9. "os"
  10. "os/exec"
  11. "strings"
  12. "time"
  13. "github.com/Unknwon/com"
  14. "github.com/gogits/git"
  15. "github.com/gogits/gogs/modules/log"
  16. "github.com/gogits/gogs/modules/process"
  17. )
  18. // Diff line types.
  19. const (
  20. DIFF_LINE_PLAIN = iota + 1
  21. DIFF_LINE_ADD
  22. DIFF_LINE_DEL
  23. DIFF_LINE_SECTION
  24. )
  25. const (
  26. DIFF_FILE_ADD = iota + 1
  27. DIFF_FILE_CHANGE
  28. DIFF_FILE_DEL
  29. )
  30. type DiffLine struct {
  31. LeftIdx int
  32. RightIdx int
  33. Type int
  34. Content string
  35. }
  36. func (d DiffLine) GetType() int {
  37. return d.Type
  38. }
  39. type DiffSection struct {
  40. Name string
  41. Lines []*DiffLine
  42. }
  43. type DiffFile struct {
  44. Name string
  45. Index int
  46. Addition, Deletion int
  47. Type int
  48. IsBin bool
  49. Sections []*DiffSection
  50. }
  51. type Diff struct {
  52. TotalAddition, TotalDeletion int
  53. Files []*DiffFile
  54. }
  55. func (diff *Diff) NumFiles() int {
  56. return len(diff.Files)
  57. }
  58. const DIFF_HEAD = "diff --git "
  59. func ParsePatch(pid int64, cmd *exec.Cmd, reader io.Reader) (*Diff, error) {
  60. scanner := bufio.NewScanner(reader)
  61. var (
  62. curFile *DiffFile
  63. curSection = &DiffSection{
  64. Lines: make([]*DiffLine, 0, 10),
  65. }
  66. leftLine, rightLine int
  67. )
  68. diff := &Diff{Files: make([]*DiffFile, 0)}
  69. var i int
  70. for scanner.Scan() {
  71. line := scanner.Text()
  72. // fmt.Println(i, line)
  73. if strings.HasPrefix(line, "+++ ") || strings.HasPrefix(line, "--- ") {
  74. continue
  75. }
  76. i = i + 1
  77. // Diff data too large.
  78. if i == 5000 {
  79. log.Warn("Diff data too large")
  80. return &Diff{}, nil
  81. }
  82. if line == "" {
  83. continue
  84. }
  85. switch {
  86. case line[0] == ' ':
  87. diffLine := &DiffLine{Type: DIFF_LINE_PLAIN, Content: line, LeftIdx: leftLine, RightIdx: rightLine}
  88. leftLine++
  89. rightLine++
  90. curSection.Lines = append(curSection.Lines, diffLine)
  91. continue
  92. case line[0] == '@':
  93. curSection = &DiffSection{}
  94. curFile.Sections = append(curFile.Sections, curSection)
  95. ss := strings.Split(line, "@@")
  96. diffLine := &DiffLine{Type: DIFF_LINE_SECTION, Content: line}
  97. curSection.Lines = append(curSection.Lines, diffLine)
  98. // Parse line number.
  99. ranges := strings.Split(ss[len(ss)-2][1:], " ")
  100. leftLine, _ = com.StrTo(strings.Split(ranges[0], ",")[0][1:]).Int()
  101. rightLine, _ = com.StrTo(strings.Split(ranges[1], ",")[0]).Int()
  102. continue
  103. case line[0] == '+':
  104. curFile.Addition++
  105. diff.TotalAddition++
  106. diffLine := &DiffLine{Type: DIFF_LINE_ADD, Content: line, RightIdx: rightLine}
  107. rightLine++
  108. curSection.Lines = append(curSection.Lines, diffLine)
  109. continue
  110. case line[0] == '-':
  111. curFile.Deletion++
  112. diff.TotalDeletion++
  113. diffLine := &DiffLine{Type: DIFF_LINE_DEL, Content: line, LeftIdx: leftLine}
  114. if leftLine > 0 {
  115. leftLine++
  116. }
  117. curSection.Lines = append(curSection.Lines, diffLine)
  118. case strings.HasPrefix(line, "Binary"):
  119. curFile.IsBin = true
  120. continue
  121. }
  122. // Get new file.
  123. if strings.HasPrefix(line, DIFF_HEAD) {
  124. fs := strings.Split(line[len(DIFF_HEAD):], " ")
  125. a := fs[0]
  126. curFile = &DiffFile{
  127. Name: a[strings.Index(a, "/")+1:],
  128. Index: len(diff.Files) + 1,
  129. Type: DIFF_FILE_CHANGE,
  130. Sections: make([]*DiffSection, 0, 10),
  131. }
  132. diff.Files = append(diff.Files, curFile)
  133. // Check file diff type.
  134. for scanner.Scan() {
  135. switch {
  136. case strings.HasPrefix(scanner.Text(), "new file"):
  137. curFile.Type = DIFF_FILE_ADD
  138. case strings.HasPrefix(scanner.Text(), "deleted"):
  139. curFile.Type = DIFF_FILE_DEL
  140. case strings.HasPrefix(scanner.Text(), "index"):
  141. curFile.Type = DIFF_FILE_CHANGE
  142. }
  143. if curFile.Type > 0 {
  144. break
  145. }
  146. }
  147. }
  148. }
  149. return diff, nil
  150. }
  151. func GetDiff(repoPath, commitid string) (*Diff, error) {
  152. repo, err := git.OpenRepository(repoPath)
  153. if err != nil {
  154. return nil, err
  155. }
  156. commit, err := repo.GetCommit(commitid)
  157. if err != nil {
  158. return nil, err
  159. }
  160. rd, wr := io.Pipe()
  161. var cmd *exec.Cmd
  162. // First commit of repository.
  163. if commit.ParentCount() == 0 {
  164. cmd = exec.Command("git", "show", commitid)
  165. } else {
  166. c, _ := commit.Parent(0)
  167. cmd = exec.Command("git", "diff", c.Id.String(), commitid)
  168. }
  169. cmd.Dir = repoPath
  170. cmd.Stdout = wr
  171. cmd.Stdin = os.Stdin
  172. cmd.Stderr = os.Stderr
  173. done := make(chan error)
  174. go func() {
  175. cmd.Start()
  176. done <- cmd.Wait()
  177. wr.Close()
  178. }()
  179. defer rd.Close()
  180. desc := fmt.Sprintf("GetDiff(%s)", repoPath)
  181. pid := process.Add(desc, cmd)
  182. go func() {
  183. // In case process became zombie.
  184. select {
  185. case <-time.After(5 * time.Minute):
  186. if errKill := process.Kill(pid); errKill != nil {
  187. log.Error(4, "git_diff.ParsePatch(Kill): %v", err)
  188. }
  189. <-done
  190. // return "", ErrExecTimeout.Error(), ErrExecTimeout
  191. case err = <-done:
  192. process.Remove(pid)
  193. }
  194. }()
  195. return ParsePatch(pid, cmd, rd)
  196. }