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.

246 lines
5.4 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/gogs/modules/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, maxlines int, 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. isTooLong bool
  68. )
  69. diff := &Diff{Files: make([]*DiffFile, 0)}
  70. var i int
  71. for scanner.Scan() {
  72. line := scanner.Text()
  73. // fmt.Println(i, line)
  74. if strings.HasPrefix(line, "+++ ") || strings.HasPrefix(line, "--- ") {
  75. continue
  76. }
  77. if line == "" {
  78. continue
  79. }
  80. i = i + 1
  81. // Diff data too large, we only show the first about maxlines lines
  82. if i == maxlines {
  83. isTooLong = true
  84. log.Warn("Diff data too large")
  85. //return &Diff{}, nil
  86. }
  87. switch {
  88. case line[0] == ' ':
  89. diffLine := &DiffLine{Type: DIFF_LINE_PLAIN, Content: line, LeftIdx: leftLine, RightIdx: rightLine}
  90. leftLine++
  91. rightLine++
  92. curSection.Lines = append(curSection.Lines, diffLine)
  93. continue
  94. case line[0] == '@':
  95. if isTooLong {
  96. return diff, nil
  97. }
  98. curSection = &DiffSection{}
  99. curFile.Sections = append(curFile.Sections, curSection)
  100. ss := strings.Split(line, "@@")
  101. diffLine := &DiffLine{Type: DIFF_LINE_SECTION, Content: line}
  102. curSection.Lines = append(curSection.Lines, diffLine)
  103. // Parse line number.
  104. ranges := strings.Split(ss[len(ss)-2][1:], " ")
  105. leftLine, _ = com.StrTo(strings.Split(ranges[0], ",")[0][1:]).Int()
  106. rightLine, _ = com.StrTo(strings.Split(ranges[1], ",")[0]).Int()
  107. continue
  108. case line[0] == '+':
  109. curFile.Addition++
  110. diff.TotalAddition++
  111. diffLine := &DiffLine{Type: DIFF_LINE_ADD, Content: line, RightIdx: rightLine}
  112. rightLine++
  113. curSection.Lines = append(curSection.Lines, diffLine)
  114. continue
  115. case line[0] == '-':
  116. curFile.Deletion++
  117. diff.TotalDeletion++
  118. diffLine := &DiffLine{Type: DIFF_LINE_DEL, Content: line, LeftIdx: leftLine}
  119. if leftLine > 0 {
  120. leftLine++
  121. }
  122. curSection.Lines = append(curSection.Lines, diffLine)
  123. case strings.HasPrefix(line, "Binary"):
  124. curFile.IsBin = true
  125. continue
  126. }
  127. // Get new file.
  128. if strings.HasPrefix(line, DIFF_HEAD) {
  129. if isTooLong {
  130. return diff, nil
  131. }
  132. fs := strings.Split(line[len(DIFF_HEAD):], " ")
  133. a := fs[0]
  134. curFile = &DiffFile{
  135. Name: a[strings.Index(a, "/")+1:],
  136. Index: len(diff.Files) + 1,
  137. Type: DIFF_FILE_CHANGE,
  138. Sections: make([]*DiffSection, 0, 10),
  139. }
  140. diff.Files = append(diff.Files, curFile)
  141. // Check file diff type.
  142. for scanner.Scan() {
  143. switch {
  144. case strings.HasPrefix(scanner.Text(), "new file"):
  145. curFile.Type = DIFF_FILE_ADD
  146. case strings.HasPrefix(scanner.Text(), "deleted"):
  147. curFile.Type = DIFF_FILE_DEL
  148. case strings.HasPrefix(scanner.Text(), "index"):
  149. curFile.Type = DIFF_FILE_CHANGE
  150. }
  151. if curFile.Type > 0 {
  152. break
  153. }
  154. }
  155. }
  156. }
  157. return diff, nil
  158. }
  159. func GetDiffRange(repoPath, beforeCommitId string, afterCommitId string, maxlines int) (*Diff, error) {
  160. repo, err := git.OpenRepository(repoPath)
  161. if err != nil {
  162. return nil, err
  163. }
  164. commit, err := repo.GetCommit(afterCommitId)
  165. if err != nil {
  166. return nil, err
  167. }
  168. rd, wr := io.Pipe()
  169. var cmd *exec.Cmd
  170. // if "after" commit given
  171. if beforeCommitId == "" {
  172. // First commit of repository.
  173. if commit.ParentCount() == 0 {
  174. cmd = exec.Command("git", "show", afterCommitId)
  175. } else {
  176. c, _ := commit.Parent(0)
  177. cmd = exec.Command("git", "diff", c.Id.String(), afterCommitId)
  178. }
  179. } else {
  180. cmd = exec.Command("git", "diff", beforeCommitId, afterCommitId)
  181. }
  182. cmd.Dir = repoPath
  183. cmd.Stdout = wr
  184. cmd.Stdin = os.Stdin
  185. cmd.Stderr = os.Stderr
  186. done := make(chan error)
  187. go func() {
  188. cmd.Start()
  189. done <- cmd.Wait()
  190. wr.Close()
  191. }()
  192. defer rd.Close()
  193. desc := fmt.Sprintf("GetDiffRange(%s)", repoPath)
  194. pid := process.Add(desc, cmd)
  195. go func() {
  196. // In case process became zombie.
  197. select {
  198. case <-time.After(5 * time.Minute):
  199. if errKill := process.Kill(pid); errKill != nil {
  200. log.Error(4, "git_diff.ParsePatch(Kill): %v", err)
  201. }
  202. <-done
  203. // return "", ErrExecTimeout.Error(), ErrExecTimeout
  204. case err = <-done:
  205. process.Remove(pid)
  206. }
  207. }()
  208. return ParsePatch(pid, maxlines, cmd, rd)
  209. }
  210. func GetDiffCommit(repoPath, commitId string, maxlines int) (*Diff, error) {
  211. return GetDiffRange(repoPath, "", commitId, maxlines)
  212. }